blob: 67c6b3c1d45f3b53af9c2d8fef5a9a94e7008f15 [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
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010063static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
64
Dave Allison20dfc792014-06-16 20:44:29 -070065enum IfCondition {
66 kCondEQ,
67 kCondNE,
68 kCondLT,
69 kCondLE,
70 kCondGT,
71 kCondGE,
72};
73
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010074class HInstructionList {
75 public:
76 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
77
78 void AddInstruction(HInstruction* instruction);
79 void RemoveInstruction(HInstruction* instruction);
80
David Brazdilc3d743f2015-04-22 13:40:50 +010081 // Insert `instruction` before/after an existing instruction `cursor`.
82 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
83 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
84
Roland Levillain6b469232014-09-25 10:10:38 +010085 // Return true if this list contains `instruction`.
86 bool Contains(HInstruction* instruction) const;
87
Roland Levillainccc07a92014-09-16 14:48:16 +010088 // Return true if `instruction1` is found before `instruction2` in
89 // this instruction list and false otherwise. Abort if none
90 // of these instructions is found.
91 bool FoundBefore(const HInstruction* instruction1,
92 const HInstruction* instruction2) const;
93
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000094 bool IsEmpty() const { return first_instruction_ == nullptr; }
95 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
96
97 // Update the block of all instructions to be `block`.
98 void SetBlockOfInstructions(HBasicBlock* block) const;
99
100 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
101 void Add(const HInstructionList& instruction_list);
102
David Brazdil2d7352b2015-04-20 14:52:42 +0100103 // Return the number of instructions in the list. This is an expensive operation.
104 size_t CountSize() const;
105
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100106 private:
107 HInstruction* first_instruction_;
108 HInstruction* last_instruction_;
109
110 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000111 friend class HGraph;
112 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100113 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100114 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100115
116 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
117};
118
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000119// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700120class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000121 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100122 HGraph(ArenaAllocator* arena,
123 const DexFile& dex_file,
124 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100125 bool should_generate_constructor_barrier,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100126 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100127 bool debuggable = false,
128 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000129 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000130 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100131 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100132 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700133 entry_block_(nullptr),
134 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100135 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100136 number_of_vregs_(0),
137 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000138 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400139 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000140 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000141 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100142 dex_file_(dex_file),
143 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100144 invoke_type_(invoke_type),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100145 should_generate_constructor_barrier_(should_generate_constructor_barrier),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000146 cached_null_constant_(nullptr),
147 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000148 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
149 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
150 cached_double_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000151
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000152 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100153 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100154 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000155
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000156 HBasicBlock* GetEntryBlock() const { return entry_block_; }
157 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000158
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000159 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
160 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000161
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000162 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100163
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000164 // Try building the SSA form of this graph, with dominance computation and loop
165 // recognition. Returns whether it was successful in doing all these steps.
166 bool TryBuildingSsa() {
167 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000168 // The SSA builder requires loops to all be natural. Specifically, the dead phi
169 // elimination phase checks the consistency of the graph when doing a post-order
170 // visit for eliminating dead phis: a dead phi can only have loop header phi
171 // users remaining when being visited.
172 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000173 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000174 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000175 }
176
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000177 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000178 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100179 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000180
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000181 // Analyze all natural loops in this graph. Returns false if one
182 // loop is not natural, that is the header does not dominate the
183 // back edge.
184 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100185
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000186 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
187 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
188
David Brazdil2d7352b2015-04-20 14:52:42 +0100189 // Removes `block` from the graph.
190 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000191
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100192 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
193 void SimplifyLoop(HBasicBlock* header);
194
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000195 int32_t GetNextInstructionId() {
196 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000197 return current_instruction_id_++;
198 }
199
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000200 int32_t GetCurrentInstructionId() const {
201 return current_instruction_id_;
202 }
203
204 void SetCurrentInstructionId(int32_t id) {
205 current_instruction_id_ = id;
206 }
207
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100208 uint16_t GetMaximumNumberOfOutVRegs() const {
209 return maximum_number_of_out_vregs_;
210 }
211
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
213 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100214 }
215
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000216 void UpdateTemporariesVRegSlots(size_t slots) {
217 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100218 }
219
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000220 size_t GetTemporariesVRegSlots() const {
221 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100222 }
223
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100224 void SetNumberOfVRegs(uint16_t number_of_vregs) {
225 number_of_vregs_ = number_of_vregs;
226 }
227
228 uint16_t GetNumberOfVRegs() const {
229 return number_of_vregs_;
230 }
231
232 void SetNumberOfInVRegs(uint16_t value) {
233 number_of_in_vregs_ = value;
234 }
235
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100236 uint16_t GetNumberOfLocalVRegs() const {
237 return number_of_vregs_ - number_of_in_vregs_;
238 }
239
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100240 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
241 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100242 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100243
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100244 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
245 return linear_order_;
246 }
247
Mark Mendell1152c922015-04-24 17:06:35 -0400248 bool HasBoundsChecks() const {
249 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800250 }
251
Mark Mendell1152c922015-04-24 17:06:35 -0400252 void SetHasBoundsChecks(bool value) {
253 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800254 }
255
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100256 bool ShouldGenerateConstructorBarrier() const {
257 return should_generate_constructor_barrier_;
258 }
259
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000260 bool IsDebuggable() const { return debuggable_; }
261
David Brazdil8d5b8b22015-03-24 10:51:52 +0000262 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000263 // already, it is created and inserted into the graph. This method is only for
264 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000265 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000266 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000267 HIntConstant* GetIntConstant(int32_t value) {
268 return CreateConstant(value, &cached_int_constants_);
269 }
270 HLongConstant* GetLongConstant(int64_t value) {
271 return CreateConstant(value, &cached_long_constants_);
272 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000273 HFloatConstant* GetFloatConstant(float value) {
274 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
275 }
276 HDoubleConstant* GetDoubleConstant(double value) {
277 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
278 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000279
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000280 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100281
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100282 const DexFile& GetDexFile() const {
283 return dex_file_;
284 }
285
286 uint32_t GetMethodIdx() const {
287 return method_idx_;
288 }
289
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100290 InvokeType GetInvokeType() const {
291 return invoke_type_;
292 }
293
David Brazdil2d7352b2015-04-20 14:52:42 +0100294 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000295 void VisitBlockForDominatorTree(HBasicBlock* block,
296 HBasicBlock* predecessor,
297 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100298 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000299 void VisitBlockForBackEdges(HBasicBlock* block,
300 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100301 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000302 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100303 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000304
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000305 template <class InstructionType, typename ValueType>
306 InstructionType* CreateConstant(ValueType value,
307 ArenaSafeMap<ValueType, InstructionType*>* cache) {
308 // Try to find an existing constant of the given value.
309 InstructionType* constant = nullptr;
310 auto cached_constant = cache->find(value);
311 if (cached_constant != cache->end()) {
312 constant = cached_constant->second;
313 }
314
315 // If not found or previously deleted, create and cache a new instruction.
316 if (constant == nullptr || constant->GetBlock() == nullptr) {
317 constant = new (arena_) InstructionType(value);
318 cache->Overwrite(value, constant);
319 InsertConstant(constant);
320 }
321 return constant;
322 }
323
David Brazdil8d5b8b22015-03-24 10:51:52 +0000324 void InsertConstant(HConstant* instruction);
325
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000326 // Cache a float constant into the graph. This method should only be
327 // called by the SsaBuilder when creating "equivalent" instructions.
328 void CacheFloatConstant(HFloatConstant* constant);
329
330 // See CacheFloatConstant comment.
331 void CacheDoubleConstant(HDoubleConstant* constant);
332
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000333 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000334
335 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000336 GrowableArray<HBasicBlock*> blocks_;
337
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100338 // List of blocks to perform a reverse post order tree traversal.
339 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000340
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100341 // List of blocks to perform a linear order tree traversal.
342 GrowableArray<HBasicBlock*> linear_order_;
343
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000344 HBasicBlock* entry_block_;
345 HBasicBlock* exit_block_;
346
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100347 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100348 uint16_t maximum_number_of_out_vregs_;
349
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100350 // The number of virtual registers in this method. Contains the parameters.
351 uint16_t number_of_vregs_;
352
353 // The number of virtual registers used by parameters of this method.
354 uint16_t number_of_in_vregs_;
355
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000356 // Number of vreg size slots that the temporaries use (used in baseline compiler).
357 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100358
Mark Mendell1152c922015-04-24 17:06:35 -0400359 // Has bounds checks. We can totally skip BCE if it's false.
360 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800361
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000362 // Indicates whether the graph should be compiled in a way that
363 // ensures full debuggability. If false, we can apply more
364 // aggressive optimizations that may limit the level of debugging.
365 const bool debuggable_;
366
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000367 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000368 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000369
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100370 // The dex file from which the method is from.
371 const DexFile& dex_file_;
372
373 // The method index in the dex file.
374 const uint32_t method_idx_;
375
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100376 // If inlined, this encodes how the callee is being invoked.
377 const InvokeType invoke_type_;
378
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100379 const bool should_generate_constructor_barrier_;
380
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000381 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000382 HNullConstant* cached_null_constant_;
383 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000384 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000385 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000386 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000387
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000388 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100389 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000390 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000391 DISALLOW_COPY_AND_ASSIGN(HGraph);
392};
393
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700394class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000395 public:
396 HLoopInformation(HBasicBlock* header, HGraph* graph)
397 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100398 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100399 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100400 // Make bit vector growable, as the number of blocks may change.
401 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100402
403 HBasicBlock* GetHeader() const {
404 return header_;
405 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000406
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000407 void SetHeader(HBasicBlock* block) {
408 header_ = block;
409 }
410
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100411 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
412 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
413 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
414
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000415 void AddBackEdge(HBasicBlock* back_edge) {
416 back_edges_.Add(back_edge);
417 }
418
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 void RemoveBackEdge(HBasicBlock* back_edge) {
420 back_edges_.Delete(back_edge);
421 }
422
David Brazdil46e2a392015-03-16 17:31:52 +0000423 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100424 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000425 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100426 }
427 return false;
428 }
429
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000430 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000431 return back_edges_.Size();
432 }
433
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100434 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100435
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100436 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
437 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100438 }
439
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100440 // Returns the lifetime position of the back edge that has the
441 // greatest lifetime position.
442 size_t GetLifetimeEnd() const;
443
444 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
445 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
446 if (back_edges_.Get(i) == existing) {
447 back_edges_.Put(i, new_back_edge);
448 return;
449 }
450 }
451 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100452 }
453
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100454 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100455 // that is the header dominates the back edge.
456 bool Populate();
457
David Brazdila4b8c212015-05-07 09:59:30 +0100458 // Reanalyzes the loop by removing loop info from its blocks and re-running
459 // Populate(). If there are no back edges left, the loop info is completely
460 // removed as well as its SuspendCheck instruction. It must be run on nested
461 // inner loops first.
462 void Update();
463
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100464 // Returns whether this loop information contains `block`.
465 // Note that this loop information *must* be populated before entering this function.
466 bool Contains(const HBasicBlock& block) const;
467
468 // Returns whether this loop information is an inner loop of `other`.
469 // Note that `other` *must* be populated before entering this function.
470 bool IsIn(const HLoopInformation& other) const;
471
472 const ArenaBitVector& GetBlocks() const { return blocks_; }
473
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000474 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000475 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000476
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000477 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100478 // Internal recursive implementation of `Populate`.
479 void PopulateRecursive(HBasicBlock* block);
480
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000481 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100482 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000483 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100484 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000485
486 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
487};
488
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100489static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100490static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100491
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000492// A block in a method. Contains the list of instructions represented
493// as a double linked list. Each block knows its predecessors and
494// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100495
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700496class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000497 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100498 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000499 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000500 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
501 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000502 loop_information_(nullptr),
503 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100504 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100505 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100506 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100507 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000508 lifetime_end_(kNoLifetime),
509 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000510
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100511 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
512 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000513 }
514
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100515 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
516 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000517 }
518
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100519 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
520 return dominated_blocks_;
521 }
522
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100523 bool IsEntryBlock() const {
524 return graph_->GetEntryBlock() == this;
525 }
526
527 bool IsExitBlock() const {
528 return graph_->GetExitBlock() == this;
529 }
530
David Brazdil46e2a392015-03-16 17:31:52 +0000531 bool IsSingleGoto() const;
532
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000533 void AddBackEdge(HBasicBlock* back_edge) {
534 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000535 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000536 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100537 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000538 loop_information_->AddBackEdge(back_edge);
539 }
540
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000541 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000542 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000543
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000544 int GetBlockId() const { return block_id_; }
545 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000546
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000547 HBasicBlock* GetDominator() const { return dominator_; }
548 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100549 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100550 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000551 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
552 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
553 if (dominated_blocks_.Get(i) == existing) {
554 dominated_blocks_.Put(i, new_block);
555 return;
556 }
557 }
558 LOG(FATAL) << "Unreachable";
559 UNREACHABLE();
560 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000561
562 int NumberOfBackEdges() const {
563 return loop_information_ == nullptr
564 ? 0
565 : loop_information_->NumberOfBackEdges();
566 }
567
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100568 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
569 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100570 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100571 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100572 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
573 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000574
575 void AddSuccessor(HBasicBlock* block) {
576 successors_.Add(block);
577 block->predecessors_.Add(this);
578 }
579
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100580 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
581 size_t successor_index = GetSuccessorIndexOf(existing);
582 DCHECK_NE(successor_index, static_cast<size_t>(-1));
583 existing->RemovePredecessor(this);
584 new_block->predecessors_.Add(this);
585 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000586 }
587
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000588 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
589 size_t predecessor_index = GetPredecessorIndexOf(existing);
590 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
591 existing->RemoveSuccessor(this);
592 new_block->successors_.Add(this);
593 predecessors_.Put(predecessor_index, new_block);
594 }
595
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100596 void RemovePredecessor(HBasicBlock* block) {
597 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100598 }
599
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000600 void RemoveSuccessor(HBasicBlock* block) {
601 successors_.Delete(block);
602 }
603
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100604 void ClearAllPredecessors() {
605 predecessors_.Reset();
606 }
607
608 void AddPredecessor(HBasicBlock* block) {
609 predecessors_.Add(block);
610 block->successors_.Add(this);
611 }
612
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100613 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100614 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100615 HBasicBlock* temp = predecessors_.Get(0);
616 predecessors_.Put(0, predecessors_.Get(1));
617 predecessors_.Put(1, temp);
618 }
619
David Brazdil769c9e52015-04-27 13:54:09 +0100620 void SwapSuccessors() {
621 DCHECK_EQ(successors_.Size(), 2u);
622 HBasicBlock* temp = successors_.Get(0);
623 successors_.Put(0, successors_.Get(1));
624 successors_.Put(1, temp);
625 }
626
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100627 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
628 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
629 if (predecessors_.Get(i) == predecessor) {
630 return i;
631 }
632 }
633 return -1;
634 }
635
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100636 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
637 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
638 if (successors_.Get(i) == successor) {
639 return i;
640 }
641 }
642 return -1;
643 }
644
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000645 // Split the block into two blocks just after `cursor`. Returns the newly
646 // created block. Note that this method just updates raw block information,
647 // like predecessors, successors, dominators, and instruction list. It does not
648 // update the graph, reverse post order, loop information, nor make sure the
649 // blocks are consistent (for example ending with a control flow instruction).
650 HBasicBlock* SplitAfter(HInstruction* cursor);
651
652 // Merge `other` at the end of `this`. Successors and dominated blocks of
653 // `other` are changed to be successors and dominated blocks of `this`. Note
654 // that this method does not update the graph, reverse post order, loop
655 // information, nor make sure the blocks are consistent (for example ending
656 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100657 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000658
659 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
660 // of `this` are moved to `other`.
661 // Note that this method does not update the graph, reverse post order, loop
662 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000663 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000664 void ReplaceWith(HBasicBlock* other);
665
David Brazdil2d7352b2015-04-20 14:52:42 +0100666 // Merge `other` at the end of `this`. This method updates loops, reverse post
667 // order, links to predecessors, successors, dominators and deletes the block
668 // from the graph. The two blocks must be successive, i.e. `this` the only
669 // predecessor of `other` and vice versa.
670 void MergeWith(HBasicBlock* other);
671
672 // Disconnects `this` from all its predecessors, successors and dominator,
673 // removes it from all loops it is included in and eventually from the graph.
674 // The block must not dominate any other block. Predecessors and successors
675 // are safely updated.
676 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000677
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000678 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100679 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100680 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100681 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100682 // Replace instruction `initial` with `replacement` within this block.
683 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
684 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100685 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100686 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000687 // RemoveInstruction and RemovePhi delete a given instruction from the respective
688 // instruction list. With 'ensure_safety' set to true, it verifies that the
689 // instruction is not in use and removes it from the use lists of its inputs.
690 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
691 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100692 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100693
694 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100695 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100696 }
697
Roland Levillain6b879dd2014-09-22 17:13:44 +0100698 bool IsLoopPreHeaderFirstPredecessor() const {
699 DCHECK(IsLoopHeader());
700 DCHECK(!GetPredecessors().IsEmpty());
701 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
702 }
703
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100704 HLoopInformation* GetLoopInformation() const {
705 return loop_information_;
706 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000707
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000708 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100709 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000710 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100711 void SetInLoop(HLoopInformation* info) {
712 if (IsLoopHeader()) {
713 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100714 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100715 loop_information_ = info;
716 } else if (loop_information_->Contains(*info->GetHeader())) {
717 // Block is currently part of an outer loop. Make it part of this inner loop.
718 // Note that a non loop header having a loop information means this loop information
719 // has already been populated
720 loop_information_ = info;
721 } else {
722 // Block is part of an inner loop. Do not update the loop information.
723 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
724 // at this point, because this method is being called while populating `info`.
725 }
726 }
727
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000728 // Raw update of the loop information.
729 void SetLoopInformation(HLoopInformation* info) {
730 loop_information_ = info;
731 }
732
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100733 bool IsInLoop() const { return loop_information_ != nullptr; }
734
David Brazdila4b8c212015-05-07 09:59:30 +0100735 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100736 bool Dominates(HBasicBlock* block) const;
737
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100738 size_t GetLifetimeStart() const { return lifetime_start_; }
739 size_t GetLifetimeEnd() const { return lifetime_end_; }
740
741 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
742 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
743
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100744 uint32_t GetDexPc() const { return dex_pc_; }
745
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000746 bool IsCatchBlock() const { return is_catch_block_; }
747 void SetIsCatchBlock() { is_catch_block_ = true; }
748
David Brazdil8d5b8b22015-03-24 10:51:52 +0000749 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000750 bool EndsWithIf() const;
751 bool HasSinglePhi() const;
752
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000753 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000754 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000755 GrowableArray<HBasicBlock*> predecessors_;
756 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100757 HInstructionList instructions_;
758 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000759 HLoopInformation* loop_information_;
760 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100761 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000762 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100763 // The dex program counter of the first instruction of this block.
764 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100765 size_t lifetime_start_;
766 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000767 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000768
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000769 friend class HGraph;
770 friend class HInstruction;
771
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000772 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
773};
774
David Brazdilb2bd1c52015-03-25 11:17:37 +0000775// Iterates over the LoopInformation of all loops which contain 'block'
776// from the innermost to the outermost.
777class HLoopInformationOutwardIterator : public ValueObject {
778 public:
779 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
780 : current_(block.GetLoopInformation()) {}
781
782 bool Done() const { return current_ == nullptr; }
783
784 void Advance() {
785 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100786 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000787 }
788
789 HLoopInformation* Current() const {
790 DCHECK(!Done());
791 return current_;
792 }
793
794 private:
795 HLoopInformation* current_;
796
797 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
798};
799
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100800#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
801 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000802 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000803 M(ArrayGet, Instruction) \
804 M(ArrayLength, Instruction) \
805 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100806 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000807 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000808 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000809 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100810 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000811 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100812 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700813 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000814 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000815 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000816 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100817 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000818 M(Exit, Instruction) \
819 M(FloatConstant, Constant) \
820 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100821 M(GreaterThan, Condition) \
822 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100823 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000824 M(InstanceFieldGet, Instruction) \
825 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000826 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100827 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000828 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000829 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100830 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000831 M(LessThan, Condition) \
832 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000833 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000834 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100835 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000836 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100837 M(Local, Instruction) \
838 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100839 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000840 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000841 M(Mul, BinaryOperation) \
842 M(Neg, UnaryOperation) \
843 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100844 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100845 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000846 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000847 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000848 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000849 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100850 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000851 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100852 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000853 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100854 M(Return, Instruction) \
855 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000856 M(Shl, BinaryOperation) \
857 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100858 M(StaticFieldGet, Instruction) \
859 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100860 M(StoreLocal, Instruction) \
861 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100862 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000863 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000864 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000865 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000866 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000867 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100869#define FOR_EACH_INSTRUCTION(M) \
870 FOR_EACH_CONCRETE_INSTRUCTION(M) \
871 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100872 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100873 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100874 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700875
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100876#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000877FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
878#undef FORWARD_DECLARATION
879
Roland Levillainccc07a92014-09-16 14:48:16 +0100880#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000881 InstructionKind GetKind() const OVERRIDE { return k##type; } \
882 const char* DebugName() const OVERRIDE { return #type; } \
883 const H##type* As##type() const OVERRIDE { return this; } \
884 H##type* As##type() OVERRIDE { return this; } \
885 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100886 return other->Is##type(); \
887 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000888 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000889
David Brazdiled596192015-01-23 10:39:45 +0000890template <typename T> class HUseList;
891
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100892template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700893class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000894 public:
David Brazdiled596192015-01-23 10:39:45 +0000895 HUseListNode* GetPrevious() const { return prev_; }
896 HUseListNode* GetNext() const { return next_; }
897 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100898 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100899 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100900
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000901 private:
David Brazdiled596192015-01-23 10:39:45 +0000902 HUseListNode(T user, size_t index)
903 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
904
905 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100906 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000907 HUseListNode<T>* prev_;
908 HUseListNode<T>* next_;
909
910 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000911
912 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
913};
914
David Brazdiled596192015-01-23 10:39:45 +0000915template <typename T>
916class HUseList : public ValueObject {
917 public:
918 HUseList() : first_(nullptr) {}
919
920 void Clear() {
921 first_ = nullptr;
922 }
923
924 // Adds a new entry at the beginning of the use list and returns
925 // the newly created node.
926 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000927 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000928 if (IsEmpty()) {
929 first_ = new_node;
930 } else {
931 first_->prev_ = new_node;
932 new_node->next_ = first_;
933 first_ = new_node;
934 }
935 return new_node;
936 }
937
938 HUseListNode<T>* GetFirst() const {
939 return first_;
940 }
941
942 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000943 DCHECK(node != nullptr);
944 DCHECK(Contains(node));
945
David Brazdiled596192015-01-23 10:39:45 +0000946 if (node->prev_ != nullptr) {
947 node->prev_->next_ = node->next_;
948 }
949 if (node->next_ != nullptr) {
950 node->next_->prev_ = node->prev_;
951 }
952 if (node == first_) {
953 first_ = node->next_;
954 }
955 }
956
David Brazdil1abb4192015-02-17 18:33:36 +0000957 bool Contains(const HUseListNode<T>* node) const {
958 if (node == nullptr) {
959 return false;
960 }
961 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
962 if (current == node) {
963 return true;
964 }
965 }
966 return false;
967 }
968
David Brazdiled596192015-01-23 10:39:45 +0000969 bool IsEmpty() const {
970 return first_ == nullptr;
971 }
972
973 bool HasOnlyOneUse() const {
974 return first_ != nullptr && first_->next_ == nullptr;
975 }
976
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100977 size_t SizeSlow() const {
978 size_t count = 0;
979 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
980 ++count;
981 }
982 return count;
983 }
984
David Brazdiled596192015-01-23 10:39:45 +0000985 private:
986 HUseListNode<T>* first_;
987};
988
989template<typename T>
990class HUseIterator : public ValueObject {
991 public:
992 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
993
994 bool Done() const { return current_ == nullptr; }
995
996 void Advance() {
997 DCHECK(!Done());
998 current_ = current_->GetNext();
999 }
1000
1001 HUseListNode<T>* Current() const {
1002 DCHECK(!Done());
1003 return current_;
1004 }
1005
1006 private:
1007 HUseListNode<T>* current_;
1008
1009 friend class HValue;
1010};
1011
David Brazdil1abb4192015-02-17 18:33:36 +00001012// This class is used by HEnvironment and HInstruction classes to record the
1013// instructions they use and pointers to the corresponding HUseListNodes kept
1014// by the used instructions.
1015template <typename T>
1016class HUserRecord : public ValueObject {
1017 public:
1018 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1019 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1020
1021 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1022 : instruction_(old_record.instruction_), use_node_(use_node) {
1023 DCHECK(instruction_ != nullptr);
1024 DCHECK(use_node_ != nullptr);
1025 DCHECK(old_record.use_node_ == nullptr);
1026 }
1027
1028 HInstruction* GetInstruction() const { return instruction_; }
1029 HUseListNode<T>* GetUseNode() const { return use_node_; }
1030
1031 private:
1032 // Instruction used by the user.
1033 HInstruction* instruction_;
1034
1035 // Corresponding entry in the use list kept by 'instruction_'.
1036 HUseListNode<T>* use_node_;
1037};
1038
Calin Juravle27df7582015-04-17 19:12:31 +01001039// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1040// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1041// flag is consider.
1042// - DependsOn suggests that there is a real dependency between side effects but it only
1043// checks DependendsOnSomething flag.
1044//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001045// Represents the side effects an instruction may have.
1046class SideEffects : public ValueObject {
1047 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001048 SideEffects() : flags_(0) {}
1049
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001050 static SideEffects None() {
1051 return SideEffects(0);
1052 }
1053
1054 static SideEffects All() {
1055 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1056 }
1057
1058 static SideEffects ChangesSomething() {
1059 return SideEffects((1 << kFlagChangesCount) - 1);
1060 }
1061
1062 static SideEffects DependsOnSomething() {
1063 int count = kFlagDependsOnCount - kFlagChangesCount;
1064 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1065 }
1066
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001067 SideEffects Union(SideEffects other) const {
1068 return SideEffects(flags_ | other.flags_);
1069 }
1070
Roland Levillain72bceff2014-09-15 18:29:00 +01001071 bool HasSideEffects() const {
1072 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1073 return (flags_ & all_bits_set) != 0;
1074 }
1075
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001076 bool HasAllSideEffects() const {
1077 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1078 return all_bits_set == (flags_ & all_bits_set);
1079 }
1080
1081 bool DependsOn(SideEffects other) const {
1082 size_t depends_flags = other.ComputeDependsFlags();
1083 return (flags_ & depends_flags) != 0;
1084 }
1085
1086 bool HasDependencies() const {
1087 int count = kFlagDependsOnCount - kFlagChangesCount;
1088 size_t all_bits_set = (1 << count) - 1;
1089 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1090 }
1091
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001092 private:
1093 static constexpr int kFlagChangesSomething = 0;
1094 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1095
1096 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1097 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1098
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001099 explicit SideEffects(size_t flags) : flags_(flags) {}
1100
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001101 size_t ComputeDependsFlags() const {
1102 return flags_ << kFlagChangesCount;
1103 }
1104
1105 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001106};
1107
David Brazdiled596192015-01-23 10:39:45 +00001108// A HEnvironment object contains the values of virtual registers at a given location.
1109class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1110 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001111 HEnvironment(ArenaAllocator* arena,
1112 size_t number_of_vregs,
1113 const DexFile& dex_file,
1114 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001115 uint32_t dex_pc,
1116 InvokeType invoke_type)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001117 : vregs_(arena, number_of_vregs),
1118 locations_(arena, number_of_vregs),
1119 parent_(nullptr),
1120 dex_file_(dex_file),
1121 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001122 dex_pc_(dex_pc),
1123 invoke_type_(invoke_type) {
David Brazdiled596192015-01-23 10:39:45 +00001124 vregs_.SetSize(number_of_vregs);
1125 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001126 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001127 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001128
1129 locations_.SetSize(number_of_vregs);
1130 for (size_t i = 0; i < number_of_vregs; ++i) {
1131 locations_.Put(i, Location());
1132 }
1133 }
1134
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001135 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy)
1136 : HEnvironment(arena,
1137 to_copy.Size(),
1138 to_copy.GetDexFile(),
1139 to_copy.GetMethodIdx(),
1140 to_copy.GetDexPc(),
1141 to_copy.GetInvokeType()) {}
1142
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001143 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001144 parent_ = new (allocator) HEnvironment(allocator, *parent);
1145 parent_->CopyFrom(parent);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001146 if (parent->GetParent() != nullptr) {
1147 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1148 }
David Brazdiled596192015-01-23 10:39:45 +00001149 }
1150
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001151 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1152 void CopyFrom(HEnvironment* environment);
1153
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001154 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1155 // input to the loop phi instead. This is for inserting instructions that
1156 // require an environment (like HDeoptimization) in the loop pre-header.
1157 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001158
1159 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001160 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001161 }
1162
1163 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001164 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001165 }
1166
David Brazdil1abb4192015-02-17 18:33:36 +00001167 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001168
1169 size_t Size() const { return vregs_.Size(); }
1170
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001171 HEnvironment* GetParent() const { return parent_; }
1172
1173 void SetLocationAt(size_t index, Location location) {
1174 locations_.Put(index, location);
1175 }
1176
1177 Location GetLocationAt(size_t index) const {
1178 return locations_.Get(index);
1179 }
1180
1181 uint32_t GetDexPc() const {
1182 return dex_pc_;
1183 }
1184
1185 uint32_t GetMethodIdx() const {
1186 return method_idx_;
1187 }
1188
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001189 InvokeType GetInvokeType() const {
1190 return invoke_type_;
1191 }
1192
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001193 const DexFile& GetDexFile() const {
1194 return dex_file_;
1195 }
1196
David Brazdiled596192015-01-23 10:39:45 +00001197 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001198 // Record instructions' use entries of this environment for constant-time removal.
1199 // It should only be called by HInstruction when a new environment use is added.
1200 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1201 DCHECK(env_use->GetUser() == this);
1202 size_t index = env_use->GetIndex();
1203 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1204 }
David Brazdiled596192015-01-23 10:39:45 +00001205
David Brazdil1abb4192015-02-17 18:33:36 +00001206 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001207 GrowableArray<Location> locations_;
1208 HEnvironment* parent_;
1209 const DexFile& dex_file_;
1210 const uint32_t method_idx_;
1211 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001212 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001213
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001214 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001215
1216 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1217};
1218
Calin Juravleacf735c2015-02-12 15:25:22 +00001219class ReferenceTypeInfo : ValueObject {
1220 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001221 typedef Handle<mirror::Class> TypeHandle;
1222
1223 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1224 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1225 if (type_handle->IsObjectClass()) {
1226 // Override the type handle to be consistent with the case when we get to
1227 // Top but don't have the Object class available. It avoids having to guess
1228 // what value the type_handle has when it's Top.
1229 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1230 } else {
1231 return ReferenceTypeInfo(type_handle, is_exact, false);
1232 }
1233 }
1234
1235 static ReferenceTypeInfo CreateTop(bool is_exact) {
1236 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001237 }
1238
1239 bool IsExact() const { return is_exact_; }
1240 bool IsTop() const { return is_top_; }
1241
1242 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1243
Calin Juravleb1498f62015-02-16 13:13:29 +00001244 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001245 if (IsTop()) {
1246 // Top (equivalent for java.lang.Object) is supertype of anything.
1247 return true;
1248 }
1249 if (rti.IsTop()) {
1250 // If we get here `this` is not Top() so it can't be a supertype.
1251 return false;
1252 }
1253 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1254 }
1255
1256 // Returns true if the type information provide the same amount of details.
1257 // Note that it does not mean that the instructions have the same actual type
1258 // (e.g. tops are equal but they can be the result of a merge).
1259 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1260 if (IsExact() != rti.IsExact()) {
1261 return false;
1262 }
1263 if (IsTop() && rti.IsTop()) {
1264 // `Top` means java.lang.Object, so the types are equivalent.
1265 return true;
1266 }
1267 if (IsTop() || rti.IsTop()) {
1268 // If only one is top or object than they are not equivalent.
1269 // NB: We need this extra check because the type_handle of `Top` is invalid
1270 // and we cannot inspect its reference.
1271 return false;
1272 }
1273
1274 // Finally check the types.
1275 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1276 }
1277
1278 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001279 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1280 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1281 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1282
Calin Juravleacf735c2015-02-12 15:25:22 +00001283 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001284 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001285 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001286 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001287 bool is_exact_;
1288 // A true value here means that the object type should be java.lang.Object.
1289 // We don't have access to the corresponding mirror object every time so this
1290 // flag acts as a substitute. When true, the TypeHandle refers to a null
1291 // pointer and should not be used.
1292 bool is_top_;
1293};
1294
1295std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1296
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001297class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001298 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001299 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001300 : previous_(nullptr),
1301 next_(nullptr),
1302 block_(nullptr),
1303 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001304 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001305 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001306 locations_(nullptr),
1307 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001308 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001309 side_effects_(side_effects),
1310 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001311
Dave Allison20dfc792014-06-16 20:44:29 -07001312 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001313
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001314#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001315 enum InstructionKind {
1316 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1317 };
1318#undef DECLARE_KIND
1319
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001320 HInstruction* GetNext() const { return next_; }
1321 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001322
Calin Juravle77520bc2015-01-12 18:45:46 +00001323 HInstruction* GetNextDisregardingMoves() const;
1324 HInstruction* GetPreviousDisregardingMoves() const;
1325
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001326 HBasicBlock* GetBlock() const { return block_; }
1327 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001328 bool IsInBlock() const { return block_ != nullptr; }
1329 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001330 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001331
Roland Levillain6b879dd2014-09-22 17:13:44 +01001332 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001333 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001334
1335 virtual void Accept(HGraphVisitor* visitor) = 0;
1336 virtual const char* DebugName() const = 0;
1337
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001338 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001339 void SetRawInputAt(size_t index, HInstruction* input) {
1340 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1341 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001342
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001343 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001344 virtual uint32_t GetDexPc() const {
1345 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1346 " does not need an environment";
1347 UNREACHABLE();
1348 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001349 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001350 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001351 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001352
Calin Juravle10e244f2015-01-26 18:54:32 +00001353 // Does not apply for all instructions, but having this at top level greatly
1354 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001355 virtual bool CanBeNull() const {
1356 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1357 return true;
1358 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001359
Calin Juravle641547a2015-04-21 22:08:51 +01001360 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1361 UNUSED(obj);
1362 return false;
1363 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001364
Calin Juravleacf735c2015-02-12 15:25:22 +00001365 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001366 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001367 reference_type_info_ = reference_type_info;
1368 }
1369
Calin Juravle61d544b2015-02-23 16:46:57 +00001370 ReferenceTypeInfo GetReferenceTypeInfo() const {
1371 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1372 return reference_type_info_;
1373 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001374
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001375 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001376 DCHECK(user != nullptr);
1377 HUseListNode<HInstruction*>* use =
1378 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1379 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001380 }
1381
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001382 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001383 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001384 HUseListNode<HEnvironment*>* env_use =
1385 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1386 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001387 }
1388
David Brazdil1abb4192015-02-17 18:33:36 +00001389 void RemoveAsUserOfInput(size_t input) {
1390 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1391 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1392 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001393
David Brazdil1abb4192015-02-17 18:33:36 +00001394 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1395 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001396
David Brazdiled596192015-01-23 10:39:45 +00001397 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1398 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001399 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001400 bool HasOnlyOneNonEnvironmentUse() const {
1401 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1402 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001403
Roland Levillain6c82d402014-10-13 16:10:27 +01001404 // Does this instruction strictly dominate `other_instruction`?
1405 // Returns false if this instruction and `other_instruction` are the same.
1406 // Aborts if this instruction and `other_instruction` are both phis.
1407 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001408
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001409 int GetId() const { return id_; }
1410 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001411
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001412 int GetSsaIndex() const { return ssa_index_; }
1413 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1414 bool HasSsaIndex() const { return ssa_index_ != -1; }
1415
1416 bool HasEnvironment() const { return environment_ != nullptr; }
1417 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001418 // Set the `environment_` field. Raw because this method does not
1419 // update the uses lists.
1420 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1421
1422 // Set the environment of this instruction, copying it from `environment`. While
1423 // copying, the uses lists are being updated.
1424 void CopyEnvironmentFrom(HEnvironment* environment) {
1425 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001426 environment_ = new (allocator) HEnvironment(allocator, *environment);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001427 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001428 if (environment->GetParent() != nullptr) {
1429 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1430 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001431 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001432
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001433 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1434 HBasicBlock* block) {
1435 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001436 environment_ = new (allocator) HEnvironment(allocator, *environment);
1437 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001438 if (environment->GetParent() != nullptr) {
1439 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1440 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001441 }
1442
Nicolas Geoffray39468442014-09-02 15:17:15 +01001443 // Returns the number of entries in the environment. Typically, that is the
1444 // number of dex registers in a method. It could be more in case of inlining.
1445 size_t EnvironmentSize() const;
1446
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001447 LocationSummary* GetLocations() const { return locations_; }
1448 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001449
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001450 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001451 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001452
Alexandre Rames188d4312015-04-09 18:30:21 +01001453 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1454 // uses of this instruction by `other` are *not* updated.
1455 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1456 ReplaceWith(other);
1457 other->ReplaceInput(this, use_index);
1458 }
1459
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001460 // Move `this` instruction before `cursor`.
1461 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001462
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001463#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001464 bool Is##type() const { return (As##type() != nullptr); } \
1465 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001466 virtual H##type* As##type() { return nullptr; }
1467
1468 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1469#undef INSTRUCTION_TYPE_CHECK
1470
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001471 // Returns whether the instruction can be moved within the graph.
1472 virtual bool CanBeMoved() const { return false; }
1473
1474 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001475 virtual bool InstructionTypeEquals(HInstruction* other) const {
1476 UNUSED(other);
1477 return false;
1478 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001479
1480 // Returns whether any data encoded in the two instructions is equal.
1481 // This method does not look at the inputs. Both instructions must be
1482 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001483 virtual bool InstructionDataEquals(HInstruction* other) const {
1484 UNUSED(other);
1485 return false;
1486 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001487
1488 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001489 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001490 // 2) Their inputs are identical.
1491 bool Equals(HInstruction* other) const;
1492
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001493 virtual InstructionKind GetKind() const = 0;
1494
1495 virtual size_t ComputeHashCode() const {
1496 size_t result = GetKind();
1497 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1498 result = (result * 31) + InputAt(i)->GetId();
1499 }
1500 return result;
1501 }
1502
1503 SideEffects GetSideEffects() const { return side_effects_; }
1504
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001505 size_t GetLifetimePosition() const { return lifetime_position_; }
1506 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1507 LiveInterval* GetLiveInterval() const { return live_interval_; }
1508 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1509 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1510
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001511 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1512
1513 // Returns whether the code generation of the instruction will require to have access
1514 // to the current method. Such instructions are:
1515 // (1): Instructions that require an environment, as calling the runtime requires
1516 // to walk the stack and have the current method stored at a specific stack address.
1517 // (2): Object literals like classes and strings, that are loaded from the dex cache
1518 // fields of the current method.
1519 bool NeedsCurrentMethod() const {
1520 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1521 }
1522
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001523 virtual bool NeedsDexCache() const { return false; }
1524
David Brazdil1abb4192015-02-17 18:33:36 +00001525 protected:
1526 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1527 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1528
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001529 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001530 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1531
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001532 HInstruction* previous_;
1533 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001534 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001535
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001536 // An instruction gets an id when it is added to the graph.
1537 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001538 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001539 int id_;
1540
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001541 // When doing liveness analysis, instructions that have uses get an SSA index.
1542 int ssa_index_;
1543
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001544 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001545 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001546
1547 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001548 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001549
Nicolas Geoffray39468442014-09-02 15:17:15 +01001550 // The environment associated with this instruction. Not null if the instruction
1551 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001552 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001553
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001554 // Set by the code generator.
1555 LocationSummary* locations_;
1556
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001557 // Set by the liveness analysis.
1558 LiveInterval* live_interval_;
1559
1560 // Set by the liveness analysis, this is the position in a linear
1561 // order of blocks where this instruction's live interval start.
1562 size_t lifetime_position_;
1563
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001564 const SideEffects side_effects_;
1565
Calin Juravleacf735c2015-02-12 15:25:22 +00001566 // TODO: for primitive types this should be marked as invalid.
1567 ReferenceTypeInfo reference_type_info_;
1568
David Brazdil1abb4192015-02-17 18:33:36 +00001569 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001570 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001571 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001572 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001573 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001574
1575 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1576};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001577std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001578
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001579class HInputIterator : public ValueObject {
1580 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001581 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001582
1583 bool Done() const { return index_ == instruction_->InputCount(); }
1584 HInstruction* Current() const { return instruction_->InputAt(index_); }
1585 void Advance() { index_++; }
1586
1587 private:
1588 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001589 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001590
1591 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1592};
1593
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001594class HInstructionIterator : public ValueObject {
1595 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001596 explicit HInstructionIterator(const HInstructionList& instructions)
1597 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001598 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001599 }
1600
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001601 bool Done() const { return instruction_ == nullptr; }
1602 HInstruction* Current() const { return instruction_; }
1603 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001604 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001605 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001606 }
1607
1608 private:
1609 HInstruction* instruction_;
1610 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001611
1612 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001613};
1614
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001615class HBackwardInstructionIterator : public ValueObject {
1616 public:
1617 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1618 : instruction_(instructions.last_instruction_) {
1619 next_ = Done() ? nullptr : instruction_->GetPrevious();
1620 }
1621
1622 bool Done() const { return instruction_ == nullptr; }
1623 HInstruction* Current() const { return instruction_; }
1624 void Advance() {
1625 instruction_ = next_;
1626 next_ = Done() ? nullptr : instruction_->GetPrevious();
1627 }
1628
1629 private:
1630 HInstruction* instruction_;
1631 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001632
1633 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001634};
1635
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001636// An embedded container with N elements of type T. Used (with partial
1637// specialization for N=0) because embedded arrays cannot have size 0.
1638template<typename T, intptr_t N>
1639class EmbeddedArray {
1640 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001641 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001642
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001643 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001644
1645 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001646 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001647 return elements_[i];
1648 }
1649
1650 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001651 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001652 return elements_[i];
1653 }
1654
1655 const T& At(intptr_t i) const {
1656 return (*this)[i];
1657 }
1658
1659 void SetAt(intptr_t i, const T& val) {
1660 (*this)[i] = val;
1661 }
1662
1663 private:
1664 T elements_[N];
1665};
1666
1667template<typename T>
1668class EmbeddedArray<T, 0> {
1669 public:
1670 intptr_t length() const { return 0; }
1671 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001672 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001673 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001674 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001675 }
1676 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001677 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001678 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001679 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001680 }
1681};
1682
1683template<intptr_t N>
1684class HTemplateInstruction: public HInstruction {
1685 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001686 HTemplateInstruction<N>(SideEffects side_effects)
1687 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001688 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001689
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001690 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001691
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001692 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001693 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1694
1695 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1696 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001697 }
1698
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001699 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001700 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001701
1702 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001703};
1704
Dave Allison20dfc792014-06-16 20:44:29 -07001705template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001706class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001707 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001708 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1709 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001710 virtual ~HExpression() {}
1711
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001712 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001713
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001714 protected:
1715 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001716};
1717
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001718// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1719// instruction that branches to the exit block.
1720class HReturnVoid : public HTemplateInstruction<0> {
1721 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001722 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001723
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001724 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001725
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001726 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001727
1728 private:
1729 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1730};
1731
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001732// Represents dex's RETURN opcodes. A HReturn is a control flow
1733// instruction that branches to the exit block.
1734class HReturn : public HTemplateInstruction<1> {
1735 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001736 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001737 SetRawInputAt(0, value);
1738 }
1739
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001740 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001741
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001742 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001743
1744 private:
1745 DISALLOW_COPY_AND_ASSIGN(HReturn);
1746};
1747
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001748// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001749// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001750// exit block.
1751class HExit : public HTemplateInstruction<0> {
1752 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001753 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001754
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001755 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001756
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001757 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001758
1759 private:
1760 DISALLOW_COPY_AND_ASSIGN(HExit);
1761};
1762
1763// Jumps from one block to another.
1764class HGoto : public HTemplateInstruction<0> {
1765 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001766 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1767
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001768 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001769
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001770 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001771 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001772 }
1773
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001774 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001775
1776 private:
1777 DISALLOW_COPY_AND_ASSIGN(HGoto);
1778};
1779
Dave Allison20dfc792014-06-16 20:44:29 -07001780
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001781// Conditional branch. A block ending with an HIf instruction must have
1782// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001783class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001784 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001785 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001786 SetRawInputAt(0, input);
1787 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001788
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001789 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001790
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001791 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001792 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001793 }
1794
1795 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001796 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001797 }
1798
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001799 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001800
1801 private:
1802 DISALLOW_COPY_AND_ASSIGN(HIf);
1803};
1804
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001805// Deoptimize to interpreter, upon checking a condition.
1806class HDeoptimize : public HTemplateInstruction<1> {
1807 public:
1808 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1809 : HTemplateInstruction(SideEffects::None()),
1810 dex_pc_(dex_pc) {
1811 SetRawInputAt(0, cond);
1812 }
1813
1814 bool NeedsEnvironment() const OVERRIDE { return true; }
1815 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001816 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001817
1818 DECLARE_INSTRUCTION(Deoptimize);
1819
1820 private:
1821 uint32_t dex_pc_;
1822
1823 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1824};
1825
Roland Levillain88cb1752014-10-20 16:36:47 +01001826class HUnaryOperation : public HExpression<1> {
1827 public:
1828 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1829 : HExpression(result_type, SideEffects::None()) {
1830 SetRawInputAt(0, input);
1831 }
1832
1833 HInstruction* GetInput() const { return InputAt(0); }
1834 Primitive::Type GetResultType() const { return GetType(); }
1835
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001836 bool CanBeMoved() const OVERRIDE { return true; }
1837 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001838 UNUSED(other);
1839 return true;
1840 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001841
Roland Levillain9240d6a2014-10-20 16:47:04 +01001842 // Try to statically evaluate `operation` and return a HConstant
1843 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001844 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001845 HConstant* TryStaticEvaluation() const;
1846
1847 // Apply this operation to `x`.
1848 virtual int32_t Evaluate(int32_t x) const = 0;
1849 virtual int64_t Evaluate(int64_t x) const = 0;
1850
Roland Levillain88cb1752014-10-20 16:36:47 +01001851 DECLARE_INSTRUCTION(UnaryOperation);
1852
1853 private:
1854 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1855};
1856
Dave Allison20dfc792014-06-16 20:44:29 -07001857class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001858 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001859 HBinaryOperation(Primitive::Type result_type,
1860 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001861 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001862 SetRawInputAt(0, left);
1863 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001864 }
1865
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001866 HInstruction* GetLeft() const { return InputAt(0); }
1867 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001868 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001869
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001870 virtual bool IsCommutative() const { return false; }
1871
1872 // Put constant on the right.
1873 // Returns whether order is changed.
1874 bool OrderInputsWithConstantOnTheRight() {
1875 HInstruction* left = InputAt(0);
1876 HInstruction* right = InputAt(1);
1877 if (left->IsConstant() && !right->IsConstant()) {
1878 ReplaceInput(right, 0);
1879 ReplaceInput(left, 1);
1880 return true;
1881 }
1882 return false;
1883 }
1884
1885 // Order inputs by instruction id, but favor constant on the right side.
1886 // This helps GVN for commutative ops.
1887 void OrderInputs() {
1888 DCHECK(IsCommutative());
1889 HInstruction* left = InputAt(0);
1890 HInstruction* right = InputAt(1);
1891 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1892 return;
1893 }
1894 if (OrderInputsWithConstantOnTheRight()) {
1895 return;
1896 }
1897 // Order according to instruction id.
1898 if (left->GetId() > right->GetId()) {
1899 ReplaceInput(right, 0);
1900 ReplaceInput(left, 1);
1901 }
1902 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001903
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001904 bool CanBeMoved() const OVERRIDE { return true; }
1905 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001906 UNUSED(other);
1907 return true;
1908 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001909
Roland Levillain9240d6a2014-10-20 16:47:04 +01001910 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001911 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001912 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001913 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001914
1915 // Apply this operation to `x` and `y`.
1916 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1917 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1918
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001919 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001920 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001921 HConstant* GetConstantRight() const;
1922
1923 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001924 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001925 HInstruction* GetLeastConstantLeft() const;
1926
Roland Levillainccc07a92014-09-16 14:48:16 +01001927 DECLARE_INSTRUCTION(BinaryOperation);
1928
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001929 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001930 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1931};
1932
Dave Allison20dfc792014-06-16 20:44:29 -07001933class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001934 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001935 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001936 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1937 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001938
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001939 bool NeedsMaterialization() const { return needs_materialization_; }
1940 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001941
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001942 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001943 // `instruction`, and disregard moves in between.
1944 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001945
Dave Allison20dfc792014-06-16 20:44:29 -07001946 DECLARE_INSTRUCTION(Condition);
1947
1948 virtual IfCondition GetCondition() const = 0;
1949
1950 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001951 // For register allocation purposes, returns whether this instruction needs to be
1952 // materialized (that is, not just be in the processor flags).
1953 bool needs_materialization_;
1954
Dave Allison20dfc792014-06-16 20:44:29 -07001955 DISALLOW_COPY_AND_ASSIGN(HCondition);
1956};
1957
1958// Instruction to check if two inputs are equal to each other.
1959class HEqual : public HCondition {
1960 public:
1961 HEqual(HInstruction* first, HInstruction* second)
1962 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001963
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001964 bool IsCommutative() const OVERRIDE { return true; }
1965
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001966 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001967 return x == y ? 1 : 0;
1968 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001969 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001970 return x == y ? 1 : 0;
1971 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001972
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001973 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001974
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001975 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001976 return kCondEQ;
1977 }
1978
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001979 private:
1980 DISALLOW_COPY_AND_ASSIGN(HEqual);
1981};
1982
Dave Allison20dfc792014-06-16 20:44:29 -07001983class HNotEqual : public HCondition {
1984 public:
1985 HNotEqual(HInstruction* first, HInstruction* second)
1986 : HCondition(first, second) {}
1987
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001988 bool IsCommutative() const OVERRIDE { return true; }
1989
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001990 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001991 return x != y ? 1 : 0;
1992 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001993 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001994 return x != y ? 1 : 0;
1995 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001996
Dave Allison20dfc792014-06-16 20:44:29 -07001997 DECLARE_INSTRUCTION(NotEqual);
1998
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001999 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002000 return kCondNE;
2001 }
2002
2003 private:
2004 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2005};
2006
2007class HLessThan : public HCondition {
2008 public:
2009 HLessThan(HInstruction* first, HInstruction* second)
2010 : HCondition(first, second) {}
2011
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002012 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002013 return x < y ? 1 : 0;
2014 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002015 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002016 return x < y ? 1 : 0;
2017 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002018
Dave Allison20dfc792014-06-16 20:44:29 -07002019 DECLARE_INSTRUCTION(LessThan);
2020
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002021 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002022 return kCondLT;
2023 }
2024
2025 private:
2026 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2027};
2028
2029class HLessThanOrEqual : public HCondition {
2030 public:
2031 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2032 : HCondition(first, second) {}
2033
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002034 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002035 return x <= y ? 1 : 0;
2036 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002037 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002038 return x <= y ? 1 : 0;
2039 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002040
Dave Allison20dfc792014-06-16 20:44:29 -07002041 DECLARE_INSTRUCTION(LessThanOrEqual);
2042
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002043 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002044 return kCondLE;
2045 }
2046
2047 private:
2048 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2049};
2050
2051class HGreaterThan : public HCondition {
2052 public:
2053 HGreaterThan(HInstruction* first, HInstruction* second)
2054 : HCondition(first, second) {}
2055
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002056 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002057 return x > y ? 1 : 0;
2058 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002059 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002060 return x > y ? 1 : 0;
2061 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002062
Dave Allison20dfc792014-06-16 20:44:29 -07002063 DECLARE_INSTRUCTION(GreaterThan);
2064
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002065 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002066 return kCondGT;
2067 }
2068
2069 private:
2070 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2071};
2072
2073class HGreaterThanOrEqual : public HCondition {
2074 public:
2075 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2076 : HCondition(first, second) {}
2077
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002078 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002079 return x >= y ? 1 : 0;
2080 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002081 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002082 return x >= y ? 1 : 0;
2083 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002084
Dave Allison20dfc792014-06-16 20:44:29 -07002085 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2086
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002087 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002088 return kCondGE;
2089 }
2090
2091 private:
2092 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2093};
2094
2095
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002096// Instruction to check how two inputs compare to each other.
2097// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2098class HCompare : public HBinaryOperation {
2099 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002100 // The bias applies for floating point operations and indicates how NaN
2101 // comparisons are treated:
2102 enum Bias {
2103 kNoBias, // bias is not applicable (i.e. for long operation)
2104 kGtBias, // return 1 for NaN comparisons
2105 kLtBias, // return -1 for NaN comparisons
2106 };
2107
2108 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
2109 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002110 DCHECK_EQ(type, first->GetType());
2111 DCHECK_EQ(type, second->GetType());
2112 }
2113
Calin Juravleddb7df22014-11-25 20:56:51 +00002114 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002115 return
2116 x == y ? 0 :
2117 x > y ? 1 :
2118 -1;
2119 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002120
2121 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002122 return
2123 x == y ? 0 :
2124 x > y ? 1 :
2125 -1;
2126 }
2127
Calin Juravleddb7df22014-11-25 20:56:51 +00002128 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2129 return bias_ == other->AsCompare()->bias_;
2130 }
2131
2132 bool IsGtBias() { return bias_ == kGtBias; }
2133
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002134 DECLARE_INSTRUCTION(Compare);
2135
2136 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002137 const Bias bias_;
2138
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002139 DISALLOW_COPY_AND_ASSIGN(HCompare);
2140};
2141
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002142// A local in the graph. Corresponds to a Dex register.
2143class HLocal : public HTemplateInstruction<0> {
2144 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002145 explicit HLocal(uint16_t reg_number)
2146 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002147
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002148 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002149
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002150 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002151
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002152 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002153 // The Dex register number.
2154 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002155
2156 DISALLOW_COPY_AND_ASSIGN(HLocal);
2157};
2158
2159// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002160class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002161 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002162 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002163 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002164 SetRawInputAt(0, local);
2165 }
2166
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002167 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2168
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002169 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002170
2171 private:
2172 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2173};
2174
2175// Store a value in a given local. This instruction has two inputs: the value
2176// and the local.
2177class HStoreLocal : public HTemplateInstruction<2> {
2178 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002179 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002180 SetRawInputAt(0, local);
2181 SetRawInputAt(1, value);
2182 }
2183
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002184 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2185
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002186 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002187
2188 private:
2189 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2190};
2191
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002192class HConstant : public HExpression<0> {
2193 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002194 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2195
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002196 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002197
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002198 virtual bool IsMinusOne() const { return false; }
2199 virtual bool IsZero() const { return false; }
2200 virtual bool IsOne() const { return false; }
2201
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002202 DECLARE_INSTRUCTION(Constant);
2203
2204 private:
2205 DISALLOW_COPY_AND_ASSIGN(HConstant);
2206};
2207
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002208class HFloatConstant : public HConstant {
2209 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002210 float GetValue() const { return value_; }
2211
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002212 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002213 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2214 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002215 }
2216
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002217 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002218
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002219 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002220 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002221 }
2222 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002223 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002224 }
2225 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002226 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2227 }
2228 bool IsNaN() const {
2229 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002230 }
2231
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002232 DECLARE_INSTRUCTION(FloatConstant);
2233
2234 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002235 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002236 explicit HFloatConstant(int32_t value)
2237 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002238
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002239 const float value_;
2240
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002241 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002242 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002243 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002244 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2245};
2246
2247class HDoubleConstant : public HConstant {
2248 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002249 double GetValue() const { return value_; }
2250
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002251 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002252 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2253 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002254 }
2255
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002256 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002257
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002258 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002259 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002260 }
2261 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002262 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002263 }
2264 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002265 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2266 }
2267 bool IsNaN() const {
2268 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002269 }
2270
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002271 DECLARE_INSTRUCTION(DoubleConstant);
2272
2273 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002274 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002275 explicit HDoubleConstant(int64_t value)
2276 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002277
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002278 const double value_;
2279
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002280 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002281 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002282 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002283 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2284};
2285
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002286class HNullConstant : public HConstant {
2287 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002288 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2289 return true;
2290 }
2291
2292 size_t ComputeHashCode() const OVERRIDE { return 0; }
2293
2294 DECLARE_INSTRUCTION(NullConstant);
2295
2296 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002297 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2298
2299 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002300 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2301};
2302
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002303// Constants of the type int. Those can be from Dex instructions, or
2304// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002305class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002306 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002307 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002308
Calin Juravle61d544b2015-02-23 16:46:57 +00002309 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002310 return other->AsIntConstant()->value_ == value_;
2311 }
2312
Calin Juravle61d544b2015-02-23 16:46:57 +00002313 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2314
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002315 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2316 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2317 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2318
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002319 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002320
2321 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002322 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2323
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002324 const int32_t value_;
2325
David Brazdil8d5b8b22015-03-24 10:51:52 +00002326 friend class HGraph;
2327 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002328 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002329 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2330};
2331
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002332class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002333 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002334 int64_t GetValue() const { return value_; }
2335
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002336 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002337 return other->AsLongConstant()->value_ == value_;
2338 }
2339
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002340 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002341
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002342 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2343 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2344 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2345
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002346 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002347
2348 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002349 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2350
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002351 const int64_t value_;
2352
David Brazdil8d5b8b22015-03-24 10:51:52 +00002353 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002354 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2355};
2356
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002357enum class Intrinsics {
2358#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2359#include "intrinsics_list.h"
2360 kNone,
2361 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2362#undef INTRINSICS_LIST
2363#undef OPTIMIZING_INTRINSICS
2364};
2365std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2366
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002367class HInvoke : public HInstruction {
2368 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002369 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002370
2371 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2372 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002373 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002374
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002375 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002376 SetRawInputAt(index, argument);
2377 }
2378
Roland Levillain3e3d7332015-04-28 11:00:54 +01002379 // Return the number of arguments. This number can be lower than
2380 // the number of inputs returned by InputCount(), as some invoke
2381 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2382 // inputs at the end of their list of inputs.
2383 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2384
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002385 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002386
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002387 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002388
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002389 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002390 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002391
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002392 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2393
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002394 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002395 return intrinsic_;
2396 }
2397
2398 void SetIntrinsic(Intrinsics intrinsic) {
2399 intrinsic_ = intrinsic;
2400 }
2401
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002402 DECLARE_INSTRUCTION(Invoke);
2403
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002404 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002405 HInvoke(ArenaAllocator* arena,
2406 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002407 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002408 Primitive::Type return_type,
2409 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002410 uint32_t dex_method_index,
2411 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002412 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002413 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002414 inputs_(arena, number_of_arguments),
2415 return_type_(return_type),
2416 dex_pc_(dex_pc),
2417 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002418 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002419 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002420 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2421 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002422 }
2423
David Brazdil1abb4192015-02-17 18:33:36 +00002424 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2425 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2426 inputs_.Put(index, input);
2427 }
2428
Roland Levillain3e3d7332015-04-28 11:00:54 +01002429 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002430 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002431 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002432 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002433 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002434 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002435 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002436
2437 private:
2438 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2439};
2440
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002441class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002442 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002443 // Requirements of this method call regarding the class
2444 // initialization (clinit) check of its declaring class.
2445 enum class ClinitCheckRequirement {
2446 kNone, // Class already initialized.
2447 kExplicit, // Static call having explicit clinit check as last input.
2448 kImplicit, // Static call implicitly requiring a clinit check.
2449 };
2450
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002451 HInvokeStaticOrDirect(ArenaAllocator* arena,
2452 uint32_t number_of_arguments,
2453 Primitive::Type return_type,
2454 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002455 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002456 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002457 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002458 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002459 InvokeType invoke_type,
2460 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002461 : HInvoke(arena,
2462 number_of_arguments,
2463 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u,
2464 return_type,
2465 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002466 dex_method_index,
2467 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002468 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002469 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002470 clinit_check_requirement_(clinit_check_requirement),
2471 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002472
Calin Juravle641547a2015-04-21 22:08:51 +01002473 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2474 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002475 // We access the method via the dex cache so we can't do an implicit null check.
2476 // TODO: for intrinsics we can generate implicit null checks.
2477 return false;
2478 }
2479
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002480 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002481 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002482 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002483 bool IsStringInit() const { return string_init_offset_ != 0; }
2484 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002485
Roland Levillain4c0eb422015-04-24 16:43:49 +01002486 // Is this instruction a call to a static method?
2487 bool IsStatic() const {
2488 return GetInvokeType() == kStatic;
2489 }
2490
Roland Levillain3e3d7332015-04-28 11:00:54 +01002491 // Remove the art::HLoadClass instruction set as last input by
2492 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2493 // the initial art::HClinitCheck instruction (only relevant for
2494 // static calls with explicit clinit check).
2495 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002496 DCHECK(IsStaticWithExplicitClinitCheck());
2497 size_t last_input_index = InputCount() - 1;
2498 HInstruction* last_input = InputAt(last_input_index);
2499 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002500 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002501 RemoveAsUserOfInput(last_input_index);
2502 inputs_.DeleteAt(last_input_index);
2503 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2504 DCHECK(IsStaticWithImplicitClinitCheck());
2505 }
2506
2507 // Is this a call to a static method whose declaring class has an
2508 // explicit intialization check in the graph?
2509 bool IsStaticWithExplicitClinitCheck() const {
2510 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2511 }
2512
2513 // Is this a call to a static method whose declaring class has an
2514 // implicit intialization check requirement?
2515 bool IsStaticWithImplicitClinitCheck() const {
2516 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2517 }
2518
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002519 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002520
Roland Levillain4c0eb422015-04-24 16:43:49 +01002521 protected:
2522 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2523 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2524 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2525 HInstruction* input = input_record.GetInstruction();
2526 // `input` is the last input of a static invoke marked as having
2527 // an explicit clinit check. It must either be:
2528 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2529 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2530 DCHECK(input != nullptr);
2531 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2532 }
2533 return input_record;
2534 }
2535
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002536 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002537 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002538 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002539 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002540 // Thread entrypoint offset for string init method if this is a string init invoke.
2541 // Note that there are multiple string init methods, each having its own offset.
2542 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002543
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002544 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002545};
2546
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002547class HInvokeVirtual : public HInvoke {
2548 public:
2549 HInvokeVirtual(ArenaAllocator* arena,
2550 uint32_t number_of_arguments,
2551 Primitive::Type return_type,
2552 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002553 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002554 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002555 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002556 vtable_index_(vtable_index) {}
2557
Calin Juravle641547a2015-04-21 22:08:51 +01002558 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002559 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002560 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002561 }
2562
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002563 uint32_t GetVTableIndex() const { return vtable_index_; }
2564
2565 DECLARE_INSTRUCTION(InvokeVirtual);
2566
2567 private:
2568 const uint32_t vtable_index_;
2569
2570 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2571};
2572
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002573class HInvokeInterface : public HInvoke {
2574 public:
2575 HInvokeInterface(ArenaAllocator* arena,
2576 uint32_t number_of_arguments,
2577 Primitive::Type return_type,
2578 uint32_t dex_pc,
2579 uint32_t dex_method_index,
2580 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002581 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002582 imt_index_(imt_index) {}
2583
Calin Juravle641547a2015-04-21 22:08:51 +01002584 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002585 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002586 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002587 }
2588
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002589 uint32_t GetImtIndex() const { return imt_index_; }
2590 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2591
2592 DECLARE_INSTRUCTION(InvokeInterface);
2593
2594 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002595 const uint32_t imt_index_;
2596
2597 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2598};
2599
Dave Allison20dfc792014-06-16 20:44:29 -07002600class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002601 public:
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002602 HNewInstance(uint32_t dex_pc,
2603 uint16_t type_index,
2604 const DexFile& dex_file,
2605 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002606 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2607 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002608 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002609 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002610 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002611
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002612 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002613 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002614 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002615
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002616 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002617 bool NeedsEnvironment() const OVERRIDE { return true; }
2618 // It may throw when called on:
2619 // - interfaces
2620 // - abstract/innaccessible/unknown classes
2621 // TODO: optimize when possible.
2622 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002623
Calin Juravle10e244f2015-01-26 18:54:32 +00002624 bool CanBeNull() const OVERRIDE { return false; }
2625
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002626 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2627
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002628 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002629
2630 private:
2631 const uint32_t dex_pc_;
2632 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002633 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002634 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002635
2636 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2637};
2638
Roland Levillain88cb1752014-10-20 16:36:47 +01002639class HNeg : public HUnaryOperation {
2640 public:
2641 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2642 : HUnaryOperation(result_type, input) {}
2643
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002644 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2645 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002646
Roland Levillain88cb1752014-10-20 16:36:47 +01002647 DECLARE_INSTRUCTION(Neg);
2648
2649 private:
2650 DISALLOW_COPY_AND_ASSIGN(HNeg);
2651};
2652
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002653class HNewArray : public HExpression<1> {
2654 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002655 HNewArray(HInstruction* length,
2656 uint32_t dex_pc,
2657 uint16_t type_index,
2658 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002659 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2660 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002661 type_index_(type_index),
2662 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002663 SetRawInputAt(0, length);
2664 }
2665
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002666 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002667 uint16_t GetTypeIndex() const { return type_index_; }
2668
2669 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002670 bool NeedsEnvironment() const OVERRIDE { return true; }
2671
Mingyao Yang0c365e62015-03-31 15:09:29 -07002672 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2673 bool CanThrow() const OVERRIDE { return true; }
2674
Calin Juravle10e244f2015-01-26 18:54:32 +00002675 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002676
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002677 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2678
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002679 DECLARE_INSTRUCTION(NewArray);
2680
2681 private:
2682 const uint32_t dex_pc_;
2683 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002684 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002685
2686 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2687};
2688
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002689class HAdd : public HBinaryOperation {
2690 public:
2691 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2692 : HBinaryOperation(result_type, left, right) {}
2693
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002694 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002695
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002696 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002697 return x + y;
2698 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002699 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002700 return x + y;
2701 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002702
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002703 DECLARE_INSTRUCTION(Add);
2704
2705 private:
2706 DISALLOW_COPY_AND_ASSIGN(HAdd);
2707};
2708
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002709class HSub : public HBinaryOperation {
2710 public:
2711 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2712 : HBinaryOperation(result_type, left, right) {}
2713
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002714 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002715 return x - y;
2716 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002717 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002718 return x - y;
2719 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002720
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002721 DECLARE_INSTRUCTION(Sub);
2722
2723 private:
2724 DISALLOW_COPY_AND_ASSIGN(HSub);
2725};
2726
Calin Juravle34bacdf2014-10-07 20:23:36 +01002727class HMul : public HBinaryOperation {
2728 public:
2729 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2730 : HBinaryOperation(result_type, left, right) {}
2731
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002732 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002733
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002734 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2735 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002736
2737 DECLARE_INSTRUCTION(Mul);
2738
2739 private:
2740 DISALLOW_COPY_AND_ASSIGN(HMul);
2741};
2742
Calin Juravle7c4954d2014-10-28 16:57:40 +00002743class HDiv : public HBinaryOperation {
2744 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002745 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2746 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002747
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002748 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002749 // Our graph structure ensures we never have 0 for `y` during constant folding.
2750 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002751 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002752 return (y == -1) ? -x : x / y;
2753 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002754
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002755 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002756 DCHECK_NE(y, 0);
2757 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2758 return (y == -1) ? -x : x / y;
2759 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002760
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002761 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002762
Calin Juravle7c4954d2014-10-28 16:57:40 +00002763 DECLARE_INSTRUCTION(Div);
2764
2765 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002766 const uint32_t dex_pc_;
2767
Calin Juravle7c4954d2014-10-28 16:57:40 +00002768 DISALLOW_COPY_AND_ASSIGN(HDiv);
2769};
2770
Calin Juravlebacfec32014-11-14 15:54:36 +00002771class HRem : public HBinaryOperation {
2772 public:
2773 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2774 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2775
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002776 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002777 DCHECK_NE(y, 0);
2778 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2779 return (y == -1) ? 0 : x % y;
2780 }
2781
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002782 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002783 DCHECK_NE(y, 0);
2784 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2785 return (y == -1) ? 0 : x % y;
2786 }
2787
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002788 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002789
2790 DECLARE_INSTRUCTION(Rem);
2791
2792 private:
2793 const uint32_t dex_pc_;
2794
2795 DISALLOW_COPY_AND_ASSIGN(HRem);
2796};
2797
Calin Juravled0d48522014-11-04 16:40:20 +00002798class HDivZeroCheck : public HExpression<1> {
2799 public:
2800 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2801 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2802 SetRawInputAt(0, value);
2803 }
2804
2805 bool CanBeMoved() const OVERRIDE { return true; }
2806
2807 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2808 UNUSED(other);
2809 return true;
2810 }
2811
2812 bool NeedsEnvironment() const OVERRIDE { return true; }
2813 bool CanThrow() const OVERRIDE { return true; }
2814
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002815 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002816
2817 DECLARE_INSTRUCTION(DivZeroCheck);
2818
2819 private:
2820 const uint32_t dex_pc_;
2821
2822 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2823};
2824
Calin Juravle9aec02f2014-11-18 23:06:35 +00002825class HShl : public HBinaryOperation {
2826 public:
2827 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2828 : HBinaryOperation(result_type, left, right) {}
2829
2830 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2831 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2832
2833 DECLARE_INSTRUCTION(Shl);
2834
2835 private:
2836 DISALLOW_COPY_AND_ASSIGN(HShl);
2837};
2838
2839class HShr : public HBinaryOperation {
2840 public:
2841 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2842 : HBinaryOperation(result_type, left, right) {}
2843
2844 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2845 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2846
2847 DECLARE_INSTRUCTION(Shr);
2848
2849 private:
2850 DISALLOW_COPY_AND_ASSIGN(HShr);
2851};
2852
2853class HUShr : public HBinaryOperation {
2854 public:
2855 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2856 : HBinaryOperation(result_type, left, right) {}
2857
2858 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2859 uint32_t ux = static_cast<uint32_t>(x);
2860 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2861 return static_cast<int32_t>(ux >> uy);
2862 }
2863
2864 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2865 uint64_t ux = static_cast<uint64_t>(x);
2866 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2867 return static_cast<int64_t>(ux >> uy);
2868 }
2869
2870 DECLARE_INSTRUCTION(UShr);
2871
2872 private:
2873 DISALLOW_COPY_AND_ASSIGN(HUShr);
2874};
2875
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002876class HAnd : public HBinaryOperation {
2877 public:
2878 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2879 : HBinaryOperation(result_type, left, right) {}
2880
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002881 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002882
2883 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2884 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2885
2886 DECLARE_INSTRUCTION(And);
2887
2888 private:
2889 DISALLOW_COPY_AND_ASSIGN(HAnd);
2890};
2891
2892class HOr : public HBinaryOperation {
2893 public:
2894 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2895 : HBinaryOperation(result_type, left, right) {}
2896
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002897 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002898
2899 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2900 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2901
2902 DECLARE_INSTRUCTION(Or);
2903
2904 private:
2905 DISALLOW_COPY_AND_ASSIGN(HOr);
2906};
2907
2908class HXor : public HBinaryOperation {
2909 public:
2910 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2911 : HBinaryOperation(result_type, left, right) {}
2912
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002913 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002914
2915 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2916 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2917
2918 DECLARE_INSTRUCTION(Xor);
2919
2920 private:
2921 DISALLOW_COPY_AND_ASSIGN(HXor);
2922};
2923
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002924// The value of a parameter in this method. Its location depends on
2925// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002926class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002927 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002928 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2929 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002930
2931 uint8_t GetIndex() const { return index_; }
2932
Calin Juravle10e244f2015-01-26 18:54:32 +00002933 bool CanBeNull() const OVERRIDE { return !is_this_; }
2934
Calin Juravle3cd4fc82015-05-14 15:15:42 +01002935 bool IsThis() const { return is_this_; }
2936
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002937 DECLARE_INSTRUCTION(ParameterValue);
2938
2939 private:
2940 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002941 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002942 const uint8_t index_;
2943
Calin Juravle10e244f2015-01-26 18:54:32 +00002944 // Whether or not the parameter value corresponds to 'this' argument.
2945 const bool is_this_;
2946
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002947 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2948};
2949
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002950class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002951 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002952 explicit HNot(Primitive::Type result_type, HInstruction* input)
2953 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002954
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002955 bool CanBeMoved() const OVERRIDE { return true; }
2956 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002957 UNUSED(other);
2958 return true;
2959 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002960
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002961 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2962 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002963
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002964 DECLARE_INSTRUCTION(Not);
2965
2966 private:
2967 DISALLOW_COPY_AND_ASSIGN(HNot);
2968};
2969
David Brazdil66d126e2015-04-03 16:02:44 +01002970class HBooleanNot : public HUnaryOperation {
2971 public:
2972 explicit HBooleanNot(HInstruction* input)
2973 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2974
2975 bool CanBeMoved() const OVERRIDE { return true; }
2976 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2977 UNUSED(other);
2978 return true;
2979 }
2980
2981 int32_t Evaluate(int32_t x) const OVERRIDE {
2982 DCHECK(IsUint<1>(x));
2983 return !x;
2984 }
2985
2986 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2987 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2988 UNREACHABLE();
2989 }
2990
2991 DECLARE_INSTRUCTION(BooleanNot);
2992
2993 private:
2994 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2995};
2996
Roland Levillaindff1f282014-11-05 14:15:05 +00002997class HTypeConversion : public HExpression<1> {
2998 public:
2999 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003000 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3001 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003002 SetRawInputAt(0, input);
3003 DCHECK_NE(input->GetType(), result_type);
3004 }
3005
3006 HInstruction* GetInput() const { return InputAt(0); }
3007 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3008 Primitive::Type GetResultType() const { return GetType(); }
3009
Roland Levillain624279f2014-12-04 11:54:28 +00003010 // Required by the x86 and ARM code generators when producing calls
3011 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003012 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003013
Roland Levillaindff1f282014-11-05 14:15:05 +00003014 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003015 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003016
Mark Mendelle82549b2015-05-06 10:55:34 -04003017 // Try to statically evaluate the conversion and return a HConstant
3018 // containing the result. If the input cannot be converted, return nullptr.
3019 HConstant* TryStaticEvaluation() const;
3020
Roland Levillaindff1f282014-11-05 14:15:05 +00003021 DECLARE_INSTRUCTION(TypeConversion);
3022
3023 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003024 const uint32_t dex_pc_;
3025
Roland Levillaindff1f282014-11-05 14:15:05 +00003026 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3027};
3028
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003029static constexpr uint32_t kNoRegNumber = -1;
3030
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003031class HPhi : public HInstruction {
3032 public:
3033 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003034 : HInstruction(SideEffects::None()),
3035 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003036 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003037 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003038 is_live_(false),
3039 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003040 inputs_.SetSize(number_of_inputs);
3041 }
3042
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003043 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3044 static Primitive::Type ToPhiType(Primitive::Type type) {
3045 switch (type) {
3046 case Primitive::kPrimBoolean:
3047 case Primitive::kPrimByte:
3048 case Primitive::kPrimShort:
3049 case Primitive::kPrimChar:
3050 return Primitive::kPrimInt;
3051 default:
3052 return type;
3053 }
3054 }
3055
Calin Juravle10e244f2015-01-26 18:54:32 +00003056 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003057
3058 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003059 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003060
Calin Juravle10e244f2015-01-26 18:54:32 +00003061 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003062 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003063
Calin Juravle10e244f2015-01-26 18:54:32 +00003064 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3065 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3066
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003067 uint32_t GetRegNumber() const { return reg_number_; }
3068
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003069 void SetDead() { is_live_ = false; }
3070 void SetLive() { is_live_ = true; }
3071 bool IsDead() const { return !is_live_; }
3072 bool IsLive() const { return is_live_; }
3073
Calin Juravlea4f88312015-04-16 12:57:19 +01003074 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3075 // An equivalent phi is a phi having the same dex register and type.
3076 // It assumes that phis with the same dex register are adjacent.
3077 HPhi* GetNextEquivalentPhiWithSameType() {
3078 HInstruction* next = GetNext();
3079 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3080 if (next->GetType() == GetType()) {
3081 return next->AsPhi();
3082 }
3083 next = next->GetNext();
3084 }
3085 return nullptr;
3086 }
3087
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003088 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003089
David Brazdil1abb4192015-02-17 18:33:36 +00003090 protected:
3091 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3092
3093 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3094 inputs_.Put(index, input);
3095 }
3096
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003097 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003098 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003099 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003100 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003101 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003102 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003103
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003104 DISALLOW_COPY_AND_ASSIGN(HPhi);
3105};
3106
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003107class HNullCheck : public HExpression<1> {
3108 public:
3109 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003110 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003111 SetRawInputAt(0, value);
3112 }
3113
Calin Juravle10e244f2015-01-26 18:54:32 +00003114 bool CanBeMoved() const OVERRIDE { return true; }
3115 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003116 UNUSED(other);
3117 return true;
3118 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003119
Calin Juravle10e244f2015-01-26 18:54:32 +00003120 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003121
Calin Juravle10e244f2015-01-26 18:54:32 +00003122 bool CanThrow() const OVERRIDE { return true; }
3123
3124 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003125
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003126 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003127
3128 DECLARE_INSTRUCTION(NullCheck);
3129
3130 private:
3131 const uint32_t dex_pc_;
3132
3133 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3134};
3135
3136class FieldInfo : public ValueObject {
3137 public:
Calin Juravle52c48962014-12-16 17:02:57 +00003138 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
3139 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003140
3141 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003142 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003143 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003144
3145 private:
3146 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003147 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003148 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003149};
3150
3151class HInstanceFieldGet : public HExpression<1> {
3152 public:
3153 HInstanceFieldGet(HInstruction* value,
3154 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003155 MemberOffset field_offset,
3156 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003157 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003158 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003159 SetRawInputAt(0, value);
3160 }
3161
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003162 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003163
3164 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3165 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3166 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003167 }
3168
Calin Juravle641547a2015-04-21 22:08:51 +01003169 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3170 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003171 }
3172
3173 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003174 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3175 }
3176
Calin Juravle52c48962014-12-16 17:02:57 +00003177 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003178 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003179 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003180 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003181
3182 DECLARE_INSTRUCTION(InstanceFieldGet);
3183
3184 private:
3185 const FieldInfo field_info_;
3186
3187 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3188};
3189
3190class HInstanceFieldSet : public HTemplateInstruction<2> {
3191 public:
3192 HInstanceFieldSet(HInstruction* object,
3193 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003194 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003195 MemberOffset field_offset,
3196 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003197 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003198 field_info_(field_offset, field_type, is_volatile),
3199 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003200 SetRawInputAt(0, object);
3201 SetRawInputAt(1, value);
3202 }
3203
Calin Juravle641547a2015-04-21 22:08:51 +01003204 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3205 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003206 }
3207
Calin Juravle52c48962014-12-16 17:02:57 +00003208 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003209 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003210 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003211 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003212 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003213 bool GetValueCanBeNull() const { return value_can_be_null_; }
3214 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003215
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003216 DECLARE_INSTRUCTION(InstanceFieldSet);
3217
3218 private:
3219 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003220 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003221
3222 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3223};
3224
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003225class HArrayGet : public HExpression<2> {
3226 public:
3227 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003228 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003229 SetRawInputAt(0, array);
3230 SetRawInputAt(1, index);
3231 }
3232
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003233 bool CanBeMoved() const OVERRIDE { return true; }
3234 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003235 UNUSED(other);
3236 return true;
3237 }
Calin Juravle641547a2015-04-21 22:08:51 +01003238 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3239 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003240 // TODO: We can be smarter here.
3241 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3242 // which generates the implicit null check. There are cases when these can be removed
3243 // to produce better code. If we ever add optimizations to do so we should allow an
3244 // implicit check here (as long as the address falls in the first page).
3245 return false;
3246 }
3247
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003248 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003249
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003250 HInstruction* GetArray() const { return InputAt(0); }
3251 HInstruction* GetIndex() const { return InputAt(1); }
3252
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003253 DECLARE_INSTRUCTION(ArrayGet);
3254
3255 private:
3256 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3257};
3258
3259class HArraySet : public HTemplateInstruction<3> {
3260 public:
3261 HArraySet(HInstruction* array,
3262 HInstruction* index,
3263 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003264 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003265 uint32_t dex_pc)
3266 : HTemplateInstruction(SideEffects::ChangesSomething()),
3267 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003268 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003269 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3270 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003271 SetRawInputAt(0, array);
3272 SetRawInputAt(1, index);
3273 SetRawInputAt(2, value);
3274 }
3275
Calin Juravle77520bc2015-01-12 18:45:46 +00003276 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003277 // We currently always call a runtime method to catch array store
3278 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003279 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003280 }
3281
Calin Juravle641547a2015-04-21 22:08:51 +01003282 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3283 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003284 // TODO: Same as for ArrayGet.
3285 return false;
3286 }
3287
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003288 void ClearNeedsTypeCheck() {
3289 needs_type_check_ = false;
3290 }
3291
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003292 void ClearValueCanBeNull() {
3293 value_can_be_null_ = false;
3294 }
3295
3296 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003297 bool NeedsTypeCheck() const { return needs_type_check_; }
3298
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003299 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003300
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003301 HInstruction* GetArray() const { return InputAt(0); }
3302 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003303 HInstruction* GetValue() const { return InputAt(2); }
3304
3305 Primitive::Type GetComponentType() const {
3306 // The Dex format does not type floating point index operations. Since the
3307 // `expected_component_type_` is set during building and can therefore not
3308 // be correct, we also check what is the value type. If it is a floating
3309 // point type, we must use that type.
3310 Primitive::Type value_type = GetValue()->GetType();
3311 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3312 ? value_type
3313 : expected_component_type_;
3314 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003315
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003316 DECLARE_INSTRUCTION(ArraySet);
3317
3318 private:
3319 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003320 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003321 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003322 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003323
3324 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3325};
3326
3327class HArrayLength : public HExpression<1> {
3328 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003329 explicit HArrayLength(HInstruction* array)
3330 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3331 // Note that arrays do not change length, so the instruction does not
3332 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003333 SetRawInputAt(0, array);
3334 }
3335
Calin Juravle77520bc2015-01-12 18:45:46 +00003336 bool CanBeMoved() const OVERRIDE { return true; }
3337 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003338 UNUSED(other);
3339 return true;
3340 }
Calin Juravle641547a2015-04-21 22:08:51 +01003341 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3342 return obj == InputAt(0);
3343 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003344
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003345 DECLARE_INSTRUCTION(ArrayLength);
3346
3347 private:
3348 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3349};
3350
3351class HBoundsCheck : public HExpression<2> {
3352 public:
3353 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003354 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003355 DCHECK(index->GetType() == Primitive::kPrimInt);
3356 SetRawInputAt(0, index);
3357 SetRawInputAt(1, length);
3358 }
3359
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003360 bool CanBeMoved() const OVERRIDE { return true; }
3361 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003362 UNUSED(other);
3363 return true;
3364 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003365
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003366 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003367
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003368 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003369
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003370 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003371
3372 DECLARE_INSTRUCTION(BoundsCheck);
3373
3374 private:
3375 const uint32_t dex_pc_;
3376
3377 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3378};
3379
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003380/**
3381 * Some DEX instructions are folded into multiple HInstructions that need
3382 * to stay live until the last HInstruction. This class
3383 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003384 * HInstruction stays live. `index` represents the stack location index of the
3385 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003386 */
3387class HTemporary : public HTemplateInstruction<0> {
3388 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003389 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003390
3391 size_t GetIndex() const { return index_; }
3392
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003393 Primitive::Type GetType() const OVERRIDE {
3394 // The previous instruction is the one that will be stored in the temporary location.
3395 DCHECK(GetPrevious() != nullptr);
3396 return GetPrevious()->GetType();
3397 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003398
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003399 DECLARE_INSTRUCTION(Temporary);
3400
3401 private:
3402 const size_t index_;
3403
3404 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3405};
3406
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003407class HSuspendCheck : public HTemplateInstruction<0> {
3408 public:
3409 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003410 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003411
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003412 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003413 return true;
3414 }
3415
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003416 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003417 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3418 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003419
3420 DECLARE_INSTRUCTION(SuspendCheck);
3421
3422 private:
3423 const uint32_t dex_pc_;
3424
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003425 // Only used for code generation, in order to share the same slow path between back edges
3426 // of a same loop.
3427 SlowPathCode* slow_path_;
3428
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003429 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3430};
3431
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003432/**
3433 * Instruction to load a Class object.
3434 */
3435class HLoadClass : public HExpression<0> {
3436 public:
3437 HLoadClass(uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003438 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003439 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003440 uint32_t dex_pc)
3441 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3442 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003443 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003444 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003445 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003446 generate_clinit_check_(false),
3447 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003448
3449 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003450
3451 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3452 return other->AsLoadClass()->type_index_ == type_index_;
3453 }
3454
3455 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3456
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003457 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003458 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003459 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003460
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003461 bool NeedsEnvironment() const OVERRIDE {
3462 // Will call runtime and load the class if the class is not loaded yet.
3463 // TODO: finer grain decision.
3464 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003465 }
3466
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003467 bool MustGenerateClinitCheck() const {
3468 return generate_clinit_check_;
3469 }
3470
Calin Juravle0ba218d2015-05-19 18:46:01 +01003471 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3472 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003473 }
3474
3475 bool CanCallRuntime() const {
3476 return MustGenerateClinitCheck() || !is_referrers_class_;
3477 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003478
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003479 bool CanThrow() const OVERRIDE {
3480 // May call runtime and and therefore can throw.
3481 // TODO: finer grain decision.
3482 return !is_referrers_class_;
3483 }
3484
Calin Juravleacf735c2015-02-12 15:25:22 +00003485 ReferenceTypeInfo GetLoadedClassRTI() {
3486 return loaded_class_rti_;
3487 }
3488
3489 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3490 // Make sure we only set exact types (the loaded class should never be merged).
3491 DCHECK(rti.IsExact());
3492 loaded_class_rti_ = rti;
3493 }
3494
3495 bool IsResolved() {
3496 return loaded_class_rti_.IsExact();
3497 }
3498
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003499 const DexFile& GetDexFile() { return dex_file_; }
3500
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003501 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3502
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003503 DECLARE_INSTRUCTION(LoadClass);
3504
3505 private:
3506 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003507 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003508 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003509 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003510 // Whether this instruction must generate the initialization check.
3511 // Used for code generation.
3512 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003513
Calin Juravleacf735c2015-02-12 15:25:22 +00003514 ReferenceTypeInfo loaded_class_rti_;
3515
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003516 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3517};
3518
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003519class HLoadString : public HExpression<0> {
3520 public:
3521 HLoadString(uint32_t string_index, uint32_t dex_pc)
3522 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3523 string_index_(string_index),
3524 dex_pc_(dex_pc) {}
3525
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003526 bool CanBeMoved() const OVERRIDE { return true; }
3527
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003528 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3529 return other->AsLoadString()->string_index_ == string_index_;
3530 }
3531
3532 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3533
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003534 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003535 uint32_t GetStringIndex() const { return string_index_; }
3536
3537 // TODO: Can we deopt or debug when we resolve a string?
3538 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003539 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003540
3541 DECLARE_INSTRUCTION(LoadString);
3542
3543 private:
3544 const uint32_t string_index_;
3545 const uint32_t dex_pc_;
3546
3547 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3548};
3549
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003550/**
3551 * Performs an initialization check on its Class object input.
3552 */
3553class HClinitCheck : public HExpression<1> {
3554 public:
3555 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003556 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003557 dex_pc_(dex_pc) {
3558 SetRawInputAt(0, constant);
3559 }
3560
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003561 bool CanBeMoved() const OVERRIDE { return true; }
3562 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3563 UNUSED(other);
3564 return true;
3565 }
3566
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003567 bool NeedsEnvironment() const OVERRIDE {
3568 // May call runtime to initialize the class.
3569 return true;
3570 }
3571
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003572 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003573
3574 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3575
3576 DECLARE_INSTRUCTION(ClinitCheck);
3577
3578 private:
3579 const uint32_t dex_pc_;
3580
3581 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3582};
3583
3584class HStaticFieldGet : public HExpression<1> {
3585 public:
3586 HStaticFieldGet(HInstruction* cls,
3587 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003588 MemberOffset field_offset,
3589 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003590 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003591 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003592 SetRawInputAt(0, cls);
3593 }
3594
Calin Juravle52c48962014-12-16 17:02:57 +00003595
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003596 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003597
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003598 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003599 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3600 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003601 }
3602
3603 size_t ComputeHashCode() const OVERRIDE {
3604 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3605 }
3606
Calin Juravle52c48962014-12-16 17:02:57 +00003607 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003608 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3609 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003610 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003611
3612 DECLARE_INSTRUCTION(StaticFieldGet);
3613
3614 private:
3615 const FieldInfo field_info_;
3616
3617 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3618};
3619
3620class HStaticFieldSet : public HTemplateInstruction<2> {
3621 public:
3622 HStaticFieldSet(HInstruction* cls,
3623 HInstruction* value,
3624 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003625 MemberOffset field_offset,
3626 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003627 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003628 field_info_(field_offset, field_type, is_volatile),
3629 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003630 SetRawInputAt(0, cls);
3631 SetRawInputAt(1, value);
3632 }
3633
Calin Juravle52c48962014-12-16 17:02:57 +00003634 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003635 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3636 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003637 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003638
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003639 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003640 bool GetValueCanBeNull() const { return value_can_be_null_; }
3641 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003642
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003643 DECLARE_INSTRUCTION(StaticFieldSet);
3644
3645 private:
3646 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003647 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003648
3649 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3650};
3651
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003652// Implement the move-exception DEX instruction.
3653class HLoadException : public HExpression<0> {
3654 public:
3655 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3656
3657 DECLARE_INSTRUCTION(LoadException);
3658
3659 private:
3660 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3661};
3662
3663class HThrow : public HTemplateInstruction<1> {
3664 public:
3665 HThrow(HInstruction* exception, uint32_t dex_pc)
3666 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3667 SetRawInputAt(0, exception);
3668 }
3669
3670 bool IsControlFlow() const OVERRIDE { return true; }
3671
3672 bool NeedsEnvironment() const OVERRIDE { return true; }
3673
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003674 bool CanThrow() const OVERRIDE { return true; }
3675
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003676 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003677
3678 DECLARE_INSTRUCTION(Throw);
3679
3680 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003681 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003682
3683 DISALLOW_COPY_AND_ASSIGN(HThrow);
3684};
3685
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003686class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003687 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003688 HInstanceOf(HInstruction* object,
3689 HLoadClass* constant,
3690 bool class_is_final,
3691 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003692 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3693 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003694 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003695 dex_pc_(dex_pc) {
3696 SetRawInputAt(0, object);
3697 SetRawInputAt(1, constant);
3698 }
3699
3700 bool CanBeMoved() const OVERRIDE { return true; }
3701
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003702 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003703 return true;
3704 }
3705
3706 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003707 return false;
3708 }
3709
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003710 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003711
3712 bool IsClassFinal() const { return class_is_final_; }
3713
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003714 // Used only in code generation.
3715 bool MustDoNullCheck() const { return must_do_null_check_; }
3716 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3717
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003718 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003719
3720 private:
3721 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003722 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003723 const uint32_t dex_pc_;
3724
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003725 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3726};
3727
Calin Juravleb1498f62015-02-16 13:13:29 +00003728class HBoundType : public HExpression<1> {
3729 public:
3730 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3731 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3732 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003733 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003734 SetRawInputAt(0, input);
3735 }
3736
3737 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3738
3739 bool CanBeNull() const OVERRIDE {
3740 // `null instanceof ClassX` always return false so we can't be null.
3741 return false;
3742 }
3743
3744 DECLARE_INSTRUCTION(BoundType);
3745
3746 private:
3747 // Encodes the most upper class that this instruction can have. In other words
3748 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3749 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3750 const ReferenceTypeInfo bound_type_;
3751
3752 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3753};
3754
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003755class HCheckCast : public HTemplateInstruction<2> {
3756 public:
3757 HCheckCast(HInstruction* object,
3758 HLoadClass* constant,
3759 bool class_is_final,
3760 uint32_t dex_pc)
3761 : HTemplateInstruction(SideEffects::None()),
3762 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003763 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003764 dex_pc_(dex_pc) {
3765 SetRawInputAt(0, object);
3766 SetRawInputAt(1, constant);
3767 }
3768
3769 bool CanBeMoved() const OVERRIDE { return true; }
3770
3771 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3772 return true;
3773 }
3774
3775 bool NeedsEnvironment() const OVERRIDE {
3776 // Instruction may throw a CheckCastError.
3777 return true;
3778 }
3779
3780 bool CanThrow() const OVERRIDE { return true; }
3781
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003782 bool MustDoNullCheck() const { return must_do_null_check_; }
3783 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3784
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003785 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003786
3787 bool IsClassFinal() const { return class_is_final_; }
3788
3789 DECLARE_INSTRUCTION(CheckCast);
3790
3791 private:
3792 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003793 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003794 const uint32_t dex_pc_;
3795
3796 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003797};
3798
Calin Juravle27df7582015-04-17 19:12:31 +01003799class HMemoryBarrier : public HTemplateInstruction<0> {
3800 public:
3801 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3802 : HTemplateInstruction(SideEffects::None()),
3803 barrier_kind_(barrier_kind) {}
3804
3805 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3806
3807 DECLARE_INSTRUCTION(MemoryBarrier);
3808
3809 private:
3810 const MemBarrierKind barrier_kind_;
3811
3812 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3813};
3814
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003815class HMonitorOperation : public HTemplateInstruction<1> {
3816 public:
3817 enum OperationKind {
3818 kEnter,
3819 kExit,
3820 };
3821
3822 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3823 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3824 SetRawInputAt(0, object);
3825 }
3826
3827 // Instruction may throw a Java exception, so we need an environment.
3828 bool NeedsEnvironment() const OVERRIDE { return true; }
3829 bool CanThrow() const OVERRIDE { return true; }
3830
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003831 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003832
3833 bool IsEnter() const { return kind_ == kEnter; }
3834
3835 DECLARE_INSTRUCTION(MonitorOperation);
3836
Calin Juravle52c48962014-12-16 17:02:57 +00003837 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003838 const OperationKind kind_;
3839 const uint32_t dex_pc_;
3840
3841 private:
3842 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3843};
3844
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003845class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003846 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003847 MoveOperands(Location source,
3848 Location destination,
3849 Primitive::Type type,
3850 HInstruction* instruction)
3851 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003852
3853 Location GetSource() const { return source_; }
3854 Location GetDestination() const { return destination_; }
3855
3856 void SetSource(Location value) { source_ = value; }
3857 void SetDestination(Location value) { destination_ = value; }
3858
3859 // The parallel move resolver marks moves as "in-progress" by clearing the
3860 // destination (but not the source).
3861 Location MarkPending() {
3862 DCHECK(!IsPending());
3863 Location dest = destination_;
3864 destination_ = Location::NoLocation();
3865 return dest;
3866 }
3867
3868 void ClearPending(Location dest) {
3869 DCHECK(IsPending());
3870 destination_ = dest;
3871 }
3872
3873 bool IsPending() const {
3874 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3875 return destination_.IsInvalid() && !source_.IsInvalid();
3876 }
3877
3878 // True if this blocks a move from the given location.
3879 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003880 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003881 }
3882
3883 // A move is redundant if it's been eliminated, if its source and
3884 // destination are the same, or if its destination is unneeded.
3885 bool IsRedundant() const {
3886 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3887 }
3888
3889 // We clear both operands to indicate move that's been eliminated.
3890 void Eliminate() {
3891 source_ = destination_ = Location::NoLocation();
3892 }
3893
3894 bool IsEliminated() const {
3895 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3896 return source_.IsInvalid();
3897 }
3898
Nicolas Geoffray90218252015-04-15 11:56:51 +01003899 bool Is64BitMove() const {
3900 return Primitive::Is64BitType(type_);
3901 }
3902
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003903 HInstruction* GetInstruction() const { return instruction_; }
3904
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003905 private:
3906 Location source_;
3907 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003908 // The type this move is for.
3909 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003910 // The instruction this move is assocatied with. Null when this move is
3911 // for moving an input in the expected locations of user (including a phi user).
3912 // This is only used in debug mode, to ensure we do not connect interval siblings
3913 // in the same parallel move.
3914 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003915};
3916
3917static constexpr size_t kDefaultNumberOfMoves = 4;
3918
3919class HParallelMove : public HTemplateInstruction<0> {
3920 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003921 explicit HParallelMove(ArenaAllocator* arena)
3922 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003923
Nicolas Geoffray90218252015-04-15 11:56:51 +01003924 void AddMove(Location source,
3925 Location destination,
3926 Primitive::Type type,
3927 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003928 DCHECK(source.IsValid());
3929 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003930 if (kIsDebugBuild) {
3931 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003932 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003933 if (moves_.Get(i).GetInstruction() == instruction) {
3934 // Special case the situation where the move is for the spill slot
3935 // of the instruction.
3936 if ((GetPrevious() == instruction)
3937 || ((GetPrevious() == nullptr)
3938 && instruction->IsPhi()
3939 && instruction->GetBlock() == GetBlock())) {
3940 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3941 << "Doing parallel moves for the same instruction.";
3942 } else {
3943 DCHECK(false) << "Doing parallel moves for the same instruction.";
3944 }
3945 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003946 }
3947 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003948 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003949 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01003950 << "Overlapped destination for two moves in a parallel move: "
3951 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
3952 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003953 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003954 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003955 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003956 }
3957
3958 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003959 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003960 }
3961
3962 size_t NumMoves() const { return moves_.Size(); }
3963
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003964 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003965
3966 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003967 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003968
3969 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3970};
3971
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003972class HGraphVisitor : public ValueObject {
3973 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003974 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3975 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003976
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003977 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003978 virtual void VisitBasicBlock(HBasicBlock* block);
3979
Roland Levillain633021e2014-10-01 14:12:25 +01003980 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003981 void VisitInsertionOrder();
3982
Roland Levillain633021e2014-10-01 14:12:25 +01003983 // Visit the graph following dominator tree reverse post-order.
3984 void VisitReversePostOrder();
3985
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003986 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003987
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003988 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003989#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003990 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3991
3992 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3993
3994#undef DECLARE_VISIT_INSTRUCTION
3995
3996 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003997 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003998
3999 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4000};
4001
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004002class HGraphDelegateVisitor : public HGraphVisitor {
4003 public:
4004 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4005 virtual ~HGraphDelegateVisitor() {}
4006
4007 // Visit functions that delegate to to super class.
4008#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004009 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004010
4011 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4012
4013#undef DECLARE_VISIT_INSTRUCTION
4014
4015 private:
4016 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4017};
4018
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004019class HInsertionOrderIterator : public ValueObject {
4020 public:
4021 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4022
4023 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4024 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4025 void Advance() { ++index_; }
4026
4027 private:
4028 const HGraph& graph_;
4029 size_t index_;
4030
4031 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4032};
4033
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004034class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004035 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004036 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4037 // Check that reverse post order of the graph has been built.
4038 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4039 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004040
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004041 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4042 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004043 void Advance() { ++index_; }
4044
4045 private:
4046 const HGraph& graph_;
4047 size_t index_;
4048
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004049 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004050};
4051
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004052class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004053 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004054 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004055 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4056 // Check that reverse post order of the graph has been built.
4057 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4058 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004059
4060 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004061 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004062 void Advance() { --index_; }
4063
4064 private:
4065 const HGraph& graph_;
4066 size_t index_;
4067
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004068 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004069};
4070
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004071class HLinearPostOrderIterator : public ValueObject {
4072 public:
4073 explicit HLinearPostOrderIterator(const HGraph& graph)
4074 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4075
4076 bool Done() const { return index_ == 0; }
4077
4078 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4079
4080 void Advance() {
4081 --index_;
4082 DCHECK_GE(index_, 0U);
4083 }
4084
4085 private:
4086 const GrowableArray<HBasicBlock*>& order_;
4087 size_t index_;
4088
4089 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4090};
4091
4092class HLinearOrderIterator : public ValueObject {
4093 public:
4094 explicit HLinearOrderIterator(const HGraph& graph)
4095 : order_(graph.GetLinearOrder()), index_(0) {}
4096
4097 bool Done() const { return index_ == order_.Size(); }
4098 HBasicBlock* Current() const { return order_.Get(index_); }
4099 void Advance() { ++index_; }
4100
4101 private:
4102 const GrowableArray<HBasicBlock*>& order_;
4103 size_t index_;
4104
4105 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4106};
4107
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004108// Iterator over the blocks that art part of the loop. Includes blocks part
4109// of an inner loop. The order in which the blocks are iterated is on their
4110// block id.
4111class HBlocksInLoopIterator : public ValueObject {
4112 public:
4113 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4114 : blocks_in_loop_(info.GetBlocks()),
4115 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4116 index_(0) {
4117 if (!blocks_in_loop_.IsBitSet(index_)) {
4118 Advance();
4119 }
4120 }
4121
4122 bool Done() const { return index_ == blocks_.Size(); }
4123 HBasicBlock* Current() const { return blocks_.Get(index_); }
4124 void Advance() {
4125 ++index_;
4126 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4127 if (blocks_in_loop_.IsBitSet(index_)) {
4128 break;
4129 }
4130 }
4131 }
4132
4133 private:
4134 const BitVector& blocks_in_loop_;
4135 const GrowableArray<HBasicBlock*>& blocks_;
4136 size_t index_;
4137
4138 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4139};
4140
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004141inline int64_t Int64FromConstant(HConstant* constant) {
4142 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4143 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4144 : constant->AsLongConstant()->GetValue();
4145}
4146
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004147} // namespace art
4148
4149#endif // ART_COMPILER_OPTIMIZING_NODES_H_