blob: 638ac95631fea5a07ada8fc72e6f603295681142 [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;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010041class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000042class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000043class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000044class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000046class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000047class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000049class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010050class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010051class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010052class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010053class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000054class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010055class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000057
58static const int kDefaultNumberOfBlocks = 8;
59static const int kDefaultNumberOfSuccessors = 2;
60static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010061static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010062static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000063static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000064
Calin Juravle9aec02f2014-11-18 23:06:35 +000065static constexpr uint32_t kMaxIntShiftValue = 0x1f;
66static constexpr uint64_t kMaxLongShiftValue = 0x3f;
67
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010068static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
69
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010070static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
71
Dave Allison20dfc792014-06-16 20:44:29 -070072enum IfCondition {
73 kCondEQ,
74 kCondNE,
75 kCondLT,
76 kCondLE,
77 kCondGT,
78 kCondGE,
79};
80
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010081class HInstructionList {
82 public:
83 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
84
85 void AddInstruction(HInstruction* instruction);
86 void RemoveInstruction(HInstruction* instruction);
87
David Brazdilc3d743f2015-04-22 13:40:50 +010088 // Insert `instruction` before/after an existing instruction `cursor`.
89 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
90 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
91
Roland Levillain6b469232014-09-25 10:10:38 +010092 // Return true if this list contains `instruction`.
93 bool Contains(HInstruction* instruction) const;
94
Roland Levillainccc07a92014-09-16 14:48:16 +010095 // Return true if `instruction1` is found before `instruction2` in
96 // this instruction list and false otherwise. Abort if none
97 // of these instructions is found.
98 bool FoundBefore(const HInstruction* instruction1,
99 const HInstruction* instruction2) const;
100
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000101 bool IsEmpty() const { return first_instruction_ == nullptr; }
102 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
103
104 // Update the block of all instructions to be `block`.
105 void SetBlockOfInstructions(HBasicBlock* block) const;
106
107 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
108 void Add(const HInstructionList& instruction_list);
109
David Brazdil2d7352b2015-04-20 14:52:42 +0100110 // Return the number of instructions in the list. This is an expensive operation.
111 size_t CountSize() const;
112
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100113 private:
114 HInstruction* first_instruction_;
115 HInstruction* last_instruction_;
116
117 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000118 friend class HGraph;
119 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100120 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100121 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100122
123 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
124};
125
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000126// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700127class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000128 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100129 HGraph(ArenaAllocator* arena,
130 const DexFile& dex_file,
131 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100132 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700133 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100134 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100135 bool debuggable = false,
136 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000137 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000138 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100139 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100140 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700141 entry_block_(nullptr),
142 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100143 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100144 number_of_vregs_(0),
145 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000146 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400147 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000148 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000149 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100150 dex_file_(dex_file),
151 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100152 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100153 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100154 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700155 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000156 cached_null_constant_(nullptr),
157 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000158 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
159 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100160 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
161 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000162
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000163 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100164 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100165 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000166
David Brazdil69ba7b72015-06-23 18:27:30 +0100167 bool IsInSsaForm() const { return in_ssa_form_; }
168
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000169 HBasicBlock* GetEntryBlock() const { return entry_block_; }
170 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100171 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000172
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000173 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
174 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000175
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000176 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100177
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000178 // Try building the SSA form of this graph, with dominance computation and loop
179 // recognition. Returns whether it was successful in doing all these steps.
180 bool TryBuildingSsa() {
181 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000182 // The SSA builder requires loops to all be natural. Specifically, the dead phi
183 // elimination phase checks the consistency of the graph when doing a post-order
184 // visit for eliminating dead phis: a dead phi can only have loop header phi
185 // users remaining when being visited.
186 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100187 // Precompute per-block try membership before entering the SSA builder,
188 // which needs the information to build catch block phis from values of
189 // locals at throwing instructions inside try blocks.
190 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000191 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100192 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000193 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000194 }
195
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100196 void ComputeDominanceInformation();
197 void ClearDominanceInformation();
198
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000199 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000200 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100201 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100202 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000203
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000204 // Analyze all natural loops in this graph. Returns false if one
205 // loop is not natural, that is the header does not dominate the
206 // back edge.
207 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100208
David Brazdilffee3d32015-07-06 11:48:53 +0100209 // Iterate over blocks to compute try block membership. Needs reverse post
210 // order and loop information.
211 void ComputeTryBlockInformation();
212
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000214 // Returns the instruction used to replace the invoke expression or null if the
215 // invoke is for a void method.
216 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000217
Mingyao Yang3584bce2015-05-19 16:01:59 -0700218 // Need to add a couple of blocks to test if the loop body is entered and
219 // put deoptimization instructions, etc.
220 void TransformLoopHeaderForBCE(HBasicBlock* header);
221
David Brazdil2d7352b2015-04-20 14:52:42 +0100222 // Removes `block` from the graph.
223 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000224
David Brazdilfc6a86a2015-06-26 10:33:45 +0000225 // Splits the edge between `block` and `successor` while preserving the
226 // indices in the predecessor/successor lists. If there are multiple edges
227 // between the blocks, the lowest indices are used.
228 // Returns the new block which is empty and has the same dex pc as `successor`.
229 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
230
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100231 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
232 void SimplifyLoop(HBasicBlock* header);
233
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000234 int32_t GetNextInstructionId() {
235 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000236 return current_instruction_id_++;
237 }
238
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000239 int32_t GetCurrentInstructionId() const {
240 return current_instruction_id_;
241 }
242
243 void SetCurrentInstructionId(int32_t id) {
244 current_instruction_id_ = id;
245 }
246
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100247 uint16_t GetMaximumNumberOfOutVRegs() const {
248 return maximum_number_of_out_vregs_;
249 }
250
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000251 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
252 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100253 }
254
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100255 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
256 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
257 }
258
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000259 void UpdateTemporariesVRegSlots(size_t slots) {
260 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100261 }
262
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000263 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100264 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000265 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100266 }
267
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100268 void SetNumberOfVRegs(uint16_t number_of_vregs) {
269 number_of_vregs_ = number_of_vregs;
270 }
271
272 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100273 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100274 return number_of_vregs_;
275 }
276
277 void SetNumberOfInVRegs(uint16_t value) {
278 number_of_in_vregs_ = value;
279 }
280
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100281 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100282 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100283 return number_of_vregs_ - number_of_in_vregs_;
284 }
285
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100286 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
287 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100288 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100289
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100290 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
291 return linear_order_;
292 }
293
Mark Mendell1152c922015-04-24 17:06:35 -0400294 bool HasBoundsChecks() const {
295 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800296 }
297
Mark Mendell1152c922015-04-24 17:06:35 -0400298 void SetHasBoundsChecks(bool value) {
299 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800300 }
301
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100302 bool ShouldGenerateConstructorBarrier() const {
303 return should_generate_constructor_barrier_;
304 }
305
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000306 bool IsDebuggable() const { return debuggable_; }
307
David Brazdil8d5b8b22015-03-24 10:51:52 +0000308 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000309 // already, it is created and inserted into the graph. This method is only for
310 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000311 HConstant* GetConstant(Primitive::Type type, int64_t value);
Calin Juravle2e768302015-07-28 14:41:11 +0000312
313 // TODO: This is problematic for the consistency of reference type propagation
314 // because it can be created anytime after the pass and thus it will be left
315 // with an invalid type.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000316 HNullConstant* GetNullConstant();
Calin Juravle2e768302015-07-28 14:41:11 +0000317
David Brazdil8d5b8b22015-03-24 10:51:52 +0000318 HIntConstant* GetIntConstant(int32_t value) {
319 return CreateConstant(value, &cached_int_constants_);
320 }
321 HLongConstant* GetLongConstant(int64_t value) {
322 return CreateConstant(value, &cached_long_constants_);
323 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000324 HFloatConstant* GetFloatConstant(float value) {
325 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
326 }
327 HDoubleConstant* GetDoubleConstant(double value) {
328 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
329 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000330
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100331 HCurrentMethod* GetCurrentMethod();
332
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000333 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100334
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100335 const DexFile& GetDexFile() const {
336 return dex_file_;
337 }
338
339 uint32_t GetMethodIdx() const {
340 return method_idx_;
341 }
342
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100343 InvokeType GetInvokeType() const {
344 return invoke_type_;
345 }
346
Mark Mendellc4701932015-04-10 13:18:51 -0400347 InstructionSet GetInstructionSet() const {
348 return instruction_set_;
349 }
350
David Brazdil2d7352b2015-04-20 14:52:42 +0100351 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000352 void VisitBlockForDominatorTree(HBasicBlock* block,
353 HBasicBlock* predecessor,
354 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100355 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000356 void VisitBlockForBackEdges(HBasicBlock* block,
357 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100358 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000359 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100360 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000361
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000362 template <class InstructionType, typename ValueType>
363 InstructionType* CreateConstant(ValueType value,
364 ArenaSafeMap<ValueType, InstructionType*>* cache) {
365 // Try to find an existing constant of the given value.
366 InstructionType* constant = nullptr;
367 auto cached_constant = cache->find(value);
368 if (cached_constant != cache->end()) {
369 constant = cached_constant->second;
370 }
371
372 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100373 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000374 if (constant == nullptr || constant->GetBlock() == nullptr) {
375 constant = new (arena_) InstructionType(value);
376 cache->Overwrite(value, constant);
377 InsertConstant(constant);
378 }
379 return constant;
380 }
381
David Brazdil8d5b8b22015-03-24 10:51:52 +0000382 void InsertConstant(HConstant* instruction);
383
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000384 // Cache a float constant into the graph. This method should only be
385 // called by the SsaBuilder when creating "equivalent" instructions.
386 void CacheFloatConstant(HFloatConstant* constant);
387
388 // See CacheFloatConstant comment.
389 void CacheDoubleConstant(HDoubleConstant* constant);
390
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000391 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000392
393 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000394 GrowableArray<HBasicBlock*> blocks_;
395
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100396 // List of blocks to perform a reverse post order tree traversal.
397 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000398
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100399 // List of blocks to perform a linear order tree traversal.
400 GrowableArray<HBasicBlock*> linear_order_;
401
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000402 HBasicBlock* entry_block_;
403 HBasicBlock* exit_block_;
404
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100405 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100406 uint16_t maximum_number_of_out_vregs_;
407
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100408 // The number of virtual registers in this method. Contains the parameters.
409 uint16_t number_of_vregs_;
410
411 // The number of virtual registers used by parameters of this method.
412 uint16_t number_of_in_vregs_;
413
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000414 // Number of vreg size slots that the temporaries use (used in baseline compiler).
415 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100416
Mark Mendell1152c922015-04-24 17:06:35 -0400417 // Has bounds checks. We can totally skip BCE if it's false.
418 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800419
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000420 // Indicates whether the graph should be compiled in a way that
421 // ensures full debuggability. If false, we can apply more
422 // aggressive optimizations that may limit the level of debugging.
423 const bool debuggable_;
424
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000425 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000426 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000427
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100428 // The dex file from which the method is from.
429 const DexFile& dex_file_;
430
431 // The method index in the dex file.
432 const uint32_t method_idx_;
433
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100434 // If inlined, this encodes how the callee is being invoked.
435 const InvokeType invoke_type_;
436
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100437 // Whether the graph has been transformed to SSA form. Only used
438 // in debug mode to ensure we are not using properties only valid
439 // for non-SSA form (like the number of temporaries).
440 bool in_ssa_form_;
441
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100442 const bool should_generate_constructor_barrier_;
443
Mathieu Chartiere401d142015-04-22 13:56:20 -0700444 const InstructionSet instruction_set_;
445
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000446 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000447 HNullConstant* cached_null_constant_;
448 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000449 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000450 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000451 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000452
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100453 HCurrentMethod* cached_current_method_;
454
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000455 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100456 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000457 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000458 DISALLOW_COPY_AND_ASSIGN(HGraph);
459};
460
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700461class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000462 public:
463 HLoopInformation(HBasicBlock* header, HGraph* graph)
464 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100465 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100466 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100467 // Make bit vector growable, as the number of blocks may change.
468 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100469
470 HBasicBlock* GetHeader() const {
471 return header_;
472 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000473
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000474 void SetHeader(HBasicBlock* block) {
475 header_ = block;
476 }
477
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100478 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
479 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
480 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
481
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000482 void AddBackEdge(HBasicBlock* back_edge) {
483 back_edges_.Add(back_edge);
484 }
485
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100486 void RemoveBackEdge(HBasicBlock* back_edge) {
487 back_edges_.Delete(back_edge);
488 }
489
David Brazdil46e2a392015-03-16 17:31:52 +0000490 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100491 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000492 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100493 }
494 return false;
495 }
496
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000497 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000498 return back_edges_.Size();
499 }
500
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100501 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100502
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100503 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
504 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100505 }
506
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100507 // Returns the lifetime position of the back edge that has the
508 // greatest lifetime position.
509 size_t GetLifetimeEnd() const;
510
511 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
512 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
513 if (back_edges_.Get(i) == existing) {
514 back_edges_.Put(i, new_back_edge);
515 return;
516 }
517 }
518 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100519 }
520
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100521 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100522 // that is the header dominates the back edge.
523 bool Populate();
524
David Brazdila4b8c212015-05-07 09:59:30 +0100525 // Reanalyzes the loop by removing loop info from its blocks and re-running
526 // Populate(). If there are no back edges left, the loop info is completely
527 // removed as well as its SuspendCheck instruction. It must be run on nested
528 // inner loops first.
529 void Update();
530
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100531 // Returns whether this loop information contains `block`.
532 // Note that this loop information *must* be populated before entering this function.
533 bool Contains(const HBasicBlock& block) const;
534
535 // Returns whether this loop information is an inner loop of `other`.
536 // Note that `other` *must* be populated before entering this function.
537 bool IsIn(const HLoopInformation& other) const;
538
539 const ArenaBitVector& GetBlocks() const { return blocks_; }
540
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000541 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000542 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000543
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000544 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100545 // Internal recursive implementation of `Populate`.
546 void PopulateRecursive(HBasicBlock* block);
547
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000548 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100549 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000550 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100551 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000552
553 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
554};
555
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100556static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100557static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100558
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000559// A block in a method. Contains the list of instructions represented
560// as a double linked list. Each block knows its predecessors and
561// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100562
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700563class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000564 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100565 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000566 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000567 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
David Brazdilb618ade2015-07-29 10:31:29 +0100568 exceptional_predecessors_(graph->GetArena(), kDefaultNumberOfExceptionalPredecessors),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000569 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000570 loop_information_(nullptr),
571 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100572 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100573 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100574 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100575 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000576 lifetime_end_(kNoLifetime),
577 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000578
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100579 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
580 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000581 }
582
David Brazdilb618ade2015-07-29 10:31:29 +0100583 const GrowableArray<HInstruction*>& GetExceptionalPredecessors() const {
584 return exceptional_predecessors_;
585 }
586
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100587 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
588 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000589 }
590
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100591 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
592 return dominated_blocks_;
593 }
594
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100595 bool IsEntryBlock() const {
596 return graph_->GetEntryBlock() == this;
597 }
598
599 bool IsExitBlock() const {
600 return graph_->GetExitBlock() == this;
601 }
602
David Brazdil46e2a392015-03-16 17:31:52 +0000603 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000604 bool IsSingleTryBoundary() const;
605
606 // Returns true if this block emits nothing but a jump.
607 bool IsSingleJump() const {
608 HLoopInformation* loop_info = GetLoopInformation();
609 return (IsSingleGoto() || IsSingleTryBoundary())
610 // Back edges generate a suspend check.
611 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
612 }
David Brazdil46e2a392015-03-16 17:31:52 +0000613
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000614 void AddBackEdge(HBasicBlock* back_edge) {
615 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000616 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000617 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100618 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000619 loop_information_->AddBackEdge(back_edge);
620 }
621
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000622 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000623 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000624
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000625 int GetBlockId() const { return block_id_; }
626 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000627
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000628 HBasicBlock* GetDominator() const { return dominator_; }
629 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100630 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100631 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000632 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
633 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
634 if (dominated_blocks_.Get(i) == existing) {
635 dominated_blocks_.Put(i, new_block);
636 return;
637 }
638 }
639 LOG(FATAL) << "Unreachable";
640 UNREACHABLE();
641 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100642 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000643
644 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100645 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000646 }
647
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100648 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
649 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100650 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100651 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100652 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
653 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000654
David Brazdilb618ade2015-07-29 10:31:29 +0100655 void AddExceptionalPredecessor(HInstruction* exceptional_predecessor);
656
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000657 void AddSuccessor(HBasicBlock* block) {
658 successors_.Add(block);
659 block->predecessors_.Add(this);
660 }
661
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100662 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
663 size_t successor_index = GetSuccessorIndexOf(existing);
664 DCHECK_NE(successor_index, static_cast<size_t>(-1));
665 existing->RemovePredecessor(this);
666 new_block->predecessors_.Add(this);
667 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000668 }
669
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000670 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
671 size_t predecessor_index = GetPredecessorIndexOf(existing);
672 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
673 existing->RemoveSuccessor(this);
674 new_block->successors_.Add(this);
675 predecessors_.Put(predecessor_index, new_block);
676 }
677
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100678 // Insert `this` between `predecessor` and `successor. This method
679 // preserves the indicies, and will update the first edge found between
680 // `predecessor` and `successor`.
681 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
682 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
683 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
684 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
685 DCHECK_NE(successor_index, static_cast<size_t>(-1));
686 successor->predecessors_.Put(predecessor_index, this);
687 predecessor->successors_.Put(successor_index, this);
688 successors_.Add(successor);
689 predecessors_.Add(predecessor);
690 }
691
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100692 void RemovePredecessor(HBasicBlock* block) {
693 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100694 }
695
David Brazdilb618ade2015-07-29 10:31:29 +0100696 void RemoveExceptionalPredecessor(HInstruction* instruction) {
697 exceptional_predecessors_.Delete(instruction);
698 }
699
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000700 void RemoveSuccessor(HBasicBlock* block) {
701 successors_.Delete(block);
702 }
703
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100704 void ClearAllPredecessors() {
705 predecessors_.Reset();
706 }
707
708 void AddPredecessor(HBasicBlock* block) {
709 predecessors_.Add(block);
710 block->successors_.Add(this);
711 }
712
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100713 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100714 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100715 HBasicBlock* temp = predecessors_.Get(0);
716 predecessors_.Put(0, predecessors_.Get(1));
717 predecessors_.Put(1, temp);
718 }
719
David Brazdil769c9e52015-04-27 13:54:09 +0100720 void SwapSuccessors() {
721 DCHECK_EQ(successors_.Size(), 2u);
722 HBasicBlock* temp = successors_.Get(0);
723 successors_.Put(0, successors_.Get(1));
724 successors_.Put(1, temp);
725 }
726
David Brazdilfc6a86a2015-06-26 10:33:45 +0000727 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100728 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
729 if (predecessors_.Get(i) == predecessor) {
730 return i;
731 }
732 }
733 return -1;
734 }
735
David Brazdilb618ade2015-07-29 10:31:29 +0100736 size_t GetExceptionalPredecessorIndexOf(HInstruction* exceptional_predecessor) const {
737 for (size_t i = 0, e = exceptional_predecessors_.Size(); i < e; ++i) {
738 if (exceptional_predecessors_.Get(i) == exceptional_predecessor) {
739 return i;
740 }
741 }
742 return -1;
743 }
744
David Brazdilfc6a86a2015-06-26 10:33:45 +0000745 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100746 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
747 if (successors_.Get(i) == successor) {
748 return i;
749 }
750 }
751 return -1;
752 }
753
David Brazdilfc6a86a2015-06-26 10:33:45 +0000754 HBasicBlock* GetSinglePredecessor() const {
755 DCHECK_EQ(GetPredecessors().Size(), 1u);
756 return GetPredecessors().Get(0);
757 }
758
759 HBasicBlock* GetSingleSuccessor() const {
760 DCHECK_EQ(GetSuccessors().Size(), 1u);
761 return GetSuccessors().Get(0);
762 }
763
764 // Returns whether the first occurrence of `predecessor` in the list of
765 // predecessors is at index `idx`.
766 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
767 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
768 return GetPredecessorIndexOf(predecessor) == idx;
769 }
770
David Brazdilffee3d32015-07-06 11:48:53 +0100771 // Returns the number of non-exceptional successors. SsaChecker ensures that
772 // these are stored at the beginning of the successor list.
773 size_t NumberOfNormalSuccessors() const {
774 return EndsWithTryBoundary() ? 1 : GetSuccessors().Size();
775 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000776
777 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100778 // created, latter block. Note that this method will add the block to the
779 // graph, create a Goto at the end of the former block and will create an edge
780 // between the blocks. It will not, however, update the reverse post order or
781 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000782 HBasicBlock* SplitBefore(HInstruction* cursor);
783
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000784 // Split the block into two blocks just after `cursor`. Returns the newly
785 // created block. Note that this method just updates raw block information,
786 // like predecessors, successors, dominators, and instruction list. It does not
787 // update the graph, reverse post order, loop information, nor make sure the
788 // blocks are consistent (for example ending with a control flow instruction).
789 HBasicBlock* SplitAfter(HInstruction* cursor);
790
791 // Merge `other` at the end of `this`. Successors and dominated blocks of
792 // `other` are changed to be successors and dominated blocks of `this`. Note
793 // that this method does not update the graph, reverse post order, loop
794 // information, nor make sure the blocks are consistent (for example ending
795 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100796 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000797
798 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
799 // of `this` are moved to `other`.
800 // Note that this method does not update the graph, reverse post order, loop
801 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000802 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000803 void ReplaceWith(HBasicBlock* other);
804
David Brazdil2d7352b2015-04-20 14:52:42 +0100805 // Merge `other` at the end of `this`. This method updates loops, reverse post
806 // order, links to predecessors, successors, dominators and deletes the block
807 // from the graph. The two blocks must be successive, i.e. `this` the only
808 // predecessor of `other` and vice versa.
809 void MergeWith(HBasicBlock* other);
810
811 // Disconnects `this` from all its predecessors, successors and dominator,
812 // removes it from all loops it is included in and eventually from the graph.
813 // The block must not dominate any other block. Predecessors and successors
814 // are safely updated.
815 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000816
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000817 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100818 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100819 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100820 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100821 // Replace instruction `initial` with `replacement` within this block.
822 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
823 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100824 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100825 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000826 // RemoveInstruction and RemovePhi delete a given instruction from the respective
827 // instruction list. With 'ensure_safety' set to true, it verifies that the
828 // instruction is not in use and removes it from the use lists of its inputs.
829 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
830 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100831 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100832
833 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100834 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100835 }
836
Roland Levillain6b879dd2014-09-22 17:13:44 +0100837 bool IsLoopPreHeaderFirstPredecessor() const {
838 DCHECK(IsLoopHeader());
839 DCHECK(!GetPredecessors().IsEmpty());
840 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
841 }
842
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100843 HLoopInformation* GetLoopInformation() const {
844 return loop_information_;
845 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000846
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000847 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100848 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000849 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100850 void SetInLoop(HLoopInformation* info) {
851 if (IsLoopHeader()) {
852 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100853 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100854 loop_information_ = info;
855 } else if (loop_information_->Contains(*info->GetHeader())) {
856 // Block is currently part of an outer loop. Make it part of this inner loop.
857 // Note that a non loop header having a loop information means this loop information
858 // has already been populated
859 loop_information_ = info;
860 } else {
861 // Block is part of an inner loop. Do not update the loop information.
862 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
863 // at this point, because this method is being called while populating `info`.
864 }
865 }
866
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000867 // Raw update of the loop information.
868 void SetLoopInformation(HLoopInformation* info) {
869 loop_information_ = info;
870 }
871
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100872 bool IsInLoop() const { return loop_information_ != nullptr; }
873
David Brazdilffee3d32015-07-06 11:48:53 +0100874 HTryBoundary* GetTryEntry() const { return try_entry_; }
875 void SetTryEntry(HTryBoundary* try_entry) { try_entry_ = try_entry; }
876 bool IsInTry() const { return try_entry_ != nullptr; }
877
878 // Returns the try entry that this block's successors should have. They will
879 // be in the same try, unless the block ends in a try boundary. In that case,
880 // the appropriate try entry will be returned.
881 HTryBoundary* ComputeTryEntryOfSuccessors() const;
882
David Brazdila4b8c212015-05-07 09:59:30 +0100883 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100884 bool Dominates(HBasicBlock* block) const;
885
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100886 size_t GetLifetimeStart() const { return lifetime_start_; }
887 size_t GetLifetimeEnd() const { return lifetime_end_; }
888
889 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
890 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
891
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100892 uint32_t GetDexPc() const { return dex_pc_; }
893
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000894 bool IsCatchBlock() const { return is_catch_block_; }
895 void SetIsCatchBlock() { is_catch_block_ = true; }
896
David Brazdil8d5b8b22015-03-24 10:51:52 +0000897 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000898 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100899 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000900 bool HasSinglePhi() const;
901
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000902 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000903 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000904 GrowableArray<HBasicBlock*> predecessors_;
David Brazdilb618ade2015-07-29 10:31:29 +0100905 GrowableArray<HInstruction*> exceptional_predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000906 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100907 HInstructionList instructions_;
908 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000909 HLoopInformation* loop_information_;
910 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100911 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000912 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100913 // The dex program counter of the first instruction of this block.
914 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100915 size_t lifetime_start_;
916 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000917 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000918
David Brazdilffee3d32015-07-06 11:48:53 +0100919 // If this block is in a try block, `try_entry_` stores one of, possibly
920 // several, TryBoundary instructions entering it.
921 HTryBoundary* try_entry_;
922
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000923 friend class HGraph;
924 friend class HInstruction;
925
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000926 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
927};
928
David Brazdilb2bd1c52015-03-25 11:17:37 +0000929// Iterates over the LoopInformation of all loops which contain 'block'
930// from the innermost to the outermost.
931class HLoopInformationOutwardIterator : public ValueObject {
932 public:
933 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
934 : current_(block.GetLoopInformation()) {}
935
936 bool Done() const { return current_ == nullptr; }
937
938 void Advance() {
939 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100940 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000941 }
942
943 HLoopInformation* Current() const {
944 DCHECK(!Done());
945 return current_;
946 }
947
948 private:
949 HLoopInformation* current_;
950
951 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
952};
953
Alexandre Ramesef20f712015-06-09 10:29:30 +0100954#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100955 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000956 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000957 M(ArrayGet, Instruction) \
958 M(ArrayLength, Instruction) \
959 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100960 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000961 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000962 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000963 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +0100964 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100965 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000966 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100967 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100968 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700969 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000970 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000971 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000972 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100973 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000974 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100975 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000976 M(FloatConstant, Constant) \
977 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100978 M(GreaterThan, Condition) \
979 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100980 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000981 M(InstanceFieldGet, Instruction) \
982 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000983 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100984 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000985 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000986 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100987 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000988 M(LessThan, Condition) \
989 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000990 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000991 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100992 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000993 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100994 M(Local, Instruction) \
995 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100996 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000997 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000998 M(Mul, BinaryOperation) \
999 M(Neg, UnaryOperation) \
1000 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001001 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001002 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001003 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001004 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001005 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001006 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001007 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001008 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001009 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001010 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001011 M(Return, Instruction) \
1012 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001013 M(Shl, BinaryOperation) \
1014 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001015 M(StaticFieldGet, Instruction) \
1016 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001017 M(StoreLocal, Instruction) \
1018 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001019 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001020 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001021 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001022 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001023 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001024 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001025 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001026
Alexandre Ramesef20f712015-06-09 10:29:30 +01001027#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1028
1029#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1030
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001031#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1032
Alexandre Ramesef20f712015-06-09 10:29:30 +01001033#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1034
1035#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1036
1037#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1038 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1039 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1040 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001041 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001042 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1043 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1044
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001045#define FOR_EACH_INSTRUCTION(M) \
1046 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1047 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001048 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001049 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001050 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001051
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001052#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001053FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1054#undef FORWARD_DECLARATION
1055
Roland Levillainccc07a92014-09-16 14:48:16 +01001056#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001057 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1058 const char* DebugName() const OVERRIDE { return #type; } \
1059 const H##type* As##type() const OVERRIDE { return this; } \
1060 H##type* As##type() OVERRIDE { return this; } \
1061 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001062 return other->Is##type(); \
1063 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001064 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001065
David Brazdiled596192015-01-23 10:39:45 +00001066template <typename T> class HUseList;
1067
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001068template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001069class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001070 public:
David Brazdiled596192015-01-23 10:39:45 +00001071 HUseListNode* GetPrevious() const { return prev_; }
1072 HUseListNode* GetNext() const { return next_; }
1073 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001074 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001075 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001076
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001077 private:
David Brazdiled596192015-01-23 10:39:45 +00001078 HUseListNode(T user, size_t index)
1079 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1080
1081 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001082 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001083 HUseListNode<T>* prev_;
1084 HUseListNode<T>* next_;
1085
1086 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001087
1088 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1089};
1090
David Brazdiled596192015-01-23 10:39:45 +00001091template <typename T>
1092class HUseList : public ValueObject {
1093 public:
1094 HUseList() : first_(nullptr) {}
1095
1096 void Clear() {
1097 first_ = nullptr;
1098 }
1099
1100 // Adds a new entry at the beginning of the use list and returns
1101 // the newly created node.
1102 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001103 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001104 if (IsEmpty()) {
1105 first_ = new_node;
1106 } else {
1107 first_->prev_ = new_node;
1108 new_node->next_ = first_;
1109 first_ = new_node;
1110 }
1111 return new_node;
1112 }
1113
1114 HUseListNode<T>* GetFirst() const {
1115 return first_;
1116 }
1117
1118 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001119 DCHECK(node != nullptr);
1120 DCHECK(Contains(node));
1121
David Brazdiled596192015-01-23 10:39:45 +00001122 if (node->prev_ != nullptr) {
1123 node->prev_->next_ = node->next_;
1124 }
1125 if (node->next_ != nullptr) {
1126 node->next_->prev_ = node->prev_;
1127 }
1128 if (node == first_) {
1129 first_ = node->next_;
1130 }
1131 }
1132
David Brazdil1abb4192015-02-17 18:33:36 +00001133 bool Contains(const HUseListNode<T>* node) const {
1134 if (node == nullptr) {
1135 return false;
1136 }
1137 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1138 if (current == node) {
1139 return true;
1140 }
1141 }
1142 return false;
1143 }
1144
David Brazdiled596192015-01-23 10:39:45 +00001145 bool IsEmpty() const {
1146 return first_ == nullptr;
1147 }
1148
1149 bool HasOnlyOneUse() const {
1150 return first_ != nullptr && first_->next_ == nullptr;
1151 }
1152
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001153 size_t SizeSlow() const {
1154 size_t count = 0;
1155 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1156 ++count;
1157 }
1158 return count;
1159 }
1160
David Brazdiled596192015-01-23 10:39:45 +00001161 private:
1162 HUseListNode<T>* first_;
1163};
1164
1165template<typename T>
1166class HUseIterator : public ValueObject {
1167 public:
1168 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1169
1170 bool Done() const { return current_ == nullptr; }
1171
1172 void Advance() {
1173 DCHECK(!Done());
1174 current_ = current_->GetNext();
1175 }
1176
1177 HUseListNode<T>* Current() const {
1178 DCHECK(!Done());
1179 return current_;
1180 }
1181
1182 private:
1183 HUseListNode<T>* current_;
1184
1185 friend class HValue;
1186};
1187
David Brazdil1abb4192015-02-17 18:33:36 +00001188// This class is used by HEnvironment and HInstruction classes to record the
1189// instructions they use and pointers to the corresponding HUseListNodes kept
1190// by the used instructions.
1191template <typename T>
1192class HUserRecord : public ValueObject {
1193 public:
1194 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1195 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1196
1197 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1198 : instruction_(old_record.instruction_), use_node_(use_node) {
1199 DCHECK(instruction_ != nullptr);
1200 DCHECK(use_node_ != nullptr);
1201 DCHECK(old_record.use_node_ == nullptr);
1202 }
1203
1204 HInstruction* GetInstruction() const { return instruction_; }
1205 HUseListNode<T>* GetUseNode() const { return use_node_; }
1206
1207 private:
1208 // Instruction used by the user.
1209 HInstruction* instruction_;
1210
1211 // Corresponding entry in the use list kept by 'instruction_'.
1212 HUseListNode<T>* use_node_;
1213};
1214
Aart Bik854a02b2015-07-14 16:07:00 -07001215/**
1216 * Side-effects representation for write/read dependences on fields/arrays.
1217 *
1218 * The dependence analysis uses type disambiguation (e.g. a float field write
1219 * cannot modify the value of an integer field read) and the access type (e.g.
1220 * a reference array write cannot modify the value of a reference field read
1221 * [although it may modify the reference fetch prior to reading the field,
1222 * which is represented by its own write/read dependence]). The analysis
1223 * makes conservative points-to assumptions on reference types (e.g. two same
1224 * typed arrays are assumed to be the same, and any reference read depends
1225 * on any reference read without further regard of its type).
1226 *
1227 * The internal representation uses the following 36-bit flags assignments:
1228 *
1229 * |ARRAY-R |FIELD-R |ARRAY-W |FIELD-W |
1230 * +---------+---------+---------+---------+
1231 * |543210987|654321098|765432109|876543210|
1232 * |DFJISCBZL|DFJISCBZL|DFJISCBZL|DFJISCBZL|
1233 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001234class SideEffects : public ValueObject {
1235 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001236 SideEffects() : flags_(0) {}
1237
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001238 static SideEffects None() {
1239 return SideEffects(0);
1240 }
1241
1242 static SideEffects All() {
Aart Bik854a02b2015-07-14 16:07:00 -07001243 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001244 }
1245
Aart Bik34c3ba92015-07-20 14:08:59 -07001246 static SideEffects AllWrites() {
1247 return SideEffects(kAllWrites);
1248 }
1249
1250 static SideEffects AllReads() {
1251 return SideEffects(kAllReads);
1252 }
1253
1254 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1255 return is_volatile
1256 ? All()
1257 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001258 }
1259
Aart Bik854a02b2015-07-14 16:07:00 -07001260 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1261 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001262 }
1263
Aart Bik34c3ba92015-07-20 14:08:59 -07001264 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1265 return is_volatile
1266 ? All()
1267 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001268 }
1269
1270 static SideEffects ArrayReadOfType(Primitive::Type type) {
1271 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1272 }
1273
1274 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001275 SideEffects Union(SideEffects other) const {
1276 return SideEffects(flags_ | other.flags_);
1277 }
1278
Aart Bik854a02b2015-07-14 16:07:00 -07001279 // Returns true if something is written.
1280 bool DoesAnyWrite() const {
1281 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001282 }
1283
Aart Bik854a02b2015-07-14 16:07:00 -07001284 // Returns true if something is read.
1285 bool DoesAnyRead() const {
1286 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001287 }
1288
Aart Bik854a02b2015-07-14 16:07:00 -07001289 // Returns true if nothing is written or read.
1290 bool DoesNothing() const {
1291 return flags_ == 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001292 }
1293
Aart Bik854a02b2015-07-14 16:07:00 -07001294 // Returns true if potentially everything is written and read
1295 // (every type and every kind of access).
1296 bool DoesAll() const {
1297 return flags_ == (kAllWrites | kAllReads);
1298 }
1299
1300 // Returns true if this may read something written by other.
1301 bool MayDependOn(SideEffects other) const {
1302 const uint64_t reads = (flags_ & kAllReads) >> kFieldReadOffset;
1303 return (other.flags_ & reads);
1304 }
1305
1306 // Returns string representation of flags (for debugging only).
1307 // Format: |DFJISCBZL|DFJISCBZL|DFJISCBZL|DFJISCBZL|
1308 std::string ToString() const {
1309 static const char *kDebug = "LZBCSIJFD";
1310 std::string flags = "|";
1311 for (int s = 35; s >= 0; s--) {
1312 const int t = s % kBits;
1313 if ((flags_ >> s) & 1)
1314 flags += kDebug[t];
1315 if (t == 0)
1316 flags += "|";
1317 }
1318 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001319 }
1320
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001321 private:
Aart Bik854a02b2015-07-14 16:07:00 -07001322 static constexpr int kBits = 9;
1323 static constexpr int kFieldWriteOffset = 0 * kBits;
1324 static constexpr int kArrayWriteOffset = 1 * kBits;
1325 static constexpr int kFieldReadOffset = 2 * kBits;
1326 static constexpr int kArrayReadOffset = 3 * kBits;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001327
Aart Bik854a02b2015-07-14 16:07:00 -07001328 static constexpr uint64_t kAllWrites = 0x0003ffff;
1329 static constexpr uint64_t kAllReads = kAllWrites << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001330
Aart Bik854a02b2015-07-14 16:07:00 -07001331 // Work around the fact that HIR aliases I/F and J/D.
1332 // TODO: remove this interceptor once HIR types are clean
1333 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1334 switch (type) {
1335 case Primitive::kPrimInt:
1336 case Primitive::kPrimFloat:
1337 return TypeFlag(Primitive::kPrimInt, offset) |
1338 TypeFlag(Primitive::kPrimFloat, offset);
1339 case Primitive::kPrimLong:
1340 case Primitive::kPrimDouble:
1341 return TypeFlag(Primitive::kPrimLong, offset) |
1342 TypeFlag(Primitive::kPrimDouble, offset);
1343 default:
1344 return TypeFlag(type, offset);
1345 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001346 }
1347
Aart Bik854a02b2015-07-14 16:07:00 -07001348 // Translates type to bit flag.
1349 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1350 CHECK_NE(type, Primitive::kPrimVoid);
1351 const uint64_t one = 1;
1352 const int shift = type; // 0-based consecutive enum
1353 DCHECK_LE(kFieldWriteOffset, shift);
1354 DCHECK_LT(shift, kArrayWriteOffset);
1355 return one << (type + offset);
1356 }
1357
1358 // Private constructor on direct flags value.
1359 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1360
1361 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001362};
1363
David Brazdiled596192015-01-23 10:39:45 +00001364// A HEnvironment object contains the values of virtual registers at a given location.
1365class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1366 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001367 HEnvironment(ArenaAllocator* arena,
1368 size_t number_of_vregs,
1369 const DexFile& dex_file,
1370 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001371 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001372 InvokeType invoke_type,
1373 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001374 : vregs_(arena, number_of_vregs),
1375 locations_(arena, number_of_vregs),
1376 parent_(nullptr),
1377 dex_file_(dex_file),
1378 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001379 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001380 invoke_type_(invoke_type),
1381 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001382 vregs_.SetSize(number_of_vregs);
1383 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001384 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001385 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001386
1387 locations_.SetSize(number_of_vregs);
1388 for (size_t i = 0; i < number_of_vregs; ++i) {
1389 locations_.Put(i, Location());
1390 }
1391 }
1392
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001393 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001394 : HEnvironment(arena,
1395 to_copy.Size(),
1396 to_copy.GetDexFile(),
1397 to_copy.GetMethodIdx(),
1398 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001399 to_copy.GetInvokeType(),
1400 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001401
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001402 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001403 if (parent_ != nullptr) {
1404 parent_->SetAndCopyParentChain(allocator, parent);
1405 } else {
1406 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1407 parent_->CopyFrom(parent);
1408 if (parent->GetParent() != nullptr) {
1409 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1410 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001411 }
David Brazdiled596192015-01-23 10:39:45 +00001412 }
1413
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001414 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1415 void CopyFrom(HEnvironment* environment);
1416
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001417 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1418 // input to the loop phi instead. This is for inserting instructions that
1419 // require an environment (like HDeoptimization) in the loop pre-header.
1420 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001421
1422 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001423 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001424 }
1425
1426 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001427 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001428 }
1429
David Brazdil1abb4192015-02-17 18:33:36 +00001430 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001431
1432 size_t Size() const { return vregs_.Size(); }
1433
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001434 HEnvironment* GetParent() const { return parent_; }
1435
1436 void SetLocationAt(size_t index, Location location) {
1437 locations_.Put(index, location);
1438 }
1439
1440 Location GetLocationAt(size_t index) const {
1441 return locations_.Get(index);
1442 }
1443
1444 uint32_t GetDexPc() const {
1445 return dex_pc_;
1446 }
1447
1448 uint32_t GetMethodIdx() const {
1449 return method_idx_;
1450 }
1451
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001452 InvokeType GetInvokeType() const {
1453 return invoke_type_;
1454 }
1455
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001456 const DexFile& GetDexFile() const {
1457 return dex_file_;
1458 }
1459
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001460 HInstruction* GetHolder() const {
1461 return holder_;
1462 }
1463
David Brazdiled596192015-01-23 10:39:45 +00001464 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001465 // Record instructions' use entries of this environment for constant-time removal.
1466 // It should only be called by HInstruction when a new environment use is added.
1467 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1468 DCHECK(env_use->GetUser() == this);
1469 size_t index = env_use->GetIndex();
1470 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1471 }
David Brazdiled596192015-01-23 10:39:45 +00001472
David Brazdil1abb4192015-02-17 18:33:36 +00001473 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001474 GrowableArray<Location> locations_;
1475 HEnvironment* parent_;
1476 const DexFile& dex_file_;
1477 const uint32_t method_idx_;
1478 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001479 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001480
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001481 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001482 HInstruction* const holder_;
1483
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001484 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001485
1486 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1487};
1488
Calin Juravleacf735c2015-02-12 15:25:22 +00001489class ReferenceTypeInfo : ValueObject {
1490 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001491 typedef Handle<mirror::Class> TypeHandle;
1492
Calin Juravle2e768302015-07-28 14:41:11 +00001493 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1494 // The constructor will check that the type_handle is valid.
1495 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001496 }
1497
Calin Juravle2e768302015-07-28 14:41:11 +00001498 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1499
1500 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1501 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001502 }
1503
Calin Juravle2e768302015-07-28 14:41:11 +00001504 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1505 return IsValidHandle(type_handle_);
1506 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001507 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001508
1509 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1510 DCHECK(IsValid());
1511 return GetTypeHandle()->IsObjectClass();
1512 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001513 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001514 DCHECK(IsValid());
1515 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001516 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001517
1518 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1519
Mathieu Chartier90443472015-07-16 20:32:27 -07001520 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001521 DCHECK(IsValid());
1522 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001523 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1524 }
1525
1526 // Returns true if the type information provide the same amount of details.
1527 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001528 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001529 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001530 if (!IsValid() && !rti.IsValid()) {
1531 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001532 return true;
1533 }
Calin Juravle2e768302015-07-28 14:41:11 +00001534 if (!IsValid() || !rti.IsValid()) {
1535 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001536 return false;
1537 }
Calin Juravle2e768302015-07-28 14:41:11 +00001538 return IsExact() == rti.IsExact()
1539 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001540 }
1541
1542 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001543 ReferenceTypeInfo();
1544 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001545
Calin Juravleacf735c2015-02-12 15:25:22 +00001546 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001547 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001548 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001549 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001550 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001551};
1552
1553std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1554
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001555class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001556 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001557 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001558 : previous_(nullptr),
1559 next_(nullptr),
1560 block_(nullptr),
1561 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001562 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001563 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001564 locations_(nullptr),
1565 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001566 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001567 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001568 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001569
Dave Allison20dfc792014-06-16 20:44:29 -07001570 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001571
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001572#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001573 enum InstructionKind {
1574 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1575 };
1576#undef DECLARE_KIND
1577
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001578 HInstruction* GetNext() const { return next_; }
1579 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001580
Calin Juravle77520bc2015-01-12 18:45:46 +00001581 HInstruction* GetNextDisregardingMoves() const;
1582 HInstruction* GetPreviousDisregardingMoves() const;
1583
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001584 HBasicBlock* GetBlock() const { return block_; }
1585 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001586 bool IsInBlock() const { return block_ != nullptr; }
1587 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001588 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001589
Roland Levillain6b879dd2014-09-22 17:13:44 +01001590 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001591 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001592
1593 virtual void Accept(HGraphVisitor* visitor) = 0;
1594 virtual const char* DebugName() const = 0;
1595
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001596 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001597 void SetRawInputAt(size_t index, HInstruction* input) {
1598 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1599 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001600
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001601 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001602 virtual uint32_t GetDexPc() const {
1603 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1604 " does not need an environment";
1605 UNREACHABLE();
1606 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001607 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001608 virtual bool CanThrow() const { return false; }
Aart Bik854a02b2015-07-14 16:07:00 -07001609
1610 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001611
Calin Juravle10e244f2015-01-26 18:54:32 +00001612 // Does not apply for all instructions, but having this at top level greatly
1613 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001614 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001615 virtual bool CanBeNull() const {
1616 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1617 return true;
1618 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001619
Calin Juravle641547a2015-04-21 22:08:51 +01001620 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1621 UNUSED(obj);
1622 return false;
1623 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001624
Calin Juravle2e768302015-07-28 14:41:11 +00001625 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001626
Calin Juravle61d544b2015-02-23 16:46:57 +00001627 ReferenceTypeInfo GetReferenceTypeInfo() const {
1628 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1629 return reference_type_info_;
1630 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001631
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001632 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001633 DCHECK(user != nullptr);
1634 HUseListNode<HInstruction*>* use =
1635 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1636 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001637 }
1638
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001639 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001640 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001641 HUseListNode<HEnvironment*>* env_use =
1642 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1643 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001644 }
1645
David Brazdil1abb4192015-02-17 18:33:36 +00001646 void RemoveAsUserOfInput(size_t input) {
1647 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1648 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1649 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001650
David Brazdil1abb4192015-02-17 18:33:36 +00001651 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1652 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001653
David Brazdiled596192015-01-23 10:39:45 +00001654 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1655 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001656 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001657 bool HasOnlyOneNonEnvironmentUse() const {
1658 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1659 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001660
Roland Levillain6c82d402014-10-13 16:10:27 +01001661 // Does this instruction strictly dominate `other_instruction`?
1662 // Returns false if this instruction and `other_instruction` are the same.
1663 // Aborts if this instruction and `other_instruction` are both phis.
1664 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001665
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001666 int GetId() const { return id_; }
1667 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001668
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001669 int GetSsaIndex() const { return ssa_index_; }
1670 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1671 bool HasSsaIndex() const { return ssa_index_ != -1; }
1672
1673 bool HasEnvironment() const { return environment_ != nullptr; }
1674 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001675 // Set the `environment_` field. Raw because this method does not
1676 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001677 void SetRawEnvironment(HEnvironment* environment) {
1678 DCHECK(environment_ == nullptr);
1679 DCHECK_EQ(environment->GetHolder(), this);
1680 environment_ = environment;
1681 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001682
1683 // Set the environment of this instruction, copying it from `environment`. While
1684 // copying, the uses lists are being updated.
1685 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001686 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001687 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001688 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001689 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001690 if (environment->GetParent() != nullptr) {
1691 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1692 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001693 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001694
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001695 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1696 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001697 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001698 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001699 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001700 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001701 if (environment->GetParent() != nullptr) {
1702 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1703 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001704 }
1705
Nicolas Geoffray39468442014-09-02 15:17:15 +01001706 // Returns the number of entries in the environment. Typically, that is the
1707 // number of dex registers in a method. It could be more in case of inlining.
1708 size_t EnvironmentSize() const;
1709
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001710 LocationSummary* GetLocations() const { return locations_; }
1711 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001712
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001713 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001714 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001715
Alexandre Rames188d4312015-04-09 18:30:21 +01001716 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1717 // uses of this instruction by `other` are *not* updated.
1718 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1719 ReplaceWith(other);
1720 other->ReplaceInput(this, use_index);
1721 }
1722
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001723 // Move `this` instruction before `cursor`.
1724 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001725
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001726#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001727 bool Is##type() const { return (As##type() != nullptr); } \
1728 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001729 virtual H##type* As##type() { return nullptr; }
1730
1731 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1732#undef INSTRUCTION_TYPE_CHECK
1733
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001734 // Returns whether the instruction can be moved within the graph.
1735 virtual bool CanBeMoved() const { return false; }
1736
1737 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001738 virtual bool InstructionTypeEquals(HInstruction* other) const {
1739 UNUSED(other);
1740 return false;
1741 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001742
1743 // Returns whether any data encoded in the two instructions is equal.
1744 // This method does not look at the inputs. Both instructions must be
1745 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001746 virtual bool InstructionDataEquals(HInstruction* other) const {
1747 UNUSED(other);
1748 return false;
1749 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001750
1751 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001752 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001753 // 2) Their inputs are identical.
1754 bool Equals(HInstruction* other) const;
1755
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001756 virtual InstructionKind GetKind() const = 0;
1757
1758 virtual size_t ComputeHashCode() const {
1759 size_t result = GetKind();
1760 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1761 result = (result * 31) + InputAt(i)->GetId();
1762 }
1763 return result;
1764 }
1765
1766 SideEffects GetSideEffects() const { return side_effects_; }
1767
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001768 size_t GetLifetimePosition() const { return lifetime_position_; }
1769 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1770 LiveInterval* GetLiveInterval() const { return live_interval_; }
1771 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1772 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1773
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001774 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1775
1776 // Returns whether the code generation of the instruction will require to have access
1777 // to the current method. Such instructions are:
1778 // (1): Instructions that require an environment, as calling the runtime requires
1779 // to walk the stack and have the current method stored at a specific stack address.
1780 // (2): Object literals like classes and strings, that are loaded from the dex cache
1781 // fields of the current method.
1782 bool NeedsCurrentMethod() const {
1783 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1784 }
1785
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001786 virtual bool NeedsDexCache() const { return false; }
1787
Mark Mendellc4701932015-04-10 13:18:51 -04001788 // Does this instruction have any use in an environment before
1789 // control flow hits 'other'?
1790 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1791
1792 // Remove all references to environment uses of this instruction.
1793 // The caller must ensure that this is safe to do.
1794 void RemoveEnvironmentUsers();
1795
David Brazdil1abb4192015-02-17 18:33:36 +00001796 protected:
1797 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1798 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1799
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001800 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001801 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1802
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001803 HInstruction* previous_;
1804 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001805 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001806
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001807 // An instruction gets an id when it is added to the graph.
1808 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001809 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001810 int id_;
1811
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001812 // When doing liveness analysis, instructions that have uses get an SSA index.
1813 int ssa_index_;
1814
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001815 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001816 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001817
1818 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001819 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001820
Nicolas Geoffray39468442014-09-02 15:17:15 +01001821 // The environment associated with this instruction. Not null if the instruction
1822 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001823 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001824
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001825 // Set by the code generator.
1826 LocationSummary* locations_;
1827
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001828 // Set by the liveness analysis.
1829 LiveInterval* live_interval_;
1830
1831 // Set by the liveness analysis, this is the position in a linear
1832 // order of blocks where this instruction's live interval start.
1833 size_t lifetime_position_;
1834
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001835 const SideEffects side_effects_;
1836
Calin Juravleacf735c2015-02-12 15:25:22 +00001837 // TODO: for primitive types this should be marked as invalid.
1838 ReferenceTypeInfo reference_type_info_;
1839
David Brazdil1abb4192015-02-17 18:33:36 +00001840 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001841 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001842 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001843 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001844 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001845
1846 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1847};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001848std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001849
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001850class HInputIterator : public ValueObject {
1851 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001852 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001853
1854 bool Done() const { return index_ == instruction_->InputCount(); }
1855 HInstruction* Current() const { return instruction_->InputAt(index_); }
1856 void Advance() { index_++; }
1857
1858 private:
1859 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001860 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001861
1862 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1863};
1864
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001865class HInstructionIterator : public ValueObject {
1866 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001867 explicit HInstructionIterator(const HInstructionList& instructions)
1868 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001869 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001870 }
1871
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001872 bool Done() const { return instruction_ == nullptr; }
1873 HInstruction* Current() const { return instruction_; }
1874 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001875 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001876 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001877 }
1878
1879 private:
1880 HInstruction* instruction_;
1881 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001882
1883 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001884};
1885
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001886class HBackwardInstructionIterator : public ValueObject {
1887 public:
1888 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1889 : instruction_(instructions.last_instruction_) {
1890 next_ = Done() ? nullptr : instruction_->GetPrevious();
1891 }
1892
1893 bool Done() const { return instruction_ == nullptr; }
1894 HInstruction* Current() const { return instruction_; }
1895 void Advance() {
1896 instruction_ = next_;
1897 next_ = Done() ? nullptr : instruction_->GetPrevious();
1898 }
1899
1900 private:
1901 HInstruction* instruction_;
1902 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001903
1904 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001905};
1906
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001907// An embedded container with N elements of type T. Used (with partial
1908// specialization for N=0) because embedded arrays cannot have size 0.
1909template<typename T, intptr_t N>
1910class EmbeddedArray {
1911 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001912 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001913
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001914 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001915
1916 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001917 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001918 return elements_[i];
1919 }
1920
1921 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001922 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001923 return elements_[i];
1924 }
1925
1926 const T& At(intptr_t i) const {
1927 return (*this)[i];
1928 }
1929
1930 void SetAt(intptr_t i, const T& val) {
1931 (*this)[i] = val;
1932 }
1933
1934 private:
1935 T elements_[N];
1936};
1937
1938template<typename T>
1939class EmbeddedArray<T, 0> {
1940 public:
1941 intptr_t length() const { return 0; }
1942 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001943 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001944 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001945 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001946 }
1947 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001948 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001949 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001950 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001951 }
1952};
1953
1954template<intptr_t N>
1955class HTemplateInstruction: public HInstruction {
1956 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001957 HTemplateInstruction<N>(SideEffects side_effects)
1958 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001959 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001960
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001961 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001962
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001963 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001964 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1965
1966 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1967 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001968 }
1969
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001970 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001971 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001972
1973 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001974};
1975
Dave Allison20dfc792014-06-16 20:44:29 -07001976template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001977class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001978 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001979 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1980 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001981 virtual ~HExpression() {}
1982
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001983 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001984
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001985 protected:
1986 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001987};
1988
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001989// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1990// instruction that branches to the exit block.
1991class HReturnVoid : public HTemplateInstruction<0> {
1992 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001993 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001994
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001995 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001996
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001997 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001998
1999 private:
2000 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2001};
2002
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002003// Represents dex's RETURN opcodes. A HReturn is a control flow
2004// instruction that branches to the exit block.
2005class HReturn : public HTemplateInstruction<1> {
2006 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002007 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002008 SetRawInputAt(0, value);
2009 }
2010
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002011 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002012
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002013 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002014
2015 private:
2016 DISALLOW_COPY_AND_ASSIGN(HReturn);
2017};
2018
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002019// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002020// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002021// exit block.
2022class HExit : public HTemplateInstruction<0> {
2023 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002024 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002025
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002026 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002027
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002028 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002029
2030 private:
2031 DISALLOW_COPY_AND_ASSIGN(HExit);
2032};
2033
2034// Jumps from one block to another.
2035class HGoto : public HTemplateInstruction<0> {
2036 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002037 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2038
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002039 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002040
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002041 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002042 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002043 }
2044
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002045 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002046
2047 private:
2048 DISALLOW_COPY_AND_ASSIGN(HGoto);
2049};
2050
Dave Allison20dfc792014-06-16 20:44:29 -07002051
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002052// Conditional branch. A block ending with an HIf instruction must have
2053// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002054class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002055 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002056 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002057 SetRawInputAt(0, input);
2058 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002059
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002060 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002061
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002062 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002063 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002064 }
2065
2066 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002067 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002068 }
2069
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002070 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002071
2072 private:
2073 DISALLOW_COPY_AND_ASSIGN(HIf);
2074};
2075
David Brazdilfc6a86a2015-06-26 10:33:45 +00002076
2077// Abstract instruction which marks the beginning and/or end of a try block and
2078// links it to the respective exception handlers. Behaves the same as a Goto in
2079// non-exceptional control flow.
2080// Normal-flow successor is stored at index zero, exception handlers under
2081// higher indices in no particular order.
2082class HTryBoundary : public HTemplateInstruction<0> {
2083 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002084 enum BoundaryKind {
2085 kEntry,
2086 kExit,
2087 };
2088
2089 explicit HTryBoundary(BoundaryKind kind)
2090 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002091
2092 bool IsControlFlow() const OVERRIDE { return true; }
2093
2094 // Returns the block's non-exceptional successor (index zero).
2095 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2096
2097 // Returns whether `handler` is among its exception handlers (non-zero index
2098 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002099 bool HasExceptionHandler(const HBasicBlock& handler) const {
2100 DCHECK(handler.IsCatchBlock());
2101 return GetBlock()->GetSuccessors().Contains(
2102 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002103 }
2104
2105 // If not present already, adds `handler` to its block's list of exception
2106 // handlers.
2107 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002108 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002109 GetBlock()->AddSuccessor(handler);
2110 }
2111 }
2112
David Brazdil56e1acc2015-06-30 15:41:36 +01002113 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002114
David Brazdilffee3d32015-07-06 11:48:53 +01002115 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2116
David Brazdilfc6a86a2015-06-26 10:33:45 +00002117 DECLARE_INSTRUCTION(TryBoundary);
2118
2119 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002120 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002121
2122 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2123};
2124
David Brazdilffee3d32015-07-06 11:48:53 +01002125// Iterator over exception handlers of a given HTryBoundary, i.e. over
2126// exceptional successors of its basic block.
2127class HExceptionHandlerIterator : public ValueObject {
2128 public:
2129 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2130 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2131
2132 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2133 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
2134 size_t CurrentSuccessorIndex() const { return index_; }
2135 void Advance() { ++index_; }
2136
2137 private:
2138 const HBasicBlock& block_;
2139 size_t index_;
2140
2141 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2142};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002143
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002144// Deoptimize to interpreter, upon checking a condition.
2145class HDeoptimize : public HTemplateInstruction<1> {
2146 public:
2147 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2148 : HTemplateInstruction(SideEffects::None()),
2149 dex_pc_(dex_pc) {
2150 SetRawInputAt(0, cond);
2151 }
2152
2153 bool NeedsEnvironment() const OVERRIDE { return true; }
2154 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002155 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002156
2157 DECLARE_INSTRUCTION(Deoptimize);
2158
2159 private:
2160 uint32_t dex_pc_;
2161
2162 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2163};
2164
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002165// Represents the ArtMethod that was passed as a first argument to
2166// the method. It is used by instructions that depend on it, like
2167// instructions that work with the dex cache.
2168class HCurrentMethod : public HExpression<0> {
2169 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002170 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002171
2172 DECLARE_INSTRUCTION(CurrentMethod);
2173
2174 private:
2175 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2176};
2177
Roland Levillain88cb1752014-10-20 16:36:47 +01002178class HUnaryOperation : public HExpression<1> {
2179 public:
2180 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2181 : HExpression(result_type, SideEffects::None()) {
2182 SetRawInputAt(0, input);
2183 }
2184
2185 HInstruction* GetInput() const { return InputAt(0); }
2186 Primitive::Type GetResultType() const { return GetType(); }
2187
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002188 bool CanBeMoved() const OVERRIDE { return true; }
2189 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002190 UNUSED(other);
2191 return true;
2192 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002193
Roland Levillain9240d6a2014-10-20 16:47:04 +01002194 // Try to statically evaluate `operation` and return a HConstant
2195 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002196 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002197 HConstant* TryStaticEvaluation() const;
2198
2199 // Apply this operation to `x`.
2200 virtual int32_t Evaluate(int32_t x) const = 0;
2201 virtual int64_t Evaluate(int64_t x) const = 0;
2202
Roland Levillain88cb1752014-10-20 16:36:47 +01002203 DECLARE_INSTRUCTION(UnaryOperation);
2204
2205 private:
2206 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2207};
2208
Dave Allison20dfc792014-06-16 20:44:29 -07002209class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002210 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002211 HBinaryOperation(Primitive::Type result_type,
2212 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002213 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002214 SetRawInputAt(0, left);
2215 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002216 }
2217
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002218 HInstruction* GetLeft() const { return InputAt(0); }
2219 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002220 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002221
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002222 virtual bool IsCommutative() const { return false; }
2223
2224 // Put constant on the right.
2225 // Returns whether order is changed.
2226 bool OrderInputsWithConstantOnTheRight() {
2227 HInstruction* left = InputAt(0);
2228 HInstruction* right = InputAt(1);
2229 if (left->IsConstant() && !right->IsConstant()) {
2230 ReplaceInput(right, 0);
2231 ReplaceInput(left, 1);
2232 return true;
2233 }
2234 return false;
2235 }
2236
2237 // Order inputs by instruction id, but favor constant on the right side.
2238 // This helps GVN for commutative ops.
2239 void OrderInputs() {
2240 DCHECK(IsCommutative());
2241 HInstruction* left = InputAt(0);
2242 HInstruction* right = InputAt(1);
2243 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2244 return;
2245 }
2246 if (OrderInputsWithConstantOnTheRight()) {
2247 return;
2248 }
2249 // Order according to instruction id.
2250 if (left->GetId() > right->GetId()) {
2251 ReplaceInput(right, 0);
2252 ReplaceInput(left, 1);
2253 }
2254 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002255
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002256 bool CanBeMoved() const OVERRIDE { return true; }
2257 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002258 UNUSED(other);
2259 return true;
2260 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002261
Roland Levillain9240d6a2014-10-20 16:47:04 +01002262 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002263 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002264 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002265 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002266
2267 // Apply this operation to `x` and `y`.
2268 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2269 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2270
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002271 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002272 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002273 HConstant* GetConstantRight() const;
2274
2275 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002276 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002277 HInstruction* GetLeastConstantLeft() const;
2278
Roland Levillainccc07a92014-09-16 14:48:16 +01002279 DECLARE_INSTRUCTION(BinaryOperation);
2280
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002281 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002282 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2283};
2284
Mark Mendellc4701932015-04-10 13:18:51 -04002285// The comparison bias applies for floating point operations and indicates how NaN
2286// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002287enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002288 kNoBias, // bias is not applicable (i.e. for long operation)
2289 kGtBias, // return 1 for NaN comparisons
2290 kLtBias, // return -1 for NaN comparisons
2291};
2292
Dave Allison20dfc792014-06-16 20:44:29 -07002293class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002294 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002295 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002296 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002297 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002298 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002299
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002300 bool NeedsMaterialization() const { return needs_materialization_; }
2301 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002302
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002303 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002304 // `instruction`, and disregard moves in between.
2305 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002306
Dave Allison20dfc792014-06-16 20:44:29 -07002307 DECLARE_INSTRUCTION(Condition);
2308
2309 virtual IfCondition GetCondition() const = 0;
2310
Mark Mendellc4701932015-04-10 13:18:51 -04002311 virtual IfCondition GetOppositeCondition() const = 0;
2312
Roland Levillain4fa13f62015-07-06 18:11:54 +01002313 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002314
2315 void SetBias(ComparisonBias bias) { bias_ = bias; }
2316
2317 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2318 return bias_ == other->AsCondition()->bias_;
2319 }
2320
Roland Levillain4fa13f62015-07-06 18:11:54 +01002321 bool IsFPConditionTrueIfNaN() const {
2322 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2323 IfCondition if_cond = GetCondition();
2324 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2325 }
2326
2327 bool IsFPConditionFalseIfNaN() const {
2328 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2329 IfCondition if_cond = GetCondition();
2330 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2331 }
2332
Dave Allison20dfc792014-06-16 20:44:29 -07002333 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002334 // For register allocation purposes, returns whether this instruction needs to be
2335 // materialized (that is, not just be in the processor flags).
2336 bool needs_materialization_;
2337
Mark Mendellc4701932015-04-10 13:18:51 -04002338 // Needed if we merge a HCompare into a HCondition.
2339 ComparisonBias bias_;
2340
Dave Allison20dfc792014-06-16 20:44:29 -07002341 DISALLOW_COPY_AND_ASSIGN(HCondition);
2342};
2343
2344// Instruction to check if two inputs are equal to each other.
2345class HEqual : public HCondition {
2346 public:
2347 HEqual(HInstruction* first, HInstruction* second)
2348 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002349
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002350 bool IsCommutative() const OVERRIDE { return true; }
2351
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002352 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002353 return x == y ? 1 : 0;
2354 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002355 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002356 return x == y ? 1 : 0;
2357 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002358
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002359 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002360
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002361 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002362 return kCondEQ;
2363 }
2364
Mark Mendellc4701932015-04-10 13:18:51 -04002365 IfCondition GetOppositeCondition() const OVERRIDE {
2366 return kCondNE;
2367 }
2368
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002369 private:
2370 DISALLOW_COPY_AND_ASSIGN(HEqual);
2371};
2372
Dave Allison20dfc792014-06-16 20:44:29 -07002373class HNotEqual : public HCondition {
2374 public:
2375 HNotEqual(HInstruction* first, HInstruction* second)
2376 : HCondition(first, second) {}
2377
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002378 bool IsCommutative() const OVERRIDE { return true; }
2379
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002380 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002381 return x != y ? 1 : 0;
2382 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002383 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002384 return x != y ? 1 : 0;
2385 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002386
Dave Allison20dfc792014-06-16 20:44:29 -07002387 DECLARE_INSTRUCTION(NotEqual);
2388
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002389 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002390 return kCondNE;
2391 }
2392
Mark Mendellc4701932015-04-10 13:18:51 -04002393 IfCondition GetOppositeCondition() const OVERRIDE {
2394 return kCondEQ;
2395 }
2396
Dave Allison20dfc792014-06-16 20:44:29 -07002397 private:
2398 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2399};
2400
2401class HLessThan : public HCondition {
2402 public:
2403 HLessThan(HInstruction* first, HInstruction* second)
2404 : HCondition(first, second) {}
2405
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002406 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002407 return x < y ? 1 : 0;
2408 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002409 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002410 return x < y ? 1 : 0;
2411 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002412
Dave Allison20dfc792014-06-16 20:44:29 -07002413 DECLARE_INSTRUCTION(LessThan);
2414
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002415 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002416 return kCondLT;
2417 }
2418
Mark Mendellc4701932015-04-10 13:18:51 -04002419 IfCondition GetOppositeCondition() const OVERRIDE {
2420 return kCondGE;
2421 }
2422
Dave Allison20dfc792014-06-16 20:44:29 -07002423 private:
2424 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2425};
2426
2427class HLessThanOrEqual : public HCondition {
2428 public:
2429 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2430 : HCondition(first, second) {}
2431
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002432 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002433 return x <= y ? 1 : 0;
2434 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002435 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002436 return x <= y ? 1 : 0;
2437 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002438
Dave Allison20dfc792014-06-16 20:44:29 -07002439 DECLARE_INSTRUCTION(LessThanOrEqual);
2440
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002441 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002442 return kCondLE;
2443 }
2444
Mark Mendellc4701932015-04-10 13:18:51 -04002445 IfCondition GetOppositeCondition() const OVERRIDE {
2446 return kCondGT;
2447 }
2448
Dave Allison20dfc792014-06-16 20:44:29 -07002449 private:
2450 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2451};
2452
2453class HGreaterThan : public HCondition {
2454 public:
2455 HGreaterThan(HInstruction* first, HInstruction* second)
2456 : HCondition(first, second) {}
2457
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002458 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002459 return x > y ? 1 : 0;
2460 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002461 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002462 return x > y ? 1 : 0;
2463 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002464
Dave Allison20dfc792014-06-16 20:44:29 -07002465 DECLARE_INSTRUCTION(GreaterThan);
2466
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002467 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002468 return kCondGT;
2469 }
2470
Mark Mendellc4701932015-04-10 13:18:51 -04002471 IfCondition GetOppositeCondition() const OVERRIDE {
2472 return kCondLE;
2473 }
2474
Dave Allison20dfc792014-06-16 20:44:29 -07002475 private:
2476 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2477};
2478
2479class HGreaterThanOrEqual : public HCondition {
2480 public:
2481 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2482 : HCondition(first, second) {}
2483
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002484 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002485 return x >= y ? 1 : 0;
2486 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002487 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002488 return x >= y ? 1 : 0;
2489 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002490
Dave Allison20dfc792014-06-16 20:44:29 -07002491 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2492
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002493 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002494 return kCondGE;
2495 }
2496
Mark Mendellc4701932015-04-10 13:18:51 -04002497 IfCondition GetOppositeCondition() const OVERRIDE {
2498 return kCondLT;
2499 }
2500
Dave Allison20dfc792014-06-16 20:44:29 -07002501 private:
2502 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2503};
2504
2505
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002506// Instruction to check how two inputs compare to each other.
2507// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2508class HCompare : public HBinaryOperation {
2509 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002510 HCompare(Primitive::Type type,
2511 HInstruction* first,
2512 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002513 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002514 uint32_t dex_pc)
2515 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002516 DCHECK_EQ(type, first->GetType());
2517 DCHECK_EQ(type, second->GetType());
2518 }
2519
Calin Juravleddb7df22014-11-25 20:56:51 +00002520 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002521 return
2522 x == y ? 0 :
2523 x > y ? 1 :
2524 -1;
2525 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002526
2527 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002528 return
2529 x == y ? 0 :
2530 x > y ? 1 :
2531 -1;
2532 }
2533
Calin Juravleddb7df22014-11-25 20:56:51 +00002534 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2535 return bias_ == other->AsCompare()->bias_;
2536 }
2537
Mark Mendellc4701932015-04-10 13:18:51 -04002538 ComparisonBias GetBias() const { return bias_; }
2539
Roland Levillain4fa13f62015-07-06 18:11:54 +01002540 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002541
Alexey Frunze4dda3372015-06-01 18:31:49 -07002542 uint32_t GetDexPc() const { return dex_pc_; }
2543
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002544 DECLARE_INSTRUCTION(Compare);
2545
2546 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002547 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002548 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002549
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002550 DISALLOW_COPY_AND_ASSIGN(HCompare);
2551};
2552
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002553// A local in the graph. Corresponds to a Dex register.
2554class HLocal : public HTemplateInstruction<0> {
2555 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002556 explicit HLocal(uint16_t reg_number)
2557 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002558
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002559 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002560
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002561 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002562
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002563 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002564 // The Dex register number.
2565 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002566
2567 DISALLOW_COPY_AND_ASSIGN(HLocal);
2568};
2569
2570// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002571class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002572 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002573 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002574 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002575 SetRawInputAt(0, local);
2576 }
2577
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002578 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2579
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002580 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002581
2582 private:
2583 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2584};
2585
2586// Store a value in a given local. This instruction has two inputs: the value
2587// and the local.
2588class HStoreLocal : public HTemplateInstruction<2> {
2589 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002590 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002591 SetRawInputAt(0, local);
2592 SetRawInputAt(1, value);
2593 }
2594
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002595 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2596
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002597 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002598
2599 private:
2600 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2601};
2602
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002603class HConstant : public HExpression<0> {
2604 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002605 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2606
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002607 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002608
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002609 virtual bool IsMinusOne() const { return false; }
2610 virtual bool IsZero() const { return false; }
2611 virtual bool IsOne() const { return false; }
2612
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002613 DECLARE_INSTRUCTION(Constant);
2614
2615 private:
2616 DISALLOW_COPY_AND_ASSIGN(HConstant);
2617};
2618
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002619class HFloatConstant : public HConstant {
2620 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002621 float GetValue() const { return value_; }
2622
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002623 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002624 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2625 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002626 }
2627
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002628 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002629
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002630 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002631 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002632 }
2633 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002634 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002635 }
2636 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002637 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2638 }
2639 bool IsNaN() const {
2640 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002641 }
2642
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002643 DECLARE_INSTRUCTION(FloatConstant);
2644
2645 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002646 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002647 explicit HFloatConstant(int32_t value)
2648 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002649
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002650 const float value_;
2651
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002652 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002653 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002654 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002655 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2656};
2657
2658class HDoubleConstant : public HConstant {
2659 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002660 double GetValue() const { return value_; }
2661
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002662 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002663 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2664 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002665 }
2666
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002667 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002668
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002669 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002670 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002671 }
2672 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002673 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002674 }
2675 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002676 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2677 }
2678 bool IsNaN() const {
2679 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002680 }
2681
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002682 DECLARE_INSTRUCTION(DoubleConstant);
2683
2684 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002685 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002686 explicit HDoubleConstant(int64_t value)
2687 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002688
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002689 const double value_;
2690
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002691 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002692 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002693 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002694 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2695};
2696
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002697class HNullConstant : public HConstant {
2698 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002699 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2700 return true;
2701 }
2702
2703 size_t ComputeHashCode() const OVERRIDE { return 0; }
2704
2705 DECLARE_INSTRUCTION(NullConstant);
2706
2707 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002708 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2709
2710 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002711 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2712};
2713
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002714// Constants of the type int. Those can be from Dex instructions, or
2715// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002716class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002717 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002718 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002719
Calin Juravle61d544b2015-02-23 16:46:57 +00002720 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002721 return other->AsIntConstant()->value_ == value_;
2722 }
2723
Calin Juravle61d544b2015-02-23 16:46:57 +00002724 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2725
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002726 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2727 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2728 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2729
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002730 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002731
2732 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002733 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2734
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002735 const int32_t value_;
2736
David Brazdil8d5b8b22015-03-24 10:51:52 +00002737 friend class HGraph;
2738 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002739 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002740 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2741};
2742
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002743class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002744 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002745 int64_t GetValue() const { return value_; }
2746
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002747 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002748 return other->AsLongConstant()->value_ == value_;
2749 }
2750
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002751 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002752
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002753 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2754 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2755 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2756
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002757 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002758
2759 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002760 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2761
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002762 const int64_t value_;
2763
David Brazdil8d5b8b22015-03-24 10:51:52 +00002764 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002765 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2766};
2767
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002768enum class Intrinsics {
2769#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2770#include "intrinsics_list.h"
2771 kNone,
2772 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2773#undef INTRINSICS_LIST
2774#undef OPTIMIZING_INTRINSICS
2775};
2776std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2777
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002778class HInvoke : public HInstruction {
2779 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002780 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002781
2782 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2783 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002784 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002785
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002786 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002787 SetRawInputAt(index, argument);
2788 }
2789
Roland Levillain3e3d7332015-04-28 11:00:54 +01002790 // Return the number of arguments. This number can be lower than
2791 // the number of inputs returned by InputCount(), as some invoke
2792 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2793 // inputs at the end of their list of inputs.
2794 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2795
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002796 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002797
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002798 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002799
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002800 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002801 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002802
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002803 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2804
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002805 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002806 return intrinsic_;
2807 }
2808
2809 void SetIntrinsic(Intrinsics intrinsic) {
2810 intrinsic_ = intrinsic;
2811 }
2812
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002813 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002814 return GetEnvironment()->GetParent() != nullptr;
2815 }
2816
2817 bool CanThrow() const OVERRIDE { return true; }
2818
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002819 DECLARE_INSTRUCTION(Invoke);
2820
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002821 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002822 HInvoke(ArenaAllocator* arena,
2823 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002824 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002825 Primitive::Type return_type,
2826 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002827 uint32_t dex_method_index,
2828 InvokeType original_invoke_type)
Aart Bik854a02b2015-07-14 16:07:00 -07002829 : HInstruction(SideEffects::All()), // assume write/read on all fields/arrays
Roland Levillain3e3d7332015-04-28 11:00:54 +01002830 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002831 inputs_(arena, number_of_arguments),
2832 return_type_(return_type),
2833 dex_pc_(dex_pc),
2834 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002835 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002836 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002837 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2838 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002839 }
2840
David Brazdil1abb4192015-02-17 18:33:36 +00002841 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2842 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2843 inputs_.Put(index, input);
2844 }
2845
Roland Levillain3e3d7332015-04-28 11:00:54 +01002846 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002847 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002848 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002849 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002850 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002851 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002852 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002853
2854 private:
2855 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2856};
2857
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002858class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002859 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002860 // Requirements of this method call regarding the class
2861 // initialization (clinit) check of its declaring class.
2862 enum class ClinitCheckRequirement {
2863 kNone, // Class already initialized.
2864 kExplicit, // Static call having explicit clinit check as last input.
2865 kImplicit, // Static call implicitly requiring a clinit check.
2866 };
2867
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002868 HInvokeStaticOrDirect(ArenaAllocator* arena,
2869 uint32_t number_of_arguments,
2870 Primitive::Type return_type,
2871 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002872 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002873 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002874 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002875 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002876 InvokeType invoke_type,
2877 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002878 : HInvoke(arena,
2879 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002880 // There is one extra argument for the HCurrentMethod node, and
2881 // potentially one other if the clinit check is explicit, and one other
2882 // if the method is a string factory.
2883 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
2884 + (string_init_offset ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002885 return_type,
2886 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002887 dex_method_index,
2888 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002889 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002890 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002891 clinit_check_requirement_(clinit_check_requirement),
2892 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002893
Calin Juravle641547a2015-04-21 22:08:51 +01002894 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2895 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002896 // We access the method via the dex cache so we can't do an implicit null check.
2897 // TODO: for intrinsics we can generate implicit null checks.
2898 return false;
2899 }
2900
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002901 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002902 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002903 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002904 bool IsStringInit() const { return string_init_offset_ != 0; }
2905 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002906 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002907
Roland Levillain4c0eb422015-04-24 16:43:49 +01002908 // Is this instruction a call to a static method?
2909 bool IsStatic() const {
2910 return GetInvokeType() == kStatic;
2911 }
2912
Roland Levillain3e3d7332015-04-28 11:00:54 +01002913 // Remove the art::HLoadClass instruction set as last input by
2914 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2915 // the initial art::HClinitCheck instruction (only relevant for
2916 // static calls with explicit clinit check).
2917 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002918 DCHECK(IsStaticWithExplicitClinitCheck());
2919 size_t last_input_index = InputCount() - 1;
2920 HInstruction* last_input = InputAt(last_input_index);
2921 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002922 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002923 RemoveAsUserOfInput(last_input_index);
2924 inputs_.DeleteAt(last_input_index);
2925 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2926 DCHECK(IsStaticWithImplicitClinitCheck());
2927 }
2928
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002929 bool IsStringFactoryFor(HFakeString* str) const {
2930 if (!IsStringInit()) return false;
2931 // +1 for the current method.
2932 if (InputCount() == (number_of_arguments_ + 1)) return false;
2933 return InputAt(InputCount() - 1)->AsFakeString() == str;
2934 }
2935
2936 void RemoveFakeStringArgumentAsLastInput() {
2937 DCHECK(IsStringInit());
2938 size_t last_input_index = InputCount() - 1;
2939 HInstruction* last_input = InputAt(last_input_index);
2940 DCHECK(last_input != nullptr);
2941 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
2942 RemoveAsUserOfInput(last_input_index);
2943 inputs_.DeleteAt(last_input_index);
2944 }
2945
Roland Levillain4c0eb422015-04-24 16:43:49 +01002946 // Is this a call to a static method whose declaring class has an
2947 // explicit intialization check in the graph?
2948 bool IsStaticWithExplicitClinitCheck() const {
2949 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2950 }
2951
2952 // Is this a call to a static method whose declaring class has an
2953 // implicit intialization check requirement?
2954 bool IsStaticWithImplicitClinitCheck() const {
2955 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2956 }
2957
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002958 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002959
Roland Levillain4c0eb422015-04-24 16:43:49 +01002960 protected:
2961 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2962 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2963 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2964 HInstruction* input = input_record.GetInstruction();
2965 // `input` is the last input of a static invoke marked as having
2966 // an explicit clinit check. It must either be:
2967 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2968 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2969 DCHECK(input != nullptr);
2970 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2971 }
2972 return input_record;
2973 }
2974
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002975 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002976 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002977 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002978 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002979 // Thread entrypoint offset for string init method if this is a string init invoke.
2980 // Note that there are multiple string init methods, each having its own offset.
2981 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002982
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002983 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002984};
2985
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002986class HInvokeVirtual : public HInvoke {
2987 public:
2988 HInvokeVirtual(ArenaAllocator* arena,
2989 uint32_t number_of_arguments,
2990 Primitive::Type return_type,
2991 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002992 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002993 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002994 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002995 vtable_index_(vtable_index) {}
2996
Calin Juravle641547a2015-04-21 22:08:51 +01002997 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002998 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002999 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003000 }
3001
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003002 uint32_t GetVTableIndex() const { return vtable_index_; }
3003
3004 DECLARE_INSTRUCTION(InvokeVirtual);
3005
3006 private:
3007 const uint32_t vtable_index_;
3008
3009 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3010};
3011
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003012class HInvokeInterface : public HInvoke {
3013 public:
3014 HInvokeInterface(ArenaAllocator* arena,
3015 uint32_t number_of_arguments,
3016 Primitive::Type return_type,
3017 uint32_t dex_pc,
3018 uint32_t dex_method_index,
3019 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003020 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003021 imt_index_(imt_index) {}
3022
Calin Juravle641547a2015-04-21 22:08:51 +01003023 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003024 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003025 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003026 }
3027
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003028 uint32_t GetImtIndex() const { return imt_index_; }
3029 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3030
3031 DECLARE_INSTRUCTION(InvokeInterface);
3032
3033 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003034 const uint32_t imt_index_;
3035
3036 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3037};
3038
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003039class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003040 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003041 HNewInstance(HCurrentMethod* current_method,
3042 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003043 uint16_t type_index,
3044 const DexFile& dex_file,
3045 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003046 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3047 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003048 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003049 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003050 entrypoint_(entrypoint) {
3051 SetRawInputAt(0, current_method);
3052 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003053
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003054 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003055 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003056 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003057
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003058 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003059 bool NeedsEnvironment() const OVERRIDE { return true; }
3060 // It may throw when called on:
3061 // - interfaces
3062 // - abstract/innaccessible/unknown classes
3063 // TODO: optimize when possible.
3064 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003065
Calin Juravle10e244f2015-01-26 18:54:32 +00003066 bool CanBeNull() const OVERRIDE { return false; }
3067
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003068 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3069
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003070 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003071
3072 private:
3073 const uint32_t dex_pc_;
3074 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003075 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003076 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003077
3078 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3079};
3080
Roland Levillain88cb1752014-10-20 16:36:47 +01003081class HNeg : public HUnaryOperation {
3082 public:
3083 explicit HNeg(Primitive::Type result_type, HInstruction* input)
3084 : HUnaryOperation(result_type, input) {}
3085
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003086 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
3087 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003088
Roland Levillain88cb1752014-10-20 16:36:47 +01003089 DECLARE_INSTRUCTION(Neg);
3090
3091 private:
3092 DISALLOW_COPY_AND_ASSIGN(HNeg);
3093};
3094
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003095class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003096 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003097 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003098 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003099 uint32_t dex_pc,
3100 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003101 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003102 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003103 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3104 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003105 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003106 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003107 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003108 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003109 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003110 }
3111
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003112 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003113 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003114 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003115
3116 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003117 bool NeedsEnvironment() const OVERRIDE { return true; }
3118
Mingyao Yang0c365e62015-03-31 15:09:29 -07003119 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3120 bool CanThrow() const OVERRIDE { return true; }
3121
Calin Juravle10e244f2015-01-26 18:54:32 +00003122 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003123
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003124 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3125
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003126 DECLARE_INSTRUCTION(NewArray);
3127
3128 private:
3129 const uint32_t dex_pc_;
3130 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003131 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003132 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003133
3134 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3135};
3136
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003137class HAdd : public HBinaryOperation {
3138 public:
3139 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3140 : HBinaryOperation(result_type, left, right) {}
3141
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003142 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003143
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003144 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003145 return x + y;
3146 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003147 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003148 return x + y;
3149 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003150
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003151 DECLARE_INSTRUCTION(Add);
3152
3153 private:
3154 DISALLOW_COPY_AND_ASSIGN(HAdd);
3155};
3156
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003157class HSub : public HBinaryOperation {
3158 public:
3159 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3160 : HBinaryOperation(result_type, left, right) {}
3161
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003162 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003163 return x - y;
3164 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003165 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003166 return x - y;
3167 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003168
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003169 DECLARE_INSTRUCTION(Sub);
3170
3171 private:
3172 DISALLOW_COPY_AND_ASSIGN(HSub);
3173};
3174
Calin Juravle34bacdf2014-10-07 20:23:36 +01003175class HMul : public HBinaryOperation {
3176 public:
3177 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3178 : HBinaryOperation(result_type, left, right) {}
3179
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003180 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003181
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003182 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
3183 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003184
3185 DECLARE_INSTRUCTION(Mul);
3186
3187 private:
3188 DISALLOW_COPY_AND_ASSIGN(HMul);
3189};
3190
Calin Juravle7c4954d2014-10-28 16:57:40 +00003191class HDiv : public HBinaryOperation {
3192 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003193 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3194 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003195
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003196 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003197 // Our graph structure ensures we never have 0 for `y` during constant folding.
3198 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003199 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003200 return (y == -1) ? -x : x / y;
3201 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003202
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003203 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003204 DCHECK_NE(y, 0);
3205 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3206 return (y == -1) ? -x : x / y;
3207 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003208
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003209 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003210
Calin Juravle7c4954d2014-10-28 16:57:40 +00003211 DECLARE_INSTRUCTION(Div);
3212
3213 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003214 const uint32_t dex_pc_;
3215
Calin Juravle7c4954d2014-10-28 16:57:40 +00003216 DISALLOW_COPY_AND_ASSIGN(HDiv);
3217};
3218
Calin Juravlebacfec32014-11-14 15:54:36 +00003219class HRem : public HBinaryOperation {
3220 public:
3221 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3222 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
3223
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003224 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003225 DCHECK_NE(y, 0);
3226 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3227 return (y == -1) ? 0 : x % y;
3228 }
3229
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003230 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003231 DCHECK_NE(y, 0);
3232 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3233 return (y == -1) ? 0 : x % y;
3234 }
3235
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003236 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003237
3238 DECLARE_INSTRUCTION(Rem);
3239
3240 private:
3241 const uint32_t dex_pc_;
3242
3243 DISALLOW_COPY_AND_ASSIGN(HRem);
3244};
3245
Calin Juravled0d48522014-11-04 16:40:20 +00003246class HDivZeroCheck : public HExpression<1> {
3247 public:
3248 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3249 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3250 SetRawInputAt(0, value);
3251 }
3252
3253 bool CanBeMoved() const OVERRIDE { return true; }
3254
3255 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3256 UNUSED(other);
3257 return true;
3258 }
3259
3260 bool NeedsEnvironment() const OVERRIDE { return true; }
3261 bool CanThrow() const OVERRIDE { return true; }
3262
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003263 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003264
3265 DECLARE_INSTRUCTION(DivZeroCheck);
3266
3267 private:
3268 const uint32_t dex_pc_;
3269
3270 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3271};
3272
Calin Juravle9aec02f2014-11-18 23:06:35 +00003273class HShl : public HBinaryOperation {
3274 public:
3275 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3276 : HBinaryOperation(result_type, left, right) {}
3277
3278 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
3279 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
3280
3281 DECLARE_INSTRUCTION(Shl);
3282
3283 private:
3284 DISALLOW_COPY_AND_ASSIGN(HShl);
3285};
3286
3287class HShr : public HBinaryOperation {
3288 public:
3289 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3290 : HBinaryOperation(result_type, left, right) {}
3291
3292 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
3293 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
3294
3295 DECLARE_INSTRUCTION(Shr);
3296
3297 private:
3298 DISALLOW_COPY_AND_ASSIGN(HShr);
3299};
3300
3301class HUShr : public HBinaryOperation {
3302 public:
3303 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3304 : HBinaryOperation(result_type, left, right) {}
3305
3306 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
3307 uint32_t ux = static_cast<uint32_t>(x);
3308 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
3309 return static_cast<int32_t>(ux >> uy);
3310 }
3311
3312 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
3313 uint64_t ux = static_cast<uint64_t>(x);
3314 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
3315 return static_cast<int64_t>(ux >> uy);
3316 }
3317
3318 DECLARE_INSTRUCTION(UShr);
3319
3320 private:
3321 DISALLOW_COPY_AND_ASSIGN(HUShr);
3322};
3323
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003324class HAnd : public HBinaryOperation {
3325 public:
3326 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3327 : HBinaryOperation(result_type, left, right) {}
3328
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003329 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003330
3331 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3332 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3333
3334 DECLARE_INSTRUCTION(And);
3335
3336 private:
3337 DISALLOW_COPY_AND_ASSIGN(HAnd);
3338};
3339
3340class HOr : public HBinaryOperation {
3341 public:
3342 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3343 : HBinaryOperation(result_type, left, right) {}
3344
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003345 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003346
3347 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3348 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3349
3350 DECLARE_INSTRUCTION(Or);
3351
3352 private:
3353 DISALLOW_COPY_AND_ASSIGN(HOr);
3354};
3355
3356class HXor : public HBinaryOperation {
3357 public:
3358 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3359 : HBinaryOperation(result_type, left, right) {}
3360
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003361 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003362
3363 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3364 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3365
3366 DECLARE_INSTRUCTION(Xor);
3367
3368 private:
3369 DISALLOW_COPY_AND_ASSIGN(HXor);
3370};
3371
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003372// The value of a parameter in this method. Its location depends on
3373// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003374class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003375 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003376 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3377 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003378
3379 uint8_t GetIndex() const { return index_; }
3380
Calin Juravle10e244f2015-01-26 18:54:32 +00003381 bool CanBeNull() const OVERRIDE { return !is_this_; }
3382
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003383 bool IsThis() const { return is_this_; }
3384
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003385 DECLARE_INSTRUCTION(ParameterValue);
3386
3387 private:
3388 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003389 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003390 const uint8_t index_;
3391
Calin Juravle10e244f2015-01-26 18:54:32 +00003392 // Whether or not the parameter value corresponds to 'this' argument.
3393 const bool is_this_;
3394
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003395 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3396};
3397
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003398class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003399 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003400 explicit HNot(Primitive::Type result_type, HInstruction* input)
3401 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003402
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003403 bool CanBeMoved() const OVERRIDE { return true; }
3404 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003405 UNUSED(other);
3406 return true;
3407 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003408
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003409 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3410 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003411
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003412 DECLARE_INSTRUCTION(Not);
3413
3414 private:
3415 DISALLOW_COPY_AND_ASSIGN(HNot);
3416};
3417
David Brazdil66d126e2015-04-03 16:02:44 +01003418class HBooleanNot : public HUnaryOperation {
3419 public:
3420 explicit HBooleanNot(HInstruction* input)
3421 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3422
3423 bool CanBeMoved() const OVERRIDE { return true; }
3424 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3425 UNUSED(other);
3426 return true;
3427 }
3428
3429 int32_t Evaluate(int32_t x) const OVERRIDE {
3430 DCHECK(IsUint<1>(x));
3431 return !x;
3432 }
3433
3434 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3435 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3436 UNREACHABLE();
3437 }
3438
3439 DECLARE_INSTRUCTION(BooleanNot);
3440
3441 private:
3442 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3443};
3444
Roland Levillaindff1f282014-11-05 14:15:05 +00003445class HTypeConversion : public HExpression<1> {
3446 public:
3447 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003448 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3449 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003450 SetRawInputAt(0, input);
3451 DCHECK_NE(input->GetType(), result_type);
3452 }
3453
3454 HInstruction* GetInput() const { return InputAt(0); }
3455 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3456 Primitive::Type GetResultType() const { return GetType(); }
3457
Roland Levillain624279f2014-12-04 11:54:28 +00003458 // Required by the x86 and ARM code generators when producing calls
3459 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003460 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003461
Roland Levillaindff1f282014-11-05 14:15:05 +00003462 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003463 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003464
Mark Mendelle82549b2015-05-06 10:55:34 -04003465 // Try to statically evaluate the conversion and return a HConstant
3466 // containing the result. If the input cannot be converted, return nullptr.
3467 HConstant* TryStaticEvaluation() const;
3468
Roland Levillaindff1f282014-11-05 14:15:05 +00003469 DECLARE_INSTRUCTION(TypeConversion);
3470
3471 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003472 const uint32_t dex_pc_;
3473
Roland Levillaindff1f282014-11-05 14:15:05 +00003474 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3475};
3476
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003477static constexpr uint32_t kNoRegNumber = -1;
3478
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003479class HPhi : public HInstruction {
3480 public:
3481 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003482 : HInstruction(SideEffects::None()),
3483 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003484 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003485 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003486 is_live_(false),
3487 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003488 inputs_.SetSize(number_of_inputs);
3489 }
3490
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003491 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3492 static Primitive::Type ToPhiType(Primitive::Type type) {
3493 switch (type) {
3494 case Primitive::kPrimBoolean:
3495 case Primitive::kPrimByte:
3496 case Primitive::kPrimShort:
3497 case Primitive::kPrimChar:
3498 return Primitive::kPrimInt;
3499 default:
3500 return type;
3501 }
3502 }
3503
David Brazdilffee3d32015-07-06 11:48:53 +01003504 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3505
Calin Juravle10e244f2015-01-26 18:54:32 +00003506 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003507
3508 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003509 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003510
Calin Juravle10e244f2015-01-26 18:54:32 +00003511 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003512 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003513
Calin Juravle10e244f2015-01-26 18:54:32 +00003514 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3515 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3516
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003517 uint32_t GetRegNumber() const { return reg_number_; }
3518
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003519 void SetDead() { is_live_ = false; }
3520 void SetLive() { is_live_ = true; }
3521 bool IsDead() const { return !is_live_; }
3522 bool IsLive() const { return is_live_; }
3523
Calin Juravlea4f88312015-04-16 12:57:19 +01003524 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3525 // An equivalent phi is a phi having the same dex register and type.
3526 // It assumes that phis with the same dex register are adjacent.
3527 HPhi* GetNextEquivalentPhiWithSameType() {
3528 HInstruction* next = GetNext();
3529 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3530 if (next->GetType() == GetType()) {
3531 return next->AsPhi();
3532 }
3533 next = next->GetNext();
3534 }
3535 return nullptr;
3536 }
3537
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003538 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003539
David Brazdil1abb4192015-02-17 18:33:36 +00003540 protected:
3541 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3542
3543 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3544 inputs_.Put(index, input);
3545 }
3546
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003547 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003548 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003549 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003550 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003551 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003552 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003553
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003554 DISALLOW_COPY_AND_ASSIGN(HPhi);
3555};
3556
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003557class HNullCheck : public HExpression<1> {
3558 public:
3559 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003560 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003561 SetRawInputAt(0, value);
3562 }
3563
Calin Juravle10e244f2015-01-26 18:54:32 +00003564 bool CanBeMoved() const OVERRIDE { return true; }
3565 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003566 UNUSED(other);
3567 return true;
3568 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003569
Calin Juravle10e244f2015-01-26 18:54:32 +00003570 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003571
Calin Juravle10e244f2015-01-26 18:54:32 +00003572 bool CanThrow() const OVERRIDE { return true; }
3573
3574 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003575
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003576 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003577
3578 DECLARE_INSTRUCTION(NullCheck);
3579
3580 private:
3581 const uint32_t dex_pc_;
3582
3583 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3584};
3585
3586class FieldInfo : public ValueObject {
3587 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003588 FieldInfo(MemberOffset field_offset,
3589 Primitive::Type field_type,
3590 bool is_volatile,
3591 uint32_t index,
3592 const DexFile& dex_file)
3593 : field_offset_(field_offset),
3594 field_type_(field_type),
3595 is_volatile_(is_volatile),
3596 index_(index),
3597 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003598
3599 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003600 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003601 uint32_t GetFieldIndex() const { return index_; }
3602 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003603 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003604
3605 private:
3606 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003607 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003608 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003609 uint32_t index_;
3610 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003611};
3612
3613class HInstanceFieldGet : public HExpression<1> {
3614 public:
3615 HInstanceFieldGet(HInstruction* value,
3616 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003617 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003618 bool is_volatile,
3619 uint32_t field_idx,
3620 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003621 : HExpression(
3622 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01003623 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003624 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003625 SetRawInputAt(0, value);
3626 }
3627
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003628 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003629
3630 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3631 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3632 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003633 }
3634
Calin Juravle641547a2015-04-21 22:08:51 +01003635 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3636 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003637 }
3638
3639 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003640 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3641 }
3642
Calin Juravle52c48962014-12-16 17:02:57 +00003643 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003644 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003645 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003646 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003647
3648 DECLARE_INSTRUCTION(InstanceFieldGet);
3649
3650 private:
3651 const FieldInfo field_info_;
3652
3653 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3654};
3655
3656class HInstanceFieldSet : public HTemplateInstruction<2> {
3657 public:
3658 HInstanceFieldSet(HInstruction* object,
3659 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003660 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003661 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003662 bool is_volatile,
3663 uint32_t field_idx,
3664 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003665 : HTemplateInstruction(
3666 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003667 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003668 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003669 SetRawInputAt(0, object);
3670 SetRawInputAt(1, value);
3671 }
3672
Calin Juravle641547a2015-04-21 22:08:51 +01003673 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3674 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003675 }
3676
Calin Juravle52c48962014-12-16 17:02:57 +00003677 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003678 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003679 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003680 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003681 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003682 bool GetValueCanBeNull() const { return value_can_be_null_; }
3683 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003684
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003685 DECLARE_INSTRUCTION(InstanceFieldSet);
3686
3687 private:
3688 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003689 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003690
3691 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3692};
3693
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003694class HArrayGet : public HExpression<2> {
3695 public:
3696 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07003697 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003698 SetRawInputAt(0, array);
3699 SetRawInputAt(1, index);
3700 }
3701
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003702 bool CanBeMoved() const OVERRIDE { return true; }
3703 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003704 UNUSED(other);
3705 return true;
3706 }
Calin Juravle641547a2015-04-21 22:08:51 +01003707 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3708 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003709 // TODO: We can be smarter here.
3710 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3711 // which generates the implicit null check. There are cases when these can be removed
3712 // to produce better code. If we ever add optimizations to do so we should allow an
3713 // implicit check here (as long as the address falls in the first page).
3714 return false;
3715 }
3716
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003717 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003718
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003719 HInstruction* GetArray() const { return InputAt(0); }
3720 HInstruction* GetIndex() const { return InputAt(1); }
3721
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003722 DECLARE_INSTRUCTION(ArrayGet);
3723
3724 private:
3725 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3726};
3727
3728class HArraySet : public HTemplateInstruction<3> {
3729 public:
3730 HArraySet(HInstruction* array,
3731 HInstruction* index,
3732 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003733 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003734 uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07003735 : HTemplateInstruction(SideEffects::ArrayWriteOfType(expected_component_type)),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003736 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003737 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003738 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3739 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003740 SetRawInputAt(0, array);
3741 SetRawInputAt(1, index);
3742 SetRawInputAt(2, value);
3743 }
3744
Calin Juravle77520bc2015-01-12 18:45:46 +00003745 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003746 // We currently always call a runtime method to catch array store
3747 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003748 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003749 }
3750
Mingyao Yang81014cb2015-06-02 03:16:27 -07003751 // Can throw ArrayStoreException.
3752 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3753
Calin Juravle641547a2015-04-21 22:08:51 +01003754 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3755 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003756 // TODO: Same as for ArrayGet.
3757 return false;
3758 }
3759
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003760 void ClearNeedsTypeCheck() {
3761 needs_type_check_ = false;
3762 }
3763
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003764 void ClearValueCanBeNull() {
3765 value_can_be_null_ = false;
3766 }
3767
3768 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003769 bool NeedsTypeCheck() const { return needs_type_check_; }
3770
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003771 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003772
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003773 HInstruction* GetArray() const { return InputAt(0); }
3774 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003775 HInstruction* GetValue() const { return InputAt(2); }
3776
3777 Primitive::Type GetComponentType() const {
3778 // The Dex format does not type floating point index operations. Since the
3779 // `expected_component_type_` is set during building and can therefore not
3780 // be correct, we also check what is the value type. If it is a floating
3781 // point type, we must use that type.
3782 Primitive::Type value_type = GetValue()->GetType();
3783 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3784 ? value_type
3785 : expected_component_type_;
3786 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003787
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003788 DECLARE_INSTRUCTION(ArraySet);
3789
3790 private:
3791 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003792 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003793 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003794 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003795
3796 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3797};
3798
3799class HArrayLength : public HExpression<1> {
3800 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003801 explicit HArrayLength(HInstruction* array)
3802 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3803 // Note that arrays do not change length, so the instruction does not
3804 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003805 SetRawInputAt(0, array);
3806 }
3807
Calin Juravle77520bc2015-01-12 18:45:46 +00003808 bool CanBeMoved() const OVERRIDE { return true; }
3809 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003810 UNUSED(other);
3811 return true;
3812 }
Calin Juravle641547a2015-04-21 22:08:51 +01003813 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3814 return obj == InputAt(0);
3815 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003816
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003817 DECLARE_INSTRUCTION(ArrayLength);
3818
3819 private:
3820 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3821};
3822
3823class HBoundsCheck : public HExpression<2> {
3824 public:
3825 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003826 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003827 DCHECK(index->GetType() == Primitive::kPrimInt);
3828 SetRawInputAt(0, index);
3829 SetRawInputAt(1, length);
3830 }
3831
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003832 bool CanBeMoved() const OVERRIDE { return true; }
3833 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003834 UNUSED(other);
3835 return true;
3836 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003837
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003838 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003839
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003840 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003841
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003842 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003843
3844 DECLARE_INSTRUCTION(BoundsCheck);
3845
3846 private:
3847 const uint32_t dex_pc_;
3848
3849 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3850};
3851
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003852/**
3853 * Some DEX instructions are folded into multiple HInstructions that need
3854 * to stay live until the last HInstruction. This class
3855 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003856 * HInstruction stays live. `index` represents the stack location index of the
3857 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003858 */
3859class HTemporary : public HTemplateInstruction<0> {
3860 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003861 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003862
3863 size_t GetIndex() const { return index_; }
3864
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003865 Primitive::Type GetType() const OVERRIDE {
3866 // The previous instruction is the one that will be stored in the temporary location.
3867 DCHECK(GetPrevious() != nullptr);
3868 return GetPrevious()->GetType();
3869 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003870
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003871 DECLARE_INSTRUCTION(Temporary);
3872
3873 private:
3874 const size_t index_;
3875
3876 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3877};
3878
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003879class HSuspendCheck : public HTemplateInstruction<0> {
3880 public:
3881 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003882 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003883
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003884 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003885 return true;
3886 }
3887
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003888 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003889 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3890 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003891
3892 DECLARE_INSTRUCTION(SuspendCheck);
3893
3894 private:
3895 const uint32_t dex_pc_;
3896
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003897 // Only used for code generation, in order to share the same slow path between back edges
3898 // of a same loop.
3899 SlowPathCode* slow_path_;
3900
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003901 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3902};
3903
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003904/**
3905 * Instruction to load a Class object.
3906 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003907class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003908 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003909 HLoadClass(HCurrentMethod* current_method,
3910 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003911 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003912 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003913 uint32_t dex_pc)
3914 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3915 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003916 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003917 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003918 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003919 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00003920 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003921 SetRawInputAt(0, current_method);
3922 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003923
3924 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003925
3926 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3927 return other->AsLoadClass()->type_index_ == type_index_;
3928 }
3929
3930 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3931
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003932 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003933 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003934 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01003935 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003936
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003937 bool NeedsEnvironment() const OVERRIDE {
3938 // Will call runtime and load the class if the class is not loaded yet.
3939 // TODO: finer grain decision.
3940 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003941 }
3942
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003943 bool MustGenerateClinitCheck() const {
3944 return generate_clinit_check_;
3945 }
3946
Calin Juravle0ba218d2015-05-19 18:46:01 +01003947 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3948 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003949 }
3950
3951 bool CanCallRuntime() const {
3952 return MustGenerateClinitCheck() || !is_referrers_class_;
3953 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003954
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003955 bool CanThrow() const OVERRIDE {
3956 // May call runtime and and therefore can throw.
3957 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003958 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003959 }
3960
Calin Juravleacf735c2015-02-12 15:25:22 +00003961 ReferenceTypeInfo GetLoadedClassRTI() {
3962 return loaded_class_rti_;
3963 }
3964
3965 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3966 // Make sure we only set exact types (the loaded class should never be merged).
3967 DCHECK(rti.IsExact());
3968 loaded_class_rti_ = rti;
3969 }
3970
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003971 const DexFile& GetDexFile() { return dex_file_; }
3972
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003973 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3974
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003975 DECLARE_INSTRUCTION(LoadClass);
3976
3977 private:
3978 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003979 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003980 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003981 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003982 // Whether this instruction must generate the initialization check.
3983 // Used for code generation.
3984 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003985
Calin Juravleacf735c2015-02-12 15:25:22 +00003986 ReferenceTypeInfo loaded_class_rti_;
3987
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003988 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3989};
3990
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003991class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003992 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003993 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003994 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3995 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003996 dex_pc_(dex_pc) {
3997 SetRawInputAt(0, current_method);
3998 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003999
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004000 bool CanBeMoved() const OVERRIDE { return true; }
4001
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004002 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4003 return other->AsLoadString()->string_index_ == string_index_;
4004 }
4005
4006 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4007
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004008 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004009 uint32_t GetStringIndex() const { return string_index_; }
4010
4011 // TODO: Can we deopt or debug when we resolve a string?
4012 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004013 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004014
4015 DECLARE_INSTRUCTION(LoadString);
4016
4017 private:
4018 const uint32_t string_index_;
4019 const uint32_t dex_pc_;
4020
4021 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4022};
4023
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004024/**
4025 * Performs an initialization check on its Class object input.
4026 */
4027class HClinitCheck : public HExpression<1> {
4028 public:
4029 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004030 : HExpression(
4031 Primitive::kPrimNot,
Aart Bik34c3ba92015-07-20 14:08:59 -07004032 SideEffects::AllWrites()), // assume write on all fields/arrays
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004033 dex_pc_(dex_pc) {
4034 SetRawInputAt(0, constant);
4035 }
4036
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004037 bool CanBeMoved() const OVERRIDE { return true; }
4038 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4039 UNUSED(other);
4040 return true;
4041 }
4042
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004043 bool NeedsEnvironment() const OVERRIDE {
4044 // May call runtime to initialize the class.
4045 return true;
4046 }
4047
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004048 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004049
4050 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4051
4052 DECLARE_INSTRUCTION(ClinitCheck);
4053
4054 private:
4055 const uint32_t dex_pc_;
4056
4057 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4058};
4059
4060class HStaticFieldGet : public HExpression<1> {
4061 public:
4062 HStaticFieldGet(HInstruction* cls,
4063 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004064 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004065 bool is_volatile,
4066 uint32_t field_idx,
4067 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004068 : HExpression(
4069 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004070 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004071 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004072 SetRawInputAt(0, cls);
4073 }
4074
Calin Juravle52c48962014-12-16 17:02:57 +00004075
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004076 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004077
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004078 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004079 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4080 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004081 }
4082
4083 size_t ComputeHashCode() const OVERRIDE {
4084 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4085 }
4086
Calin Juravle52c48962014-12-16 17:02:57 +00004087 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004088 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4089 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004090 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004091
4092 DECLARE_INSTRUCTION(StaticFieldGet);
4093
4094 private:
4095 const FieldInfo field_info_;
4096
4097 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4098};
4099
4100class HStaticFieldSet : public HTemplateInstruction<2> {
4101 public:
4102 HStaticFieldSet(HInstruction* cls,
4103 HInstruction* value,
4104 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004105 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004106 bool is_volatile,
4107 uint32_t field_idx,
4108 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004109 : HTemplateInstruction(
4110 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004111 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004112 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004113 SetRawInputAt(0, cls);
4114 SetRawInputAt(1, value);
4115 }
4116
Calin Juravle52c48962014-12-16 17:02:57 +00004117 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004118 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4119 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004120 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004121
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004122 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004123 bool GetValueCanBeNull() const { return value_can_be_null_; }
4124 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004125
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004126 DECLARE_INSTRUCTION(StaticFieldSet);
4127
4128 private:
4129 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004130 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004131
4132 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4133};
4134
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004135// Implement the move-exception DEX instruction.
4136class HLoadException : public HExpression<0> {
4137 public:
4138 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4139
4140 DECLARE_INSTRUCTION(LoadException);
4141
4142 private:
4143 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4144};
4145
David Brazdilcb1c0552015-08-04 16:22:25 +01004146// Implicit part of move-exception which clears thread-local exception storage.
4147// Must not be removed because the runtime expects the TLS to get cleared.
4148class HClearException : public HTemplateInstruction<0> {
4149 public:
4150 HClearException() : HTemplateInstruction(SideEffects::AllWrites()) {}
4151
4152 DECLARE_INSTRUCTION(ClearException);
4153
4154 private:
4155 DISALLOW_COPY_AND_ASSIGN(HClearException);
4156};
4157
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004158class HThrow : public HTemplateInstruction<1> {
4159 public:
4160 HThrow(HInstruction* exception, uint32_t dex_pc)
4161 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
4162 SetRawInputAt(0, exception);
4163 }
4164
4165 bool IsControlFlow() const OVERRIDE { return true; }
4166
4167 bool NeedsEnvironment() const OVERRIDE { return true; }
4168
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004169 bool CanThrow() const OVERRIDE { return true; }
4170
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004171 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004172
4173 DECLARE_INSTRUCTION(Throw);
4174
4175 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004176 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004177
4178 DISALLOW_COPY_AND_ASSIGN(HThrow);
4179};
4180
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004181class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004182 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004183 HInstanceOf(HInstruction* object,
4184 HLoadClass* constant,
4185 bool class_is_final,
4186 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004187 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
4188 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004189 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004190 dex_pc_(dex_pc) {
4191 SetRawInputAt(0, object);
4192 SetRawInputAt(1, constant);
4193 }
4194
4195 bool CanBeMoved() const OVERRIDE { return true; }
4196
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004197 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004198 return true;
4199 }
4200
4201 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004202 return false;
4203 }
4204
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004205 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004206
4207 bool IsClassFinal() const { return class_is_final_; }
4208
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004209 // Used only in code generation.
4210 bool MustDoNullCheck() const { return must_do_null_check_; }
4211 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4212
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004213 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004214
4215 private:
4216 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004217 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004218 const uint32_t dex_pc_;
4219
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004220 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4221};
4222
Calin Juravleb1498f62015-02-16 13:13:29 +00004223class HBoundType : public HExpression<1> {
4224 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004225 // Constructs an HBoundType with the given upper_bound.
4226 // Ensures that the upper_bound is valid.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004227 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004228 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004229 upper_bound_(upper_bound),
4230 upper_can_be_null_(upper_can_be_null),
4231 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004232 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004233 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004234 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004235 }
4236
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004237 // GetUpper* should only be used in reference type propagation.
4238 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4239 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004240
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004241 void SetCanBeNull(bool can_be_null) {
4242 DCHECK(upper_can_be_null_ || !can_be_null);
4243 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004244 }
4245
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004246 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4247
Calin Juravleb1498f62015-02-16 13:13:29 +00004248 DECLARE_INSTRUCTION(BoundType);
4249
4250 private:
4251 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004252 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4253 // It is used to bound the type in cases like:
4254 // if (x instanceof ClassX) {
4255 // // uper_bound_ will be ClassX
4256 // }
4257 const ReferenceTypeInfo upper_bound_;
4258 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4259 // is false then can_be_null_ cannot be true).
4260 const bool upper_can_be_null_;
4261 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004262
4263 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4264};
4265
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004266class HCheckCast : public HTemplateInstruction<2> {
4267 public:
4268 HCheckCast(HInstruction* object,
4269 HLoadClass* constant,
4270 bool class_is_final,
4271 uint32_t dex_pc)
4272 : HTemplateInstruction(SideEffects::None()),
4273 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004274 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004275 dex_pc_(dex_pc) {
4276 SetRawInputAt(0, object);
4277 SetRawInputAt(1, constant);
4278 }
4279
4280 bool CanBeMoved() const OVERRIDE { return true; }
4281
4282 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4283 return true;
4284 }
4285
4286 bool NeedsEnvironment() const OVERRIDE {
4287 // Instruction may throw a CheckCastError.
4288 return true;
4289 }
4290
4291 bool CanThrow() const OVERRIDE { return true; }
4292
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004293 bool MustDoNullCheck() const { return must_do_null_check_; }
4294 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4295
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004296 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004297
4298 bool IsClassFinal() const { return class_is_final_; }
4299
4300 DECLARE_INSTRUCTION(CheckCast);
4301
4302 private:
4303 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004304 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004305 const uint32_t dex_pc_;
4306
4307 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004308};
4309
Calin Juravle27df7582015-04-17 19:12:31 +01004310class HMemoryBarrier : public HTemplateInstruction<0> {
4311 public:
4312 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004313 : HTemplateInstruction(
4314 SideEffects::All()), // assume write/read on all fields/arrays
Calin Juravle27df7582015-04-17 19:12:31 +01004315 barrier_kind_(barrier_kind) {}
4316
4317 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4318
4319 DECLARE_INSTRUCTION(MemoryBarrier);
4320
4321 private:
4322 const MemBarrierKind barrier_kind_;
4323
4324 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4325};
4326
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004327class HMonitorOperation : public HTemplateInstruction<1> {
4328 public:
4329 enum OperationKind {
4330 kEnter,
4331 kExit,
4332 };
4333
4334 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004335 : HTemplateInstruction(SideEffects::All()), // assume write/read on all fields/arrays
4336 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004337 SetRawInputAt(0, object);
4338 }
4339
4340 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004341 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4342
4343 bool CanThrow() const OVERRIDE {
4344 // Verifier guarantees that monitor-exit cannot throw.
4345 // This is important because it allows the HGraphBuilder to remove
4346 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4347 return IsEnter();
4348 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004349
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004350 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004351
4352 bool IsEnter() const { return kind_ == kEnter; }
4353
4354 DECLARE_INSTRUCTION(MonitorOperation);
4355
Calin Juravle52c48962014-12-16 17:02:57 +00004356 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004357 const OperationKind kind_;
4358 const uint32_t dex_pc_;
4359
4360 private:
4361 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4362};
4363
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004364/**
4365 * A HInstruction used as a marker for the replacement of new + <init>
4366 * of a String to a call to a StringFactory. Only baseline will see
4367 * the node at code generation, where it will be be treated as null.
4368 * When compiling non-baseline, `HFakeString` instructions are being removed
4369 * in the instruction simplifier.
4370 */
4371class HFakeString : public HTemplateInstruction<0> {
4372 public:
4373 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4374
4375 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4376
4377 DECLARE_INSTRUCTION(FakeString);
4378
4379 private:
4380 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4381};
4382
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004383class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004384 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004385 MoveOperands(Location source,
4386 Location destination,
4387 Primitive::Type type,
4388 HInstruction* instruction)
4389 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004390
4391 Location GetSource() const { return source_; }
4392 Location GetDestination() const { return destination_; }
4393
4394 void SetSource(Location value) { source_ = value; }
4395 void SetDestination(Location value) { destination_ = value; }
4396
4397 // The parallel move resolver marks moves as "in-progress" by clearing the
4398 // destination (but not the source).
4399 Location MarkPending() {
4400 DCHECK(!IsPending());
4401 Location dest = destination_;
4402 destination_ = Location::NoLocation();
4403 return dest;
4404 }
4405
4406 void ClearPending(Location dest) {
4407 DCHECK(IsPending());
4408 destination_ = dest;
4409 }
4410
4411 bool IsPending() const {
4412 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4413 return destination_.IsInvalid() && !source_.IsInvalid();
4414 }
4415
4416 // True if this blocks a move from the given location.
4417 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004418 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004419 }
4420
4421 // A move is redundant if it's been eliminated, if its source and
4422 // destination are the same, or if its destination is unneeded.
4423 bool IsRedundant() const {
4424 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4425 }
4426
4427 // We clear both operands to indicate move that's been eliminated.
4428 void Eliminate() {
4429 source_ = destination_ = Location::NoLocation();
4430 }
4431
4432 bool IsEliminated() const {
4433 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4434 return source_.IsInvalid();
4435 }
4436
Alexey Frunze4dda3372015-06-01 18:31:49 -07004437 Primitive::Type GetType() const { return type_; }
4438
Nicolas Geoffray90218252015-04-15 11:56:51 +01004439 bool Is64BitMove() const {
4440 return Primitive::Is64BitType(type_);
4441 }
4442
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004443 HInstruction* GetInstruction() const { return instruction_; }
4444
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004445 private:
4446 Location source_;
4447 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004448 // The type this move is for.
4449 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004450 // The instruction this move is assocatied with. Null when this move is
4451 // for moving an input in the expected locations of user (including a phi user).
4452 // This is only used in debug mode, to ensure we do not connect interval siblings
4453 // in the same parallel move.
4454 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004455};
4456
4457static constexpr size_t kDefaultNumberOfMoves = 4;
4458
4459class HParallelMove : public HTemplateInstruction<0> {
4460 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004461 explicit HParallelMove(ArenaAllocator* arena)
4462 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004463
Nicolas Geoffray90218252015-04-15 11:56:51 +01004464 void AddMove(Location source,
4465 Location destination,
4466 Primitive::Type type,
4467 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004468 DCHECK(source.IsValid());
4469 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004470 if (kIsDebugBuild) {
4471 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004472 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004473 if (moves_.Get(i).GetInstruction() == instruction) {
4474 // Special case the situation where the move is for the spill slot
4475 // of the instruction.
4476 if ((GetPrevious() == instruction)
4477 || ((GetPrevious() == nullptr)
4478 && instruction->IsPhi()
4479 && instruction->GetBlock() == GetBlock())) {
4480 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4481 << "Doing parallel moves for the same instruction.";
4482 } else {
4483 DCHECK(false) << "Doing parallel moves for the same instruction.";
4484 }
4485 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004486 }
4487 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004488 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004489 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004490 << "Overlapped destination for two moves in a parallel move: "
4491 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4492 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004493 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004494 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004495 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004496 }
4497
4498 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004499 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004500 }
4501
4502 size_t NumMoves() const { return moves_.Size(); }
4503
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004504 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004505
4506 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004507 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004508
4509 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4510};
4511
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004512class HGraphVisitor : public ValueObject {
4513 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004514 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4515 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004516
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004517 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004518 virtual void VisitBasicBlock(HBasicBlock* block);
4519
Roland Levillain633021e2014-10-01 14:12:25 +01004520 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004521 void VisitInsertionOrder();
4522
Roland Levillain633021e2014-10-01 14:12:25 +01004523 // Visit the graph following dominator tree reverse post-order.
4524 void VisitReversePostOrder();
4525
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004526 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004527
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004528 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004529#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004530 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4531
4532 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4533
4534#undef DECLARE_VISIT_INSTRUCTION
4535
4536 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004537 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004538
4539 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4540};
4541
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004542class HGraphDelegateVisitor : public HGraphVisitor {
4543 public:
4544 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4545 virtual ~HGraphDelegateVisitor() {}
4546
4547 // Visit functions that delegate to to super class.
4548#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004549 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004550
4551 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4552
4553#undef DECLARE_VISIT_INSTRUCTION
4554
4555 private:
4556 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4557};
4558
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004559class HInsertionOrderIterator : public ValueObject {
4560 public:
4561 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4562
4563 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4564 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4565 void Advance() { ++index_; }
4566
4567 private:
4568 const HGraph& graph_;
4569 size_t index_;
4570
4571 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4572};
4573
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004574class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004575 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004576 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4577 // Check that reverse post order of the graph has been built.
4578 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4579 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004580
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004581 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4582 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004583 void Advance() { ++index_; }
4584
4585 private:
4586 const HGraph& graph_;
4587 size_t index_;
4588
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004589 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004590};
4591
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004592class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004593 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004594 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004595 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4596 // Check that reverse post order of the graph has been built.
4597 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4598 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004599
4600 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004601 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004602 void Advance() { --index_; }
4603
4604 private:
4605 const HGraph& graph_;
4606 size_t index_;
4607
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004608 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004609};
4610
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004611class HLinearPostOrderIterator : public ValueObject {
4612 public:
4613 explicit HLinearPostOrderIterator(const HGraph& graph)
4614 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4615
4616 bool Done() const { return index_ == 0; }
4617
4618 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4619
4620 void Advance() {
4621 --index_;
4622 DCHECK_GE(index_, 0U);
4623 }
4624
4625 private:
4626 const GrowableArray<HBasicBlock*>& order_;
4627 size_t index_;
4628
4629 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4630};
4631
4632class HLinearOrderIterator : public ValueObject {
4633 public:
4634 explicit HLinearOrderIterator(const HGraph& graph)
4635 : order_(graph.GetLinearOrder()), index_(0) {}
4636
4637 bool Done() const { return index_ == order_.Size(); }
4638 HBasicBlock* Current() const { return order_.Get(index_); }
4639 void Advance() { ++index_; }
4640
4641 private:
4642 const GrowableArray<HBasicBlock*>& order_;
4643 size_t index_;
4644
4645 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4646};
4647
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004648// Iterator over the blocks that art part of the loop. Includes blocks part
4649// of an inner loop. The order in which the blocks are iterated is on their
4650// block id.
4651class HBlocksInLoopIterator : public ValueObject {
4652 public:
4653 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4654 : blocks_in_loop_(info.GetBlocks()),
4655 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4656 index_(0) {
4657 if (!blocks_in_loop_.IsBitSet(index_)) {
4658 Advance();
4659 }
4660 }
4661
4662 bool Done() const { return index_ == blocks_.Size(); }
4663 HBasicBlock* Current() const { return blocks_.Get(index_); }
4664 void Advance() {
4665 ++index_;
4666 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4667 if (blocks_in_loop_.IsBitSet(index_)) {
4668 break;
4669 }
4670 }
4671 }
4672
4673 private:
4674 const BitVector& blocks_in_loop_;
4675 const GrowableArray<HBasicBlock*>& blocks_;
4676 size_t index_;
4677
4678 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4679};
4680
Mingyao Yang3584bce2015-05-19 16:01:59 -07004681// Iterator over the blocks that art part of the loop. Includes blocks part
4682// of an inner loop. The order in which the blocks are iterated is reverse
4683// post order.
4684class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4685 public:
4686 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4687 : blocks_in_loop_(info.GetBlocks()),
4688 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4689 index_(0) {
4690 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4691 Advance();
4692 }
4693 }
4694
4695 bool Done() const { return index_ == blocks_.Size(); }
4696 HBasicBlock* Current() const { return blocks_.Get(index_); }
4697 void Advance() {
4698 ++index_;
4699 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4700 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4701 break;
4702 }
4703 }
4704 }
4705
4706 private:
4707 const BitVector& blocks_in_loop_;
4708 const GrowableArray<HBasicBlock*>& blocks_;
4709 size_t index_;
4710
4711 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4712};
4713
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004714inline int64_t Int64FromConstant(HConstant* constant) {
4715 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4716 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4717 : constant->AsLongConstant()->GetValue();
4718}
4719
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004720} // namespace art
4721
4722#endif // ART_COMPILER_OPTIMIZING_NODES_H_