blob: 12ace413b7770129ce06f2f3fd77ccbc2365c031 [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_; }
2390
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002391 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2392
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002393 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002394 return intrinsic_;
2395 }
2396
2397 void SetIntrinsic(Intrinsics intrinsic) {
2398 intrinsic_ = intrinsic;
2399 }
2400
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002401 DECLARE_INSTRUCTION(Invoke);
2402
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002403 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002404 HInvoke(ArenaAllocator* arena,
2405 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002406 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002407 Primitive::Type return_type,
2408 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002409 uint32_t dex_method_index,
2410 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002411 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002412 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002413 inputs_(arena, number_of_arguments),
2414 return_type_(return_type),
2415 dex_pc_(dex_pc),
2416 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002417 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002418 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002419 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2420 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002421 }
2422
David Brazdil1abb4192015-02-17 18:33:36 +00002423 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2424 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2425 inputs_.Put(index, input);
2426 }
2427
Roland Levillain3e3d7332015-04-28 11:00:54 +01002428 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002429 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002430 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002431 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002432 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002433 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002434 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002435
2436 private:
2437 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2438};
2439
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002440class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002441 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002442 // Requirements of this method call regarding the class
2443 // initialization (clinit) check of its declaring class.
2444 enum class ClinitCheckRequirement {
2445 kNone, // Class already initialized.
2446 kExplicit, // Static call having explicit clinit check as last input.
2447 kImplicit, // Static call implicitly requiring a clinit check.
2448 };
2449
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002450 HInvokeStaticOrDirect(ArenaAllocator* arena,
2451 uint32_t number_of_arguments,
2452 Primitive::Type return_type,
2453 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002454 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002455 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002456 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002457 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002458 InvokeType invoke_type,
2459 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002460 : HInvoke(arena,
2461 number_of_arguments,
2462 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u,
2463 return_type,
2464 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002465 dex_method_index,
2466 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002467 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002468 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002469 clinit_check_requirement_(clinit_check_requirement),
2470 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002471
Calin Juravle641547a2015-04-21 22:08:51 +01002472 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2473 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002474 // We access the method via the dex cache so we can't do an implicit null check.
2475 // TODO: for intrinsics we can generate implicit null checks.
2476 return false;
2477 }
2478
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002479 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002480 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002481 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002482 bool IsStringInit() const { return string_init_offset_ != 0; }
2483 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002484
Roland Levillain4c0eb422015-04-24 16:43:49 +01002485 // Is this instruction a call to a static method?
2486 bool IsStatic() const {
2487 return GetInvokeType() == kStatic;
2488 }
2489
Roland Levillain3e3d7332015-04-28 11:00:54 +01002490 // Remove the art::HLoadClass instruction set as last input by
2491 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2492 // the initial art::HClinitCheck instruction (only relevant for
2493 // static calls with explicit clinit check).
2494 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002495 DCHECK(IsStaticWithExplicitClinitCheck());
2496 size_t last_input_index = InputCount() - 1;
2497 HInstruction* last_input = InputAt(last_input_index);
2498 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002499 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002500 RemoveAsUserOfInput(last_input_index);
2501 inputs_.DeleteAt(last_input_index);
2502 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2503 DCHECK(IsStaticWithImplicitClinitCheck());
2504 }
2505
2506 // Is this a call to a static method whose declaring class has an
2507 // explicit intialization check in the graph?
2508 bool IsStaticWithExplicitClinitCheck() const {
2509 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2510 }
2511
2512 // Is this a call to a static method whose declaring class has an
2513 // implicit intialization check requirement?
2514 bool IsStaticWithImplicitClinitCheck() const {
2515 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2516 }
2517
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002518 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002519
Roland Levillain4c0eb422015-04-24 16:43:49 +01002520 protected:
2521 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2522 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2523 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2524 HInstruction* input = input_record.GetInstruction();
2525 // `input` is the last input of a static invoke marked as having
2526 // an explicit clinit check. It must either be:
2527 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2528 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2529 DCHECK(input != nullptr);
2530 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2531 }
2532 return input_record;
2533 }
2534
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002535 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002536 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002537 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002538 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002539 // Thread entrypoint offset for string init method if this is a string init invoke.
2540 // Note that there are multiple string init methods, each having its own offset.
2541 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002542
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002543 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002544};
2545
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002546class HInvokeVirtual : public HInvoke {
2547 public:
2548 HInvokeVirtual(ArenaAllocator* arena,
2549 uint32_t number_of_arguments,
2550 Primitive::Type return_type,
2551 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002552 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002553 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002554 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002555 vtable_index_(vtable_index) {}
2556
Calin Juravle641547a2015-04-21 22:08:51 +01002557 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002558 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002559 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002560 }
2561
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002562 uint32_t GetVTableIndex() const { return vtable_index_; }
2563
2564 DECLARE_INSTRUCTION(InvokeVirtual);
2565
2566 private:
2567 const uint32_t vtable_index_;
2568
2569 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2570};
2571
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002572class HInvokeInterface : public HInvoke {
2573 public:
2574 HInvokeInterface(ArenaAllocator* arena,
2575 uint32_t number_of_arguments,
2576 Primitive::Type return_type,
2577 uint32_t dex_pc,
2578 uint32_t dex_method_index,
2579 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002580 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002581 imt_index_(imt_index) {}
2582
Calin Juravle641547a2015-04-21 22:08:51 +01002583 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002584 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002585 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002586 }
2587
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002588 uint32_t GetImtIndex() const { return imt_index_; }
2589 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2590
2591 DECLARE_INSTRUCTION(InvokeInterface);
2592
2593 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002594 const uint32_t imt_index_;
2595
2596 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2597};
2598
Dave Allison20dfc792014-06-16 20:44:29 -07002599class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002600 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002601 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002602 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2603 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002604 type_index_(type_index),
2605 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002606
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002607 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002608 uint16_t GetTypeIndex() const { return type_index_; }
2609
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002610 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002611 bool NeedsEnvironment() const OVERRIDE { return true; }
2612 // It may throw when called on:
2613 // - interfaces
2614 // - abstract/innaccessible/unknown classes
2615 // TODO: optimize when possible.
2616 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002617
Calin Juravle10e244f2015-01-26 18:54:32 +00002618 bool CanBeNull() const OVERRIDE { return false; }
2619
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002620 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2621
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002622 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002623
2624 private:
2625 const uint32_t dex_pc_;
2626 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002627 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002628
2629 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2630};
2631
Roland Levillain88cb1752014-10-20 16:36:47 +01002632class HNeg : public HUnaryOperation {
2633 public:
2634 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2635 : HUnaryOperation(result_type, input) {}
2636
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002637 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2638 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002639
Roland Levillain88cb1752014-10-20 16:36:47 +01002640 DECLARE_INSTRUCTION(Neg);
2641
2642 private:
2643 DISALLOW_COPY_AND_ASSIGN(HNeg);
2644};
2645
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002646class HNewArray : public HExpression<1> {
2647 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002648 HNewArray(HInstruction* length,
2649 uint32_t dex_pc,
2650 uint16_t type_index,
2651 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002652 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2653 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002654 type_index_(type_index),
2655 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002656 SetRawInputAt(0, length);
2657 }
2658
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002659 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002660 uint16_t GetTypeIndex() const { return type_index_; }
2661
2662 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002663 bool NeedsEnvironment() const OVERRIDE { return true; }
2664
Mingyao Yang0c365e62015-03-31 15:09:29 -07002665 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2666 bool CanThrow() const OVERRIDE { return true; }
2667
Calin Juravle10e244f2015-01-26 18:54:32 +00002668 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002669
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002670 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2671
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002672 DECLARE_INSTRUCTION(NewArray);
2673
2674 private:
2675 const uint32_t dex_pc_;
2676 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002677 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002678
2679 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2680};
2681
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002682class HAdd : public HBinaryOperation {
2683 public:
2684 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2685 : HBinaryOperation(result_type, left, right) {}
2686
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002687 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002688
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002689 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002690 return x + y;
2691 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002692 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002693 return x + y;
2694 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002695
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002696 DECLARE_INSTRUCTION(Add);
2697
2698 private:
2699 DISALLOW_COPY_AND_ASSIGN(HAdd);
2700};
2701
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002702class HSub : public HBinaryOperation {
2703 public:
2704 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2705 : HBinaryOperation(result_type, left, right) {}
2706
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002707 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002708 return x - y;
2709 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002710 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002711 return x - y;
2712 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002713
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002714 DECLARE_INSTRUCTION(Sub);
2715
2716 private:
2717 DISALLOW_COPY_AND_ASSIGN(HSub);
2718};
2719
Calin Juravle34bacdf2014-10-07 20:23:36 +01002720class HMul : public HBinaryOperation {
2721 public:
2722 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2723 : HBinaryOperation(result_type, left, right) {}
2724
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002725 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002726
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002727 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2728 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002729
2730 DECLARE_INSTRUCTION(Mul);
2731
2732 private:
2733 DISALLOW_COPY_AND_ASSIGN(HMul);
2734};
2735
Calin Juravle7c4954d2014-10-28 16:57:40 +00002736class HDiv : public HBinaryOperation {
2737 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002738 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2739 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002740
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002741 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002742 // Our graph structure ensures we never have 0 for `y` during constant folding.
2743 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002744 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002745 return (y == -1) ? -x : x / y;
2746 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002747
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002748 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002749 DCHECK_NE(y, 0);
2750 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2751 return (y == -1) ? -x : x / y;
2752 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002753
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002754 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002755
Calin Juravle7c4954d2014-10-28 16:57:40 +00002756 DECLARE_INSTRUCTION(Div);
2757
2758 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002759 const uint32_t dex_pc_;
2760
Calin Juravle7c4954d2014-10-28 16:57:40 +00002761 DISALLOW_COPY_AND_ASSIGN(HDiv);
2762};
2763
Calin Juravlebacfec32014-11-14 15:54:36 +00002764class HRem : public HBinaryOperation {
2765 public:
2766 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2767 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2768
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002769 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002770 DCHECK_NE(y, 0);
2771 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2772 return (y == -1) ? 0 : x % y;
2773 }
2774
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002775 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002776 DCHECK_NE(y, 0);
2777 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2778 return (y == -1) ? 0 : x % y;
2779 }
2780
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002781 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002782
2783 DECLARE_INSTRUCTION(Rem);
2784
2785 private:
2786 const uint32_t dex_pc_;
2787
2788 DISALLOW_COPY_AND_ASSIGN(HRem);
2789};
2790
Calin Juravled0d48522014-11-04 16:40:20 +00002791class HDivZeroCheck : public HExpression<1> {
2792 public:
2793 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2794 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2795 SetRawInputAt(0, value);
2796 }
2797
2798 bool CanBeMoved() const OVERRIDE { return true; }
2799
2800 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2801 UNUSED(other);
2802 return true;
2803 }
2804
2805 bool NeedsEnvironment() const OVERRIDE { return true; }
2806 bool CanThrow() const OVERRIDE { return true; }
2807
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002808 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002809
2810 DECLARE_INSTRUCTION(DivZeroCheck);
2811
2812 private:
2813 const uint32_t dex_pc_;
2814
2815 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2816};
2817
Calin Juravle9aec02f2014-11-18 23:06:35 +00002818class HShl : public HBinaryOperation {
2819 public:
2820 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2821 : HBinaryOperation(result_type, left, right) {}
2822
2823 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2824 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2825
2826 DECLARE_INSTRUCTION(Shl);
2827
2828 private:
2829 DISALLOW_COPY_AND_ASSIGN(HShl);
2830};
2831
2832class HShr : public HBinaryOperation {
2833 public:
2834 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2835 : HBinaryOperation(result_type, left, right) {}
2836
2837 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2838 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2839
2840 DECLARE_INSTRUCTION(Shr);
2841
2842 private:
2843 DISALLOW_COPY_AND_ASSIGN(HShr);
2844};
2845
2846class HUShr : public HBinaryOperation {
2847 public:
2848 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2849 : HBinaryOperation(result_type, left, right) {}
2850
2851 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2852 uint32_t ux = static_cast<uint32_t>(x);
2853 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2854 return static_cast<int32_t>(ux >> uy);
2855 }
2856
2857 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2858 uint64_t ux = static_cast<uint64_t>(x);
2859 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2860 return static_cast<int64_t>(ux >> uy);
2861 }
2862
2863 DECLARE_INSTRUCTION(UShr);
2864
2865 private:
2866 DISALLOW_COPY_AND_ASSIGN(HUShr);
2867};
2868
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002869class HAnd : public HBinaryOperation {
2870 public:
2871 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2872 : HBinaryOperation(result_type, left, right) {}
2873
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002874 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002875
2876 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2877 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2878
2879 DECLARE_INSTRUCTION(And);
2880
2881 private:
2882 DISALLOW_COPY_AND_ASSIGN(HAnd);
2883};
2884
2885class HOr : public HBinaryOperation {
2886 public:
2887 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2888 : HBinaryOperation(result_type, left, right) {}
2889
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002890 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002891
2892 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2893 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2894
2895 DECLARE_INSTRUCTION(Or);
2896
2897 private:
2898 DISALLOW_COPY_AND_ASSIGN(HOr);
2899};
2900
2901class HXor : public HBinaryOperation {
2902 public:
2903 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2904 : HBinaryOperation(result_type, left, right) {}
2905
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002906 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002907
2908 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2909 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2910
2911 DECLARE_INSTRUCTION(Xor);
2912
2913 private:
2914 DISALLOW_COPY_AND_ASSIGN(HXor);
2915};
2916
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002917// The value of a parameter in this method. Its location depends on
2918// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002919class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002920 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002921 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2922 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002923
2924 uint8_t GetIndex() const { return index_; }
2925
Calin Juravle10e244f2015-01-26 18:54:32 +00002926 bool CanBeNull() const OVERRIDE { return !is_this_; }
2927
Calin Juravle3cd4fc82015-05-14 15:15:42 +01002928 bool IsThis() const { return is_this_; }
2929
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002930 DECLARE_INSTRUCTION(ParameterValue);
2931
2932 private:
2933 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002934 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002935 const uint8_t index_;
2936
Calin Juravle10e244f2015-01-26 18:54:32 +00002937 // Whether or not the parameter value corresponds to 'this' argument.
2938 const bool is_this_;
2939
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002940 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2941};
2942
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002943class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002944 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002945 explicit HNot(Primitive::Type result_type, HInstruction* input)
2946 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002947
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002948 bool CanBeMoved() const OVERRIDE { return true; }
2949 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002950 UNUSED(other);
2951 return true;
2952 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002953
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002954 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2955 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002956
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002957 DECLARE_INSTRUCTION(Not);
2958
2959 private:
2960 DISALLOW_COPY_AND_ASSIGN(HNot);
2961};
2962
David Brazdil66d126e2015-04-03 16:02:44 +01002963class HBooleanNot : public HUnaryOperation {
2964 public:
2965 explicit HBooleanNot(HInstruction* input)
2966 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2967
2968 bool CanBeMoved() const OVERRIDE { return true; }
2969 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2970 UNUSED(other);
2971 return true;
2972 }
2973
2974 int32_t Evaluate(int32_t x) const OVERRIDE {
2975 DCHECK(IsUint<1>(x));
2976 return !x;
2977 }
2978
2979 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2980 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2981 UNREACHABLE();
2982 }
2983
2984 DECLARE_INSTRUCTION(BooleanNot);
2985
2986 private:
2987 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2988};
2989
Roland Levillaindff1f282014-11-05 14:15:05 +00002990class HTypeConversion : public HExpression<1> {
2991 public:
2992 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002993 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2994 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002995 SetRawInputAt(0, input);
2996 DCHECK_NE(input->GetType(), result_type);
2997 }
2998
2999 HInstruction* GetInput() const { return InputAt(0); }
3000 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3001 Primitive::Type GetResultType() const { return GetType(); }
3002
Roland Levillain624279f2014-12-04 11:54:28 +00003003 // Required by the x86 and ARM code generators when producing calls
3004 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003005 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003006
Roland Levillaindff1f282014-11-05 14:15:05 +00003007 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003008 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003009
Mark Mendelle82549b2015-05-06 10:55:34 -04003010 // Try to statically evaluate the conversion and return a HConstant
3011 // containing the result. If the input cannot be converted, return nullptr.
3012 HConstant* TryStaticEvaluation() const;
3013
Roland Levillaindff1f282014-11-05 14:15:05 +00003014 DECLARE_INSTRUCTION(TypeConversion);
3015
3016 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003017 const uint32_t dex_pc_;
3018
Roland Levillaindff1f282014-11-05 14:15:05 +00003019 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3020};
3021
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003022static constexpr uint32_t kNoRegNumber = -1;
3023
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003024class HPhi : public HInstruction {
3025 public:
3026 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003027 : HInstruction(SideEffects::None()),
3028 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003029 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003030 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003031 is_live_(false),
3032 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003033 inputs_.SetSize(number_of_inputs);
3034 }
3035
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003036 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3037 static Primitive::Type ToPhiType(Primitive::Type type) {
3038 switch (type) {
3039 case Primitive::kPrimBoolean:
3040 case Primitive::kPrimByte:
3041 case Primitive::kPrimShort:
3042 case Primitive::kPrimChar:
3043 return Primitive::kPrimInt;
3044 default:
3045 return type;
3046 }
3047 }
3048
Calin Juravle10e244f2015-01-26 18:54:32 +00003049 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003050
3051 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003052 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003053
Calin Juravle10e244f2015-01-26 18:54:32 +00003054 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003055 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003056
Calin Juravle10e244f2015-01-26 18:54:32 +00003057 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3058 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3059
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003060 uint32_t GetRegNumber() const { return reg_number_; }
3061
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003062 void SetDead() { is_live_ = false; }
3063 void SetLive() { is_live_ = true; }
3064 bool IsDead() const { return !is_live_; }
3065 bool IsLive() const { return is_live_; }
3066
Calin Juravlea4f88312015-04-16 12:57:19 +01003067 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3068 // An equivalent phi is a phi having the same dex register and type.
3069 // It assumes that phis with the same dex register are adjacent.
3070 HPhi* GetNextEquivalentPhiWithSameType() {
3071 HInstruction* next = GetNext();
3072 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3073 if (next->GetType() == GetType()) {
3074 return next->AsPhi();
3075 }
3076 next = next->GetNext();
3077 }
3078 return nullptr;
3079 }
3080
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003081 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003082
David Brazdil1abb4192015-02-17 18:33:36 +00003083 protected:
3084 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3085
3086 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3087 inputs_.Put(index, input);
3088 }
3089
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003090 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003091 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003092 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003093 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003094 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003095 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003096
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003097 DISALLOW_COPY_AND_ASSIGN(HPhi);
3098};
3099
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003100class HNullCheck : public HExpression<1> {
3101 public:
3102 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003103 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003104 SetRawInputAt(0, value);
3105 }
3106
Calin Juravle10e244f2015-01-26 18:54:32 +00003107 bool CanBeMoved() const OVERRIDE { return true; }
3108 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003109 UNUSED(other);
3110 return true;
3111 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003112
Calin Juravle10e244f2015-01-26 18:54:32 +00003113 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003114
Calin Juravle10e244f2015-01-26 18:54:32 +00003115 bool CanThrow() const OVERRIDE { return true; }
3116
3117 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003118
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003119 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003120
3121 DECLARE_INSTRUCTION(NullCheck);
3122
3123 private:
3124 const uint32_t dex_pc_;
3125
3126 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3127};
3128
3129class FieldInfo : public ValueObject {
3130 public:
Calin Juravle52c48962014-12-16 17:02:57 +00003131 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
3132 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003133
3134 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003135 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003136 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003137
3138 private:
3139 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003140 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003141 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003142};
3143
3144class HInstanceFieldGet : public HExpression<1> {
3145 public:
3146 HInstanceFieldGet(HInstruction* value,
3147 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003148 MemberOffset field_offset,
3149 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003150 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003151 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003152 SetRawInputAt(0, value);
3153 }
3154
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003155 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003156
3157 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3158 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3159 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003160 }
3161
Calin Juravle641547a2015-04-21 22:08:51 +01003162 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3163 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003164 }
3165
3166 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003167 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3168 }
3169
Calin Juravle52c48962014-12-16 17:02:57 +00003170 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003171 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003172 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003173 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003174
3175 DECLARE_INSTRUCTION(InstanceFieldGet);
3176
3177 private:
3178 const FieldInfo field_info_;
3179
3180 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3181};
3182
3183class HInstanceFieldSet : public HTemplateInstruction<2> {
3184 public:
3185 HInstanceFieldSet(HInstruction* object,
3186 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003187 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003188 MemberOffset field_offset,
3189 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003190 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003191 field_info_(field_offset, field_type, is_volatile),
3192 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003193 SetRawInputAt(0, object);
3194 SetRawInputAt(1, value);
3195 }
3196
Calin Juravle641547a2015-04-21 22:08:51 +01003197 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3198 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003199 }
3200
Calin Juravle52c48962014-12-16 17:02:57 +00003201 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003202 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003203 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003204 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003205 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003206 bool GetValueCanBeNull() const { return value_can_be_null_; }
3207 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003208
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003209 DECLARE_INSTRUCTION(InstanceFieldSet);
3210
3211 private:
3212 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003213 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003214
3215 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3216};
3217
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003218class HArrayGet : public HExpression<2> {
3219 public:
3220 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003221 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003222 SetRawInputAt(0, array);
3223 SetRawInputAt(1, index);
3224 }
3225
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003226 bool CanBeMoved() const OVERRIDE { return true; }
3227 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003228 UNUSED(other);
3229 return true;
3230 }
Calin Juravle641547a2015-04-21 22:08:51 +01003231 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3232 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003233 // TODO: We can be smarter here.
3234 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3235 // which generates the implicit null check. There are cases when these can be removed
3236 // to produce better code. If we ever add optimizations to do so we should allow an
3237 // implicit check here (as long as the address falls in the first page).
3238 return false;
3239 }
3240
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003241 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003242
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003243 HInstruction* GetArray() const { return InputAt(0); }
3244 HInstruction* GetIndex() const { return InputAt(1); }
3245
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003246 DECLARE_INSTRUCTION(ArrayGet);
3247
3248 private:
3249 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3250};
3251
3252class HArraySet : public HTemplateInstruction<3> {
3253 public:
3254 HArraySet(HInstruction* array,
3255 HInstruction* index,
3256 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003257 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003258 uint32_t dex_pc)
3259 : HTemplateInstruction(SideEffects::ChangesSomething()),
3260 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003261 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003262 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3263 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003264 SetRawInputAt(0, array);
3265 SetRawInputAt(1, index);
3266 SetRawInputAt(2, value);
3267 }
3268
Calin Juravle77520bc2015-01-12 18:45:46 +00003269 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003270 // We currently always call a runtime method to catch array store
3271 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003272 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003273 }
3274
Calin Juravle641547a2015-04-21 22:08:51 +01003275 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3276 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003277 // TODO: Same as for ArrayGet.
3278 return false;
3279 }
3280
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003281 void ClearNeedsTypeCheck() {
3282 needs_type_check_ = false;
3283 }
3284
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003285 void ClearValueCanBeNull() {
3286 value_can_be_null_ = false;
3287 }
3288
3289 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003290 bool NeedsTypeCheck() const { return needs_type_check_; }
3291
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003292 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003293
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003294 HInstruction* GetArray() const { return InputAt(0); }
3295 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003296 HInstruction* GetValue() const { return InputAt(2); }
3297
3298 Primitive::Type GetComponentType() const {
3299 // The Dex format does not type floating point index operations. Since the
3300 // `expected_component_type_` is set during building and can therefore not
3301 // be correct, we also check what is the value type. If it is a floating
3302 // point type, we must use that type.
3303 Primitive::Type value_type = GetValue()->GetType();
3304 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3305 ? value_type
3306 : expected_component_type_;
3307 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003308
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003309 DECLARE_INSTRUCTION(ArraySet);
3310
3311 private:
3312 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003313 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003314 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003315 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003316
3317 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3318};
3319
3320class HArrayLength : public HExpression<1> {
3321 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003322 explicit HArrayLength(HInstruction* array)
3323 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3324 // Note that arrays do not change length, so the instruction does not
3325 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003326 SetRawInputAt(0, array);
3327 }
3328
Calin Juravle77520bc2015-01-12 18:45:46 +00003329 bool CanBeMoved() const OVERRIDE { return true; }
3330 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003331 UNUSED(other);
3332 return true;
3333 }
Calin Juravle641547a2015-04-21 22:08:51 +01003334 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3335 return obj == InputAt(0);
3336 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003337
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003338 DECLARE_INSTRUCTION(ArrayLength);
3339
3340 private:
3341 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3342};
3343
3344class HBoundsCheck : public HExpression<2> {
3345 public:
3346 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003347 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003348 DCHECK(index->GetType() == Primitive::kPrimInt);
3349 SetRawInputAt(0, index);
3350 SetRawInputAt(1, length);
3351 }
3352
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003353 bool CanBeMoved() const OVERRIDE { return true; }
3354 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003355 UNUSED(other);
3356 return true;
3357 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003358
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003359 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003360
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003361 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003362
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003363 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003364
3365 DECLARE_INSTRUCTION(BoundsCheck);
3366
3367 private:
3368 const uint32_t dex_pc_;
3369
3370 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3371};
3372
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003373/**
3374 * Some DEX instructions are folded into multiple HInstructions that need
3375 * to stay live until the last HInstruction. This class
3376 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003377 * HInstruction stays live. `index` represents the stack location index of the
3378 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003379 */
3380class HTemporary : public HTemplateInstruction<0> {
3381 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003382 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003383
3384 size_t GetIndex() const { return index_; }
3385
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003386 Primitive::Type GetType() const OVERRIDE {
3387 // The previous instruction is the one that will be stored in the temporary location.
3388 DCHECK(GetPrevious() != nullptr);
3389 return GetPrevious()->GetType();
3390 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003391
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003392 DECLARE_INSTRUCTION(Temporary);
3393
3394 private:
3395 const size_t index_;
3396
3397 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3398};
3399
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003400class HSuspendCheck : public HTemplateInstruction<0> {
3401 public:
3402 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003403 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003404
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003405 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003406 return true;
3407 }
3408
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003409 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003410 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3411 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003412
3413 DECLARE_INSTRUCTION(SuspendCheck);
3414
3415 private:
3416 const uint32_t dex_pc_;
3417
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003418 // Only used for code generation, in order to share the same slow path between back edges
3419 // of a same loop.
3420 SlowPathCode* slow_path_;
3421
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003422 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3423};
3424
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003425/**
3426 * Instruction to load a Class object.
3427 */
3428class HLoadClass : public HExpression<0> {
3429 public:
3430 HLoadClass(uint16_t type_index,
3431 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003432 uint32_t dex_pc)
3433 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3434 type_index_(type_index),
3435 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003436 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003437 generate_clinit_check_(false),
3438 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003439
3440 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003441
3442 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3443 return other->AsLoadClass()->type_index_ == type_index_;
3444 }
3445
3446 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3447
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003448 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003449 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003450 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003451
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003452 bool NeedsEnvironment() const OVERRIDE {
3453 // Will call runtime and load the class if the class is not loaded yet.
3454 // TODO: finer grain decision.
3455 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003456 }
3457
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003458 bool MustGenerateClinitCheck() const {
3459 return generate_clinit_check_;
3460 }
3461
Calin Juravle0ba218d2015-05-19 18:46:01 +01003462 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3463 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003464 }
3465
3466 bool CanCallRuntime() const {
3467 return MustGenerateClinitCheck() || !is_referrers_class_;
3468 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003469
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003470 bool CanThrow() const OVERRIDE {
3471 // May call runtime and and therefore can throw.
3472 // TODO: finer grain decision.
3473 return !is_referrers_class_;
3474 }
3475
Calin Juravleacf735c2015-02-12 15:25:22 +00003476 ReferenceTypeInfo GetLoadedClassRTI() {
3477 return loaded_class_rti_;
3478 }
3479
3480 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3481 // Make sure we only set exact types (the loaded class should never be merged).
3482 DCHECK(rti.IsExact());
3483 loaded_class_rti_ = rti;
3484 }
3485
3486 bool IsResolved() {
3487 return loaded_class_rti_.IsExact();
3488 }
3489
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003490 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3491
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003492 DECLARE_INSTRUCTION(LoadClass);
3493
3494 private:
3495 const uint16_t type_index_;
3496 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003497 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003498 // Whether this instruction must generate the initialization check.
3499 // Used for code generation.
3500 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003501
Calin Juravleacf735c2015-02-12 15:25:22 +00003502 ReferenceTypeInfo loaded_class_rti_;
3503
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003504 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3505};
3506
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003507class HLoadString : public HExpression<0> {
3508 public:
3509 HLoadString(uint32_t string_index, uint32_t dex_pc)
3510 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3511 string_index_(string_index),
3512 dex_pc_(dex_pc) {}
3513
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003514 bool CanBeMoved() const OVERRIDE { return true; }
3515
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003516 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3517 return other->AsLoadString()->string_index_ == string_index_;
3518 }
3519
3520 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3521
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003522 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003523 uint32_t GetStringIndex() const { return string_index_; }
3524
3525 // TODO: Can we deopt or debug when we resolve a string?
3526 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003527 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003528
3529 DECLARE_INSTRUCTION(LoadString);
3530
3531 private:
3532 const uint32_t string_index_;
3533 const uint32_t dex_pc_;
3534
3535 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3536};
3537
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003538/**
3539 * Performs an initialization check on its Class object input.
3540 */
3541class HClinitCheck : public HExpression<1> {
3542 public:
3543 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003544 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003545 dex_pc_(dex_pc) {
3546 SetRawInputAt(0, constant);
3547 }
3548
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003549 bool CanBeMoved() const OVERRIDE { return true; }
3550 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3551 UNUSED(other);
3552 return true;
3553 }
3554
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003555 bool NeedsEnvironment() const OVERRIDE {
3556 // May call runtime to initialize the class.
3557 return true;
3558 }
3559
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003560 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003561
3562 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3563
3564 DECLARE_INSTRUCTION(ClinitCheck);
3565
3566 private:
3567 const uint32_t dex_pc_;
3568
3569 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3570};
3571
3572class HStaticFieldGet : public HExpression<1> {
3573 public:
3574 HStaticFieldGet(HInstruction* cls,
3575 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003576 MemberOffset field_offset,
3577 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003578 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003579 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003580 SetRawInputAt(0, cls);
3581 }
3582
Calin Juravle52c48962014-12-16 17:02:57 +00003583
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003584 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003585
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003586 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003587 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3588 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003589 }
3590
3591 size_t ComputeHashCode() const OVERRIDE {
3592 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3593 }
3594
Calin Juravle52c48962014-12-16 17:02:57 +00003595 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003596 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3597 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003598 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003599
3600 DECLARE_INSTRUCTION(StaticFieldGet);
3601
3602 private:
3603 const FieldInfo field_info_;
3604
3605 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3606};
3607
3608class HStaticFieldSet : public HTemplateInstruction<2> {
3609 public:
3610 HStaticFieldSet(HInstruction* cls,
3611 HInstruction* value,
3612 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003613 MemberOffset field_offset,
3614 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003615 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003616 field_info_(field_offset, field_type, is_volatile),
3617 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003618 SetRawInputAt(0, cls);
3619 SetRawInputAt(1, value);
3620 }
3621
Calin Juravle52c48962014-12-16 17:02:57 +00003622 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003623 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3624 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003625 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003626
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003627 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003628 bool GetValueCanBeNull() const { return value_can_be_null_; }
3629 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003630
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003631 DECLARE_INSTRUCTION(StaticFieldSet);
3632
3633 private:
3634 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003635 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003636
3637 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3638};
3639
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003640// Implement the move-exception DEX instruction.
3641class HLoadException : public HExpression<0> {
3642 public:
3643 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3644
3645 DECLARE_INSTRUCTION(LoadException);
3646
3647 private:
3648 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3649};
3650
3651class HThrow : public HTemplateInstruction<1> {
3652 public:
3653 HThrow(HInstruction* exception, uint32_t dex_pc)
3654 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3655 SetRawInputAt(0, exception);
3656 }
3657
3658 bool IsControlFlow() const OVERRIDE { return true; }
3659
3660 bool NeedsEnvironment() const OVERRIDE { return true; }
3661
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003662 bool CanThrow() const OVERRIDE { return true; }
3663
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003664 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003665
3666 DECLARE_INSTRUCTION(Throw);
3667
3668 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003669 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003670
3671 DISALLOW_COPY_AND_ASSIGN(HThrow);
3672};
3673
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003674class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003675 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003676 HInstanceOf(HInstruction* object,
3677 HLoadClass* constant,
3678 bool class_is_final,
3679 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003680 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3681 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003682 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003683 dex_pc_(dex_pc) {
3684 SetRawInputAt(0, object);
3685 SetRawInputAt(1, constant);
3686 }
3687
3688 bool CanBeMoved() const OVERRIDE { return true; }
3689
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003690 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003691 return true;
3692 }
3693
3694 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003695 return false;
3696 }
3697
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003698 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003699
3700 bool IsClassFinal() const { return class_is_final_; }
3701
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003702 // Used only in code generation.
3703 bool MustDoNullCheck() const { return must_do_null_check_; }
3704 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3705
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003706 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003707
3708 private:
3709 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003710 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003711 const uint32_t dex_pc_;
3712
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003713 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3714};
3715
Calin Juravleb1498f62015-02-16 13:13:29 +00003716class HBoundType : public HExpression<1> {
3717 public:
3718 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3719 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3720 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003721 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003722 SetRawInputAt(0, input);
3723 }
3724
3725 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3726
3727 bool CanBeNull() const OVERRIDE {
3728 // `null instanceof ClassX` always return false so we can't be null.
3729 return false;
3730 }
3731
3732 DECLARE_INSTRUCTION(BoundType);
3733
3734 private:
3735 // Encodes the most upper class that this instruction can have. In other words
3736 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3737 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3738 const ReferenceTypeInfo bound_type_;
3739
3740 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3741};
3742
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003743class HCheckCast : public HTemplateInstruction<2> {
3744 public:
3745 HCheckCast(HInstruction* object,
3746 HLoadClass* constant,
3747 bool class_is_final,
3748 uint32_t dex_pc)
3749 : HTemplateInstruction(SideEffects::None()),
3750 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003751 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003752 dex_pc_(dex_pc) {
3753 SetRawInputAt(0, object);
3754 SetRawInputAt(1, constant);
3755 }
3756
3757 bool CanBeMoved() const OVERRIDE { return true; }
3758
3759 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3760 return true;
3761 }
3762
3763 bool NeedsEnvironment() const OVERRIDE {
3764 // Instruction may throw a CheckCastError.
3765 return true;
3766 }
3767
3768 bool CanThrow() const OVERRIDE { return true; }
3769
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003770 bool MustDoNullCheck() const { return must_do_null_check_; }
3771 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3772
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003773 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003774
3775 bool IsClassFinal() const { return class_is_final_; }
3776
3777 DECLARE_INSTRUCTION(CheckCast);
3778
3779 private:
3780 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003781 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003782 const uint32_t dex_pc_;
3783
3784 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003785};
3786
Calin Juravle27df7582015-04-17 19:12:31 +01003787class HMemoryBarrier : public HTemplateInstruction<0> {
3788 public:
3789 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3790 : HTemplateInstruction(SideEffects::None()),
3791 barrier_kind_(barrier_kind) {}
3792
3793 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3794
3795 DECLARE_INSTRUCTION(MemoryBarrier);
3796
3797 private:
3798 const MemBarrierKind barrier_kind_;
3799
3800 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3801};
3802
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003803class HMonitorOperation : public HTemplateInstruction<1> {
3804 public:
3805 enum OperationKind {
3806 kEnter,
3807 kExit,
3808 };
3809
3810 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3811 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3812 SetRawInputAt(0, object);
3813 }
3814
3815 // Instruction may throw a Java exception, so we need an environment.
3816 bool NeedsEnvironment() const OVERRIDE { return true; }
3817 bool CanThrow() const OVERRIDE { return true; }
3818
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003819 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003820
3821 bool IsEnter() const { return kind_ == kEnter; }
3822
3823 DECLARE_INSTRUCTION(MonitorOperation);
3824
Calin Juravle52c48962014-12-16 17:02:57 +00003825 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003826 const OperationKind kind_;
3827 const uint32_t dex_pc_;
3828
3829 private:
3830 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3831};
3832
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003833class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003834 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003835 MoveOperands(Location source,
3836 Location destination,
3837 Primitive::Type type,
3838 HInstruction* instruction)
3839 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003840
3841 Location GetSource() const { return source_; }
3842 Location GetDestination() const { return destination_; }
3843
3844 void SetSource(Location value) { source_ = value; }
3845 void SetDestination(Location value) { destination_ = value; }
3846
3847 // The parallel move resolver marks moves as "in-progress" by clearing the
3848 // destination (but not the source).
3849 Location MarkPending() {
3850 DCHECK(!IsPending());
3851 Location dest = destination_;
3852 destination_ = Location::NoLocation();
3853 return dest;
3854 }
3855
3856 void ClearPending(Location dest) {
3857 DCHECK(IsPending());
3858 destination_ = dest;
3859 }
3860
3861 bool IsPending() const {
3862 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3863 return destination_.IsInvalid() && !source_.IsInvalid();
3864 }
3865
3866 // True if this blocks a move from the given location.
3867 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003868 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003869 }
3870
3871 // A move is redundant if it's been eliminated, if its source and
3872 // destination are the same, or if its destination is unneeded.
3873 bool IsRedundant() const {
3874 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3875 }
3876
3877 // We clear both operands to indicate move that's been eliminated.
3878 void Eliminate() {
3879 source_ = destination_ = Location::NoLocation();
3880 }
3881
3882 bool IsEliminated() const {
3883 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3884 return source_.IsInvalid();
3885 }
3886
Nicolas Geoffray90218252015-04-15 11:56:51 +01003887 bool Is64BitMove() const {
3888 return Primitive::Is64BitType(type_);
3889 }
3890
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003891 HInstruction* GetInstruction() const { return instruction_; }
3892
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003893 private:
3894 Location source_;
3895 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003896 // The type this move is for.
3897 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003898 // The instruction this move is assocatied with. Null when this move is
3899 // for moving an input in the expected locations of user (including a phi user).
3900 // This is only used in debug mode, to ensure we do not connect interval siblings
3901 // in the same parallel move.
3902 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003903};
3904
3905static constexpr size_t kDefaultNumberOfMoves = 4;
3906
3907class HParallelMove : public HTemplateInstruction<0> {
3908 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003909 explicit HParallelMove(ArenaAllocator* arena)
3910 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003911
Nicolas Geoffray90218252015-04-15 11:56:51 +01003912 void AddMove(Location source,
3913 Location destination,
3914 Primitive::Type type,
3915 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003916 DCHECK(source.IsValid());
3917 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003918 if (kIsDebugBuild) {
3919 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003920 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003921 if (moves_.Get(i).GetInstruction() == instruction) {
3922 // Special case the situation where the move is for the spill slot
3923 // of the instruction.
3924 if ((GetPrevious() == instruction)
3925 || ((GetPrevious() == nullptr)
3926 && instruction->IsPhi()
3927 && instruction->GetBlock() == GetBlock())) {
3928 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3929 << "Doing parallel moves for the same instruction.";
3930 } else {
3931 DCHECK(false) << "Doing parallel moves for the same instruction.";
3932 }
3933 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003934 }
3935 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003936 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003937 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01003938 << "Overlapped destination for two moves in a parallel move: "
3939 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
3940 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003941 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003942 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003943 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003944 }
3945
3946 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003947 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003948 }
3949
3950 size_t NumMoves() const { return moves_.Size(); }
3951
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003952 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003953
3954 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003955 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003956
3957 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3958};
3959
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003960class HGraphVisitor : public ValueObject {
3961 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003962 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3963 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003964
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003965 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003966 virtual void VisitBasicBlock(HBasicBlock* block);
3967
Roland Levillain633021e2014-10-01 14:12:25 +01003968 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003969 void VisitInsertionOrder();
3970
Roland Levillain633021e2014-10-01 14:12:25 +01003971 // Visit the graph following dominator tree reverse post-order.
3972 void VisitReversePostOrder();
3973
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003974 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003975
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003976 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003977#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003978 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3979
3980 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3981
3982#undef DECLARE_VISIT_INSTRUCTION
3983
3984 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003985 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003986
3987 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3988};
3989
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003990class HGraphDelegateVisitor : public HGraphVisitor {
3991 public:
3992 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3993 virtual ~HGraphDelegateVisitor() {}
3994
3995 // Visit functions that delegate to to super class.
3996#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003997 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003998
3999 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4000
4001#undef DECLARE_VISIT_INSTRUCTION
4002
4003 private:
4004 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4005};
4006
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004007class HInsertionOrderIterator : public ValueObject {
4008 public:
4009 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4010
4011 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4012 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4013 void Advance() { ++index_; }
4014
4015 private:
4016 const HGraph& graph_;
4017 size_t index_;
4018
4019 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4020};
4021
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004022class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004023 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004024 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4025 // Check that reverse post order of the graph has been built.
4026 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4027 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004028
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004029 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4030 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004031 void Advance() { ++index_; }
4032
4033 private:
4034 const HGraph& graph_;
4035 size_t index_;
4036
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004037 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004038};
4039
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004040class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004041 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004042 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004043 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4044 // Check that reverse post order of the graph has been built.
4045 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4046 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004047
4048 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004049 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004050 void Advance() { --index_; }
4051
4052 private:
4053 const HGraph& graph_;
4054 size_t index_;
4055
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004056 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004057};
4058
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004059class HLinearPostOrderIterator : public ValueObject {
4060 public:
4061 explicit HLinearPostOrderIterator(const HGraph& graph)
4062 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4063
4064 bool Done() const { return index_ == 0; }
4065
4066 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4067
4068 void Advance() {
4069 --index_;
4070 DCHECK_GE(index_, 0U);
4071 }
4072
4073 private:
4074 const GrowableArray<HBasicBlock*>& order_;
4075 size_t index_;
4076
4077 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4078};
4079
4080class HLinearOrderIterator : public ValueObject {
4081 public:
4082 explicit HLinearOrderIterator(const HGraph& graph)
4083 : order_(graph.GetLinearOrder()), index_(0) {}
4084
4085 bool Done() const { return index_ == order_.Size(); }
4086 HBasicBlock* Current() const { return order_.Get(index_); }
4087 void Advance() { ++index_; }
4088
4089 private:
4090 const GrowableArray<HBasicBlock*>& order_;
4091 size_t index_;
4092
4093 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4094};
4095
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004096// Iterator over the blocks that art part of the loop. Includes blocks part
4097// of an inner loop. The order in which the blocks are iterated is on their
4098// block id.
4099class HBlocksInLoopIterator : public ValueObject {
4100 public:
4101 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4102 : blocks_in_loop_(info.GetBlocks()),
4103 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4104 index_(0) {
4105 if (!blocks_in_loop_.IsBitSet(index_)) {
4106 Advance();
4107 }
4108 }
4109
4110 bool Done() const { return index_ == blocks_.Size(); }
4111 HBasicBlock* Current() const { return blocks_.Get(index_); }
4112 void Advance() {
4113 ++index_;
4114 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4115 if (blocks_in_loop_.IsBitSet(index_)) {
4116 break;
4117 }
4118 }
4119 }
4120
4121 private:
4122 const BitVector& blocks_in_loop_;
4123 const GrowableArray<HBasicBlock*>& blocks_;
4124 size_t index_;
4125
4126 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4127};
4128
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004129inline int64_t Int64FromConstant(HConstant* constant) {
4130 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4131 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4132 : constant->AsLongConstant()->GetValue();
4133}
4134
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004135} // namespace art
4136
4137#endif // ART_COMPILER_OPTIMIZING_NODES_H_