blob: 0f5b1abbbf2bd84d1b651d7b7d26505f30080649 [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;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010038class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000039class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010040class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000041class HFloatConstant;
42class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000044class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000045class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000046class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000047class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010048class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010049class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010050class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000051class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010052class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000054
55static const int kDefaultNumberOfBlocks = 8;
56static const int kDefaultNumberOfSuccessors = 2;
57static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010058static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000059static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000060
Calin Juravle9aec02f2014-11-18 23:06:35 +000061static constexpr uint32_t kMaxIntShiftValue = 0x1f;
62static constexpr uint64_t kMaxLongShiftValue = 0x3f;
63
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010064static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
65
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010066static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
67
Dave Allison20dfc792014-06-16 20:44:29 -070068enum IfCondition {
69 kCondEQ,
70 kCondNE,
71 kCondLT,
72 kCondLE,
73 kCondGT,
74 kCondGE,
75};
76
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010077class HInstructionList {
78 public:
79 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
80
81 void AddInstruction(HInstruction* instruction);
82 void RemoveInstruction(HInstruction* instruction);
83
David Brazdilc3d743f2015-04-22 13:40:50 +010084 // Insert `instruction` before/after an existing instruction `cursor`.
85 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
86 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
87
Roland Levillain6b469232014-09-25 10:10:38 +010088 // Return true if this list contains `instruction`.
89 bool Contains(HInstruction* instruction) const;
90
Roland Levillainccc07a92014-09-16 14:48:16 +010091 // Return true if `instruction1` is found before `instruction2` in
92 // this instruction list and false otherwise. Abort if none
93 // of these instructions is found.
94 bool FoundBefore(const HInstruction* instruction1,
95 const HInstruction* instruction2) const;
96
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000097 bool IsEmpty() const { return first_instruction_ == nullptr; }
98 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
99
100 // Update the block of all instructions to be `block`.
101 void SetBlockOfInstructions(HBasicBlock* block) const;
102
103 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
104 void Add(const HInstructionList& instruction_list);
105
David Brazdil2d7352b2015-04-20 14:52:42 +0100106 // Return the number of instructions in the list. This is an expensive operation.
107 size_t CountSize() const;
108
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109 private:
110 HInstruction* first_instruction_;
111 HInstruction* last_instruction_;
112
113 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000114 friend class HGraph;
115 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100116 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100117 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100118
119 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
120};
121
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000122// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700123class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000124 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100125 HGraph(ArenaAllocator* arena,
126 const DexFile& dex_file,
127 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100128 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700129 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100130 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100131 bool debuggable = false,
132 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000133 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000134 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100135 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100136 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700137 entry_block_(nullptr),
138 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100139 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140 number_of_vregs_(0),
141 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000142 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400143 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000144 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000145 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100146 dex_file_(dex_file),
147 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100148 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100149 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100150 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700151 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000152 cached_null_constant_(nullptr),
153 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000154 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
155 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100156 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
157 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000158
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000159 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100160 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100161 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000162
David Brazdil69ba7b72015-06-23 18:27:30 +0100163 bool IsInSsaForm() const { return in_ssa_form_; }
164
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000165 HBasicBlock* GetEntryBlock() const { return entry_block_; }
166 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100167 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000168
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000169 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
170 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000171
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000172 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100173
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000174 // Try building the SSA form of this graph, with dominance computation and loop
175 // recognition. Returns whether it was successful in doing all these steps.
176 bool TryBuildingSsa() {
177 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000178 // The SSA builder requires loops to all be natural. Specifically, the dead phi
179 // elimination phase checks the consistency of the graph when doing a post-order
180 // visit for eliminating dead phis: a dead phi can only have loop header phi
181 // users remaining when being visited.
182 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000183 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100184 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000185 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000186 }
187
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100188 void ComputeDominanceInformation();
189 void ClearDominanceInformation();
190
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000191 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000192 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100193 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000194
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000195 // Analyze all natural loops in this graph. Returns false if one
196 // loop is not natural, that is the header does not dominate the
197 // back edge.
198 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100199
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000200 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
201 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
202
Mingyao Yang3584bce2015-05-19 16:01:59 -0700203 // Need to add a couple of blocks to test if the loop body is entered and
204 // put deoptimization instructions, etc.
205 void TransformLoopHeaderForBCE(HBasicBlock* header);
206
David Brazdil2d7352b2015-04-20 14:52:42 +0100207 // Removes `block` from the graph.
208 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000209
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100210 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
211 void SimplifyLoop(HBasicBlock* header);
212
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 int32_t GetNextInstructionId() {
214 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000215 return current_instruction_id_++;
216 }
217
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000218 int32_t GetCurrentInstructionId() const {
219 return current_instruction_id_;
220 }
221
222 void SetCurrentInstructionId(int32_t id) {
223 current_instruction_id_ = id;
224 }
225
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100226 uint16_t GetMaximumNumberOfOutVRegs() const {
227 return maximum_number_of_out_vregs_;
228 }
229
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000230 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
231 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100232 }
233
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100234 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
235 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
236 }
237
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000238 void UpdateTemporariesVRegSlots(size_t slots) {
239 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100240 }
241
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000242 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100243 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000244 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100245 }
246
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100247 void SetNumberOfVRegs(uint16_t number_of_vregs) {
248 number_of_vregs_ = number_of_vregs;
249 }
250
251 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100252 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100253 return number_of_vregs_;
254 }
255
256 void SetNumberOfInVRegs(uint16_t value) {
257 number_of_in_vregs_ = value;
258 }
259
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100260 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100261 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100262 return number_of_vregs_ - number_of_in_vregs_;
263 }
264
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100265 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
266 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100267 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100268
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100269 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
270 return linear_order_;
271 }
272
Mark Mendell1152c922015-04-24 17:06:35 -0400273 bool HasBoundsChecks() const {
274 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800275 }
276
Mark Mendell1152c922015-04-24 17:06:35 -0400277 void SetHasBoundsChecks(bool value) {
278 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800279 }
280
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100281 bool ShouldGenerateConstructorBarrier() const {
282 return should_generate_constructor_barrier_;
283 }
284
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000285 bool IsDebuggable() const { return debuggable_; }
286
David Brazdil8d5b8b22015-03-24 10:51:52 +0000287 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000288 // already, it is created and inserted into the graph. This method is only for
289 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000290 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000291 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000292 HIntConstant* GetIntConstant(int32_t value) {
293 return CreateConstant(value, &cached_int_constants_);
294 }
295 HLongConstant* GetLongConstant(int64_t value) {
296 return CreateConstant(value, &cached_long_constants_);
297 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000298 HFloatConstant* GetFloatConstant(float value) {
299 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
300 }
301 HDoubleConstant* GetDoubleConstant(double value) {
302 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
303 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000304
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100305 HCurrentMethod* GetCurrentMethod();
306
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000307 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100308
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100309 const DexFile& GetDexFile() const {
310 return dex_file_;
311 }
312
313 uint32_t GetMethodIdx() const {
314 return method_idx_;
315 }
316
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100317 InvokeType GetInvokeType() const {
318 return invoke_type_;
319 }
320
David Brazdil2d7352b2015-04-20 14:52:42 +0100321 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000322 void VisitBlockForDominatorTree(HBasicBlock* block,
323 HBasicBlock* predecessor,
324 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100325 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000326 void VisitBlockForBackEdges(HBasicBlock* block,
327 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100328 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000329 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100330 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000331
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000332 template <class InstructionType, typename ValueType>
333 InstructionType* CreateConstant(ValueType value,
334 ArenaSafeMap<ValueType, InstructionType*>* cache) {
335 // Try to find an existing constant of the given value.
336 InstructionType* constant = nullptr;
337 auto cached_constant = cache->find(value);
338 if (cached_constant != cache->end()) {
339 constant = cached_constant->second;
340 }
341
342 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100343 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000344 if (constant == nullptr || constant->GetBlock() == nullptr) {
345 constant = new (arena_) InstructionType(value);
346 cache->Overwrite(value, constant);
347 InsertConstant(constant);
348 }
349 return constant;
350 }
351
David Brazdil8d5b8b22015-03-24 10:51:52 +0000352 void InsertConstant(HConstant* instruction);
353
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000354 // Cache a float constant into the graph. This method should only be
355 // called by the SsaBuilder when creating "equivalent" instructions.
356 void CacheFloatConstant(HFloatConstant* constant);
357
358 // See CacheFloatConstant comment.
359 void CacheDoubleConstant(HDoubleConstant* constant);
360
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000361 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000362
363 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000364 GrowableArray<HBasicBlock*> blocks_;
365
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100366 // List of blocks to perform a reverse post order tree traversal.
367 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000368
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100369 // List of blocks to perform a linear order tree traversal.
370 GrowableArray<HBasicBlock*> linear_order_;
371
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000372 HBasicBlock* entry_block_;
373 HBasicBlock* exit_block_;
374
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100375 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100376 uint16_t maximum_number_of_out_vregs_;
377
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100378 // The number of virtual registers in this method. Contains the parameters.
379 uint16_t number_of_vregs_;
380
381 // The number of virtual registers used by parameters of this method.
382 uint16_t number_of_in_vregs_;
383
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000384 // Number of vreg size slots that the temporaries use (used in baseline compiler).
385 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100386
Mark Mendell1152c922015-04-24 17:06:35 -0400387 // Has bounds checks. We can totally skip BCE if it's false.
388 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800389
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000390 // Indicates whether the graph should be compiled in a way that
391 // ensures full debuggability. If false, we can apply more
392 // aggressive optimizations that may limit the level of debugging.
393 const bool debuggable_;
394
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000395 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000396 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000397
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100398 // The dex file from which the method is from.
399 const DexFile& dex_file_;
400
401 // The method index in the dex file.
402 const uint32_t method_idx_;
403
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100404 // If inlined, this encodes how the callee is being invoked.
405 const InvokeType invoke_type_;
406
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100407 // Whether the graph has been transformed to SSA form. Only used
408 // in debug mode to ensure we are not using properties only valid
409 // for non-SSA form (like the number of temporaries).
410 bool in_ssa_form_;
411
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100412 const bool should_generate_constructor_barrier_;
413
Mathieu Chartiere401d142015-04-22 13:56:20 -0700414 const InstructionSet instruction_set_;
415
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000416 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000417 HNullConstant* cached_null_constant_;
418 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000419 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000420 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000421 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000422
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100423 HCurrentMethod* cached_current_method_;
424
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000425 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100426 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000427 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000428 DISALLOW_COPY_AND_ASSIGN(HGraph);
429};
430
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700431class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000432 public:
433 HLoopInformation(HBasicBlock* header, HGraph* graph)
434 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100435 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100436 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100437 // Make bit vector growable, as the number of blocks may change.
438 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100439
440 HBasicBlock* GetHeader() const {
441 return header_;
442 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000443
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000444 void SetHeader(HBasicBlock* block) {
445 header_ = block;
446 }
447
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100448 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
449 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
450 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
451
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000452 void AddBackEdge(HBasicBlock* back_edge) {
453 back_edges_.Add(back_edge);
454 }
455
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100456 void RemoveBackEdge(HBasicBlock* back_edge) {
457 back_edges_.Delete(back_edge);
458 }
459
David Brazdil46e2a392015-03-16 17:31:52 +0000460 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100461 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000462 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100463 }
464 return false;
465 }
466
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000467 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000468 return back_edges_.Size();
469 }
470
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100471 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100472
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100473 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
474 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100475 }
476
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100477 // Returns the lifetime position of the back edge that has the
478 // greatest lifetime position.
479 size_t GetLifetimeEnd() const;
480
481 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
482 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
483 if (back_edges_.Get(i) == existing) {
484 back_edges_.Put(i, new_back_edge);
485 return;
486 }
487 }
488 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100489 }
490
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100491 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100492 // that is the header dominates the back edge.
493 bool Populate();
494
David Brazdila4b8c212015-05-07 09:59:30 +0100495 // Reanalyzes the loop by removing loop info from its blocks and re-running
496 // Populate(). If there are no back edges left, the loop info is completely
497 // removed as well as its SuspendCheck instruction. It must be run on nested
498 // inner loops first.
499 void Update();
500
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100501 // Returns whether this loop information contains `block`.
502 // Note that this loop information *must* be populated before entering this function.
503 bool Contains(const HBasicBlock& block) const;
504
505 // Returns whether this loop information is an inner loop of `other`.
506 // Note that `other` *must* be populated before entering this function.
507 bool IsIn(const HLoopInformation& other) const;
508
509 const ArenaBitVector& GetBlocks() const { return blocks_; }
510
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000511 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000512 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000513
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000514 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100515 // Internal recursive implementation of `Populate`.
516 void PopulateRecursive(HBasicBlock* block);
517
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000518 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100519 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000520 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100521 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000522
523 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
524};
525
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100526static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100527static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100528
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000529// A block in a method. Contains the list of instructions represented
530// as a double linked list. Each block knows its predecessors and
531// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100532
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700533class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000534 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100535 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000536 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000537 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
538 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000539 loop_information_(nullptr),
540 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100541 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100542 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100543 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100544 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000545 lifetime_end_(kNoLifetime),
546 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000547
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100548 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
549 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000550 }
551
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100552 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
553 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000554 }
555
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100556 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
557 return dominated_blocks_;
558 }
559
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100560 bool IsEntryBlock() const {
561 return graph_->GetEntryBlock() == this;
562 }
563
564 bool IsExitBlock() const {
565 return graph_->GetExitBlock() == this;
566 }
567
David Brazdil46e2a392015-03-16 17:31:52 +0000568 bool IsSingleGoto() const;
569
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000570 void AddBackEdge(HBasicBlock* back_edge) {
571 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000572 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000573 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100574 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000575 loop_information_->AddBackEdge(back_edge);
576 }
577
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000578 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000579 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000580
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000581 int GetBlockId() const { return block_id_; }
582 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000583
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000584 HBasicBlock* GetDominator() const { return dominator_; }
585 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100586 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100587 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000588 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
589 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
590 if (dominated_blocks_.Get(i) == existing) {
591 dominated_blocks_.Put(i, new_block);
592 return;
593 }
594 }
595 LOG(FATAL) << "Unreachable";
596 UNREACHABLE();
597 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100598 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000599
600 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100601 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000602 }
603
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100604 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
605 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100606 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100607 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100608 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
609 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000610
611 void AddSuccessor(HBasicBlock* block) {
612 successors_.Add(block);
613 block->predecessors_.Add(this);
614 }
615
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100616 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
617 size_t successor_index = GetSuccessorIndexOf(existing);
618 DCHECK_NE(successor_index, static_cast<size_t>(-1));
619 existing->RemovePredecessor(this);
620 new_block->predecessors_.Add(this);
621 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000622 }
623
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000624 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
625 size_t predecessor_index = GetPredecessorIndexOf(existing);
626 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
627 existing->RemoveSuccessor(this);
628 new_block->successors_.Add(this);
629 predecessors_.Put(predecessor_index, new_block);
630 }
631
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100632 // Insert `this` between `predecessor` and `successor. This method
633 // preserves the indicies, and will update the first edge found between
634 // `predecessor` and `successor`.
635 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
636 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
637 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
638 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
639 DCHECK_NE(successor_index, static_cast<size_t>(-1));
640 successor->predecessors_.Put(predecessor_index, this);
641 predecessor->successors_.Put(successor_index, this);
642 successors_.Add(successor);
643 predecessors_.Add(predecessor);
644 }
645
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100646 void RemovePredecessor(HBasicBlock* block) {
647 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100648 }
649
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000650 void RemoveSuccessor(HBasicBlock* block) {
651 successors_.Delete(block);
652 }
653
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100654 void ClearAllPredecessors() {
655 predecessors_.Reset();
656 }
657
658 void AddPredecessor(HBasicBlock* block) {
659 predecessors_.Add(block);
660 block->successors_.Add(this);
661 }
662
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100663 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100664 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100665 HBasicBlock* temp = predecessors_.Get(0);
666 predecessors_.Put(0, predecessors_.Get(1));
667 predecessors_.Put(1, temp);
668 }
669
David Brazdil769c9e52015-04-27 13:54:09 +0100670 void SwapSuccessors() {
671 DCHECK_EQ(successors_.Size(), 2u);
672 HBasicBlock* temp = successors_.Get(0);
673 successors_.Put(0, successors_.Get(1));
674 successors_.Put(1, temp);
675 }
676
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100677 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
678 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
679 if (predecessors_.Get(i) == predecessor) {
680 return i;
681 }
682 }
683 return -1;
684 }
685
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100686 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
687 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
688 if (successors_.Get(i) == successor) {
689 return i;
690 }
691 }
692 return -1;
693 }
694
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000695 // Split the block into two blocks just after `cursor`. Returns the newly
696 // created block. Note that this method just updates raw block information,
697 // like predecessors, successors, dominators, and instruction list. It does not
698 // update the graph, reverse post order, loop information, nor make sure the
699 // blocks are consistent (for example ending with a control flow instruction).
700 HBasicBlock* SplitAfter(HInstruction* cursor);
701
702 // Merge `other` at the end of `this`. Successors and dominated blocks of
703 // `other` are changed to be successors and dominated blocks of `this`. Note
704 // that this method does not update the graph, reverse post order, loop
705 // information, nor make sure the blocks are consistent (for example ending
706 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100707 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000708
709 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
710 // of `this` are moved to `other`.
711 // Note that this method does not update the graph, reverse post order, loop
712 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000713 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000714 void ReplaceWith(HBasicBlock* other);
715
David Brazdil2d7352b2015-04-20 14:52:42 +0100716 // Merge `other` at the end of `this`. This method updates loops, reverse post
717 // order, links to predecessors, successors, dominators and deletes the block
718 // from the graph. The two blocks must be successive, i.e. `this` the only
719 // predecessor of `other` and vice versa.
720 void MergeWith(HBasicBlock* other);
721
722 // Disconnects `this` from all its predecessors, successors and dominator,
723 // removes it from all loops it is included in and eventually from the graph.
724 // The block must not dominate any other block. Predecessors and successors
725 // are safely updated.
726 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000727
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000728 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100729 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100730 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100731 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100732 // Replace instruction `initial` with `replacement` within this block.
733 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
734 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100735 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100736 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000737 // RemoveInstruction and RemovePhi delete a given instruction from the respective
738 // instruction list. With 'ensure_safety' set to true, it verifies that the
739 // instruction is not in use and removes it from the use lists of its inputs.
740 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
741 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100742 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100743
744 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100745 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100746 }
747
Roland Levillain6b879dd2014-09-22 17:13:44 +0100748 bool IsLoopPreHeaderFirstPredecessor() const {
749 DCHECK(IsLoopHeader());
750 DCHECK(!GetPredecessors().IsEmpty());
751 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
752 }
753
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100754 HLoopInformation* GetLoopInformation() const {
755 return loop_information_;
756 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000757
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000758 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100759 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000760 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100761 void SetInLoop(HLoopInformation* info) {
762 if (IsLoopHeader()) {
763 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100764 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100765 loop_information_ = info;
766 } else if (loop_information_->Contains(*info->GetHeader())) {
767 // Block is currently part of an outer loop. Make it part of this inner loop.
768 // Note that a non loop header having a loop information means this loop information
769 // has already been populated
770 loop_information_ = info;
771 } else {
772 // Block is part of an inner loop. Do not update the loop information.
773 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
774 // at this point, because this method is being called while populating `info`.
775 }
776 }
777
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000778 // Raw update of the loop information.
779 void SetLoopInformation(HLoopInformation* info) {
780 loop_information_ = info;
781 }
782
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100783 bool IsInLoop() const { return loop_information_ != nullptr; }
784
David Brazdila4b8c212015-05-07 09:59:30 +0100785 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 bool Dominates(HBasicBlock* block) const;
787
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100788 size_t GetLifetimeStart() const { return lifetime_start_; }
789 size_t GetLifetimeEnd() const { return lifetime_end_; }
790
791 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
792 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
793
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100794 uint32_t GetDexPc() const { return dex_pc_; }
795
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000796 bool IsCatchBlock() const { return is_catch_block_; }
797 void SetIsCatchBlock() { is_catch_block_ = true; }
798
David Brazdil8d5b8b22015-03-24 10:51:52 +0000799 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000800 bool EndsWithIf() const;
801 bool HasSinglePhi() const;
802
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000804 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000805 GrowableArray<HBasicBlock*> predecessors_;
806 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100807 HInstructionList instructions_;
808 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000809 HLoopInformation* loop_information_;
810 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100811 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000812 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100813 // The dex program counter of the first instruction of this block.
814 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100815 size_t lifetime_start_;
816 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000817 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000818
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000819 friend class HGraph;
820 friend class HInstruction;
821
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000822 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
823};
824
David Brazdilb2bd1c52015-03-25 11:17:37 +0000825// Iterates over the LoopInformation of all loops which contain 'block'
826// from the innermost to the outermost.
827class HLoopInformationOutwardIterator : public ValueObject {
828 public:
829 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
830 : current_(block.GetLoopInformation()) {}
831
832 bool Done() const { return current_ == nullptr; }
833
834 void Advance() {
835 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100836 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000837 }
838
839 HLoopInformation* Current() const {
840 DCHECK(!Done());
841 return current_;
842 }
843
844 private:
845 HLoopInformation* current_;
846
847 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
848};
849
Alexandre Ramesef20f712015-06-09 10:29:30 +0100850#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100851 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000852 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000853 M(ArrayGet, Instruction) \
854 M(ArrayLength, Instruction) \
855 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100856 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000857 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000858 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000859 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100860 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000861 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100862 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100863 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700864 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000865 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000866 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000867 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100868 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000869 M(Exit, Instruction) \
870 M(FloatConstant, Constant) \
871 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100872 M(GreaterThan, Condition) \
873 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100874 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000875 M(InstanceFieldGet, Instruction) \
876 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000877 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100878 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000879 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000880 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100881 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000882 M(LessThan, Condition) \
883 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000884 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000885 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100886 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000887 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100888 M(Local, Instruction) \
889 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100890 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000891 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000892 M(Mul, BinaryOperation) \
893 M(Neg, UnaryOperation) \
894 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100895 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100896 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000897 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000898 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000899 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000900 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100901 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000902 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100903 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000904 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100905 M(Return, Instruction) \
906 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000907 M(Shl, BinaryOperation) \
908 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100909 M(StaticFieldGet, Instruction) \
910 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100911 M(StoreLocal, Instruction) \
912 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100913 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000914 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000915 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000916 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000917 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000918 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000919
Alexandre Ramesef20f712015-06-09 10:29:30 +0100920#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
921
922#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
923
924#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
925
926#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
927
928#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
929 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
930 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
931 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
932 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
933 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
934
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100935#define FOR_EACH_INSTRUCTION(M) \
936 FOR_EACH_CONCRETE_INSTRUCTION(M) \
937 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100938 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100939 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100940 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700941
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100942#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000943FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
944#undef FORWARD_DECLARATION
945
Roland Levillainccc07a92014-09-16 14:48:16 +0100946#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000947 InstructionKind GetKind() const OVERRIDE { return k##type; } \
948 const char* DebugName() const OVERRIDE { return #type; } \
949 const H##type* As##type() const OVERRIDE { return this; } \
950 H##type* As##type() OVERRIDE { return this; } \
951 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100952 return other->Is##type(); \
953 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000954 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000955
David Brazdiled596192015-01-23 10:39:45 +0000956template <typename T> class HUseList;
957
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100958template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700959class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000960 public:
David Brazdiled596192015-01-23 10:39:45 +0000961 HUseListNode* GetPrevious() const { return prev_; }
962 HUseListNode* GetNext() const { return next_; }
963 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100964 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100965 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100966
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000967 private:
David Brazdiled596192015-01-23 10:39:45 +0000968 HUseListNode(T user, size_t index)
969 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
970
971 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100972 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000973 HUseListNode<T>* prev_;
974 HUseListNode<T>* next_;
975
976 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000977
978 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
979};
980
David Brazdiled596192015-01-23 10:39:45 +0000981template <typename T>
982class HUseList : public ValueObject {
983 public:
984 HUseList() : first_(nullptr) {}
985
986 void Clear() {
987 first_ = nullptr;
988 }
989
990 // Adds a new entry at the beginning of the use list and returns
991 // the newly created node.
992 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000993 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000994 if (IsEmpty()) {
995 first_ = new_node;
996 } else {
997 first_->prev_ = new_node;
998 new_node->next_ = first_;
999 first_ = new_node;
1000 }
1001 return new_node;
1002 }
1003
1004 HUseListNode<T>* GetFirst() const {
1005 return first_;
1006 }
1007
1008 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001009 DCHECK(node != nullptr);
1010 DCHECK(Contains(node));
1011
David Brazdiled596192015-01-23 10:39:45 +00001012 if (node->prev_ != nullptr) {
1013 node->prev_->next_ = node->next_;
1014 }
1015 if (node->next_ != nullptr) {
1016 node->next_->prev_ = node->prev_;
1017 }
1018 if (node == first_) {
1019 first_ = node->next_;
1020 }
1021 }
1022
David Brazdil1abb4192015-02-17 18:33:36 +00001023 bool Contains(const HUseListNode<T>* node) const {
1024 if (node == nullptr) {
1025 return false;
1026 }
1027 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1028 if (current == node) {
1029 return true;
1030 }
1031 }
1032 return false;
1033 }
1034
David Brazdiled596192015-01-23 10:39:45 +00001035 bool IsEmpty() const {
1036 return first_ == nullptr;
1037 }
1038
1039 bool HasOnlyOneUse() const {
1040 return first_ != nullptr && first_->next_ == nullptr;
1041 }
1042
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001043 size_t SizeSlow() const {
1044 size_t count = 0;
1045 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1046 ++count;
1047 }
1048 return count;
1049 }
1050
David Brazdiled596192015-01-23 10:39:45 +00001051 private:
1052 HUseListNode<T>* first_;
1053};
1054
1055template<typename T>
1056class HUseIterator : public ValueObject {
1057 public:
1058 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1059
1060 bool Done() const { return current_ == nullptr; }
1061
1062 void Advance() {
1063 DCHECK(!Done());
1064 current_ = current_->GetNext();
1065 }
1066
1067 HUseListNode<T>* Current() const {
1068 DCHECK(!Done());
1069 return current_;
1070 }
1071
1072 private:
1073 HUseListNode<T>* current_;
1074
1075 friend class HValue;
1076};
1077
David Brazdil1abb4192015-02-17 18:33:36 +00001078// This class is used by HEnvironment and HInstruction classes to record the
1079// instructions they use and pointers to the corresponding HUseListNodes kept
1080// by the used instructions.
1081template <typename T>
1082class HUserRecord : public ValueObject {
1083 public:
1084 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1085 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1086
1087 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1088 : instruction_(old_record.instruction_), use_node_(use_node) {
1089 DCHECK(instruction_ != nullptr);
1090 DCHECK(use_node_ != nullptr);
1091 DCHECK(old_record.use_node_ == nullptr);
1092 }
1093
1094 HInstruction* GetInstruction() const { return instruction_; }
1095 HUseListNode<T>* GetUseNode() const { return use_node_; }
1096
1097 private:
1098 // Instruction used by the user.
1099 HInstruction* instruction_;
1100
1101 // Corresponding entry in the use list kept by 'instruction_'.
1102 HUseListNode<T>* use_node_;
1103};
1104
Calin Juravle27df7582015-04-17 19:12:31 +01001105// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1106// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1107// flag is consider.
1108// - DependsOn suggests that there is a real dependency between side effects but it only
1109// checks DependendsOnSomething flag.
1110//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001111// Represents the side effects an instruction may have.
1112class SideEffects : public ValueObject {
1113 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001114 SideEffects() : flags_(0) {}
1115
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001116 static SideEffects None() {
1117 return SideEffects(0);
1118 }
1119
1120 static SideEffects All() {
1121 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1122 }
1123
1124 static SideEffects ChangesSomething() {
1125 return SideEffects((1 << kFlagChangesCount) - 1);
1126 }
1127
1128 static SideEffects DependsOnSomething() {
1129 int count = kFlagDependsOnCount - kFlagChangesCount;
1130 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1131 }
1132
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001133 SideEffects Union(SideEffects other) const {
1134 return SideEffects(flags_ | other.flags_);
1135 }
1136
Roland Levillain72bceff2014-09-15 18:29:00 +01001137 bool HasSideEffects() const {
1138 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1139 return (flags_ & all_bits_set) != 0;
1140 }
1141
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001142 bool HasAllSideEffects() const {
1143 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1144 return all_bits_set == (flags_ & all_bits_set);
1145 }
1146
1147 bool DependsOn(SideEffects other) const {
1148 size_t depends_flags = other.ComputeDependsFlags();
1149 return (flags_ & depends_flags) != 0;
1150 }
1151
1152 bool HasDependencies() const {
1153 int count = kFlagDependsOnCount - kFlagChangesCount;
1154 size_t all_bits_set = (1 << count) - 1;
1155 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1156 }
1157
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001158 private:
1159 static constexpr int kFlagChangesSomething = 0;
1160 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1161
1162 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1163 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1164
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001165 explicit SideEffects(size_t flags) : flags_(flags) {}
1166
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001167 size_t ComputeDependsFlags() const {
1168 return flags_ << kFlagChangesCount;
1169 }
1170
1171 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001172};
1173
David Brazdiled596192015-01-23 10:39:45 +00001174// A HEnvironment object contains the values of virtual registers at a given location.
1175class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1176 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001177 HEnvironment(ArenaAllocator* arena,
1178 size_t number_of_vregs,
1179 const DexFile& dex_file,
1180 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001181 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001182 InvokeType invoke_type,
1183 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001184 : vregs_(arena, number_of_vregs),
1185 locations_(arena, number_of_vregs),
1186 parent_(nullptr),
1187 dex_file_(dex_file),
1188 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001189 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001190 invoke_type_(invoke_type),
1191 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001192 vregs_.SetSize(number_of_vregs);
1193 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001194 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001195 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001196
1197 locations_.SetSize(number_of_vregs);
1198 for (size_t i = 0; i < number_of_vregs; ++i) {
1199 locations_.Put(i, Location());
1200 }
1201 }
1202
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001203 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001204 : HEnvironment(arena,
1205 to_copy.Size(),
1206 to_copy.GetDexFile(),
1207 to_copy.GetMethodIdx(),
1208 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001209 to_copy.GetInvokeType(),
1210 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001211
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001212 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001213 if (parent_ != nullptr) {
1214 parent_->SetAndCopyParentChain(allocator, parent);
1215 } else {
1216 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1217 parent_->CopyFrom(parent);
1218 if (parent->GetParent() != nullptr) {
1219 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1220 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001221 }
David Brazdiled596192015-01-23 10:39:45 +00001222 }
1223
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001224 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1225 void CopyFrom(HEnvironment* environment);
1226
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001227 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1228 // input to the loop phi instead. This is for inserting instructions that
1229 // require an environment (like HDeoptimization) in the loop pre-header.
1230 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001231
1232 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001233 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001234 }
1235
1236 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001237 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001238 }
1239
David Brazdil1abb4192015-02-17 18:33:36 +00001240 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001241
1242 size_t Size() const { return vregs_.Size(); }
1243
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001244 HEnvironment* GetParent() const { return parent_; }
1245
1246 void SetLocationAt(size_t index, Location location) {
1247 locations_.Put(index, location);
1248 }
1249
1250 Location GetLocationAt(size_t index) const {
1251 return locations_.Get(index);
1252 }
1253
1254 uint32_t GetDexPc() const {
1255 return dex_pc_;
1256 }
1257
1258 uint32_t GetMethodIdx() const {
1259 return method_idx_;
1260 }
1261
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001262 InvokeType GetInvokeType() const {
1263 return invoke_type_;
1264 }
1265
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001266 const DexFile& GetDexFile() const {
1267 return dex_file_;
1268 }
1269
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001270 HInstruction* GetHolder() const {
1271 return holder_;
1272 }
1273
David Brazdiled596192015-01-23 10:39:45 +00001274 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001275 // Record instructions' use entries of this environment for constant-time removal.
1276 // It should only be called by HInstruction when a new environment use is added.
1277 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1278 DCHECK(env_use->GetUser() == this);
1279 size_t index = env_use->GetIndex();
1280 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1281 }
David Brazdiled596192015-01-23 10:39:45 +00001282
David Brazdil1abb4192015-02-17 18:33:36 +00001283 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001284 GrowableArray<Location> locations_;
1285 HEnvironment* parent_;
1286 const DexFile& dex_file_;
1287 const uint32_t method_idx_;
1288 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001289 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001290
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001291 // The instruction that holds this environment. Only used in debug mode
1292 // to ensure the graph is consistent.
1293 HInstruction* const holder_;
1294
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001295 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001296
1297 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1298};
1299
Calin Juravleacf735c2015-02-12 15:25:22 +00001300class ReferenceTypeInfo : ValueObject {
1301 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001302 typedef Handle<mirror::Class> TypeHandle;
1303
1304 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1305 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1306 if (type_handle->IsObjectClass()) {
1307 // Override the type handle to be consistent with the case when we get to
1308 // Top but don't have the Object class available. It avoids having to guess
1309 // what value the type_handle has when it's Top.
1310 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1311 } else {
1312 return ReferenceTypeInfo(type_handle, is_exact, false);
1313 }
1314 }
1315
1316 static ReferenceTypeInfo CreateTop(bool is_exact) {
1317 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001318 }
1319
1320 bool IsExact() const { return is_exact_; }
1321 bool IsTop() const { return is_top_; }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001322 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1323 return !IsTop() && GetTypeHandle()->IsInterface();
1324 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001325
1326 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1327
Calin Juravleb1498f62015-02-16 13:13:29 +00001328 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001329 if (IsTop()) {
1330 // Top (equivalent for java.lang.Object) is supertype of anything.
1331 return true;
1332 }
1333 if (rti.IsTop()) {
1334 // If we get here `this` is not Top() so it can't be a supertype.
1335 return false;
1336 }
1337 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1338 }
1339
1340 // Returns true if the type information provide the same amount of details.
1341 // Note that it does not mean that the instructions have the same actual type
1342 // (e.g. tops are equal but they can be the result of a merge).
1343 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1344 if (IsExact() != rti.IsExact()) {
1345 return false;
1346 }
1347 if (IsTop() && rti.IsTop()) {
1348 // `Top` means java.lang.Object, so the types are equivalent.
1349 return true;
1350 }
1351 if (IsTop() || rti.IsTop()) {
1352 // If only one is top or object than they are not equivalent.
1353 // NB: We need this extra check because the type_handle of `Top` is invalid
1354 // and we cannot inspect its reference.
1355 return false;
1356 }
1357
1358 // Finally check the types.
1359 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1360 }
1361
1362 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001363 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1364 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1365 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1366
Calin Juravleacf735c2015-02-12 15:25:22 +00001367 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001368 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001369 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001370 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001371 bool is_exact_;
1372 // A true value here means that the object type should be java.lang.Object.
1373 // We don't have access to the corresponding mirror object every time so this
1374 // flag acts as a substitute. When true, the TypeHandle refers to a null
1375 // pointer and should not be used.
1376 bool is_top_;
1377};
1378
1379std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1380
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001381class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001382 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001383 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001384 : previous_(nullptr),
1385 next_(nullptr),
1386 block_(nullptr),
1387 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001388 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001389 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001390 locations_(nullptr),
1391 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001392 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001393 side_effects_(side_effects),
1394 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001395
Dave Allison20dfc792014-06-16 20:44:29 -07001396 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001397
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001398#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001399 enum InstructionKind {
1400 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1401 };
1402#undef DECLARE_KIND
1403
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001404 HInstruction* GetNext() const { return next_; }
1405 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001406
Calin Juravle77520bc2015-01-12 18:45:46 +00001407 HInstruction* GetNextDisregardingMoves() const;
1408 HInstruction* GetPreviousDisregardingMoves() const;
1409
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001410 HBasicBlock* GetBlock() const { return block_; }
1411 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001412 bool IsInBlock() const { return block_ != nullptr; }
1413 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001414 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001415
Roland Levillain6b879dd2014-09-22 17:13:44 +01001416 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001417 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001418
1419 virtual void Accept(HGraphVisitor* visitor) = 0;
1420 virtual const char* DebugName() const = 0;
1421
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001422 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001423 void SetRawInputAt(size_t index, HInstruction* input) {
1424 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1425 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001426
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001427 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001428 virtual uint32_t GetDexPc() const {
1429 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1430 " does not need an environment";
1431 UNREACHABLE();
1432 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001433 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001434 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001435 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001436
Calin Juravle10e244f2015-01-26 18:54:32 +00001437 // Does not apply for all instructions, but having this at top level greatly
1438 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001439 virtual bool CanBeNull() const {
1440 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1441 return true;
1442 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001443
Calin Juravle641547a2015-04-21 22:08:51 +01001444 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1445 UNUSED(obj);
1446 return false;
1447 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001448
Calin Juravleacf735c2015-02-12 15:25:22 +00001449 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001450 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001451 reference_type_info_ = reference_type_info;
1452 }
1453
Calin Juravle61d544b2015-02-23 16:46:57 +00001454 ReferenceTypeInfo GetReferenceTypeInfo() const {
1455 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1456 return reference_type_info_;
1457 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001458
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001459 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001460 DCHECK(user != nullptr);
1461 HUseListNode<HInstruction*>* use =
1462 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1463 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001464 }
1465
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001466 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001467 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001468 HUseListNode<HEnvironment*>* env_use =
1469 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1470 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001471 }
1472
David Brazdil1abb4192015-02-17 18:33:36 +00001473 void RemoveAsUserOfInput(size_t input) {
1474 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1475 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1476 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001477
David Brazdil1abb4192015-02-17 18:33:36 +00001478 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1479 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001480
David Brazdiled596192015-01-23 10:39:45 +00001481 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1482 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001483 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001484 bool HasOnlyOneNonEnvironmentUse() const {
1485 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1486 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001487
Roland Levillain6c82d402014-10-13 16:10:27 +01001488 // Does this instruction strictly dominate `other_instruction`?
1489 // Returns false if this instruction and `other_instruction` are the same.
1490 // Aborts if this instruction and `other_instruction` are both phis.
1491 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001492
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001493 int GetId() const { return id_; }
1494 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001495
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001496 int GetSsaIndex() const { return ssa_index_; }
1497 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1498 bool HasSsaIndex() const { return ssa_index_ != -1; }
1499
1500 bool HasEnvironment() const { return environment_ != nullptr; }
1501 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001502 // Set the `environment_` field. Raw because this method does not
1503 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001504 void SetRawEnvironment(HEnvironment* environment) {
1505 DCHECK(environment_ == nullptr);
1506 DCHECK_EQ(environment->GetHolder(), this);
1507 environment_ = environment;
1508 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001509
1510 // Set the environment of this instruction, copying it from `environment`. While
1511 // copying, the uses lists are being updated.
1512 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001513 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001514 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001515 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001516 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001517 if (environment->GetParent() != nullptr) {
1518 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1519 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001520 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001521
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001522 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1523 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001524 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001525 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001526 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001527 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001528 if (environment->GetParent() != nullptr) {
1529 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1530 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001531 }
1532
Nicolas Geoffray39468442014-09-02 15:17:15 +01001533 // Returns the number of entries in the environment. Typically, that is the
1534 // number of dex registers in a method. It could be more in case of inlining.
1535 size_t EnvironmentSize() const;
1536
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001537 LocationSummary* GetLocations() const { return locations_; }
1538 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001539
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001540 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001541 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001542
Alexandre Rames188d4312015-04-09 18:30:21 +01001543 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1544 // uses of this instruction by `other` are *not* updated.
1545 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1546 ReplaceWith(other);
1547 other->ReplaceInput(this, use_index);
1548 }
1549
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001550 // Move `this` instruction before `cursor`.
1551 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001552
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001553#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001554 bool Is##type() const { return (As##type() != nullptr); } \
1555 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001556 virtual H##type* As##type() { return nullptr; }
1557
1558 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1559#undef INSTRUCTION_TYPE_CHECK
1560
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001561 // Returns whether the instruction can be moved within the graph.
1562 virtual bool CanBeMoved() const { return false; }
1563
1564 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001565 virtual bool InstructionTypeEquals(HInstruction* other) const {
1566 UNUSED(other);
1567 return false;
1568 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001569
1570 // Returns whether any data encoded in the two instructions is equal.
1571 // This method does not look at the inputs. Both instructions must be
1572 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001573 virtual bool InstructionDataEquals(HInstruction* other) const {
1574 UNUSED(other);
1575 return false;
1576 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001577
1578 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001579 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001580 // 2) Their inputs are identical.
1581 bool Equals(HInstruction* other) const;
1582
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001583 virtual InstructionKind GetKind() const = 0;
1584
1585 virtual size_t ComputeHashCode() const {
1586 size_t result = GetKind();
1587 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1588 result = (result * 31) + InputAt(i)->GetId();
1589 }
1590 return result;
1591 }
1592
1593 SideEffects GetSideEffects() const { return side_effects_; }
1594
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001595 size_t GetLifetimePosition() const { return lifetime_position_; }
1596 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1597 LiveInterval* GetLiveInterval() const { return live_interval_; }
1598 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1599 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1600
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001601 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1602
1603 // Returns whether the code generation of the instruction will require to have access
1604 // to the current method. Such instructions are:
1605 // (1): Instructions that require an environment, as calling the runtime requires
1606 // to walk the stack and have the current method stored at a specific stack address.
1607 // (2): Object literals like classes and strings, that are loaded from the dex cache
1608 // fields of the current method.
1609 bool NeedsCurrentMethod() const {
1610 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1611 }
1612
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001613 virtual bool NeedsDexCache() const { return false; }
1614
David Brazdil1abb4192015-02-17 18:33:36 +00001615 protected:
1616 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1617 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1618
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001619 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001620 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1621
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001622 HInstruction* previous_;
1623 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001624 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001625
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001626 // An instruction gets an id when it is added to the graph.
1627 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001628 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001629 int id_;
1630
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001631 // When doing liveness analysis, instructions that have uses get an SSA index.
1632 int ssa_index_;
1633
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001634 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001635 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001636
1637 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001638 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001639
Nicolas Geoffray39468442014-09-02 15:17:15 +01001640 // The environment associated with this instruction. Not null if the instruction
1641 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001642 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001643
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001644 // Set by the code generator.
1645 LocationSummary* locations_;
1646
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001647 // Set by the liveness analysis.
1648 LiveInterval* live_interval_;
1649
1650 // Set by the liveness analysis, this is the position in a linear
1651 // order of blocks where this instruction's live interval start.
1652 size_t lifetime_position_;
1653
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001654 const SideEffects side_effects_;
1655
Calin Juravleacf735c2015-02-12 15:25:22 +00001656 // TODO: for primitive types this should be marked as invalid.
1657 ReferenceTypeInfo reference_type_info_;
1658
David Brazdil1abb4192015-02-17 18:33:36 +00001659 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001660 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001661 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001662 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001663 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001664
1665 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1666};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001667std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001668
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001669class HInputIterator : public ValueObject {
1670 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001671 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001672
1673 bool Done() const { return index_ == instruction_->InputCount(); }
1674 HInstruction* Current() const { return instruction_->InputAt(index_); }
1675 void Advance() { index_++; }
1676
1677 private:
1678 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001679 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001680
1681 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1682};
1683
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001684class HInstructionIterator : public ValueObject {
1685 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001686 explicit HInstructionIterator(const HInstructionList& instructions)
1687 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001688 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001689 }
1690
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001691 bool Done() const { return instruction_ == nullptr; }
1692 HInstruction* Current() const { return instruction_; }
1693 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001694 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001695 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001696 }
1697
1698 private:
1699 HInstruction* instruction_;
1700 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001701
1702 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001703};
1704
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001705class HBackwardInstructionIterator : public ValueObject {
1706 public:
1707 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1708 : instruction_(instructions.last_instruction_) {
1709 next_ = Done() ? nullptr : instruction_->GetPrevious();
1710 }
1711
1712 bool Done() const { return instruction_ == nullptr; }
1713 HInstruction* Current() const { return instruction_; }
1714 void Advance() {
1715 instruction_ = next_;
1716 next_ = Done() ? nullptr : instruction_->GetPrevious();
1717 }
1718
1719 private:
1720 HInstruction* instruction_;
1721 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001722
1723 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001724};
1725
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001726// An embedded container with N elements of type T. Used (with partial
1727// specialization for N=0) because embedded arrays cannot have size 0.
1728template<typename T, intptr_t N>
1729class EmbeddedArray {
1730 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001731 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001732
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001733 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001734
1735 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001736 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001737 return elements_[i];
1738 }
1739
1740 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001741 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001742 return elements_[i];
1743 }
1744
1745 const T& At(intptr_t i) const {
1746 return (*this)[i];
1747 }
1748
1749 void SetAt(intptr_t i, const T& val) {
1750 (*this)[i] = val;
1751 }
1752
1753 private:
1754 T elements_[N];
1755};
1756
1757template<typename T>
1758class EmbeddedArray<T, 0> {
1759 public:
1760 intptr_t length() const { return 0; }
1761 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001762 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001763 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001764 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001765 }
1766 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001767 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001768 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001769 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001770 }
1771};
1772
1773template<intptr_t N>
1774class HTemplateInstruction: public HInstruction {
1775 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001776 HTemplateInstruction<N>(SideEffects side_effects)
1777 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001778 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001779
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001780 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001781
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001782 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001783 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1784
1785 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1786 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001787 }
1788
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001789 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001790 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001791
1792 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001793};
1794
Dave Allison20dfc792014-06-16 20:44:29 -07001795template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001796class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001797 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001798 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1799 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001800 virtual ~HExpression() {}
1801
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001802 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001803
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001804 protected:
1805 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001806};
1807
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001808// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1809// instruction that branches to the exit block.
1810class HReturnVoid : public HTemplateInstruction<0> {
1811 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001812 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001813
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001814 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001815
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001816 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001817
1818 private:
1819 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1820};
1821
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001822// Represents dex's RETURN opcodes. A HReturn is a control flow
1823// instruction that branches to the exit block.
1824class HReturn : public HTemplateInstruction<1> {
1825 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001826 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001827 SetRawInputAt(0, value);
1828 }
1829
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001830 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001831
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001832 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001833
1834 private:
1835 DISALLOW_COPY_AND_ASSIGN(HReturn);
1836};
1837
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001838// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001839// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001840// exit block.
1841class HExit : public HTemplateInstruction<0> {
1842 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001843 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001844
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001845 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001846
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001847 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001848
1849 private:
1850 DISALLOW_COPY_AND_ASSIGN(HExit);
1851};
1852
1853// Jumps from one block to another.
1854class HGoto : public HTemplateInstruction<0> {
1855 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001856 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1857
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001858 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001859
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001860 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001861 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001862 }
1863
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001864 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001865
1866 private:
1867 DISALLOW_COPY_AND_ASSIGN(HGoto);
1868};
1869
Dave Allison20dfc792014-06-16 20:44:29 -07001870
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001871// Conditional branch. A block ending with an HIf instruction must have
1872// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001873class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001874 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001875 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001876 SetRawInputAt(0, input);
1877 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001878
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001879 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001880
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001881 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001882 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001883 }
1884
1885 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001886 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001887 }
1888
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001889 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001890
1891 private:
1892 DISALLOW_COPY_AND_ASSIGN(HIf);
1893};
1894
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001895// Deoptimize to interpreter, upon checking a condition.
1896class HDeoptimize : public HTemplateInstruction<1> {
1897 public:
1898 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1899 : HTemplateInstruction(SideEffects::None()),
1900 dex_pc_(dex_pc) {
1901 SetRawInputAt(0, cond);
1902 }
1903
1904 bool NeedsEnvironment() const OVERRIDE { return true; }
1905 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001906 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001907
1908 DECLARE_INSTRUCTION(Deoptimize);
1909
1910 private:
1911 uint32_t dex_pc_;
1912
1913 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1914};
1915
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001916// Represents the ArtMethod that was passed as a first argument to
1917// the method. It is used by instructions that depend on it, like
1918// instructions that work with the dex cache.
1919class HCurrentMethod : public HExpression<0> {
1920 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001921 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001922
1923 DECLARE_INSTRUCTION(CurrentMethod);
1924
1925 private:
1926 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
1927};
1928
Roland Levillain88cb1752014-10-20 16:36:47 +01001929class HUnaryOperation : public HExpression<1> {
1930 public:
1931 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1932 : HExpression(result_type, SideEffects::None()) {
1933 SetRawInputAt(0, input);
1934 }
1935
1936 HInstruction* GetInput() const { return InputAt(0); }
1937 Primitive::Type GetResultType() const { return GetType(); }
1938
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001939 bool CanBeMoved() const OVERRIDE { return true; }
1940 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001941 UNUSED(other);
1942 return true;
1943 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001944
Roland Levillain9240d6a2014-10-20 16:47:04 +01001945 // Try to statically evaluate `operation` and return a HConstant
1946 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001947 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001948 HConstant* TryStaticEvaluation() const;
1949
1950 // Apply this operation to `x`.
1951 virtual int32_t Evaluate(int32_t x) const = 0;
1952 virtual int64_t Evaluate(int64_t x) const = 0;
1953
Roland Levillain88cb1752014-10-20 16:36:47 +01001954 DECLARE_INSTRUCTION(UnaryOperation);
1955
1956 private:
1957 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1958};
1959
Dave Allison20dfc792014-06-16 20:44:29 -07001960class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001961 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001962 HBinaryOperation(Primitive::Type result_type,
1963 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001964 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001965 SetRawInputAt(0, left);
1966 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001967 }
1968
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001969 HInstruction* GetLeft() const { return InputAt(0); }
1970 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001971 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001972
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001973 virtual bool IsCommutative() const { return false; }
1974
1975 // Put constant on the right.
1976 // Returns whether order is changed.
1977 bool OrderInputsWithConstantOnTheRight() {
1978 HInstruction* left = InputAt(0);
1979 HInstruction* right = InputAt(1);
1980 if (left->IsConstant() && !right->IsConstant()) {
1981 ReplaceInput(right, 0);
1982 ReplaceInput(left, 1);
1983 return true;
1984 }
1985 return false;
1986 }
1987
1988 // Order inputs by instruction id, but favor constant on the right side.
1989 // This helps GVN for commutative ops.
1990 void OrderInputs() {
1991 DCHECK(IsCommutative());
1992 HInstruction* left = InputAt(0);
1993 HInstruction* right = InputAt(1);
1994 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1995 return;
1996 }
1997 if (OrderInputsWithConstantOnTheRight()) {
1998 return;
1999 }
2000 // Order according to instruction id.
2001 if (left->GetId() > right->GetId()) {
2002 ReplaceInput(right, 0);
2003 ReplaceInput(left, 1);
2004 }
2005 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002006
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002007 bool CanBeMoved() const OVERRIDE { return true; }
2008 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002009 UNUSED(other);
2010 return true;
2011 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002012
Roland Levillain9240d6a2014-10-20 16:47:04 +01002013 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002014 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002015 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002016 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002017
2018 // Apply this operation to `x` and `y`.
2019 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2020 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2021
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002022 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002023 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002024 HConstant* GetConstantRight() const;
2025
2026 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002027 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002028 HInstruction* GetLeastConstantLeft() const;
2029
Roland Levillainccc07a92014-09-16 14:48:16 +01002030 DECLARE_INSTRUCTION(BinaryOperation);
2031
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002032 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002033 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2034};
2035
Dave Allison20dfc792014-06-16 20:44:29 -07002036class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002037 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002038 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002039 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
2040 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002041
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002042 bool NeedsMaterialization() const { return needs_materialization_; }
2043 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002044
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002045 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002046 // `instruction`, and disregard moves in between.
2047 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002048
Dave Allison20dfc792014-06-16 20:44:29 -07002049 DECLARE_INSTRUCTION(Condition);
2050
2051 virtual IfCondition GetCondition() const = 0;
2052
2053 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002054 // For register allocation purposes, returns whether this instruction needs to be
2055 // materialized (that is, not just be in the processor flags).
2056 bool needs_materialization_;
2057
Dave Allison20dfc792014-06-16 20:44:29 -07002058 DISALLOW_COPY_AND_ASSIGN(HCondition);
2059};
2060
2061// Instruction to check if two inputs are equal to each other.
2062class HEqual : public HCondition {
2063 public:
2064 HEqual(HInstruction* first, HInstruction* second)
2065 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002066
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002067 bool IsCommutative() const OVERRIDE { return true; }
2068
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002069 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002070 return x == y ? 1 : 0;
2071 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002072 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002073 return x == y ? 1 : 0;
2074 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002075
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002076 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002077
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002078 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002079 return kCondEQ;
2080 }
2081
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002082 private:
2083 DISALLOW_COPY_AND_ASSIGN(HEqual);
2084};
2085
Dave Allison20dfc792014-06-16 20:44:29 -07002086class HNotEqual : public HCondition {
2087 public:
2088 HNotEqual(HInstruction* first, HInstruction* second)
2089 : HCondition(first, second) {}
2090
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002091 bool IsCommutative() const OVERRIDE { return true; }
2092
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002093 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002094 return x != y ? 1 : 0;
2095 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002096 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002097 return x != y ? 1 : 0;
2098 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002099
Dave Allison20dfc792014-06-16 20:44:29 -07002100 DECLARE_INSTRUCTION(NotEqual);
2101
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002102 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002103 return kCondNE;
2104 }
2105
2106 private:
2107 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2108};
2109
2110class HLessThan : public HCondition {
2111 public:
2112 HLessThan(HInstruction* first, HInstruction* second)
2113 : HCondition(first, second) {}
2114
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002115 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002116 return x < y ? 1 : 0;
2117 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002118 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002119 return x < y ? 1 : 0;
2120 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002121
Dave Allison20dfc792014-06-16 20:44:29 -07002122 DECLARE_INSTRUCTION(LessThan);
2123
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002124 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002125 return kCondLT;
2126 }
2127
2128 private:
2129 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2130};
2131
2132class HLessThanOrEqual : public HCondition {
2133 public:
2134 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2135 : HCondition(first, second) {}
2136
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002137 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002138 return x <= y ? 1 : 0;
2139 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002140 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002141 return x <= y ? 1 : 0;
2142 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002143
Dave Allison20dfc792014-06-16 20:44:29 -07002144 DECLARE_INSTRUCTION(LessThanOrEqual);
2145
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002146 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002147 return kCondLE;
2148 }
2149
2150 private:
2151 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2152};
2153
2154class HGreaterThan : public HCondition {
2155 public:
2156 HGreaterThan(HInstruction* first, HInstruction* second)
2157 : HCondition(first, second) {}
2158
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002159 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002160 return x > y ? 1 : 0;
2161 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002162 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002163 return x > y ? 1 : 0;
2164 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002165
Dave Allison20dfc792014-06-16 20:44:29 -07002166 DECLARE_INSTRUCTION(GreaterThan);
2167
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002168 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002169 return kCondGT;
2170 }
2171
2172 private:
2173 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2174};
2175
2176class HGreaterThanOrEqual : public HCondition {
2177 public:
2178 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2179 : HCondition(first, second) {}
2180
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002181 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002182 return x >= y ? 1 : 0;
2183 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002184 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002185 return x >= y ? 1 : 0;
2186 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002187
Dave Allison20dfc792014-06-16 20:44:29 -07002188 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2189
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002190 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002191 return kCondGE;
2192 }
2193
2194 private:
2195 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2196};
2197
2198
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002199// Instruction to check how two inputs compare to each other.
2200// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2201class HCompare : public HBinaryOperation {
2202 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002203 // The bias applies for floating point operations and indicates how NaN
2204 // comparisons are treated:
2205 enum Bias {
2206 kNoBias, // bias is not applicable (i.e. for long operation)
2207 kGtBias, // return 1 for NaN comparisons
2208 kLtBias, // return -1 for NaN comparisons
2209 };
2210
Alexey Frunze4dda3372015-06-01 18:31:49 -07002211 HCompare(Primitive::Type type,
2212 HInstruction* first,
2213 HInstruction* second,
2214 Bias bias,
2215 uint32_t dex_pc)
2216 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002217 DCHECK_EQ(type, first->GetType());
2218 DCHECK_EQ(type, second->GetType());
2219 }
2220
Calin Juravleddb7df22014-11-25 20:56:51 +00002221 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002222 return
2223 x == y ? 0 :
2224 x > y ? 1 :
2225 -1;
2226 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002227
2228 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002229 return
2230 x == y ? 0 :
2231 x > y ? 1 :
2232 -1;
2233 }
2234
Calin Juravleddb7df22014-11-25 20:56:51 +00002235 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2236 return bias_ == other->AsCompare()->bias_;
2237 }
2238
2239 bool IsGtBias() { return bias_ == kGtBias; }
2240
Alexey Frunze4dda3372015-06-01 18:31:49 -07002241 uint32_t GetDexPc() const { return dex_pc_; }
2242
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002243 DECLARE_INSTRUCTION(Compare);
2244
2245 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002246 const Bias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002247 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002248
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002249 DISALLOW_COPY_AND_ASSIGN(HCompare);
2250};
2251
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002252// A local in the graph. Corresponds to a Dex register.
2253class HLocal : public HTemplateInstruction<0> {
2254 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002255 explicit HLocal(uint16_t reg_number)
2256 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002257
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002258 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002259
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002260 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002261
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002262 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002263 // The Dex register number.
2264 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002265
2266 DISALLOW_COPY_AND_ASSIGN(HLocal);
2267};
2268
2269// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002270class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002271 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002272 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002273 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002274 SetRawInputAt(0, local);
2275 }
2276
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002277 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2278
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002279 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002280
2281 private:
2282 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2283};
2284
2285// Store a value in a given local. This instruction has two inputs: the value
2286// and the local.
2287class HStoreLocal : public HTemplateInstruction<2> {
2288 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002289 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002290 SetRawInputAt(0, local);
2291 SetRawInputAt(1, value);
2292 }
2293
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002294 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2295
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002296 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002297
2298 private:
2299 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2300};
2301
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002302class HConstant : public HExpression<0> {
2303 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002304 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2305
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002306 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002307
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002308 virtual bool IsMinusOne() const { return false; }
2309 virtual bool IsZero() const { return false; }
2310 virtual bool IsOne() const { return false; }
2311
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002312 DECLARE_INSTRUCTION(Constant);
2313
2314 private:
2315 DISALLOW_COPY_AND_ASSIGN(HConstant);
2316};
2317
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002318class HFloatConstant : public HConstant {
2319 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002320 float GetValue() const { return value_; }
2321
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002322 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002323 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2324 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002325 }
2326
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002327 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002328
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002329 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002330 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002331 }
2332 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002333 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002334 }
2335 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002336 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2337 }
2338 bool IsNaN() const {
2339 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002340 }
2341
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002342 DECLARE_INSTRUCTION(FloatConstant);
2343
2344 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002345 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002346 explicit HFloatConstant(int32_t value)
2347 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002348
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002349 const float value_;
2350
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002351 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002352 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002353 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002354 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2355};
2356
2357class HDoubleConstant : public HConstant {
2358 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002359 double GetValue() const { return value_; }
2360
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002361 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002362 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2363 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002364 }
2365
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002366 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002367
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002368 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002369 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002370 }
2371 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002372 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002373 }
2374 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002375 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2376 }
2377 bool IsNaN() const {
2378 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002379 }
2380
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002381 DECLARE_INSTRUCTION(DoubleConstant);
2382
2383 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002384 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002385 explicit HDoubleConstant(int64_t value)
2386 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002387
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002388 const double value_;
2389
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002390 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002391 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002392 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002393 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2394};
2395
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002396class HNullConstant : public HConstant {
2397 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002398 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2399 return true;
2400 }
2401
2402 size_t ComputeHashCode() const OVERRIDE { return 0; }
2403
2404 DECLARE_INSTRUCTION(NullConstant);
2405
2406 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002407 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2408
2409 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002410 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2411};
2412
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002413// Constants of the type int. Those can be from Dex instructions, or
2414// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002415class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002416 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002417 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002418
Calin Juravle61d544b2015-02-23 16:46:57 +00002419 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002420 return other->AsIntConstant()->value_ == value_;
2421 }
2422
Calin Juravle61d544b2015-02-23 16:46:57 +00002423 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2424
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002425 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2426 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2427 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2428
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002429 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002430
2431 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002432 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2433
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002434 const int32_t value_;
2435
David Brazdil8d5b8b22015-03-24 10:51:52 +00002436 friend class HGraph;
2437 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002438 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002439 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2440};
2441
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002442class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002443 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002444 int64_t GetValue() const { return value_; }
2445
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002446 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002447 return other->AsLongConstant()->value_ == value_;
2448 }
2449
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002450 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002451
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002452 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2453 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2454 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2455
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002456 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002457
2458 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002459 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2460
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002461 const int64_t value_;
2462
David Brazdil8d5b8b22015-03-24 10:51:52 +00002463 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002464 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2465};
2466
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002467enum class Intrinsics {
2468#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2469#include "intrinsics_list.h"
2470 kNone,
2471 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2472#undef INTRINSICS_LIST
2473#undef OPTIMIZING_INTRINSICS
2474};
2475std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2476
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002477class HInvoke : public HInstruction {
2478 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002479 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002480
2481 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2482 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002483 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002484
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002485 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002486 SetRawInputAt(index, argument);
2487 }
2488
Roland Levillain3e3d7332015-04-28 11:00:54 +01002489 // Return the number of arguments. This number can be lower than
2490 // the number of inputs returned by InputCount(), as some invoke
2491 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2492 // inputs at the end of their list of inputs.
2493 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2494
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002495 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002496
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002497 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002498
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002499 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002500 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002501
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002502 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2503
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002504 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002505 return intrinsic_;
2506 }
2507
2508 void SetIntrinsic(Intrinsics intrinsic) {
2509 intrinsic_ = intrinsic;
2510 }
2511
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002512 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002513 return GetEnvironment()->GetParent() != nullptr;
2514 }
2515
2516 bool CanThrow() const OVERRIDE { return true; }
2517
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002518 DECLARE_INSTRUCTION(Invoke);
2519
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002520 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002521 HInvoke(ArenaAllocator* arena,
2522 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002523 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002524 Primitive::Type return_type,
2525 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002526 uint32_t dex_method_index,
2527 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002528 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002529 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002530 inputs_(arena, number_of_arguments),
2531 return_type_(return_type),
2532 dex_pc_(dex_pc),
2533 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002534 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002535 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002536 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2537 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002538 }
2539
David Brazdil1abb4192015-02-17 18:33:36 +00002540 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2541 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2542 inputs_.Put(index, input);
2543 }
2544
Roland Levillain3e3d7332015-04-28 11:00:54 +01002545 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002546 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002547 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002548 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002549 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002550 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002551 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002552
2553 private:
2554 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2555};
2556
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002557class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002558 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002559 // Requirements of this method call regarding the class
2560 // initialization (clinit) check of its declaring class.
2561 enum class ClinitCheckRequirement {
2562 kNone, // Class already initialized.
2563 kExplicit, // Static call having explicit clinit check as last input.
2564 kImplicit, // Static call implicitly requiring a clinit check.
2565 };
2566
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002567 HInvokeStaticOrDirect(ArenaAllocator* arena,
2568 uint32_t number_of_arguments,
2569 Primitive::Type return_type,
2570 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002571 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002572 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002573 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002574 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002575 InvokeType invoke_type,
2576 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002577 : HInvoke(arena,
2578 number_of_arguments,
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002579 // There is one extra argument for the HCurrentMethod node, and
2580 // potentially one other if the clinit check is explicit.
2581 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 2u : 1u,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002582 return_type,
2583 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002584 dex_method_index,
2585 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002586 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002587 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002588 clinit_check_requirement_(clinit_check_requirement),
2589 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002590
Calin Juravle641547a2015-04-21 22:08:51 +01002591 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2592 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002593 // We access the method via the dex cache so we can't do an implicit null check.
2594 // TODO: for intrinsics we can generate implicit null checks.
2595 return false;
2596 }
2597
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002598 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002599 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002600 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002601 bool IsStringInit() const { return string_init_offset_ != 0; }
2602 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002603 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002604
Roland Levillain4c0eb422015-04-24 16:43:49 +01002605 // Is this instruction a call to a static method?
2606 bool IsStatic() const {
2607 return GetInvokeType() == kStatic;
2608 }
2609
Roland Levillain3e3d7332015-04-28 11:00:54 +01002610 // Remove the art::HLoadClass instruction set as last input by
2611 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2612 // the initial art::HClinitCheck instruction (only relevant for
2613 // static calls with explicit clinit check).
2614 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002615 DCHECK(IsStaticWithExplicitClinitCheck());
2616 size_t last_input_index = InputCount() - 1;
2617 HInstruction* last_input = InputAt(last_input_index);
2618 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002619 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002620 RemoveAsUserOfInput(last_input_index);
2621 inputs_.DeleteAt(last_input_index);
2622 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2623 DCHECK(IsStaticWithImplicitClinitCheck());
2624 }
2625
2626 // Is this a call to a static method whose declaring class has an
2627 // explicit intialization check in the graph?
2628 bool IsStaticWithExplicitClinitCheck() const {
2629 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2630 }
2631
2632 // Is this a call to a static method whose declaring class has an
2633 // implicit intialization check requirement?
2634 bool IsStaticWithImplicitClinitCheck() const {
2635 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2636 }
2637
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002638 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002639
Roland Levillain4c0eb422015-04-24 16:43:49 +01002640 protected:
2641 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2642 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2643 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2644 HInstruction* input = input_record.GetInstruction();
2645 // `input` is the last input of a static invoke marked as having
2646 // an explicit clinit check. It must either be:
2647 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2648 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2649 DCHECK(input != nullptr);
2650 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2651 }
2652 return input_record;
2653 }
2654
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002655 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002656 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002657 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002658 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002659 // Thread entrypoint offset for string init method if this is a string init invoke.
2660 // Note that there are multiple string init methods, each having its own offset.
2661 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002662
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002663 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002664};
2665
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002666class HInvokeVirtual : public HInvoke {
2667 public:
2668 HInvokeVirtual(ArenaAllocator* arena,
2669 uint32_t number_of_arguments,
2670 Primitive::Type return_type,
2671 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002672 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002673 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002674 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002675 vtable_index_(vtable_index) {}
2676
Calin Juravle641547a2015-04-21 22:08:51 +01002677 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002678 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002679 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002680 }
2681
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002682 uint32_t GetVTableIndex() const { return vtable_index_; }
2683
2684 DECLARE_INSTRUCTION(InvokeVirtual);
2685
2686 private:
2687 const uint32_t vtable_index_;
2688
2689 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2690};
2691
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002692class HInvokeInterface : public HInvoke {
2693 public:
2694 HInvokeInterface(ArenaAllocator* arena,
2695 uint32_t number_of_arguments,
2696 Primitive::Type return_type,
2697 uint32_t dex_pc,
2698 uint32_t dex_method_index,
2699 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002700 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002701 imt_index_(imt_index) {}
2702
Calin Juravle641547a2015-04-21 22:08:51 +01002703 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002704 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002705 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002706 }
2707
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002708 uint32_t GetImtIndex() const { return imt_index_; }
2709 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2710
2711 DECLARE_INSTRUCTION(InvokeInterface);
2712
2713 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002714 const uint32_t imt_index_;
2715
2716 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2717};
2718
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002719class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002720 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002721 HNewInstance(HCurrentMethod* current_method,
2722 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002723 uint16_t type_index,
2724 const DexFile& dex_file,
2725 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002726 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2727 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002728 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002729 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002730 entrypoint_(entrypoint) {
2731 SetRawInputAt(0, current_method);
2732 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002733
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002734 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002735 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002736 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002737
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002738 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002739 bool NeedsEnvironment() const OVERRIDE { return true; }
2740 // It may throw when called on:
2741 // - interfaces
2742 // - abstract/innaccessible/unknown classes
2743 // TODO: optimize when possible.
2744 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002745
Calin Juravle10e244f2015-01-26 18:54:32 +00002746 bool CanBeNull() const OVERRIDE { return false; }
2747
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002748 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2749
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002750 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002751
2752 private:
2753 const uint32_t dex_pc_;
2754 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002755 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002756 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002757
2758 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2759};
2760
Roland Levillain88cb1752014-10-20 16:36:47 +01002761class HNeg : public HUnaryOperation {
2762 public:
2763 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2764 : HUnaryOperation(result_type, input) {}
2765
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002766 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2767 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002768
Roland Levillain88cb1752014-10-20 16:36:47 +01002769 DECLARE_INSTRUCTION(Neg);
2770
2771 private:
2772 DISALLOW_COPY_AND_ASSIGN(HNeg);
2773};
2774
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002775class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002776 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002777 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002778 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002779 uint32_t dex_pc,
2780 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002781 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002782 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002783 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2784 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002785 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002786 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002787 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002788 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002789 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002790 }
2791
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002792 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002793 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002794 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002795
2796 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002797 bool NeedsEnvironment() const OVERRIDE { return true; }
2798
Mingyao Yang0c365e62015-03-31 15:09:29 -07002799 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2800 bool CanThrow() const OVERRIDE { return true; }
2801
Calin Juravle10e244f2015-01-26 18:54:32 +00002802 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002803
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002804 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2805
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002806 DECLARE_INSTRUCTION(NewArray);
2807
2808 private:
2809 const uint32_t dex_pc_;
2810 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002811 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002812 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002813
2814 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2815};
2816
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002817class HAdd : public HBinaryOperation {
2818 public:
2819 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2820 : HBinaryOperation(result_type, left, right) {}
2821
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002822 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002823
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002824 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002825 return x + y;
2826 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002827 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002828 return x + y;
2829 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002830
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002831 DECLARE_INSTRUCTION(Add);
2832
2833 private:
2834 DISALLOW_COPY_AND_ASSIGN(HAdd);
2835};
2836
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002837class HSub : public HBinaryOperation {
2838 public:
2839 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2840 : HBinaryOperation(result_type, left, right) {}
2841
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002842 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002843 return x - y;
2844 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002845 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002846 return x - y;
2847 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002848
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002849 DECLARE_INSTRUCTION(Sub);
2850
2851 private:
2852 DISALLOW_COPY_AND_ASSIGN(HSub);
2853};
2854
Calin Juravle34bacdf2014-10-07 20:23:36 +01002855class HMul : public HBinaryOperation {
2856 public:
2857 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2858 : HBinaryOperation(result_type, left, right) {}
2859
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002860 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002861
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002862 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2863 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002864
2865 DECLARE_INSTRUCTION(Mul);
2866
2867 private:
2868 DISALLOW_COPY_AND_ASSIGN(HMul);
2869};
2870
Calin Juravle7c4954d2014-10-28 16:57:40 +00002871class HDiv : public HBinaryOperation {
2872 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002873 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2874 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002875
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002876 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002877 // Our graph structure ensures we never have 0 for `y` during constant folding.
2878 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002879 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002880 return (y == -1) ? -x : x / y;
2881 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002882
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002883 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002884 DCHECK_NE(y, 0);
2885 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2886 return (y == -1) ? -x : x / y;
2887 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002888
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002889 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002890
Calin Juravle7c4954d2014-10-28 16:57:40 +00002891 DECLARE_INSTRUCTION(Div);
2892
2893 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002894 const uint32_t dex_pc_;
2895
Calin Juravle7c4954d2014-10-28 16:57:40 +00002896 DISALLOW_COPY_AND_ASSIGN(HDiv);
2897};
2898
Calin Juravlebacfec32014-11-14 15:54:36 +00002899class HRem : public HBinaryOperation {
2900 public:
2901 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2902 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2903
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002904 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002905 DCHECK_NE(y, 0);
2906 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2907 return (y == -1) ? 0 : x % y;
2908 }
2909
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002910 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002911 DCHECK_NE(y, 0);
2912 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2913 return (y == -1) ? 0 : x % y;
2914 }
2915
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002916 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002917
2918 DECLARE_INSTRUCTION(Rem);
2919
2920 private:
2921 const uint32_t dex_pc_;
2922
2923 DISALLOW_COPY_AND_ASSIGN(HRem);
2924};
2925
Calin Juravled0d48522014-11-04 16:40:20 +00002926class HDivZeroCheck : public HExpression<1> {
2927 public:
2928 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2929 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2930 SetRawInputAt(0, value);
2931 }
2932
2933 bool CanBeMoved() const OVERRIDE { return true; }
2934
2935 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2936 UNUSED(other);
2937 return true;
2938 }
2939
2940 bool NeedsEnvironment() const OVERRIDE { return true; }
2941 bool CanThrow() const OVERRIDE { return true; }
2942
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002943 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002944
2945 DECLARE_INSTRUCTION(DivZeroCheck);
2946
2947 private:
2948 const uint32_t dex_pc_;
2949
2950 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2951};
2952
Calin Juravle9aec02f2014-11-18 23:06:35 +00002953class HShl : public HBinaryOperation {
2954 public:
2955 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2956 : HBinaryOperation(result_type, left, right) {}
2957
2958 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2959 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2960
2961 DECLARE_INSTRUCTION(Shl);
2962
2963 private:
2964 DISALLOW_COPY_AND_ASSIGN(HShl);
2965};
2966
2967class HShr : public HBinaryOperation {
2968 public:
2969 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2970 : HBinaryOperation(result_type, left, right) {}
2971
2972 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2973 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2974
2975 DECLARE_INSTRUCTION(Shr);
2976
2977 private:
2978 DISALLOW_COPY_AND_ASSIGN(HShr);
2979};
2980
2981class HUShr : public HBinaryOperation {
2982 public:
2983 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2984 : HBinaryOperation(result_type, left, right) {}
2985
2986 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2987 uint32_t ux = static_cast<uint32_t>(x);
2988 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2989 return static_cast<int32_t>(ux >> uy);
2990 }
2991
2992 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2993 uint64_t ux = static_cast<uint64_t>(x);
2994 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2995 return static_cast<int64_t>(ux >> uy);
2996 }
2997
2998 DECLARE_INSTRUCTION(UShr);
2999
3000 private:
3001 DISALLOW_COPY_AND_ASSIGN(HUShr);
3002};
3003
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003004class HAnd : public HBinaryOperation {
3005 public:
3006 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3007 : HBinaryOperation(result_type, left, right) {}
3008
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003009 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003010
3011 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3012 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3013
3014 DECLARE_INSTRUCTION(And);
3015
3016 private:
3017 DISALLOW_COPY_AND_ASSIGN(HAnd);
3018};
3019
3020class HOr : public HBinaryOperation {
3021 public:
3022 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3023 : HBinaryOperation(result_type, left, right) {}
3024
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003025 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003026
3027 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3028 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3029
3030 DECLARE_INSTRUCTION(Or);
3031
3032 private:
3033 DISALLOW_COPY_AND_ASSIGN(HOr);
3034};
3035
3036class HXor : public HBinaryOperation {
3037 public:
3038 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3039 : HBinaryOperation(result_type, left, right) {}
3040
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003041 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003042
3043 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3044 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3045
3046 DECLARE_INSTRUCTION(Xor);
3047
3048 private:
3049 DISALLOW_COPY_AND_ASSIGN(HXor);
3050};
3051
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003052// The value of a parameter in this method. Its location depends on
3053// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003054class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003055 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003056 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3057 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003058
3059 uint8_t GetIndex() const { return index_; }
3060
Calin Juravle10e244f2015-01-26 18:54:32 +00003061 bool CanBeNull() const OVERRIDE { return !is_this_; }
3062
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003063 bool IsThis() const { return is_this_; }
3064
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003065 DECLARE_INSTRUCTION(ParameterValue);
3066
3067 private:
3068 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003069 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003070 const uint8_t index_;
3071
Calin Juravle10e244f2015-01-26 18:54:32 +00003072 // Whether or not the parameter value corresponds to 'this' argument.
3073 const bool is_this_;
3074
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003075 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3076};
3077
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003078class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003079 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003080 explicit HNot(Primitive::Type result_type, HInstruction* input)
3081 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003082
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003083 bool CanBeMoved() const OVERRIDE { return true; }
3084 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003085 UNUSED(other);
3086 return true;
3087 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003088
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003089 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3090 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003091
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003092 DECLARE_INSTRUCTION(Not);
3093
3094 private:
3095 DISALLOW_COPY_AND_ASSIGN(HNot);
3096};
3097
David Brazdil66d126e2015-04-03 16:02:44 +01003098class HBooleanNot : public HUnaryOperation {
3099 public:
3100 explicit HBooleanNot(HInstruction* input)
3101 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3102
3103 bool CanBeMoved() const OVERRIDE { return true; }
3104 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3105 UNUSED(other);
3106 return true;
3107 }
3108
3109 int32_t Evaluate(int32_t x) const OVERRIDE {
3110 DCHECK(IsUint<1>(x));
3111 return !x;
3112 }
3113
3114 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3115 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3116 UNREACHABLE();
3117 }
3118
3119 DECLARE_INSTRUCTION(BooleanNot);
3120
3121 private:
3122 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3123};
3124
Roland Levillaindff1f282014-11-05 14:15:05 +00003125class HTypeConversion : public HExpression<1> {
3126 public:
3127 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003128 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3129 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003130 SetRawInputAt(0, input);
3131 DCHECK_NE(input->GetType(), result_type);
3132 }
3133
3134 HInstruction* GetInput() const { return InputAt(0); }
3135 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3136 Primitive::Type GetResultType() const { return GetType(); }
3137
Roland Levillain624279f2014-12-04 11:54:28 +00003138 // Required by the x86 and ARM code generators when producing calls
3139 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003140 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003141
Roland Levillaindff1f282014-11-05 14:15:05 +00003142 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003143 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003144
Mark Mendelle82549b2015-05-06 10:55:34 -04003145 // Try to statically evaluate the conversion and return a HConstant
3146 // containing the result. If the input cannot be converted, return nullptr.
3147 HConstant* TryStaticEvaluation() const;
3148
Roland Levillaindff1f282014-11-05 14:15:05 +00003149 DECLARE_INSTRUCTION(TypeConversion);
3150
3151 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003152 const uint32_t dex_pc_;
3153
Roland Levillaindff1f282014-11-05 14:15:05 +00003154 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3155};
3156
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003157static constexpr uint32_t kNoRegNumber = -1;
3158
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003159class HPhi : public HInstruction {
3160 public:
3161 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003162 : HInstruction(SideEffects::None()),
3163 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003164 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003165 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003166 is_live_(false),
3167 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003168 inputs_.SetSize(number_of_inputs);
3169 }
3170
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003171 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3172 static Primitive::Type ToPhiType(Primitive::Type type) {
3173 switch (type) {
3174 case Primitive::kPrimBoolean:
3175 case Primitive::kPrimByte:
3176 case Primitive::kPrimShort:
3177 case Primitive::kPrimChar:
3178 return Primitive::kPrimInt;
3179 default:
3180 return type;
3181 }
3182 }
3183
Calin Juravle10e244f2015-01-26 18:54:32 +00003184 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003185
3186 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003187 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003188
Calin Juravle10e244f2015-01-26 18:54:32 +00003189 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003190 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003191
Calin Juravle10e244f2015-01-26 18:54:32 +00003192 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3193 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3194
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003195 uint32_t GetRegNumber() const { return reg_number_; }
3196
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003197 void SetDead() { is_live_ = false; }
3198 void SetLive() { is_live_ = true; }
3199 bool IsDead() const { return !is_live_; }
3200 bool IsLive() const { return is_live_; }
3201
Calin Juravlea4f88312015-04-16 12:57:19 +01003202 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3203 // An equivalent phi is a phi having the same dex register and type.
3204 // It assumes that phis with the same dex register are adjacent.
3205 HPhi* GetNextEquivalentPhiWithSameType() {
3206 HInstruction* next = GetNext();
3207 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3208 if (next->GetType() == GetType()) {
3209 return next->AsPhi();
3210 }
3211 next = next->GetNext();
3212 }
3213 return nullptr;
3214 }
3215
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003216 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003217
David Brazdil1abb4192015-02-17 18:33:36 +00003218 protected:
3219 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3220
3221 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3222 inputs_.Put(index, input);
3223 }
3224
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003225 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003226 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003227 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003228 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003229 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003230 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003231
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003232 DISALLOW_COPY_AND_ASSIGN(HPhi);
3233};
3234
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003235class HNullCheck : public HExpression<1> {
3236 public:
3237 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003238 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003239 SetRawInputAt(0, value);
3240 }
3241
Calin Juravle10e244f2015-01-26 18:54:32 +00003242 bool CanBeMoved() const OVERRIDE { return true; }
3243 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003244 UNUSED(other);
3245 return true;
3246 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003247
Calin Juravle10e244f2015-01-26 18:54:32 +00003248 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003249
Calin Juravle10e244f2015-01-26 18:54:32 +00003250 bool CanThrow() const OVERRIDE { return true; }
3251
3252 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003253
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003254 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003255
3256 DECLARE_INSTRUCTION(NullCheck);
3257
3258 private:
3259 const uint32_t dex_pc_;
3260
3261 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3262};
3263
3264class FieldInfo : public ValueObject {
3265 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003266 FieldInfo(MemberOffset field_offset,
3267 Primitive::Type field_type,
3268 bool is_volatile,
3269 uint32_t index,
3270 const DexFile& dex_file)
3271 : field_offset_(field_offset),
3272 field_type_(field_type),
3273 is_volatile_(is_volatile),
3274 index_(index),
3275 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003276
3277 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003278 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003279 uint32_t GetFieldIndex() const { return index_; }
3280 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003281 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003282
3283 private:
3284 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003285 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003286 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003287 uint32_t index_;
3288 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003289};
3290
3291class HInstanceFieldGet : public HExpression<1> {
3292 public:
3293 HInstanceFieldGet(HInstruction* value,
3294 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003295 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003296 bool is_volatile,
3297 uint32_t field_idx,
3298 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003299 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003300 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003301 SetRawInputAt(0, value);
3302 }
3303
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003304 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003305
3306 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3307 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3308 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003309 }
3310
Calin Juravle641547a2015-04-21 22:08:51 +01003311 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3312 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003313 }
3314
3315 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003316 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3317 }
3318
Calin Juravle52c48962014-12-16 17:02:57 +00003319 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003320 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003321 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003322 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003323
3324 DECLARE_INSTRUCTION(InstanceFieldGet);
3325
3326 private:
3327 const FieldInfo field_info_;
3328
3329 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3330};
3331
3332class HInstanceFieldSet : public HTemplateInstruction<2> {
3333 public:
3334 HInstanceFieldSet(HInstruction* object,
3335 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003336 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003337 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003338 bool is_volatile,
3339 uint32_t field_idx,
3340 const DexFile& dex_file)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003341 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003342 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003343 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003344 SetRawInputAt(0, object);
3345 SetRawInputAt(1, value);
3346 }
3347
Calin Juravle641547a2015-04-21 22:08:51 +01003348 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3349 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003350 }
3351
Calin Juravle52c48962014-12-16 17:02:57 +00003352 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003353 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003354 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003355 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003356 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003357 bool GetValueCanBeNull() const { return value_can_be_null_; }
3358 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003359
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003360 DECLARE_INSTRUCTION(InstanceFieldSet);
3361
3362 private:
3363 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003364 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003365
3366 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3367};
3368
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003369class HArrayGet : public HExpression<2> {
3370 public:
3371 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003372 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003373 SetRawInputAt(0, array);
3374 SetRawInputAt(1, index);
3375 }
3376
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003377 bool CanBeMoved() const OVERRIDE { return true; }
3378 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003379 UNUSED(other);
3380 return true;
3381 }
Calin Juravle641547a2015-04-21 22:08:51 +01003382 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3383 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003384 // TODO: We can be smarter here.
3385 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3386 // which generates the implicit null check. There are cases when these can be removed
3387 // to produce better code. If we ever add optimizations to do so we should allow an
3388 // implicit check here (as long as the address falls in the first page).
3389 return false;
3390 }
3391
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003392 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003393
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003394 HInstruction* GetArray() const { return InputAt(0); }
3395 HInstruction* GetIndex() const { return InputAt(1); }
3396
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003397 DECLARE_INSTRUCTION(ArrayGet);
3398
3399 private:
3400 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3401};
3402
3403class HArraySet : public HTemplateInstruction<3> {
3404 public:
3405 HArraySet(HInstruction* array,
3406 HInstruction* index,
3407 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003408 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003409 uint32_t dex_pc)
3410 : HTemplateInstruction(SideEffects::ChangesSomething()),
3411 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003412 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003413 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3414 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003415 SetRawInputAt(0, array);
3416 SetRawInputAt(1, index);
3417 SetRawInputAt(2, value);
3418 }
3419
Calin Juravle77520bc2015-01-12 18:45:46 +00003420 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003421 // We currently always call a runtime method to catch array store
3422 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003423 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003424 }
3425
Mingyao Yang81014cb2015-06-02 03:16:27 -07003426 // Can throw ArrayStoreException.
3427 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3428
Calin Juravle641547a2015-04-21 22:08:51 +01003429 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3430 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003431 // TODO: Same as for ArrayGet.
3432 return false;
3433 }
3434
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003435 void ClearNeedsTypeCheck() {
3436 needs_type_check_ = false;
3437 }
3438
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003439 void ClearValueCanBeNull() {
3440 value_can_be_null_ = false;
3441 }
3442
3443 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003444 bool NeedsTypeCheck() const { return needs_type_check_; }
3445
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003446 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003447
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003448 HInstruction* GetArray() const { return InputAt(0); }
3449 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003450 HInstruction* GetValue() const { return InputAt(2); }
3451
3452 Primitive::Type GetComponentType() const {
3453 // The Dex format does not type floating point index operations. Since the
3454 // `expected_component_type_` is set during building and can therefore not
3455 // be correct, we also check what is the value type. If it is a floating
3456 // point type, we must use that type.
3457 Primitive::Type value_type = GetValue()->GetType();
3458 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3459 ? value_type
3460 : expected_component_type_;
3461 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003462
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003463 DECLARE_INSTRUCTION(ArraySet);
3464
3465 private:
3466 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003467 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003468 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003469 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003470
3471 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3472};
3473
3474class HArrayLength : public HExpression<1> {
3475 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003476 explicit HArrayLength(HInstruction* array)
3477 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3478 // Note that arrays do not change length, so the instruction does not
3479 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003480 SetRawInputAt(0, array);
3481 }
3482
Calin Juravle77520bc2015-01-12 18:45:46 +00003483 bool CanBeMoved() const OVERRIDE { return true; }
3484 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003485 UNUSED(other);
3486 return true;
3487 }
Calin Juravle641547a2015-04-21 22:08:51 +01003488 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3489 return obj == InputAt(0);
3490 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003491
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003492 DECLARE_INSTRUCTION(ArrayLength);
3493
3494 private:
3495 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3496};
3497
3498class HBoundsCheck : public HExpression<2> {
3499 public:
3500 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003501 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003502 DCHECK(index->GetType() == Primitive::kPrimInt);
3503 SetRawInputAt(0, index);
3504 SetRawInputAt(1, length);
3505 }
3506
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003507 bool CanBeMoved() const OVERRIDE { return true; }
3508 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003509 UNUSED(other);
3510 return true;
3511 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003512
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003513 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003514
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003515 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003516
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003517 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003518
3519 DECLARE_INSTRUCTION(BoundsCheck);
3520
3521 private:
3522 const uint32_t dex_pc_;
3523
3524 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3525};
3526
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003527/**
3528 * Some DEX instructions are folded into multiple HInstructions that need
3529 * to stay live until the last HInstruction. This class
3530 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003531 * HInstruction stays live. `index` represents the stack location index of the
3532 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003533 */
3534class HTemporary : public HTemplateInstruction<0> {
3535 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003536 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003537
3538 size_t GetIndex() const { return index_; }
3539
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003540 Primitive::Type GetType() const OVERRIDE {
3541 // The previous instruction is the one that will be stored in the temporary location.
3542 DCHECK(GetPrevious() != nullptr);
3543 return GetPrevious()->GetType();
3544 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003545
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003546 DECLARE_INSTRUCTION(Temporary);
3547
3548 private:
3549 const size_t index_;
3550
3551 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3552};
3553
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003554class HSuspendCheck : public HTemplateInstruction<0> {
3555 public:
3556 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003557 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003558
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003559 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003560 return true;
3561 }
3562
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003563 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003564 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3565 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003566
3567 DECLARE_INSTRUCTION(SuspendCheck);
3568
3569 private:
3570 const uint32_t dex_pc_;
3571
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003572 // Only used for code generation, in order to share the same slow path between back edges
3573 // of a same loop.
3574 SlowPathCode* slow_path_;
3575
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003576 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3577};
3578
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003579/**
3580 * Instruction to load a Class object.
3581 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003582class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003583 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003584 HLoadClass(HCurrentMethod* current_method,
3585 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003586 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003587 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003588 uint32_t dex_pc)
3589 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3590 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003591 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003592 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003593 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003594 generate_clinit_check_(false),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003595 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
3596 SetRawInputAt(0, current_method);
3597 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003598
3599 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003600
3601 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3602 return other->AsLoadClass()->type_index_ == type_index_;
3603 }
3604
3605 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3606
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003607 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003608 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003609 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003610
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003611 bool NeedsEnvironment() const OVERRIDE {
3612 // Will call runtime and load the class if the class is not loaded yet.
3613 // TODO: finer grain decision.
3614 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003615 }
3616
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003617 bool MustGenerateClinitCheck() const {
3618 return generate_clinit_check_;
3619 }
3620
Calin Juravle0ba218d2015-05-19 18:46:01 +01003621 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3622 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003623 }
3624
3625 bool CanCallRuntime() const {
3626 return MustGenerateClinitCheck() || !is_referrers_class_;
3627 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003628
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003629 bool CanThrow() const OVERRIDE {
3630 // May call runtime and and therefore can throw.
3631 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003632 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003633 }
3634
Calin Juravleacf735c2015-02-12 15:25:22 +00003635 ReferenceTypeInfo GetLoadedClassRTI() {
3636 return loaded_class_rti_;
3637 }
3638
3639 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3640 // Make sure we only set exact types (the loaded class should never be merged).
3641 DCHECK(rti.IsExact());
3642 loaded_class_rti_ = rti;
3643 }
3644
3645 bool IsResolved() {
3646 return loaded_class_rti_.IsExact();
3647 }
3648
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003649 const DexFile& GetDexFile() { return dex_file_; }
3650
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003651 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3652
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003653 DECLARE_INSTRUCTION(LoadClass);
3654
3655 private:
3656 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003657 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003658 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003659 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003660 // Whether this instruction must generate the initialization check.
3661 // Used for code generation.
3662 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003663
Calin Juravleacf735c2015-02-12 15:25:22 +00003664 ReferenceTypeInfo loaded_class_rti_;
3665
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003666 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3667};
3668
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003669class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003670 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003671 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003672 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3673 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003674 dex_pc_(dex_pc) {
3675 SetRawInputAt(0, current_method);
3676 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003677
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003678 bool CanBeMoved() const OVERRIDE { return true; }
3679
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003680 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3681 return other->AsLoadString()->string_index_ == string_index_;
3682 }
3683
3684 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3685
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003686 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003687 uint32_t GetStringIndex() const { return string_index_; }
3688
3689 // TODO: Can we deopt or debug when we resolve a string?
3690 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003691 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003692
3693 DECLARE_INSTRUCTION(LoadString);
3694
3695 private:
3696 const uint32_t string_index_;
3697 const uint32_t dex_pc_;
3698
3699 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3700};
3701
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003702/**
3703 * Performs an initialization check on its Class object input.
3704 */
3705class HClinitCheck : public HExpression<1> {
3706 public:
3707 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003708 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003709 dex_pc_(dex_pc) {
3710 SetRawInputAt(0, constant);
3711 }
3712
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003713 bool CanBeMoved() const OVERRIDE { return true; }
3714 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3715 UNUSED(other);
3716 return true;
3717 }
3718
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003719 bool NeedsEnvironment() const OVERRIDE {
3720 // May call runtime to initialize the class.
3721 return true;
3722 }
3723
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003724 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003725
3726 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3727
3728 DECLARE_INSTRUCTION(ClinitCheck);
3729
3730 private:
3731 const uint32_t dex_pc_;
3732
3733 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3734};
3735
3736class HStaticFieldGet : public HExpression<1> {
3737 public:
3738 HStaticFieldGet(HInstruction* cls,
3739 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003740 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003741 bool is_volatile,
3742 uint32_t field_idx,
3743 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003744 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003745 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003746 SetRawInputAt(0, cls);
3747 }
3748
Calin Juravle52c48962014-12-16 17:02:57 +00003749
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003750 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003751
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003752 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003753 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3754 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003755 }
3756
3757 size_t ComputeHashCode() const OVERRIDE {
3758 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3759 }
3760
Calin Juravle52c48962014-12-16 17:02:57 +00003761 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003762 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3763 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003764 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003765
3766 DECLARE_INSTRUCTION(StaticFieldGet);
3767
3768 private:
3769 const FieldInfo field_info_;
3770
3771 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3772};
3773
3774class HStaticFieldSet : public HTemplateInstruction<2> {
3775 public:
3776 HStaticFieldSet(HInstruction* cls,
3777 HInstruction* value,
3778 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003779 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003780 bool is_volatile,
3781 uint32_t field_idx,
3782 const DexFile& dex_file)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003783 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003784 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003785 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003786 SetRawInputAt(0, cls);
3787 SetRawInputAt(1, value);
3788 }
3789
Calin Juravle52c48962014-12-16 17:02:57 +00003790 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003791 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3792 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003793 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003794
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003795 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003796 bool GetValueCanBeNull() const { return value_can_be_null_; }
3797 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003798
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003799 DECLARE_INSTRUCTION(StaticFieldSet);
3800
3801 private:
3802 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003803 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003804
3805 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3806};
3807
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003808// Implement the move-exception DEX instruction.
3809class HLoadException : public HExpression<0> {
3810 public:
3811 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3812
3813 DECLARE_INSTRUCTION(LoadException);
3814
3815 private:
3816 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3817};
3818
3819class HThrow : public HTemplateInstruction<1> {
3820 public:
3821 HThrow(HInstruction* exception, uint32_t dex_pc)
3822 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3823 SetRawInputAt(0, exception);
3824 }
3825
3826 bool IsControlFlow() const OVERRIDE { return true; }
3827
3828 bool NeedsEnvironment() const OVERRIDE { return true; }
3829
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003830 bool CanThrow() const OVERRIDE { return true; }
3831
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003832 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003833
3834 DECLARE_INSTRUCTION(Throw);
3835
3836 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003837 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003838
3839 DISALLOW_COPY_AND_ASSIGN(HThrow);
3840};
3841
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003842class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003843 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003844 HInstanceOf(HInstruction* object,
3845 HLoadClass* constant,
3846 bool class_is_final,
3847 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003848 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3849 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003850 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003851 dex_pc_(dex_pc) {
3852 SetRawInputAt(0, object);
3853 SetRawInputAt(1, constant);
3854 }
3855
3856 bool CanBeMoved() const OVERRIDE { return true; }
3857
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003858 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003859 return true;
3860 }
3861
3862 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003863 return false;
3864 }
3865
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003866 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003867
3868 bool IsClassFinal() const { return class_is_final_; }
3869
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003870 // Used only in code generation.
3871 bool MustDoNullCheck() const { return must_do_null_check_; }
3872 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3873
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003874 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003875
3876 private:
3877 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003878 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003879 const uint32_t dex_pc_;
3880
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003881 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3882};
3883
Calin Juravleb1498f62015-02-16 13:13:29 +00003884class HBoundType : public HExpression<1> {
3885 public:
3886 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3887 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3888 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003889 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003890 SetRawInputAt(0, input);
3891 }
3892
3893 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3894
3895 bool CanBeNull() const OVERRIDE {
3896 // `null instanceof ClassX` always return false so we can't be null.
3897 return false;
3898 }
3899
3900 DECLARE_INSTRUCTION(BoundType);
3901
3902 private:
3903 // Encodes the most upper class that this instruction can have. In other words
3904 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3905 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3906 const ReferenceTypeInfo bound_type_;
3907
3908 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3909};
3910
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003911class HCheckCast : public HTemplateInstruction<2> {
3912 public:
3913 HCheckCast(HInstruction* object,
3914 HLoadClass* constant,
3915 bool class_is_final,
3916 uint32_t dex_pc)
3917 : HTemplateInstruction(SideEffects::None()),
3918 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003919 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003920 dex_pc_(dex_pc) {
3921 SetRawInputAt(0, object);
3922 SetRawInputAt(1, constant);
3923 }
3924
3925 bool CanBeMoved() const OVERRIDE { return true; }
3926
3927 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3928 return true;
3929 }
3930
3931 bool NeedsEnvironment() const OVERRIDE {
3932 // Instruction may throw a CheckCastError.
3933 return true;
3934 }
3935
3936 bool CanThrow() const OVERRIDE { return true; }
3937
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003938 bool MustDoNullCheck() const { return must_do_null_check_; }
3939 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3940
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003941 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003942
3943 bool IsClassFinal() const { return class_is_final_; }
3944
3945 DECLARE_INSTRUCTION(CheckCast);
3946
3947 private:
3948 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003949 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003950 const uint32_t dex_pc_;
3951
3952 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003953};
3954
Calin Juravle27df7582015-04-17 19:12:31 +01003955class HMemoryBarrier : public HTemplateInstruction<0> {
3956 public:
3957 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3958 : HTemplateInstruction(SideEffects::None()),
3959 barrier_kind_(barrier_kind) {}
3960
3961 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3962
3963 DECLARE_INSTRUCTION(MemoryBarrier);
3964
3965 private:
3966 const MemBarrierKind barrier_kind_;
3967
3968 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3969};
3970
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003971class HMonitorOperation : public HTemplateInstruction<1> {
3972 public:
3973 enum OperationKind {
3974 kEnter,
3975 kExit,
3976 };
3977
3978 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3979 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3980 SetRawInputAt(0, object);
3981 }
3982
3983 // Instruction may throw a Java exception, so we need an environment.
3984 bool NeedsEnvironment() const OVERRIDE { return true; }
3985 bool CanThrow() const OVERRIDE { return true; }
3986
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003987 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003988
3989 bool IsEnter() const { return kind_ == kEnter; }
3990
3991 DECLARE_INSTRUCTION(MonitorOperation);
3992
Calin Juravle52c48962014-12-16 17:02:57 +00003993 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003994 const OperationKind kind_;
3995 const uint32_t dex_pc_;
3996
3997 private:
3998 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3999};
4000
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004001class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004002 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004003 MoveOperands(Location source,
4004 Location destination,
4005 Primitive::Type type,
4006 HInstruction* instruction)
4007 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004008
4009 Location GetSource() const { return source_; }
4010 Location GetDestination() const { return destination_; }
4011
4012 void SetSource(Location value) { source_ = value; }
4013 void SetDestination(Location value) { destination_ = value; }
4014
4015 // The parallel move resolver marks moves as "in-progress" by clearing the
4016 // destination (but not the source).
4017 Location MarkPending() {
4018 DCHECK(!IsPending());
4019 Location dest = destination_;
4020 destination_ = Location::NoLocation();
4021 return dest;
4022 }
4023
4024 void ClearPending(Location dest) {
4025 DCHECK(IsPending());
4026 destination_ = dest;
4027 }
4028
4029 bool IsPending() const {
4030 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4031 return destination_.IsInvalid() && !source_.IsInvalid();
4032 }
4033
4034 // True if this blocks a move from the given location.
4035 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004036 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004037 }
4038
4039 // A move is redundant if it's been eliminated, if its source and
4040 // destination are the same, or if its destination is unneeded.
4041 bool IsRedundant() const {
4042 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4043 }
4044
4045 // We clear both operands to indicate move that's been eliminated.
4046 void Eliminate() {
4047 source_ = destination_ = Location::NoLocation();
4048 }
4049
4050 bool IsEliminated() const {
4051 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4052 return source_.IsInvalid();
4053 }
4054
Alexey Frunze4dda3372015-06-01 18:31:49 -07004055 Primitive::Type GetType() const { return type_; }
4056
Nicolas Geoffray90218252015-04-15 11:56:51 +01004057 bool Is64BitMove() const {
4058 return Primitive::Is64BitType(type_);
4059 }
4060
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004061 HInstruction* GetInstruction() const { return instruction_; }
4062
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004063 private:
4064 Location source_;
4065 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004066 // The type this move is for.
4067 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004068 // The instruction this move is assocatied with. Null when this move is
4069 // for moving an input in the expected locations of user (including a phi user).
4070 // This is only used in debug mode, to ensure we do not connect interval siblings
4071 // in the same parallel move.
4072 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004073};
4074
4075static constexpr size_t kDefaultNumberOfMoves = 4;
4076
4077class HParallelMove : public HTemplateInstruction<0> {
4078 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004079 explicit HParallelMove(ArenaAllocator* arena)
4080 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004081
Nicolas Geoffray90218252015-04-15 11:56:51 +01004082 void AddMove(Location source,
4083 Location destination,
4084 Primitive::Type type,
4085 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004086 DCHECK(source.IsValid());
4087 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004088 if (kIsDebugBuild) {
4089 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004090 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004091 if (moves_.Get(i).GetInstruction() == instruction) {
4092 // Special case the situation where the move is for the spill slot
4093 // of the instruction.
4094 if ((GetPrevious() == instruction)
4095 || ((GetPrevious() == nullptr)
4096 && instruction->IsPhi()
4097 && instruction->GetBlock() == GetBlock())) {
4098 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4099 << "Doing parallel moves for the same instruction.";
4100 } else {
4101 DCHECK(false) << "Doing parallel moves for the same instruction.";
4102 }
4103 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004104 }
4105 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004106 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004107 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004108 << "Overlapped destination for two moves in a parallel move: "
4109 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4110 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004111 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004112 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004113 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004114 }
4115
4116 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004117 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004118 }
4119
4120 size_t NumMoves() const { return moves_.Size(); }
4121
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004122 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004123
4124 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004125 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004126
4127 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4128};
4129
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004130class HGraphVisitor : public ValueObject {
4131 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004132 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4133 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004134
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004135 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004136 virtual void VisitBasicBlock(HBasicBlock* block);
4137
Roland Levillain633021e2014-10-01 14:12:25 +01004138 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004139 void VisitInsertionOrder();
4140
Roland Levillain633021e2014-10-01 14:12:25 +01004141 // Visit the graph following dominator tree reverse post-order.
4142 void VisitReversePostOrder();
4143
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004144 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004145
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004146 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004147#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004148 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4149
4150 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4151
4152#undef DECLARE_VISIT_INSTRUCTION
4153
4154 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004155 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004156
4157 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4158};
4159
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004160class HGraphDelegateVisitor : public HGraphVisitor {
4161 public:
4162 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4163 virtual ~HGraphDelegateVisitor() {}
4164
4165 // Visit functions that delegate to to super class.
4166#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004167 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004168
4169 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4170
4171#undef DECLARE_VISIT_INSTRUCTION
4172
4173 private:
4174 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4175};
4176
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004177class HInsertionOrderIterator : public ValueObject {
4178 public:
4179 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4180
4181 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4182 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4183 void Advance() { ++index_; }
4184
4185 private:
4186 const HGraph& graph_;
4187 size_t index_;
4188
4189 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4190};
4191
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004192class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004193 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004194 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4195 // Check that reverse post order of the graph has been built.
4196 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4197 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004198
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004199 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4200 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004201 void Advance() { ++index_; }
4202
4203 private:
4204 const HGraph& graph_;
4205 size_t index_;
4206
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004207 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004208};
4209
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004210class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004211 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004212 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004213 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4214 // Check that reverse post order of the graph has been built.
4215 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4216 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004217
4218 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004219 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004220 void Advance() { --index_; }
4221
4222 private:
4223 const HGraph& graph_;
4224 size_t index_;
4225
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004226 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004227};
4228
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004229class HLinearPostOrderIterator : public ValueObject {
4230 public:
4231 explicit HLinearPostOrderIterator(const HGraph& graph)
4232 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4233
4234 bool Done() const { return index_ == 0; }
4235
4236 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4237
4238 void Advance() {
4239 --index_;
4240 DCHECK_GE(index_, 0U);
4241 }
4242
4243 private:
4244 const GrowableArray<HBasicBlock*>& order_;
4245 size_t index_;
4246
4247 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4248};
4249
4250class HLinearOrderIterator : public ValueObject {
4251 public:
4252 explicit HLinearOrderIterator(const HGraph& graph)
4253 : order_(graph.GetLinearOrder()), index_(0) {}
4254
4255 bool Done() const { return index_ == order_.Size(); }
4256 HBasicBlock* Current() const { return order_.Get(index_); }
4257 void Advance() { ++index_; }
4258
4259 private:
4260 const GrowableArray<HBasicBlock*>& order_;
4261 size_t index_;
4262
4263 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4264};
4265
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004266// Iterator over the blocks that art part of the loop. Includes blocks part
4267// of an inner loop. The order in which the blocks are iterated is on their
4268// block id.
4269class HBlocksInLoopIterator : public ValueObject {
4270 public:
4271 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4272 : blocks_in_loop_(info.GetBlocks()),
4273 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4274 index_(0) {
4275 if (!blocks_in_loop_.IsBitSet(index_)) {
4276 Advance();
4277 }
4278 }
4279
4280 bool Done() const { return index_ == blocks_.Size(); }
4281 HBasicBlock* Current() const { return blocks_.Get(index_); }
4282 void Advance() {
4283 ++index_;
4284 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4285 if (blocks_in_loop_.IsBitSet(index_)) {
4286 break;
4287 }
4288 }
4289 }
4290
4291 private:
4292 const BitVector& blocks_in_loop_;
4293 const GrowableArray<HBasicBlock*>& blocks_;
4294 size_t index_;
4295
4296 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4297};
4298
Mingyao Yang3584bce2015-05-19 16:01:59 -07004299// Iterator over the blocks that art part of the loop. Includes blocks part
4300// of an inner loop. The order in which the blocks are iterated is reverse
4301// post order.
4302class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4303 public:
4304 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4305 : blocks_in_loop_(info.GetBlocks()),
4306 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4307 index_(0) {
4308 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4309 Advance();
4310 }
4311 }
4312
4313 bool Done() const { return index_ == blocks_.Size(); }
4314 HBasicBlock* Current() const { return blocks_.Get(index_); }
4315 void Advance() {
4316 ++index_;
4317 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4318 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4319 break;
4320 }
4321 }
4322 }
4323
4324 private:
4325 const BitVector& blocks_in_loop_;
4326 const GrowableArray<HBasicBlock*>& blocks_;
4327 size_t index_;
4328
4329 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4330};
4331
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004332inline int64_t Int64FromConstant(HConstant* constant) {
4333 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4334 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4335 : constant->AsLongConstant()->GetValue();
4336}
4337
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004338} // namespace art
4339
4340#endif // ART_COMPILER_OPTIMIZING_NODES_H_