blob: 53a4d84eacf5181170392f35879f8f34d39eebf1 [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
1071 void CopyFrom(HEnvironment* env);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001072 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1073 // input to the loop phi instead. This is for inserting instructions that
1074 // require an environment (like HDeoptimization) in the loop pre-header.
1075 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001076
1077 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001078 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001079 }
1080
1081 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001082 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001083 }
1084
David Brazdil1abb4192015-02-17 18:33:36 +00001085 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001086
1087 size_t Size() const { return vregs_.Size(); }
1088
1089 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001090 // Record instructions' use entries of this environment for constant-time removal.
1091 // It should only be called by HInstruction when a new environment use is added.
1092 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1093 DCHECK(env_use->GetUser() == this);
1094 size_t index = env_use->GetIndex();
1095 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1096 }
David Brazdiled596192015-01-23 10:39:45 +00001097
David Brazdil1abb4192015-02-17 18:33:36 +00001098 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001099
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001100 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001101
1102 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1103};
1104
Calin Juravleacf735c2015-02-12 15:25:22 +00001105class ReferenceTypeInfo : ValueObject {
1106 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001107 typedef Handle<mirror::Class> TypeHandle;
1108
1109 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1111 if (type_handle->IsObjectClass()) {
1112 // Override the type handle to be consistent with the case when we get to
1113 // Top but don't have the Object class available. It avoids having to guess
1114 // what value the type_handle has when it's Top.
1115 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1116 } else {
1117 return ReferenceTypeInfo(type_handle, is_exact, false);
1118 }
1119 }
1120
1121 static ReferenceTypeInfo CreateTop(bool is_exact) {
1122 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001123 }
1124
1125 bool IsExact() const { return is_exact_; }
1126 bool IsTop() const { return is_top_; }
1127
1128 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1129
Calin Juravleb1498f62015-02-16 13:13:29 +00001130 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001131 if (IsTop()) {
1132 // Top (equivalent for java.lang.Object) is supertype of anything.
1133 return true;
1134 }
1135 if (rti.IsTop()) {
1136 // If we get here `this` is not Top() so it can't be a supertype.
1137 return false;
1138 }
1139 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1140 }
1141
1142 // Returns true if the type information provide the same amount of details.
1143 // Note that it does not mean that the instructions have the same actual type
1144 // (e.g. tops are equal but they can be the result of a merge).
1145 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1146 if (IsExact() != rti.IsExact()) {
1147 return false;
1148 }
1149 if (IsTop() && rti.IsTop()) {
1150 // `Top` means java.lang.Object, so the types are equivalent.
1151 return true;
1152 }
1153 if (IsTop() || rti.IsTop()) {
1154 // If only one is top or object than they are not equivalent.
1155 // NB: We need this extra check because the type_handle of `Top` is invalid
1156 // and we cannot inspect its reference.
1157 return false;
1158 }
1159
1160 // Finally check the types.
1161 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1162 }
1163
1164 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001165 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1166 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1167 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1168
Calin Juravleacf735c2015-02-12 15:25:22 +00001169 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001170 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001171 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001172 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001173 bool is_exact_;
1174 // A true value here means that the object type should be java.lang.Object.
1175 // We don't have access to the corresponding mirror object every time so this
1176 // flag acts as a substitute. When true, the TypeHandle refers to a null
1177 // pointer and should not be used.
1178 bool is_top_;
1179};
1180
1181std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1182
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001183class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001184 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001185 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001186 : previous_(nullptr),
1187 next_(nullptr),
1188 block_(nullptr),
1189 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001190 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001191 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001192 locations_(nullptr),
1193 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001194 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001195 side_effects_(side_effects),
1196 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001197
Dave Allison20dfc792014-06-16 20:44:29 -07001198 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001199
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001200#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001201 enum InstructionKind {
1202 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1203 };
1204#undef DECLARE_KIND
1205
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001206 HInstruction* GetNext() const { return next_; }
1207 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001208
Calin Juravle77520bc2015-01-12 18:45:46 +00001209 HInstruction* GetNextDisregardingMoves() const;
1210 HInstruction* GetPreviousDisregardingMoves() const;
1211
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001212 HBasicBlock* GetBlock() const { return block_; }
1213 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001214 bool IsInBlock() const { return block_ != nullptr; }
1215 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001216 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001217
Roland Levillain6b879dd2014-09-22 17:13:44 +01001218 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001219 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001220
1221 virtual void Accept(HGraphVisitor* visitor) = 0;
1222 virtual const char* DebugName() const = 0;
1223
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001224 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001225 void SetRawInputAt(size_t index, HInstruction* input) {
1226 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1227 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001228
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001229 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001230 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001231 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001232 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001233
Calin Juravle10e244f2015-01-26 18:54:32 +00001234 // Does not apply for all instructions, but having this at top level greatly
1235 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001236 virtual bool CanBeNull() const {
1237 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1238 return true;
1239 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001240
Calin Juravle641547a2015-04-21 22:08:51 +01001241 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1242 UNUSED(obj);
1243 return false;
1244 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001245
Calin Juravleacf735c2015-02-12 15:25:22 +00001246 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001247 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001248 reference_type_info_ = reference_type_info;
1249 }
1250
Calin Juravle61d544b2015-02-23 16:46:57 +00001251 ReferenceTypeInfo GetReferenceTypeInfo() const {
1252 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1253 return reference_type_info_;
1254 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001255
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001256 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001257 DCHECK(user != nullptr);
1258 HUseListNode<HInstruction*>* use =
1259 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1260 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001261 }
1262
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001263 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001264 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001265 HUseListNode<HEnvironment*>* env_use =
1266 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1267 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001268 }
1269
David Brazdil1abb4192015-02-17 18:33:36 +00001270 void RemoveAsUserOfInput(size_t input) {
1271 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1272 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1273 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001274
David Brazdil1abb4192015-02-17 18:33:36 +00001275 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1276 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001277
David Brazdiled596192015-01-23 10:39:45 +00001278 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1279 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001280 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001281 bool HasOnlyOneNonEnvironmentUse() const {
1282 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1283 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001284
Roland Levillain6c82d402014-10-13 16:10:27 +01001285 // Does this instruction strictly dominate `other_instruction`?
1286 // Returns false if this instruction and `other_instruction` are the same.
1287 // Aborts if this instruction and `other_instruction` are both phis.
1288 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001289
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001290 int GetId() const { return id_; }
1291 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001292
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001293 int GetSsaIndex() const { return ssa_index_; }
1294 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1295 bool HasSsaIndex() const { return ssa_index_ != -1; }
1296
1297 bool HasEnvironment() const { return environment_ != nullptr; }
1298 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001299 // Set the `environment_` field. Raw because this method does not
1300 // update the uses lists.
1301 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1302
1303 // Set the environment of this instruction, copying it from `environment`. While
1304 // copying, the uses lists are being updated.
1305 void CopyEnvironmentFrom(HEnvironment* environment) {
1306 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1307 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1308 environment_->CopyFrom(environment);
1309 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001310
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001311 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1312 HBasicBlock* block) {
1313 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1314 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1315 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
1316 }
1317
Nicolas Geoffray39468442014-09-02 15:17:15 +01001318 // Returns the number of entries in the environment. Typically, that is the
1319 // number of dex registers in a method. It could be more in case of inlining.
1320 size_t EnvironmentSize() const;
1321
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001322 LocationSummary* GetLocations() const { return locations_; }
1323 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001324
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001325 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001326 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001327
Alexandre Rames188d4312015-04-09 18:30:21 +01001328 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1329 // uses of this instruction by `other` are *not* updated.
1330 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1331 ReplaceWith(other);
1332 other->ReplaceInput(this, use_index);
1333 }
1334
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001335 // Move `this` instruction before `cursor`.
1336 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001337
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001338#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001339 bool Is##type() const { return (As##type() != nullptr); } \
1340 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001341 virtual H##type* As##type() { return nullptr; }
1342
1343 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1344#undef INSTRUCTION_TYPE_CHECK
1345
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001346 // Returns whether the instruction can be moved within the graph.
1347 virtual bool CanBeMoved() const { return false; }
1348
1349 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001350 virtual bool InstructionTypeEquals(HInstruction* other) const {
1351 UNUSED(other);
1352 return false;
1353 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001354
1355 // Returns whether any data encoded in the two instructions is equal.
1356 // This method does not look at the inputs. Both instructions must be
1357 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001358 virtual bool InstructionDataEquals(HInstruction* other) const {
1359 UNUSED(other);
1360 return false;
1361 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001362
1363 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001364 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001365 // 2) Their inputs are identical.
1366 bool Equals(HInstruction* other) const;
1367
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001368 virtual InstructionKind GetKind() const = 0;
1369
1370 virtual size_t ComputeHashCode() const {
1371 size_t result = GetKind();
1372 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1373 result = (result * 31) + InputAt(i)->GetId();
1374 }
1375 return result;
1376 }
1377
1378 SideEffects GetSideEffects() const { return side_effects_; }
1379
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001380 size_t GetLifetimePosition() const { return lifetime_position_; }
1381 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1382 LiveInterval* GetLiveInterval() const { return live_interval_; }
1383 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1384 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1385
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001386 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1387
1388 // Returns whether the code generation of the instruction will require to have access
1389 // to the current method. Such instructions are:
1390 // (1): Instructions that require an environment, as calling the runtime requires
1391 // to walk the stack and have the current method stored at a specific stack address.
1392 // (2): Object literals like classes and strings, that are loaded from the dex cache
1393 // fields of the current method.
1394 bool NeedsCurrentMethod() const {
1395 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1396 }
1397
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001398 virtual bool NeedsDexCache() const { return false; }
1399
David Brazdil1abb4192015-02-17 18:33:36 +00001400 protected:
1401 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1402 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1403
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001404 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001405 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1406
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001407 HInstruction* previous_;
1408 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001409 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001410
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001411 // An instruction gets an id when it is added to the graph.
1412 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001413 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001414 int id_;
1415
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001416 // When doing liveness analysis, instructions that have uses get an SSA index.
1417 int ssa_index_;
1418
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001419 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001420 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001421
1422 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001423 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001424
Nicolas Geoffray39468442014-09-02 15:17:15 +01001425 // The environment associated with this instruction. Not null if the instruction
1426 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001427 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001428
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001429 // Set by the code generator.
1430 LocationSummary* locations_;
1431
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001432 // Set by the liveness analysis.
1433 LiveInterval* live_interval_;
1434
1435 // Set by the liveness analysis, this is the position in a linear
1436 // order of blocks where this instruction's live interval start.
1437 size_t lifetime_position_;
1438
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001439 const SideEffects side_effects_;
1440
Calin Juravleacf735c2015-02-12 15:25:22 +00001441 // TODO: for primitive types this should be marked as invalid.
1442 ReferenceTypeInfo reference_type_info_;
1443
David Brazdil1abb4192015-02-17 18:33:36 +00001444 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001445 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001446 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001447 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001448 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001449
1450 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1451};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001452std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001453
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001454class HInputIterator : public ValueObject {
1455 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001456 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001457
1458 bool Done() const { return index_ == instruction_->InputCount(); }
1459 HInstruction* Current() const { return instruction_->InputAt(index_); }
1460 void Advance() { index_++; }
1461
1462 private:
1463 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001464 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001465
1466 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1467};
1468
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001469class HInstructionIterator : public ValueObject {
1470 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001471 explicit HInstructionIterator(const HInstructionList& instructions)
1472 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001473 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001474 }
1475
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001476 bool Done() const { return instruction_ == nullptr; }
1477 HInstruction* Current() const { return instruction_; }
1478 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001479 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001480 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001481 }
1482
1483 private:
1484 HInstruction* instruction_;
1485 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001486
1487 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001488};
1489
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001490class HBackwardInstructionIterator : public ValueObject {
1491 public:
1492 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1493 : instruction_(instructions.last_instruction_) {
1494 next_ = Done() ? nullptr : instruction_->GetPrevious();
1495 }
1496
1497 bool Done() const { return instruction_ == nullptr; }
1498 HInstruction* Current() const { return instruction_; }
1499 void Advance() {
1500 instruction_ = next_;
1501 next_ = Done() ? nullptr : instruction_->GetPrevious();
1502 }
1503
1504 private:
1505 HInstruction* instruction_;
1506 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001507
1508 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001509};
1510
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001511// An embedded container with N elements of type T. Used (with partial
1512// specialization for N=0) because embedded arrays cannot have size 0.
1513template<typename T, intptr_t N>
1514class EmbeddedArray {
1515 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001516 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001517
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001518 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001519
1520 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001521 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001522 return elements_[i];
1523 }
1524
1525 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001526 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001527 return elements_[i];
1528 }
1529
1530 const T& At(intptr_t i) const {
1531 return (*this)[i];
1532 }
1533
1534 void SetAt(intptr_t i, const T& val) {
1535 (*this)[i] = val;
1536 }
1537
1538 private:
1539 T elements_[N];
1540};
1541
1542template<typename T>
1543class EmbeddedArray<T, 0> {
1544 public:
1545 intptr_t length() const { return 0; }
1546 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001547 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001548 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001549 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001550 }
1551 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001552 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001553 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001554 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001555 }
1556};
1557
1558template<intptr_t N>
1559class HTemplateInstruction: public HInstruction {
1560 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001561 HTemplateInstruction<N>(SideEffects side_effects)
1562 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001563 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001564
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001565 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001566
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001567 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001568 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1569
1570 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1571 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001572 }
1573
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001574 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001575 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001576
1577 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001578};
1579
Dave Allison20dfc792014-06-16 20:44:29 -07001580template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001581class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001582 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001583 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1584 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001585 virtual ~HExpression() {}
1586
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001587 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001588
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001589 protected:
1590 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001591};
1592
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001593// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1594// instruction that branches to the exit block.
1595class HReturnVoid : public HTemplateInstruction<0> {
1596 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001597 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001598
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001599 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001600
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001601 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001602
1603 private:
1604 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1605};
1606
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001607// Represents dex's RETURN opcodes. A HReturn is a control flow
1608// instruction that branches to the exit block.
1609class HReturn : public HTemplateInstruction<1> {
1610 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001611 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001612 SetRawInputAt(0, value);
1613 }
1614
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001615 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001616
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001617 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001618
1619 private:
1620 DISALLOW_COPY_AND_ASSIGN(HReturn);
1621};
1622
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001623// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001624// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001625// exit block.
1626class HExit : public HTemplateInstruction<0> {
1627 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001628 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001629
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001630 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001631
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001632 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001633
1634 private:
1635 DISALLOW_COPY_AND_ASSIGN(HExit);
1636};
1637
1638// Jumps from one block to another.
1639class HGoto : public HTemplateInstruction<0> {
1640 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001641 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1642
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001643 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001644
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001645 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001646 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001647 }
1648
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001649 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001650
1651 private:
1652 DISALLOW_COPY_AND_ASSIGN(HGoto);
1653};
1654
Dave Allison20dfc792014-06-16 20:44:29 -07001655
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001656// Conditional branch. A block ending with an HIf instruction must have
1657// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001658class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001659 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001660 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001661 SetRawInputAt(0, input);
1662 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001663
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001664 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001665
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001666 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001667 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001668 }
1669
1670 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001671 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001672 }
1673
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001674 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001675
1676 private:
1677 DISALLOW_COPY_AND_ASSIGN(HIf);
1678};
1679
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001680// Deoptimize to interpreter, upon checking a condition.
1681class HDeoptimize : public HTemplateInstruction<1> {
1682 public:
1683 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1684 : HTemplateInstruction(SideEffects::None()),
1685 dex_pc_(dex_pc) {
1686 SetRawInputAt(0, cond);
1687 }
1688
1689 bool NeedsEnvironment() const OVERRIDE { return true; }
1690 bool CanThrow() const OVERRIDE { return true; }
1691 uint32_t GetDexPc() const { return dex_pc_; }
1692
1693 DECLARE_INSTRUCTION(Deoptimize);
1694
1695 private:
1696 uint32_t dex_pc_;
1697
1698 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1699};
1700
Roland Levillain88cb1752014-10-20 16:36:47 +01001701class HUnaryOperation : public HExpression<1> {
1702 public:
1703 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1704 : HExpression(result_type, SideEffects::None()) {
1705 SetRawInputAt(0, input);
1706 }
1707
1708 HInstruction* GetInput() const { return InputAt(0); }
1709 Primitive::Type GetResultType() const { return GetType(); }
1710
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001711 bool CanBeMoved() const OVERRIDE { return true; }
1712 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001713 UNUSED(other);
1714 return true;
1715 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001716
Roland Levillain9240d6a2014-10-20 16:47:04 +01001717 // Try to statically evaluate `operation` and return a HConstant
1718 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001719 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001720 HConstant* TryStaticEvaluation() const;
1721
1722 // Apply this operation to `x`.
1723 virtual int32_t Evaluate(int32_t x) const = 0;
1724 virtual int64_t Evaluate(int64_t x) const = 0;
1725
Roland Levillain88cb1752014-10-20 16:36:47 +01001726 DECLARE_INSTRUCTION(UnaryOperation);
1727
1728 private:
1729 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1730};
1731
Dave Allison20dfc792014-06-16 20:44:29 -07001732class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001733 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001734 HBinaryOperation(Primitive::Type result_type,
1735 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001736 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001737 SetRawInputAt(0, left);
1738 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001739 }
1740
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001741 HInstruction* GetLeft() const { return InputAt(0); }
1742 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001743 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001744
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001745 virtual bool IsCommutative() const { return false; }
1746
1747 // Put constant on the right.
1748 // Returns whether order is changed.
1749 bool OrderInputsWithConstantOnTheRight() {
1750 HInstruction* left = InputAt(0);
1751 HInstruction* right = InputAt(1);
1752 if (left->IsConstant() && !right->IsConstant()) {
1753 ReplaceInput(right, 0);
1754 ReplaceInput(left, 1);
1755 return true;
1756 }
1757 return false;
1758 }
1759
1760 // Order inputs by instruction id, but favor constant on the right side.
1761 // This helps GVN for commutative ops.
1762 void OrderInputs() {
1763 DCHECK(IsCommutative());
1764 HInstruction* left = InputAt(0);
1765 HInstruction* right = InputAt(1);
1766 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1767 return;
1768 }
1769 if (OrderInputsWithConstantOnTheRight()) {
1770 return;
1771 }
1772 // Order according to instruction id.
1773 if (left->GetId() > right->GetId()) {
1774 ReplaceInput(right, 0);
1775 ReplaceInput(left, 1);
1776 }
1777 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001778
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001779 bool CanBeMoved() const OVERRIDE { return true; }
1780 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001781 UNUSED(other);
1782 return true;
1783 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001784
Roland Levillain9240d6a2014-10-20 16:47:04 +01001785 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001786 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001787 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001788 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001789
1790 // Apply this operation to `x` and `y`.
1791 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1792 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1793
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001794 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001795 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001796 HConstant* GetConstantRight() const;
1797
1798 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001799 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001800 HInstruction* GetLeastConstantLeft() const;
1801
Roland Levillainccc07a92014-09-16 14:48:16 +01001802 DECLARE_INSTRUCTION(BinaryOperation);
1803
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001804 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001805 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1806};
1807
Dave Allison20dfc792014-06-16 20:44:29 -07001808class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001809 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001810 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001811 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1812 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001813
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001814 bool NeedsMaterialization() const { return needs_materialization_; }
1815 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001816
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001817 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001818 // `instruction`, and disregard moves in between.
1819 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001820
Dave Allison20dfc792014-06-16 20:44:29 -07001821 DECLARE_INSTRUCTION(Condition);
1822
1823 virtual IfCondition GetCondition() const = 0;
1824
1825 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001826 // For register allocation purposes, returns whether this instruction needs to be
1827 // materialized (that is, not just be in the processor flags).
1828 bool needs_materialization_;
1829
Dave Allison20dfc792014-06-16 20:44:29 -07001830 DISALLOW_COPY_AND_ASSIGN(HCondition);
1831};
1832
1833// Instruction to check if two inputs are equal to each other.
1834class HEqual : public HCondition {
1835 public:
1836 HEqual(HInstruction* first, HInstruction* second)
1837 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001838
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001839 bool IsCommutative() const OVERRIDE { return true; }
1840
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001841 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001842 return x == y ? 1 : 0;
1843 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001844 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001845 return x == y ? 1 : 0;
1846 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001847
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001848 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001849
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001850 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001851 return kCondEQ;
1852 }
1853
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001854 private:
1855 DISALLOW_COPY_AND_ASSIGN(HEqual);
1856};
1857
Dave Allison20dfc792014-06-16 20:44:29 -07001858class HNotEqual : public HCondition {
1859 public:
1860 HNotEqual(HInstruction* first, HInstruction* second)
1861 : HCondition(first, second) {}
1862
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001863 bool IsCommutative() const OVERRIDE { return true; }
1864
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001865 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001866 return x != y ? 1 : 0;
1867 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001868 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001869 return x != y ? 1 : 0;
1870 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001871
Dave Allison20dfc792014-06-16 20:44:29 -07001872 DECLARE_INSTRUCTION(NotEqual);
1873
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001874 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001875 return kCondNE;
1876 }
1877
1878 private:
1879 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1880};
1881
1882class HLessThan : public HCondition {
1883 public:
1884 HLessThan(HInstruction* first, HInstruction* second)
1885 : HCondition(first, second) {}
1886
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001887 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001888 return x < y ? 1 : 0;
1889 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001890 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001891 return x < y ? 1 : 0;
1892 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001893
Dave Allison20dfc792014-06-16 20:44:29 -07001894 DECLARE_INSTRUCTION(LessThan);
1895
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001896 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001897 return kCondLT;
1898 }
1899
1900 private:
1901 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1902};
1903
1904class HLessThanOrEqual : public HCondition {
1905 public:
1906 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1907 : HCondition(first, second) {}
1908
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001909 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001910 return x <= y ? 1 : 0;
1911 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001912 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001913 return x <= y ? 1 : 0;
1914 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001915
Dave Allison20dfc792014-06-16 20:44:29 -07001916 DECLARE_INSTRUCTION(LessThanOrEqual);
1917
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001918 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001919 return kCondLE;
1920 }
1921
1922 private:
1923 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1924};
1925
1926class HGreaterThan : public HCondition {
1927 public:
1928 HGreaterThan(HInstruction* first, HInstruction* second)
1929 : HCondition(first, second) {}
1930
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001931 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001932 return x > y ? 1 : 0;
1933 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001934 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001935 return x > y ? 1 : 0;
1936 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001937
Dave Allison20dfc792014-06-16 20:44:29 -07001938 DECLARE_INSTRUCTION(GreaterThan);
1939
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001940 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001941 return kCondGT;
1942 }
1943
1944 private:
1945 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1946};
1947
1948class HGreaterThanOrEqual : public HCondition {
1949 public:
1950 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1951 : HCondition(first, second) {}
1952
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001953 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001954 return x >= y ? 1 : 0;
1955 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001956 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001957 return x >= y ? 1 : 0;
1958 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001959
Dave Allison20dfc792014-06-16 20:44:29 -07001960 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1961
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001962 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001963 return kCondGE;
1964 }
1965
1966 private:
1967 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1968};
1969
1970
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001971// Instruction to check how two inputs compare to each other.
1972// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1973class HCompare : public HBinaryOperation {
1974 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001975 // The bias applies for floating point operations and indicates how NaN
1976 // comparisons are treated:
1977 enum Bias {
1978 kNoBias, // bias is not applicable (i.e. for long operation)
1979 kGtBias, // return 1 for NaN comparisons
1980 kLtBias, // return -1 for NaN comparisons
1981 };
1982
1983 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1984 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001985 DCHECK_EQ(type, first->GetType());
1986 DCHECK_EQ(type, second->GetType());
1987 }
1988
Calin Juravleddb7df22014-11-25 20:56:51 +00001989 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001990 return
1991 x == y ? 0 :
1992 x > y ? 1 :
1993 -1;
1994 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001995
1996 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001997 return
1998 x == y ? 0 :
1999 x > y ? 1 :
2000 -1;
2001 }
2002
Calin Juravleddb7df22014-11-25 20:56:51 +00002003 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2004 return bias_ == other->AsCompare()->bias_;
2005 }
2006
2007 bool IsGtBias() { return bias_ == kGtBias; }
2008
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002009 DECLARE_INSTRUCTION(Compare);
2010
2011 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002012 const Bias bias_;
2013
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002014 DISALLOW_COPY_AND_ASSIGN(HCompare);
2015};
2016
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002017// A local in the graph. Corresponds to a Dex register.
2018class HLocal : public HTemplateInstruction<0> {
2019 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002020 explicit HLocal(uint16_t reg_number)
2021 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002022
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002023 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002024
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002025 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002026
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002027 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002028 // The Dex register number.
2029 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002030
2031 DISALLOW_COPY_AND_ASSIGN(HLocal);
2032};
2033
2034// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002035class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002036 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002037 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002038 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002039 SetRawInputAt(0, local);
2040 }
2041
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002042 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2043
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002044 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002045
2046 private:
2047 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2048};
2049
2050// Store a value in a given local. This instruction has two inputs: the value
2051// and the local.
2052class HStoreLocal : public HTemplateInstruction<2> {
2053 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002054 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002055 SetRawInputAt(0, local);
2056 SetRawInputAt(1, value);
2057 }
2058
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002059 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2060
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002061 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002062
2063 private:
2064 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2065};
2066
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002067class HConstant : public HExpression<0> {
2068 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002069 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2070
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002071 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002072
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002073 virtual bool IsMinusOne() const { return false; }
2074 virtual bool IsZero() const { return false; }
2075 virtual bool IsOne() const { return false; }
2076
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002077 DECLARE_INSTRUCTION(Constant);
2078
2079 private:
2080 DISALLOW_COPY_AND_ASSIGN(HConstant);
2081};
2082
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002083class HFloatConstant : public HConstant {
2084 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002085 float GetValue() const { return value_; }
2086
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002087 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002088 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2089 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002090 }
2091
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002092 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002093
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002094 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002095 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2096 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002097 }
2098 bool IsZero() const OVERRIDE {
2099 return AsFloatConstant()->GetValue() == 0.0f;
2100 }
2101 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002102 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2103 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002104 }
2105
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002106 DECLARE_INSTRUCTION(FloatConstant);
2107
2108 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002109 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002110 explicit HFloatConstant(int32_t value)
2111 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002112
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002113 const float value_;
2114
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002115 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002116 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002117 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002118 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2119};
2120
2121class HDoubleConstant : public HConstant {
2122 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002123 double GetValue() const { return value_; }
2124
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002125 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002126 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2127 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002128 }
2129
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002130 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002131
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002132 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002133 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2134 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002135 }
2136 bool IsZero() const OVERRIDE {
2137 return AsDoubleConstant()->GetValue() == 0.0;
2138 }
2139 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002140 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2141 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002142 }
2143
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002144 DECLARE_INSTRUCTION(DoubleConstant);
2145
2146 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002147 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002148 explicit HDoubleConstant(int64_t value)
2149 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002150
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002151 const double value_;
2152
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002153 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002154 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002155 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002156 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2157};
2158
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002159class HNullConstant : public HConstant {
2160 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002161 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2162 return true;
2163 }
2164
2165 size_t ComputeHashCode() const OVERRIDE { return 0; }
2166
2167 DECLARE_INSTRUCTION(NullConstant);
2168
2169 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002170 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2171
2172 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002173 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2174};
2175
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002176// Constants of the type int. Those can be from Dex instructions, or
2177// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002178class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002179 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002180 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002181
Calin Juravle61d544b2015-02-23 16:46:57 +00002182 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002183 return other->AsIntConstant()->value_ == value_;
2184 }
2185
Calin Juravle61d544b2015-02-23 16:46:57 +00002186 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2187
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002188 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2189 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2190 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2191
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002192 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002193
2194 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002195 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2196
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002197 const int32_t value_;
2198
David Brazdil8d5b8b22015-03-24 10:51:52 +00002199 friend class HGraph;
2200 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002201 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002202 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2203};
2204
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002205class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002206 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002207 int64_t GetValue() const { return value_; }
2208
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002209 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002210 return other->AsLongConstant()->value_ == value_;
2211 }
2212
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002213 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002214
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002215 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2216 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2217 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2218
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002219 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002220
2221 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002222 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2223
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002224 const int64_t value_;
2225
David Brazdil8d5b8b22015-03-24 10:51:52 +00002226 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002227 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2228};
2229
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002230enum class Intrinsics {
2231#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2232#include "intrinsics_list.h"
2233 kNone,
2234 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2235#undef INTRINSICS_LIST
2236#undef OPTIMIZING_INTRINSICS
2237};
2238std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2239
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002240class HInvoke : public HInstruction {
2241 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002242 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002243
2244 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2245 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002246 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002247
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002248 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002249 SetRawInputAt(index, argument);
2250 }
2251
Roland Levillain3e3d7332015-04-28 11:00:54 +01002252 // Return the number of arguments. This number can be lower than
2253 // the number of inputs returned by InputCount(), as some invoke
2254 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2255 // inputs at the end of their list of inputs.
2256 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2257
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002258 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002259
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002260 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002261
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002262 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2263
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002264 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002265 return intrinsic_;
2266 }
2267
2268 void SetIntrinsic(Intrinsics intrinsic) {
2269 intrinsic_ = intrinsic;
2270 }
2271
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002272 DECLARE_INSTRUCTION(Invoke);
2273
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002274 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002275 HInvoke(ArenaAllocator* arena,
2276 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002277 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002278 Primitive::Type return_type,
2279 uint32_t dex_pc,
2280 uint32_t dex_method_index)
2281 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002282 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002283 inputs_(arena, number_of_arguments),
2284 return_type_(return_type),
2285 dex_pc_(dex_pc),
2286 dex_method_index_(dex_method_index),
2287 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002288 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2289 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002290 }
2291
David Brazdil1abb4192015-02-17 18:33:36 +00002292 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2293 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2294 inputs_.Put(index, input);
2295 }
2296
Roland Levillain3e3d7332015-04-28 11:00:54 +01002297 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002298 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002299 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002300 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002301 const uint32_t dex_method_index_;
2302 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002303
2304 private:
2305 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2306};
2307
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002308class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002309 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002310 // Requirements of this method call regarding the class
2311 // initialization (clinit) check of its declaring class.
2312 enum class ClinitCheckRequirement {
2313 kNone, // Class already initialized.
2314 kExplicit, // Static call having explicit clinit check as last input.
2315 kImplicit, // Static call implicitly requiring a clinit check.
2316 };
2317
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002318 HInvokeStaticOrDirect(ArenaAllocator* arena,
2319 uint32_t number_of_arguments,
2320 Primitive::Type return_type,
2321 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002322 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002323 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002324 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002325 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002326 InvokeType invoke_type,
2327 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002328 : HInvoke(arena,
2329 number_of_arguments,
2330 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u,
2331 return_type,
2332 dex_pc,
2333 dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002334 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002335 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002336 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002337 clinit_check_requirement_(clinit_check_requirement),
2338 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002339
Calin Juravle641547a2015-04-21 22:08:51 +01002340 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2341 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002342 // We access the method via the dex cache so we can't do an implicit null check.
2343 // TODO: for intrinsics we can generate implicit null checks.
2344 return false;
2345 }
2346
Nicolas Geoffray79041292015-03-26 10:05:54 +00002347 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002348 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002349 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002350 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002351 bool IsStringInit() const { return string_init_offset_ != 0; }
2352 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002353
Roland Levillain4c0eb422015-04-24 16:43:49 +01002354 // Is this instruction a call to a static method?
2355 bool IsStatic() const {
2356 return GetInvokeType() == kStatic;
2357 }
2358
Roland Levillain3e3d7332015-04-28 11:00:54 +01002359 // Remove the art::HLoadClass instruction set as last input by
2360 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2361 // the initial art::HClinitCheck instruction (only relevant for
2362 // static calls with explicit clinit check).
2363 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002364 DCHECK(IsStaticWithExplicitClinitCheck());
2365 size_t last_input_index = InputCount() - 1;
2366 HInstruction* last_input = InputAt(last_input_index);
2367 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002368 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002369 RemoveAsUserOfInput(last_input_index);
2370 inputs_.DeleteAt(last_input_index);
2371 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2372 DCHECK(IsStaticWithImplicitClinitCheck());
2373 }
2374
2375 // Is this a call to a static method whose declaring class has an
2376 // explicit intialization check in the graph?
2377 bool IsStaticWithExplicitClinitCheck() const {
2378 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2379 }
2380
2381 // Is this a call to a static method whose declaring class has an
2382 // implicit intialization check requirement?
2383 bool IsStaticWithImplicitClinitCheck() const {
2384 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2385 }
2386
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002387 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002388
Roland Levillain4c0eb422015-04-24 16:43:49 +01002389 protected:
2390 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2391 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2392 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2393 HInstruction* input = input_record.GetInstruction();
2394 // `input` is the last input of a static invoke marked as having
2395 // an explicit clinit check. It must either be:
2396 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2397 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2398 DCHECK(input != nullptr);
2399 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2400 }
2401 return input_record;
2402 }
2403
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002404 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002405 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002406 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002407 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002408 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002409 // Thread entrypoint offset for string init method if this is a string init invoke.
2410 // Note that there are multiple string init methods, each having its own offset.
2411 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002412
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002413 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002414};
2415
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002416class HInvokeVirtual : public HInvoke {
2417 public:
2418 HInvokeVirtual(ArenaAllocator* arena,
2419 uint32_t number_of_arguments,
2420 Primitive::Type return_type,
2421 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002422 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002423 uint32_t vtable_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002424 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002425 vtable_index_(vtable_index) {}
2426
Calin Juravle641547a2015-04-21 22:08:51 +01002427 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002428 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002429 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002430 }
2431
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002432 uint32_t GetVTableIndex() const { return vtable_index_; }
2433
2434 DECLARE_INSTRUCTION(InvokeVirtual);
2435
2436 private:
2437 const uint32_t vtable_index_;
2438
2439 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2440};
2441
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002442class HInvokeInterface : public HInvoke {
2443 public:
2444 HInvokeInterface(ArenaAllocator* arena,
2445 uint32_t number_of_arguments,
2446 Primitive::Type return_type,
2447 uint32_t dex_pc,
2448 uint32_t dex_method_index,
2449 uint32_t imt_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002450 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002451 imt_index_(imt_index) {}
2452
Calin Juravle641547a2015-04-21 22:08:51 +01002453 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002454 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002455 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002456 }
2457
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002458 uint32_t GetImtIndex() const { return imt_index_; }
2459 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2460
2461 DECLARE_INSTRUCTION(InvokeInterface);
2462
2463 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002464 const uint32_t imt_index_;
2465
2466 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2467};
2468
Dave Allison20dfc792014-06-16 20:44:29 -07002469class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002470 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002471 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002472 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2473 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002474 type_index_(type_index),
2475 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002476
2477 uint32_t GetDexPc() const { return dex_pc_; }
2478 uint16_t GetTypeIndex() const { return type_index_; }
2479
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002480 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002481 bool NeedsEnvironment() const OVERRIDE { return true; }
2482 // It may throw when called on:
2483 // - interfaces
2484 // - abstract/innaccessible/unknown classes
2485 // TODO: optimize when possible.
2486 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002487
Calin Juravle10e244f2015-01-26 18:54:32 +00002488 bool CanBeNull() const OVERRIDE { return false; }
2489
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002490 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2491
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002492 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002493
2494 private:
2495 const uint32_t dex_pc_;
2496 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002497 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002498
2499 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2500};
2501
Roland Levillain88cb1752014-10-20 16:36:47 +01002502class HNeg : public HUnaryOperation {
2503 public:
2504 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2505 : HUnaryOperation(result_type, input) {}
2506
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002507 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2508 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002509
Roland Levillain88cb1752014-10-20 16:36:47 +01002510 DECLARE_INSTRUCTION(Neg);
2511
2512 private:
2513 DISALLOW_COPY_AND_ASSIGN(HNeg);
2514};
2515
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002516class HNewArray : public HExpression<1> {
2517 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002518 HNewArray(HInstruction* length,
2519 uint32_t dex_pc,
2520 uint16_t type_index,
2521 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002522 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2523 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002524 type_index_(type_index),
2525 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002526 SetRawInputAt(0, length);
2527 }
2528
2529 uint32_t GetDexPc() const { return dex_pc_; }
2530 uint16_t GetTypeIndex() const { return type_index_; }
2531
2532 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002533 bool NeedsEnvironment() const OVERRIDE { return true; }
2534
Mingyao Yang0c365e62015-03-31 15:09:29 -07002535 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2536 bool CanThrow() const OVERRIDE { return true; }
2537
Calin Juravle10e244f2015-01-26 18:54:32 +00002538 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002539
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002540 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2541
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002542 DECLARE_INSTRUCTION(NewArray);
2543
2544 private:
2545 const uint32_t dex_pc_;
2546 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002547 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002548
2549 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2550};
2551
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002552class HAdd : public HBinaryOperation {
2553 public:
2554 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2555 : HBinaryOperation(result_type, left, right) {}
2556
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002557 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002558
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002559 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002560 return x + y;
2561 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002562 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002563 return x + y;
2564 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002565
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002566 DECLARE_INSTRUCTION(Add);
2567
2568 private:
2569 DISALLOW_COPY_AND_ASSIGN(HAdd);
2570};
2571
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002572class HSub : public HBinaryOperation {
2573 public:
2574 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2575 : HBinaryOperation(result_type, left, right) {}
2576
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002577 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002578 return x - y;
2579 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002580 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002581 return x - y;
2582 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002583
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002584 DECLARE_INSTRUCTION(Sub);
2585
2586 private:
2587 DISALLOW_COPY_AND_ASSIGN(HSub);
2588};
2589
Calin Juravle34bacdf2014-10-07 20:23:36 +01002590class HMul : public HBinaryOperation {
2591 public:
2592 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2593 : HBinaryOperation(result_type, left, right) {}
2594
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002595 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002596
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002597 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2598 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002599
2600 DECLARE_INSTRUCTION(Mul);
2601
2602 private:
2603 DISALLOW_COPY_AND_ASSIGN(HMul);
2604};
2605
Calin Juravle7c4954d2014-10-28 16:57:40 +00002606class HDiv : public HBinaryOperation {
2607 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002608 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2609 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002610
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002611 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002612 // Our graph structure ensures we never have 0 for `y` during constant folding.
2613 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002614 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002615 return (y == -1) ? -x : x / y;
2616 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002617
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002618 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002619 DCHECK_NE(y, 0);
2620 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2621 return (y == -1) ? -x : x / y;
2622 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002623
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002624 uint32_t GetDexPc() const { return dex_pc_; }
2625
Calin Juravle7c4954d2014-10-28 16:57:40 +00002626 DECLARE_INSTRUCTION(Div);
2627
2628 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002629 const uint32_t dex_pc_;
2630
Calin Juravle7c4954d2014-10-28 16:57:40 +00002631 DISALLOW_COPY_AND_ASSIGN(HDiv);
2632};
2633
Calin Juravlebacfec32014-11-14 15:54:36 +00002634class HRem : public HBinaryOperation {
2635 public:
2636 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2637 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2638
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002639 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002640 DCHECK_NE(y, 0);
2641 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2642 return (y == -1) ? 0 : x % y;
2643 }
2644
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002645 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002646 DCHECK_NE(y, 0);
2647 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2648 return (y == -1) ? 0 : x % y;
2649 }
2650
2651 uint32_t GetDexPc() const { return dex_pc_; }
2652
2653 DECLARE_INSTRUCTION(Rem);
2654
2655 private:
2656 const uint32_t dex_pc_;
2657
2658 DISALLOW_COPY_AND_ASSIGN(HRem);
2659};
2660
Calin Juravled0d48522014-11-04 16:40:20 +00002661class HDivZeroCheck : public HExpression<1> {
2662 public:
2663 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2664 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2665 SetRawInputAt(0, value);
2666 }
2667
2668 bool CanBeMoved() const OVERRIDE { return true; }
2669
2670 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2671 UNUSED(other);
2672 return true;
2673 }
2674
2675 bool NeedsEnvironment() const OVERRIDE { return true; }
2676 bool CanThrow() const OVERRIDE { return true; }
2677
2678 uint32_t GetDexPc() const { return dex_pc_; }
2679
2680 DECLARE_INSTRUCTION(DivZeroCheck);
2681
2682 private:
2683 const uint32_t dex_pc_;
2684
2685 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2686};
2687
Calin Juravle9aec02f2014-11-18 23:06:35 +00002688class HShl : public HBinaryOperation {
2689 public:
2690 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2691 : HBinaryOperation(result_type, left, right) {}
2692
2693 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2694 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2695
2696 DECLARE_INSTRUCTION(Shl);
2697
2698 private:
2699 DISALLOW_COPY_AND_ASSIGN(HShl);
2700};
2701
2702class HShr : public HBinaryOperation {
2703 public:
2704 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2705 : HBinaryOperation(result_type, left, right) {}
2706
2707 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2708 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2709
2710 DECLARE_INSTRUCTION(Shr);
2711
2712 private:
2713 DISALLOW_COPY_AND_ASSIGN(HShr);
2714};
2715
2716class HUShr : public HBinaryOperation {
2717 public:
2718 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2719 : HBinaryOperation(result_type, left, right) {}
2720
2721 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2722 uint32_t ux = static_cast<uint32_t>(x);
2723 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2724 return static_cast<int32_t>(ux >> uy);
2725 }
2726
2727 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2728 uint64_t ux = static_cast<uint64_t>(x);
2729 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2730 return static_cast<int64_t>(ux >> uy);
2731 }
2732
2733 DECLARE_INSTRUCTION(UShr);
2734
2735 private:
2736 DISALLOW_COPY_AND_ASSIGN(HUShr);
2737};
2738
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002739class HAnd : public HBinaryOperation {
2740 public:
2741 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2742 : HBinaryOperation(result_type, left, right) {}
2743
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002744 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002745
2746 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2747 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2748
2749 DECLARE_INSTRUCTION(And);
2750
2751 private:
2752 DISALLOW_COPY_AND_ASSIGN(HAnd);
2753};
2754
2755class HOr : public HBinaryOperation {
2756 public:
2757 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2758 : HBinaryOperation(result_type, left, right) {}
2759
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002760 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002761
2762 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2763 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2764
2765 DECLARE_INSTRUCTION(Or);
2766
2767 private:
2768 DISALLOW_COPY_AND_ASSIGN(HOr);
2769};
2770
2771class HXor : public HBinaryOperation {
2772 public:
2773 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2774 : HBinaryOperation(result_type, left, right) {}
2775
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002776 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002777
2778 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2779 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2780
2781 DECLARE_INSTRUCTION(Xor);
2782
2783 private:
2784 DISALLOW_COPY_AND_ASSIGN(HXor);
2785};
2786
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002787// The value of a parameter in this method. Its location depends on
2788// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002789class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002790 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002791 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2792 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002793
2794 uint8_t GetIndex() const { return index_; }
2795
Calin Juravle10e244f2015-01-26 18:54:32 +00002796 bool CanBeNull() const OVERRIDE { return !is_this_; }
2797
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002798 DECLARE_INSTRUCTION(ParameterValue);
2799
2800 private:
2801 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002802 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002803 const uint8_t index_;
2804
Calin Juravle10e244f2015-01-26 18:54:32 +00002805 // Whether or not the parameter value corresponds to 'this' argument.
2806 const bool is_this_;
2807
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002808 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2809};
2810
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002811class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002812 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002813 explicit HNot(Primitive::Type result_type, HInstruction* input)
2814 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002815
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002816 bool CanBeMoved() const OVERRIDE { return true; }
2817 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002818 UNUSED(other);
2819 return true;
2820 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002821
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002822 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2823 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002824
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002825 DECLARE_INSTRUCTION(Not);
2826
2827 private:
2828 DISALLOW_COPY_AND_ASSIGN(HNot);
2829};
2830
David Brazdil66d126e2015-04-03 16:02:44 +01002831class HBooleanNot : public HUnaryOperation {
2832 public:
2833 explicit HBooleanNot(HInstruction* input)
2834 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2835
2836 bool CanBeMoved() const OVERRIDE { return true; }
2837 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2838 UNUSED(other);
2839 return true;
2840 }
2841
2842 int32_t Evaluate(int32_t x) const OVERRIDE {
2843 DCHECK(IsUint<1>(x));
2844 return !x;
2845 }
2846
2847 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2848 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2849 UNREACHABLE();
2850 }
2851
2852 DECLARE_INSTRUCTION(BooleanNot);
2853
2854 private:
2855 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2856};
2857
Roland Levillaindff1f282014-11-05 14:15:05 +00002858class HTypeConversion : public HExpression<1> {
2859 public:
2860 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002861 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2862 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002863 SetRawInputAt(0, input);
2864 DCHECK_NE(input->GetType(), result_type);
2865 }
2866
2867 HInstruction* GetInput() const { return InputAt(0); }
2868 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2869 Primitive::Type GetResultType() const { return GetType(); }
2870
Roland Levillain624279f2014-12-04 11:54:28 +00002871 // Required by the x86 and ARM code generators when producing calls
2872 // to the runtime.
2873 uint32_t GetDexPc() const { return dex_pc_; }
2874
Roland Levillaindff1f282014-11-05 14:15:05 +00002875 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002876 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002877
2878 DECLARE_INSTRUCTION(TypeConversion);
2879
2880 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002881 const uint32_t dex_pc_;
2882
Roland Levillaindff1f282014-11-05 14:15:05 +00002883 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2884};
2885
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002886static constexpr uint32_t kNoRegNumber = -1;
2887
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002888class HPhi : public HInstruction {
2889 public:
2890 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002891 : HInstruction(SideEffects::None()),
2892 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002893 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002894 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002895 is_live_(false),
2896 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002897 inputs_.SetSize(number_of_inputs);
2898 }
2899
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002900 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2901 static Primitive::Type ToPhiType(Primitive::Type type) {
2902 switch (type) {
2903 case Primitive::kPrimBoolean:
2904 case Primitive::kPrimByte:
2905 case Primitive::kPrimShort:
2906 case Primitive::kPrimChar:
2907 return Primitive::kPrimInt;
2908 default:
2909 return type;
2910 }
2911 }
2912
Calin Juravle10e244f2015-01-26 18:54:32 +00002913 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002914
2915 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01002916 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002917
Calin Juravle10e244f2015-01-26 18:54:32 +00002918 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002919 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002920
Calin Juravle10e244f2015-01-26 18:54:32 +00002921 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2922 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2923
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002924 uint32_t GetRegNumber() const { return reg_number_; }
2925
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002926 void SetDead() { is_live_ = false; }
2927 void SetLive() { is_live_ = true; }
2928 bool IsDead() const { return !is_live_; }
2929 bool IsLive() const { return is_live_; }
2930
Calin Juravlea4f88312015-04-16 12:57:19 +01002931 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2932 // An equivalent phi is a phi having the same dex register and type.
2933 // It assumes that phis with the same dex register are adjacent.
2934 HPhi* GetNextEquivalentPhiWithSameType() {
2935 HInstruction* next = GetNext();
2936 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2937 if (next->GetType() == GetType()) {
2938 return next->AsPhi();
2939 }
2940 next = next->GetNext();
2941 }
2942 return nullptr;
2943 }
2944
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002945 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002946
David Brazdil1abb4192015-02-17 18:33:36 +00002947 protected:
2948 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2949
2950 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2951 inputs_.Put(index, input);
2952 }
2953
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002954 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002955 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002956 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002957 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002958 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002959 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002960
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002961 DISALLOW_COPY_AND_ASSIGN(HPhi);
2962};
2963
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002964class HNullCheck : public HExpression<1> {
2965 public:
2966 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002967 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002968 SetRawInputAt(0, value);
2969 }
2970
Calin Juravle10e244f2015-01-26 18:54:32 +00002971 bool CanBeMoved() const OVERRIDE { return true; }
2972 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002973 UNUSED(other);
2974 return true;
2975 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002976
Calin Juravle10e244f2015-01-26 18:54:32 +00002977 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002978
Calin Juravle10e244f2015-01-26 18:54:32 +00002979 bool CanThrow() const OVERRIDE { return true; }
2980
2981 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002982
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002983 uint32_t GetDexPc() const { return dex_pc_; }
2984
2985 DECLARE_INSTRUCTION(NullCheck);
2986
2987 private:
2988 const uint32_t dex_pc_;
2989
2990 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2991};
2992
2993class FieldInfo : public ValueObject {
2994 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002995 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2996 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002997
2998 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002999 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003000 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003001
3002 private:
3003 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003004 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003005 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003006};
3007
3008class HInstanceFieldGet : public HExpression<1> {
3009 public:
3010 HInstanceFieldGet(HInstruction* value,
3011 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003012 MemberOffset field_offset,
3013 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003014 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003015 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003016 SetRawInputAt(0, value);
3017 }
3018
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003019 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003020
3021 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3022 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3023 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003024 }
3025
Calin Juravle641547a2015-04-21 22:08:51 +01003026 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3027 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003028 }
3029
3030 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003031 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3032 }
3033
Calin Juravle52c48962014-12-16 17:02:57 +00003034 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003035 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003036 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003037 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003038
3039 DECLARE_INSTRUCTION(InstanceFieldGet);
3040
3041 private:
3042 const FieldInfo field_info_;
3043
3044 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3045};
3046
3047class HInstanceFieldSet : public HTemplateInstruction<2> {
3048 public:
3049 HInstanceFieldSet(HInstruction* object,
3050 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003051 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003052 MemberOffset field_offset,
3053 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003054 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003055 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003056 SetRawInputAt(0, object);
3057 SetRawInputAt(1, value);
3058 }
3059
Calin Juravle641547a2015-04-21 22:08:51 +01003060 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3061 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003062 }
3063
Calin Juravle52c48962014-12-16 17:02:57 +00003064 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003065 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003066 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003067 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003068 HInstruction* GetValue() const { return InputAt(1); }
3069
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003070 DECLARE_INSTRUCTION(InstanceFieldSet);
3071
3072 private:
3073 const FieldInfo field_info_;
3074
3075 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3076};
3077
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003078class HArrayGet : public HExpression<2> {
3079 public:
3080 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003081 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003082 SetRawInputAt(0, array);
3083 SetRawInputAt(1, index);
3084 }
3085
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003086 bool CanBeMoved() const OVERRIDE { return true; }
3087 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003088 UNUSED(other);
3089 return true;
3090 }
Calin Juravle641547a2015-04-21 22:08:51 +01003091 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3092 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003093 // TODO: We can be smarter here.
3094 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3095 // which generates the implicit null check. There are cases when these can be removed
3096 // to produce better code. If we ever add optimizations to do so we should allow an
3097 // implicit check here (as long as the address falls in the first page).
3098 return false;
3099 }
3100
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003101 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003102
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003103 HInstruction* GetArray() const { return InputAt(0); }
3104 HInstruction* GetIndex() const { return InputAt(1); }
3105
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003106 DECLARE_INSTRUCTION(ArrayGet);
3107
3108 private:
3109 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3110};
3111
3112class HArraySet : public HTemplateInstruction<3> {
3113 public:
3114 HArraySet(HInstruction* array,
3115 HInstruction* index,
3116 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003117 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003118 uint32_t dex_pc)
3119 : HTemplateInstruction(SideEffects::ChangesSomething()),
3120 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003121 expected_component_type_(expected_component_type),
3122 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003123 SetRawInputAt(0, array);
3124 SetRawInputAt(1, index);
3125 SetRawInputAt(2, value);
3126 }
3127
Calin Juravle77520bc2015-01-12 18:45:46 +00003128 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003129 // We currently always call a runtime method to catch array store
3130 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003131 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003132 }
3133
Calin Juravle641547a2015-04-21 22:08:51 +01003134 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3135 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003136 // TODO: Same as for ArrayGet.
3137 return false;
3138 }
3139
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003140 void ClearNeedsTypeCheck() {
3141 needs_type_check_ = false;
3142 }
3143
3144 bool NeedsTypeCheck() const { return needs_type_check_; }
3145
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003146 uint32_t GetDexPc() const { return dex_pc_; }
3147
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003148 HInstruction* GetArray() const { return InputAt(0); }
3149 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003150 HInstruction* GetValue() const { return InputAt(2); }
3151
3152 Primitive::Type GetComponentType() const {
3153 // The Dex format does not type floating point index operations. Since the
3154 // `expected_component_type_` is set during building and can therefore not
3155 // be correct, we also check what is the value type. If it is a floating
3156 // point type, we must use that type.
3157 Primitive::Type value_type = GetValue()->GetType();
3158 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3159 ? value_type
3160 : expected_component_type_;
3161 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003162
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003163 DECLARE_INSTRUCTION(ArraySet);
3164
3165 private:
3166 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003167 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003168 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003169
3170 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3171};
3172
3173class HArrayLength : public HExpression<1> {
3174 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003175 explicit HArrayLength(HInstruction* array)
3176 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3177 // Note that arrays do not change length, so the instruction does not
3178 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003179 SetRawInputAt(0, array);
3180 }
3181
Calin Juravle77520bc2015-01-12 18:45:46 +00003182 bool CanBeMoved() const OVERRIDE { return true; }
3183 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003184 UNUSED(other);
3185 return true;
3186 }
Calin Juravle641547a2015-04-21 22:08:51 +01003187 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3188 return obj == InputAt(0);
3189 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003190
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003191 DECLARE_INSTRUCTION(ArrayLength);
3192
3193 private:
3194 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3195};
3196
3197class HBoundsCheck : public HExpression<2> {
3198 public:
3199 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003200 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003201 DCHECK(index->GetType() == Primitive::kPrimInt);
3202 SetRawInputAt(0, index);
3203 SetRawInputAt(1, length);
3204 }
3205
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003206 bool CanBeMoved() const OVERRIDE { return true; }
3207 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003208 UNUSED(other);
3209 return true;
3210 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003211
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003212 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003213
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003214 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003215
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003216 uint32_t GetDexPc() const { return dex_pc_; }
3217
3218 DECLARE_INSTRUCTION(BoundsCheck);
3219
3220 private:
3221 const uint32_t dex_pc_;
3222
3223 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3224};
3225
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003226/**
3227 * Some DEX instructions are folded into multiple HInstructions that need
3228 * to stay live until the last HInstruction. This class
3229 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003230 * HInstruction stays live. `index` represents the stack location index of the
3231 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003232 */
3233class HTemporary : public HTemplateInstruction<0> {
3234 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003235 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003236
3237 size_t GetIndex() const { return index_; }
3238
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003239 Primitive::Type GetType() const OVERRIDE {
3240 // The previous instruction is the one that will be stored in the temporary location.
3241 DCHECK(GetPrevious() != nullptr);
3242 return GetPrevious()->GetType();
3243 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003244
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003245 DECLARE_INSTRUCTION(Temporary);
3246
3247 private:
3248 const size_t index_;
3249
3250 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3251};
3252
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003253class HSuspendCheck : public HTemplateInstruction<0> {
3254 public:
3255 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003256 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003257
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003258 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003259 return true;
3260 }
3261
3262 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003263 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3264 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003265
3266 DECLARE_INSTRUCTION(SuspendCheck);
3267
3268 private:
3269 const uint32_t dex_pc_;
3270
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003271 // Only used for code generation, in order to share the same slow path between back edges
3272 // of a same loop.
3273 SlowPathCode* slow_path_;
3274
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003275 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3276};
3277
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003278/**
3279 * Instruction to load a Class object.
3280 */
3281class HLoadClass : public HExpression<0> {
3282 public:
3283 HLoadClass(uint16_t type_index,
3284 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003285 uint32_t dex_pc)
3286 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3287 type_index_(type_index),
3288 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003289 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003290 generate_clinit_check_(false),
3291 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003292
3293 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003294
3295 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3296 return other->AsLoadClass()->type_index_ == type_index_;
3297 }
3298
3299 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3300
3301 uint32_t GetDexPc() const { return dex_pc_; }
3302 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003303 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003304
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003305 bool NeedsEnvironment() const OVERRIDE {
3306 // Will call runtime and load the class if the class is not loaded yet.
3307 // TODO: finer grain decision.
3308 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003309 }
3310
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003311 bool MustGenerateClinitCheck() const {
3312 return generate_clinit_check_;
3313 }
3314
3315 void SetMustGenerateClinitCheck() {
3316 generate_clinit_check_ = true;
3317 }
3318
3319 bool CanCallRuntime() const {
3320 return MustGenerateClinitCheck() || !is_referrers_class_;
3321 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003322
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003323 bool CanThrow() const OVERRIDE {
3324 // May call runtime and and therefore can throw.
3325 // TODO: finer grain decision.
3326 return !is_referrers_class_;
3327 }
3328
Calin Juravleacf735c2015-02-12 15:25:22 +00003329 ReferenceTypeInfo GetLoadedClassRTI() {
3330 return loaded_class_rti_;
3331 }
3332
3333 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3334 // Make sure we only set exact types (the loaded class should never be merged).
3335 DCHECK(rti.IsExact());
3336 loaded_class_rti_ = rti;
3337 }
3338
3339 bool IsResolved() {
3340 return loaded_class_rti_.IsExact();
3341 }
3342
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003343 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3344
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003345 DECLARE_INSTRUCTION(LoadClass);
3346
3347 private:
3348 const uint16_t type_index_;
3349 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003350 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003351 // Whether this instruction must generate the initialization check.
3352 // Used for code generation.
3353 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003354
Calin Juravleacf735c2015-02-12 15:25:22 +00003355 ReferenceTypeInfo loaded_class_rti_;
3356
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003357 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3358};
3359
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003360class HLoadString : public HExpression<0> {
3361 public:
3362 HLoadString(uint32_t string_index, uint32_t dex_pc)
3363 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3364 string_index_(string_index),
3365 dex_pc_(dex_pc) {}
3366
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003367 bool CanBeMoved() const OVERRIDE { return true; }
3368
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003369 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3370 return other->AsLoadString()->string_index_ == string_index_;
3371 }
3372
3373 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3374
3375 uint32_t GetDexPc() const { return dex_pc_; }
3376 uint32_t GetStringIndex() const { return string_index_; }
3377
3378 // TODO: Can we deopt or debug when we resolve a string?
3379 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003380 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003381
3382 DECLARE_INSTRUCTION(LoadString);
3383
3384 private:
3385 const uint32_t string_index_;
3386 const uint32_t dex_pc_;
3387
3388 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3389};
3390
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003391/**
3392 * Performs an initialization check on its Class object input.
3393 */
3394class HClinitCheck : public HExpression<1> {
3395 public:
3396 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003397 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003398 dex_pc_(dex_pc) {
3399 SetRawInputAt(0, constant);
3400 }
3401
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003402 bool CanBeMoved() const OVERRIDE { return true; }
3403 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3404 UNUSED(other);
3405 return true;
3406 }
3407
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003408 bool NeedsEnvironment() const OVERRIDE {
3409 // May call runtime to initialize the class.
3410 return true;
3411 }
3412
3413 uint32_t GetDexPc() const { return dex_pc_; }
3414
3415 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3416
3417 DECLARE_INSTRUCTION(ClinitCheck);
3418
3419 private:
3420 const uint32_t dex_pc_;
3421
3422 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3423};
3424
3425class HStaticFieldGet : public HExpression<1> {
3426 public:
3427 HStaticFieldGet(HInstruction* cls,
3428 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003429 MemberOffset field_offset,
3430 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003431 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003432 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003433 SetRawInputAt(0, cls);
3434 }
3435
Calin Juravle52c48962014-12-16 17:02:57 +00003436
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003437 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003438
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003439 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003440 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3441 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003442 }
3443
3444 size_t ComputeHashCode() const OVERRIDE {
3445 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3446 }
3447
Calin Juravle52c48962014-12-16 17:02:57 +00003448 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003449 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3450 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003451 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003452
3453 DECLARE_INSTRUCTION(StaticFieldGet);
3454
3455 private:
3456 const FieldInfo field_info_;
3457
3458 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3459};
3460
3461class HStaticFieldSet : public HTemplateInstruction<2> {
3462 public:
3463 HStaticFieldSet(HInstruction* cls,
3464 HInstruction* value,
3465 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003466 MemberOffset field_offset,
3467 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003468 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003469 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003470 SetRawInputAt(0, cls);
3471 SetRawInputAt(1, value);
3472 }
3473
Calin Juravle52c48962014-12-16 17:02:57 +00003474 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003475 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3476 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003477 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003478
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003479 HInstruction* GetValue() const { return InputAt(1); }
3480
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003481 DECLARE_INSTRUCTION(StaticFieldSet);
3482
3483 private:
3484 const FieldInfo field_info_;
3485
3486 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3487};
3488
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003489// Implement the move-exception DEX instruction.
3490class HLoadException : public HExpression<0> {
3491 public:
3492 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3493
3494 DECLARE_INSTRUCTION(LoadException);
3495
3496 private:
3497 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3498};
3499
3500class HThrow : public HTemplateInstruction<1> {
3501 public:
3502 HThrow(HInstruction* exception, uint32_t dex_pc)
3503 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3504 SetRawInputAt(0, exception);
3505 }
3506
3507 bool IsControlFlow() const OVERRIDE { return true; }
3508
3509 bool NeedsEnvironment() const OVERRIDE { return true; }
3510
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003511 bool CanThrow() const OVERRIDE { return true; }
3512
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003513 uint32_t GetDexPc() const { return dex_pc_; }
3514
3515 DECLARE_INSTRUCTION(Throw);
3516
3517 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003518 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003519
3520 DISALLOW_COPY_AND_ASSIGN(HThrow);
3521};
3522
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003523class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003524 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003525 HInstanceOf(HInstruction* object,
3526 HLoadClass* constant,
3527 bool class_is_final,
3528 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003529 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3530 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003531 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003532 dex_pc_(dex_pc) {
3533 SetRawInputAt(0, object);
3534 SetRawInputAt(1, constant);
3535 }
3536
3537 bool CanBeMoved() const OVERRIDE { return true; }
3538
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003539 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003540 return true;
3541 }
3542
3543 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003544 return false;
3545 }
3546
3547 uint32_t GetDexPc() const { return dex_pc_; }
3548
3549 bool IsClassFinal() const { return class_is_final_; }
3550
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003551 // Used only in code generation.
3552 bool MustDoNullCheck() const { return must_do_null_check_; }
3553 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3554
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003555 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003556
3557 private:
3558 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003559 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003560 const uint32_t dex_pc_;
3561
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003562 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3563};
3564
Calin Juravleb1498f62015-02-16 13:13:29 +00003565class HBoundType : public HExpression<1> {
3566 public:
3567 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3568 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3569 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003570 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003571 SetRawInputAt(0, input);
3572 }
3573
3574 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3575
3576 bool CanBeNull() const OVERRIDE {
3577 // `null instanceof ClassX` always return false so we can't be null.
3578 return false;
3579 }
3580
3581 DECLARE_INSTRUCTION(BoundType);
3582
3583 private:
3584 // Encodes the most upper class that this instruction can have. In other words
3585 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3586 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3587 const ReferenceTypeInfo bound_type_;
3588
3589 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3590};
3591
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003592class HCheckCast : public HTemplateInstruction<2> {
3593 public:
3594 HCheckCast(HInstruction* object,
3595 HLoadClass* constant,
3596 bool class_is_final,
3597 uint32_t dex_pc)
3598 : HTemplateInstruction(SideEffects::None()),
3599 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003600 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003601 dex_pc_(dex_pc) {
3602 SetRawInputAt(0, object);
3603 SetRawInputAt(1, constant);
3604 }
3605
3606 bool CanBeMoved() const OVERRIDE { return true; }
3607
3608 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3609 return true;
3610 }
3611
3612 bool NeedsEnvironment() const OVERRIDE {
3613 // Instruction may throw a CheckCastError.
3614 return true;
3615 }
3616
3617 bool CanThrow() const OVERRIDE { return true; }
3618
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003619 bool MustDoNullCheck() const { return must_do_null_check_; }
3620 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3621
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003622 uint32_t GetDexPc() const { return dex_pc_; }
3623
3624 bool IsClassFinal() const { return class_is_final_; }
3625
3626 DECLARE_INSTRUCTION(CheckCast);
3627
3628 private:
3629 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003630 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003631 const uint32_t dex_pc_;
3632
3633 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003634};
3635
Calin Juravle27df7582015-04-17 19:12:31 +01003636class HMemoryBarrier : public HTemplateInstruction<0> {
3637 public:
3638 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3639 : HTemplateInstruction(SideEffects::None()),
3640 barrier_kind_(barrier_kind) {}
3641
3642 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3643
3644 DECLARE_INSTRUCTION(MemoryBarrier);
3645
3646 private:
3647 const MemBarrierKind barrier_kind_;
3648
3649 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3650};
3651
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003652class HMonitorOperation : public HTemplateInstruction<1> {
3653 public:
3654 enum OperationKind {
3655 kEnter,
3656 kExit,
3657 };
3658
3659 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3660 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3661 SetRawInputAt(0, object);
3662 }
3663
3664 // Instruction may throw a Java exception, so we need an environment.
3665 bool NeedsEnvironment() const OVERRIDE { return true; }
3666 bool CanThrow() const OVERRIDE { return true; }
3667
3668 uint32_t GetDexPc() const { return dex_pc_; }
3669
3670 bool IsEnter() const { return kind_ == kEnter; }
3671
3672 DECLARE_INSTRUCTION(MonitorOperation);
3673
Calin Juravle52c48962014-12-16 17:02:57 +00003674 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003675 const OperationKind kind_;
3676 const uint32_t dex_pc_;
3677
3678 private:
3679 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3680};
3681
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003682class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003683 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003684 MoveOperands(Location source,
3685 Location destination,
3686 Primitive::Type type,
3687 HInstruction* instruction)
3688 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003689
3690 Location GetSource() const { return source_; }
3691 Location GetDestination() const { return destination_; }
3692
3693 void SetSource(Location value) { source_ = value; }
3694 void SetDestination(Location value) { destination_ = value; }
3695
3696 // The parallel move resolver marks moves as "in-progress" by clearing the
3697 // destination (but not the source).
3698 Location MarkPending() {
3699 DCHECK(!IsPending());
3700 Location dest = destination_;
3701 destination_ = Location::NoLocation();
3702 return dest;
3703 }
3704
3705 void ClearPending(Location dest) {
3706 DCHECK(IsPending());
3707 destination_ = dest;
3708 }
3709
3710 bool IsPending() const {
3711 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3712 return destination_.IsInvalid() && !source_.IsInvalid();
3713 }
3714
3715 // True if this blocks a move from the given location.
3716 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003717 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003718 }
3719
3720 // A move is redundant if it's been eliminated, if its source and
3721 // destination are the same, or if its destination is unneeded.
3722 bool IsRedundant() const {
3723 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3724 }
3725
3726 // We clear both operands to indicate move that's been eliminated.
3727 void Eliminate() {
3728 source_ = destination_ = Location::NoLocation();
3729 }
3730
3731 bool IsEliminated() const {
3732 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3733 return source_.IsInvalid();
3734 }
3735
Nicolas Geoffray90218252015-04-15 11:56:51 +01003736 bool Is64BitMove() const {
3737 return Primitive::Is64BitType(type_);
3738 }
3739
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003740 HInstruction* GetInstruction() const { return instruction_; }
3741
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003742 private:
3743 Location source_;
3744 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003745 // The type this move is for.
3746 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003747 // The instruction this move is assocatied with. Null when this move is
3748 // for moving an input in the expected locations of user (including a phi user).
3749 // This is only used in debug mode, to ensure we do not connect interval siblings
3750 // in the same parallel move.
3751 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003752};
3753
3754static constexpr size_t kDefaultNumberOfMoves = 4;
3755
3756class HParallelMove : public HTemplateInstruction<0> {
3757 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003758 explicit HParallelMove(ArenaAllocator* arena)
3759 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003760
Nicolas Geoffray90218252015-04-15 11:56:51 +01003761 void AddMove(Location source,
3762 Location destination,
3763 Primitive::Type type,
3764 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003765 DCHECK(source.IsValid());
3766 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003767 if (kIsDebugBuild) {
3768 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003769 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003770 if (moves_.Get(i).GetInstruction() == instruction) {
3771 // Special case the situation where the move is for the spill slot
3772 // of the instruction.
3773 if ((GetPrevious() == instruction)
3774 || ((GetPrevious() == nullptr)
3775 && instruction->IsPhi()
3776 && instruction->GetBlock() == GetBlock())) {
3777 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3778 << "Doing parallel moves for the same instruction.";
3779 } else {
3780 DCHECK(false) << "Doing parallel moves for the same instruction.";
3781 }
3782 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003783 }
3784 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003785 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003786 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3787 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003788 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003789 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003790 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003791 }
3792
3793 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003794 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003795 }
3796
3797 size_t NumMoves() const { return moves_.Size(); }
3798
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003799 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003800
3801 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003802 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003803
3804 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3805};
3806
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003807class HGraphVisitor : public ValueObject {
3808 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003809 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3810 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003811
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003812 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003813 virtual void VisitBasicBlock(HBasicBlock* block);
3814
Roland Levillain633021e2014-10-01 14:12:25 +01003815 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003816 void VisitInsertionOrder();
3817
Roland Levillain633021e2014-10-01 14:12:25 +01003818 // Visit the graph following dominator tree reverse post-order.
3819 void VisitReversePostOrder();
3820
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003821 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003822
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003823 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003824#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003825 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3826
3827 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3828
3829#undef DECLARE_VISIT_INSTRUCTION
3830
3831 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003832 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003833
3834 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3835};
3836
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003837class HGraphDelegateVisitor : public HGraphVisitor {
3838 public:
3839 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3840 virtual ~HGraphDelegateVisitor() {}
3841
3842 // Visit functions that delegate to to super class.
3843#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003844 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003845
3846 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3847
3848#undef DECLARE_VISIT_INSTRUCTION
3849
3850 private:
3851 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3852};
3853
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003854class HInsertionOrderIterator : public ValueObject {
3855 public:
3856 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3857
3858 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3859 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3860 void Advance() { ++index_; }
3861
3862 private:
3863 const HGraph& graph_;
3864 size_t index_;
3865
3866 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3867};
3868
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003869class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003870 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003871 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3872 // Check that reverse post order of the graph has been built.
3873 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3874 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003875
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003876 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3877 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003878 void Advance() { ++index_; }
3879
3880 private:
3881 const HGraph& graph_;
3882 size_t index_;
3883
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003884 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003885};
3886
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003887class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003888 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003889 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003890 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3891 // Check that reverse post order of the graph has been built.
3892 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3893 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003894
3895 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003896 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003897 void Advance() { --index_; }
3898
3899 private:
3900 const HGraph& graph_;
3901 size_t index_;
3902
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003903 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003904};
3905
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003906class HLinearPostOrderIterator : public ValueObject {
3907 public:
3908 explicit HLinearPostOrderIterator(const HGraph& graph)
3909 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3910
3911 bool Done() const { return index_ == 0; }
3912
3913 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3914
3915 void Advance() {
3916 --index_;
3917 DCHECK_GE(index_, 0U);
3918 }
3919
3920 private:
3921 const GrowableArray<HBasicBlock*>& order_;
3922 size_t index_;
3923
3924 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3925};
3926
3927class HLinearOrderIterator : public ValueObject {
3928 public:
3929 explicit HLinearOrderIterator(const HGraph& graph)
3930 : order_(graph.GetLinearOrder()), index_(0) {}
3931
3932 bool Done() const { return index_ == order_.Size(); }
3933 HBasicBlock* Current() const { return order_.Get(index_); }
3934 void Advance() { ++index_; }
3935
3936 private:
3937 const GrowableArray<HBasicBlock*>& order_;
3938 size_t index_;
3939
3940 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3941};
3942
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003943// Iterator over the blocks that art part of the loop. Includes blocks part
3944// of an inner loop. The order in which the blocks are iterated is on their
3945// block id.
3946class HBlocksInLoopIterator : public ValueObject {
3947 public:
3948 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3949 : blocks_in_loop_(info.GetBlocks()),
3950 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3951 index_(0) {
3952 if (!blocks_in_loop_.IsBitSet(index_)) {
3953 Advance();
3954 }
3955 }
3956
3957 bool Done() const { return index_ == blocks_.Size(); }
3958 HBasicBlock* Current() const { return blocks_.Get(index_); }
3959 void Advance() {
3960 ++index_;
3961 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3962 if (blocks_in_loop_.IsBitSet(index_)) {
3963 break;
3964 }
3965 }
3966 }
3967
3968 private:
3969 const BitVector& blocks_in_loop_;
3970 const GrowableArray<HBasicBlock*>& blocks_;
3971 size_t index_;
3972
3973 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3974};
3975
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003976inline int64_t Int64FromConstant(HConstant* constant) {
3977 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3978 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3979 : constant->AsLongConstant()->GetValue();
3980}
3981
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003982} // namespace art
3983
3984#endif // ART_COMPILER_OPTIMIZING_NODES_H_