blob: 9b8521d968fcd5cd7470895af3e81dc89ea4271e [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) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100964 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000965 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100966 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100967 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700968 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000969 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000970 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000971 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100972 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000973 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100974 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000975 M(FloatConstant, Constant) \
976 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100977 M(GreaterThan, Condition) \
978 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100979 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000980 M(InstanceFieldGet, Instruction) \
981 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000982 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100983 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000984 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000985 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100986 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000987 M(LessThan, Condition) \
988 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000989 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000990 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100991 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000992 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100993 M(Local, Instruction) \
994 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100995 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000996 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000997 M(Mul, BinaryOperation) \
998 M(Neg, UnaryOperation) \
999 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001000 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001001 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001002 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001003 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001004 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001005 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001006 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001007 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001008 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001009 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001010 M(Return, Instruction) \
1011 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001012 M(Shl, BinaryOperation) \
1013 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001014 M(StaticFieldGet, Instruction) \
1015 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001016 M(StoreLocal, Instruction) \
1017 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001018 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001019 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001020 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001021 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001022 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001023 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001024 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001025
Alexandre Ramesef20f712015-06-09 10:29:30 +01001026#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1027
1028#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1029
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001030#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1031
Alexandre Ramesef20f712015-06-09 10:29:30 +01001032#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1033
1034#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1035
1036#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1037 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1038 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1039 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001040 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001041 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1042 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1043
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001044#define FOR_EACH_INSTRUCTION(M) \
1045 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1046 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001047 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001048 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001049 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001050
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001051#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001052FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1053#undef FORWARD_DECLARATION
1054
Roland Levillainccc07a92014-09-16 14:48:16 +01001055#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001056 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1057 const char* DebugName() const OVERRIDE { return #type; } \
1058 const H##type* As##type() const OVERRIDE { return this; } \
1059 H##type* As##type() OVERRIDE { return this; } \
1060 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001061 return other->Is##type(); \
1062 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001063 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001064
David Brazdiled596192015-01-23 10:39:45 +00001065template <typename T> class HUseList;
1066
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001067template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001068class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001069 public:
David Brazdiled596192015-01-23 10:39:45 +00001070 HUseListNode* GetPrevious() const { return prev_; }
1071 HUseListNode* GetNext() const { return next_; }
1072 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001073 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001074 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001075
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001076 private:
David Brazdiled596192015-01-23 10:39:45 +00001077 HUseListNode(T user, size_t index)
1078 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1079
1080 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001081 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001082 HUseListNode<T>* prev_;
1083 HUseListNode<T>* next_;
1084
1085 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001086
1087 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1088};
1089
David Brazdiled596192015-01-23 10:39:45 +00001090template <typename T>
1091class HUseList : public ValueObject {
1092 public:
1093 HUseList() : first_(nullptr) {}
1094
1095 void Clear() {
1096 first_ = nullptr;
1097 }
1098
1099 // Adds a new entry at the beginning of the use list and returns
1100 // the newly created node.
1101 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001102 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001103 if (IsEmpty()) {
1104 first_ = new_node;
1105 } else {
1106 first_->prev_ = new_node;
1107 new_node->next_ = first_;
1108 first_ = new_node;
1109 }
1110 return new_node;
1111 }
1112
1113 HUseListNode<T>* GetFirst() const {
1114 return first_;
1115 }
1116
1117 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001118 DCHECK(node != nullptr);
1119 DCHECK(Contains(node));
1120
David Brazdiled596192015-01-23 10:39:45 +00001121 if (node->prev_ != nullptr) {
1122 node->prev_->next_ = node->next_;
1123 }
1124 if (node->next_ != nullptr) {
1125 node->next_->prev_ = node->prev_;
1126 }
1127 if (node == first_) {
1128 first_ = node->next_;
1129 }
1130 }
1131
David Brazdil1abb4192015-02-17 18:33:36 +00001132 bool Contains(const HUseListNode<T>* node) const {
1133 if (node == nullptr) {
1134 return false;
1135 }
1136 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1137 if (current == node) {
1138 return true;
1139 }
1140 }
1141 return false;
1142 }
1143
David Brazdiled596192015-01-23 10:39:45 +00001144 bool IsEmpty() const {
1145 return first_ == nullptr;
1146 }
1147
1148 bool HasOnlyOneUse() const {
1149 return first_ != nullptr && first_->next_ == nullptr;
1150 }
1151
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001152 size_t SizeSlow() const {
1153 size_t count = 0;
1154 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1155 ++count;
1156 }
1157 return count;
1158 }
1159
David Brazdiled596192015-01-23 10:39:45 +00001160 private:
1161 HUseListNode<T>* first_;
1162};
1163
1164template<typename T>
1165class HUseIterator : public ValueObject {
1166 public:
1167 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1168
1169 bool Done() const { return current_ == nullptr; }
1170
1171 void Advance() {
1172 DCHECK(!Done());
1173 current_ = current_->GetNext();
1174 }
1175
1176 HUseListNode<T>* Current() const {
1177 DCHECK(!Done());
1178 return current_;
1179 }
1180
1181 private:
1182 HUseListNode<T>* current_;
1183
1184 friend class HValue;
1185};
1186
David Brazdil1abb4192015-02-17 18:33:36 +00001187// This class is used by HEnvironment and HInstruction classes to record the
1188// instructions they use and pointers to the corresponding HUseListNodes kept
1189// by the used instructions.
1190template <typename T>
1191class HUserRecord : public ValueObject {
1192 public:
1193 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1194 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1195
1196 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1197 : instruction_(old_record.instruction_), use_node_(use_node) {
1198 DCHECK(instruction_ != nullptr);
1199 DCHECK(use_node_ != nullptr);
1200 DCHECK(old_record.use_node_ == nullptr);
1201 }
1202
1203 HInstruction* GetInstruction() const { return instruction_; }
1204 HUseListNode<T>* GetUseNode() const { return use_node_; }
1205
1206 private:
1207 // Instruction used by the user.
1208 HInstruction* instruction_;
1209
1210 // Corresponding entry in the use list kept by 'instruction_'.
1211 HUseListNode<T>* use_node_;
1212};
1213
Aart Bik854a02b2015-07-14 16:07:00 -07001214/**
1215 * Side-effects representation for write/read dependences on fields/arrays.
1216 *
1217 * The dependence analysis uses type disambiguation (e.g. a float field write
1218 * cannot modify the value of an integer field read) and the access type (e.g.
1219 * a reference array write cannot modify the value of a reference field read
1220 * [although it may modify the reference fetch prior to reading the field,
1221 * which is represented by its own write/read dependence]). The analysis
1222 * makes conservative points-to assumptions on reference types (e.g. two same
1223 * typed arrays are assumed to be the same, and any reference read depends
1224 * on any reference read without further regard of its type).
1225 *
1226 * The internal representation uses the following 36-bit flags assignments:
1227 *
1228 * |ARRAY-R |FIELD-R |ARRAY-W |FIELD-W |
1229 * +---------+---------+---------+---------+
1230 * |543210987|654321098|765432109|876543210|
1231 * |DFJISCBZL|DFJISCBZL|DFJISCBZL|DFJISCBZL|
1232 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001233class SideEffects : public ValueObject {
1234 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001235 SideEffects() : flags_(0) {}
1236
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001237 static SideEffects None() {
1238 return SideEffects(0);
1239 }
1240
1241 static SideEffects All() {
Aart Bik854a02b2015-07-14 16:07:00 -07001242 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001243 }
1244
Aart Bik34c3ba92015-07-20 14:08:59 -07001245 static SideEffects AllWrites() {
1246 return SideEffects(kAllWrites);
1247 }
1248
1249 static SideEffects AllReads() {
1250 return SideEffects(kAllReads);
1251 }
1252
1253 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1254 return is_volatile
1255 ? All()
1256 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001257 }
1258
Aart Bik854a02b2015-07-14 16:07:00 -07001259 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1260 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001261 }
1262
Aart Bik34c3ba92015-07-20 14:08:59 -07001263 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1264 return is_volatile
1265 ? All()
1266 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001267 }
1268
1269 static SideEffects ArrayReadOfType(Primitive::Type type) {
1270 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1271 }
1272
1273 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001274 SideEffects Union(SideEffects other) const {
1275 return SideEffects(flags_ | other.flags_);
1276 }
1277
Aart Bik854a02b2015-07-14 16:07:00 -07001278 // Returns true if something is written.
1279 bool DoesAnyWrite() const {
1280 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001281 }
1282
Aart Bik854a02b2015-07-14 16:07:00 -07001283 // Returns true if something is read.
1284 bool DoesAnyRead() const {
1285 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001286 }
1287
Aart Bik854a02b2015-07-14 16:07:00 -07001288 // Returns true if nothing is written or read.
1289 bool DoesNothing() const {
1290 return flags_ == 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001291 }
1292
Aart Bik854a02b2015-07-14 16:07:00 -07001293 // Returns true if potentially everything is written and read
1294 // (every type and every kind of access).
1295 bool DoesAll() const {
1296 return flags_ == (kAllWrites | kAllReads);
1297 }
1298
1299 // Returns true if this may read something written by other.
1300 bool MayDependOn(SideEffects other) const {
1301 const uint64_t reads = (flags_ & kAllReads) >> kFieldReadOffset;
1302 return (other.flags_ & reads);
1303 }
1304
1305 // Returns string representation of flags (for debugging only).
1306 // Format: |DFJISCBZL|DFJISCBZL|DFJISCBZL|DFJISCBZL|
1307 std::string ToString() const {
1308 static const char *kDebug = "LZBCSIJFD";
1309 std::string flags = "|";
1310 for (int s = 35; s >= 0; s--) {
1311 const int t = s % kBits;
1312 if ((flags_ >> s) & 1)
1313 flags += kDebug[t];
1314 if (t == 0)
1315 flags += "|";
1316 }
1317 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001318 }
1319
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001320 private:
Aart Bik854a02b2015-07-14 16:07:00 -07001321 static constexpr int kBits = 9;
1322 static constexpr int kFieldWriteOffset = 0 * kBits;
1323 static constexpr int kArrayWriteOffset = 1 * kBits;
1324 static constexpr int kFieldReadOffset = 2 * kBits;
1325 static constexpr int kArrayReadOffset = 3 * kBits;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001326
Aart Bik854a02b2015-07-14 16:07:00 -07001327 static constexpr uint64_t kAllWrites = 0x0003ffff;
1328 static constexpr uint64_t kAllReads = kAllWrites << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001329
Aart Bik854a02b2015-07-14 16:07:00 -07001330 // Work around the fact that HIR aliases I/F and J/D.
1331 // TODO: remove this interceptor once HIR types are clean
1332 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1333 switch (type) {
1334 case Primitive::kPrimInt:
1335 case Primitive::kPrimFloat:
1336 return TypeFlag(Primitive::kPrimInt, offset) |
1337 TypeFlag(Primitive::kPrimFloat, offset);
1338 case Primitive::kPrimLong:
1339 case Primitive::kPrimDouble:
1340 return TypeFlag(Primitive::kPrimLong, offset) |
1341 TypeFlag(Primitive::kPrimDouble, offset);
1342 default:
1343 return TypeFlag(type, offset);
1344 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001345 }
1346
Aart Bik854a02b2015-07-14 16:07:00 -07001347 // Translates type to bit flag.
1348 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1349 CHECK_NE(type, Primitive::kPrimVoid);
1350 const uint64_t one = 1;
1351 const int shift = type; // 0-based consecutive enum
1352 DCHECK_LE(kFieldWriteOffset, shift);
1353 DCHECK_LT(shift, kArrayWriteOffset);
1354 return one << (type + offset);
1355 }
1356
1357 // Private constructor on direct flags value.
1358 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1359
1360 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001361};
1362
David Brazdiled596192015-01-23 10:39:45 +00001363// A HEnvironment object contains the values of virtual registers at a given location.
1364class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1365 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001366 HEnvironment(ArenaAllocator* arena,
1367 size_t number_of_vregs,
1368 const DexFile& dex_file,
1369 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001370 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001371 InvokeType invoke_type,
1372 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001373 : vregs_(arena, number_of_vregs),
1374 locations_(arena, number_of_vregs),
1375 parent_(nullptr),
1376 dex_file_(dex_file),
1377 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001378 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001379 invoke_type_(invoke_type),
1380 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001381 vregs_.SetSize(number_of_vregs);
1382 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001383 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001384 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001385
1386 locations_.SetSize(number_of_vregs);
1387 for (size_t i = 0; i < number_of_vregs; ++i) {
1388 locations_.Put(i, Location());
1389 }
1390 }
1391
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001392 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001393 : HEnvironment(arena,
1394 to_copy.Size(),
1395 to_copy.GetDexFile(),
1396 to_copy.GetMethodIdx(),
1397 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001398 to_copy.GetInvokeType(),
1399 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001400
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001401 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001402 if (parent_ != nullptr) {
1403 parent_->SetAndCopyParentChain(allocator, parent);
1404 } else {
1405 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1406 parent_->CopyFrom(parent);
1407 if (parent->GetParent() != nullptr) {
1408 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1409 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001410 }
David Brazdiled596192015-01-23 10:39:45 +00001411 }
1412
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001413 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1414 void CopyFrom(HEnvironment* environment);
1415
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001416 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1417 // input to the loop phi instead. This is for inserting instructions that
1418 // require an environment (like HDeoptimization) in the loop pre-header.
1419 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001420
1421 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001422 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001423 }
1424
1425 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001426 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001427 }
1428
David Brazdil1abb4192015-02-17 18:33:36 +00001429 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001430
1431 size_t Size() const { return vregs_.Size(); }
1432
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001433 HEnvironment* GetParent() const { return parent_; }
1434
1435 void SetLocationAt(size_t index, Location location) {
1436 locations_.Put(index, location);
1437 }
1438
1439 Location GetLocationAt(size_t index) const {
1440 return locations_.Get(index);
1441 }
1442
1443 uint32_t GetDexPc() const {
1444 return dex_pc_;
1445 }
1446
1447 uint32_t GetMethodIdx() const {
1448 return method_idx_;
1449 }
1450
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001451 InvokeType GetInvokeType() const {
1452 return invoke_type_;
1453 }
1454
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001455 const DexFile& GetDexFile() const {
1456 return dex_file_;
1457 }
1458
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001459 HInstruction* GetHolder() const {
1460 return holder_;
1461 }
1462
David Brazdiled596192015-01-23 10:39:45 +00001463 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001464 // Record instructions' use entries of this environment for constant-time removal.
1465 // It should only be called by HInstruction when a new environment use is added.
1466 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1467 DCHECK(env_use->GetUser() == this);
1468 size_t index = env_use->GetIndex();
1469 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1470 }
David Brazdiled596192015-01-23 10:39:45 +00001471
David Brazdil1abb4192015-02-17 18:33:36 +00001472 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001473 GrowableArray<Location> locations_;
1474 HEnvironment* parent_;
1475 const DexFile& dex_file_;
1476 const uint32_t method_idx_;
1477 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001478 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001479
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001480 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001481 HInstruction* const holder_;
1482
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001483 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001484
1485 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1486};
1487
Calin Juravleacf735c2015-02-12 15:25:22 +00001488class ReferenceTypeInfo : ValueObject {
1489 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001490 typedef Handle<mirror::Class> TypeHandle;
1491
Calin Juravle2e768302015-07-28 14:41:11 +00001492 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1493 // The constructor will check that the type_handle is valid.
1494 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001495 }
1496
Calin Juravle2e768302015-07-28 14:41:11 +00001497 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1498
1499 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1500 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001501 }
1502
Calin Juravle2e768302015-07-28 14:41:11 +00001503 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1504 return IsValidHandle(type_handle_);
1505 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001506 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001507
1508 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1509 DCHECK(IsValid());
1510 return GetTypeHandle()->IsObjectClass();
1511 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001512 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001513 DCHECK(IsValid());
1514 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001515 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001516
1517 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1518
Mathieu Chartier90443472015-07-16 20:32:27 -07001519 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001520 DCHECK(IsValid());
1521 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001522 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1523 }
1524
1525 // Returns true if the type information provide the same amount of details.
1526 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001527 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001528 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001529 if (!IsValid() && !rti.IsValid()) {
1530 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001531 return true;
1532 }
Calin Juravle2e768302015-07-28 14:41:11 +00001533 if (!IsValid() || !rti.IsValid()) {
1534 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001535 return false;
1536 }
Calin Juravle2e768302015-07-28 14:41:11 +00001537 return IsExact() == rti.IsExact()
1538 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001539 }
1540
1541 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001542 ReferenceTypeInfo();
1543 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001544
Calin Juravleacf735c2015-02-12 15:25:22 +00001545 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001546 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001547 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001548 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001549 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001550};
1551
1552std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1553
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001554class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001555 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001556 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001557 : previous_(nullptr),
1558 next_(nullptr),
1559 block_(nullptr),
1560 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001561 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001562 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001563 locations_(nullptr),
1564 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001565 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001566 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001567 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001568
Dave Allison20dfc792014-06-16 20:44:29 -07001569 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001570
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001571#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001572 enum InstructionKind {
1573 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1574 };
1575#undef DECLARE_KIND
1576
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001577 HInstruction* GetNext() const { return next_; }
1578 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001579
Calin Juravle77520bc2015-01-12 18:45:46 +00001580 HInstruction* GetNextDisregardingMoves() const;
1581 HInstruction* GetPreviousDisregardingMoves() const;
1582
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001583 HBasicBlock* GetBlock() const { return block_; }
1584 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001585 bool IsInBlock() const { return block_ != nullptr; }
1586 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001587 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001588
Roland Levillain6b879dd2014-09-22 17:13:44 +01001589 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001590 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001591
1592 virtual void Accept(HGraphVisitor* visitor) = 0;
1593 virtual const char* DebugName() const = 0;
1594
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001595 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001596 void SetRawInputAt(size_t index, HInstruction* input) {
1597 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1598 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001599
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001600 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001601 virtual uint32_t GetDexPc() const {
1602 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1603 " does not need an environment";
1604 UNREACHABLE();
1605 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001606 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001607 virtual bool CanThrow() const { return false; }
Aart Bik854a02b2015-07-14 16:07:00 -07001608
1609 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001610
Calin Juravle10e244f2015-01-26 18:54:32 +00001611 // Does not apply for all instructions, but having this at top level greatly
1612 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001613 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001614 virtual bool CanBeNull() const {
1615 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1616 return true;
1617 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001618
Calin Juravle641547a2015-04-21 22:08:51 +01001619 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1620 UNUSED(obj);
1621 return false;
1622 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001623
Calin Juravle2e768302015-07-28 14:41:11 +00001624 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001625
Calin Juravle61d544b2015-02-23 16:46:57 +00001626 ReferenceTypeInfo GetReferenceTypeInfo() const {
1627 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1628 return reference_type_info_;
1629 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001630
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001631 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001632 DCHECK(user != nullptr);
1633 HUseListNode<HInstruction*>* use =
1634 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1635 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001636 }
1637
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001638 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001639 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001640 HUseListNode<HEnvironment*>* env_use =
1641 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1642 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001643 }
1644
David Brazdil1abb4192015-02-17 18:33:36 +00001645 void RemoveAsUserOfInput(size_t input) {
1646 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1647 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1648 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001649
David Brazdil1abb4192015-02-17 18:33:36 +00001650 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1651 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001652
David Brazdiled596192015-01-23 10:39:45 +00001653 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1654 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001655 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001656 bool HasOnlyOneNonEnvironmentUse() const {
1657 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1658 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001659
Roland Levillain6c82d402014-10-13 16:10:27 +01001660 // Does this instruction strictly dominate `other_instruction`?
1661 // Returns false if this instruction and `other_instruction` are the same.
1662 // Aborts if this instruction and `other_instruction` are both phis.
1663 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001664
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001665 int GetId() const { return id_; }
1666 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001667
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001668 int GetSsaIndex() const { return ssa_index_; }
1669 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1670 bool HasSsaIndex() const { return ssa_index_ != -1; }
1671
1672 bool HasEnvironment() const { return environment_ != nullptr; }
1673 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001674 // Set the `environment_` field. Raw because this method does not
1675 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001676 void SetRawEnvironment(HEnvironment* environment) {
1677 DCHECK(environment_ == nullptr);
1678 DCHECK_EQ(environment->GetHolder(), this);
1679 environment_ = environment;
1680 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001681
1682 // Set the environment of this instruction, copying it from `environment`. While
1683 // copying, the uses lists are being updated.
1684 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001685 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001686 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001687 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001688 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001689 if (environment->GetParent() != nullptr) {
1690 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1691 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001692 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001693
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001694 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1695 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001696 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001697 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001698 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001699 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001700 if (environment->GetParent() != nullptr) {
1701 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1702 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001703 }
1704
Nicolas Geoffray39468442014-09-02 15:17:15 +01001705 // Returns the number of entries in the environment. Typically, that is the
1706 // number of dex registers in a method. It could be more in case of inlining.
1707 size_t EnvironmentSize() const;
1708
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001709 LocationSummary* GetLocations() const { return locations_; }
1710 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001711
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001712 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001713 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001714
Alexandre Rames188d4312015-04-09 18:30:21 +01001715 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1716 // uses of this instruction by `other` are *not* updated.
1717 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1718 ReplaceWith(other);
1719 other->ReplaceInput(this, use_index);
1720 }
1721
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001722 // Move `this` instruction before `cursor`.
1723 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001724
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001725#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001726 bool Is##type() const { return (As##type() != nullptr); } \
1727 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001728 virtual H##type* As##type() { return nullptr; }
1729
1730 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1731#undef INSTRUCTION_TYPE_CHECK
1732
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001733 // Returns whether the instruction can be moved within the graph.
1734 virtual bool CanBeMoved() const { return false; }
1735
1736 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001737 virtual bool InstructionTypeEquals(HInstruction* other) const {
1738 UNUSED(other);
1739 return false;
1740 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001741
1742 // Returns whether any data encoded in the two instructions is equal.
1743 // This method does not look at the inputs. Both instructions must be
1744 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001745 virtual bool InstructionDataEquals(HInstruction* other) const {
1746 UNUSED(other);
1747 return false;
1748 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001749
1750 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001751 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001752 // 2) Their inputs are identical.
1753 bool Equals(HInstruction* other) const;
1754
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001755 virtual InstructionKind GetKind() const = 0;
1756
1757 virtual size_t ComputeHashCode() const {
1758 size_t result = GetKind();
1759 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1760 result = (result * 31) + InputAt(i)->GetId();
1761 }
1762 return result;
1763 }
1764
1765 SideEffects GetSideEffects() const { return side_effects_; }
1766
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001767 size_t GetLifetimePosition() const { return lifetime_position_; }
1768 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1769 LiveInterval* GetLiveInterval() const { return live_interval_; }
1770 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1771 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1772
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001773 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1774
1775 // Returns whether the code generation of the instruction will require to have access
1776 // to the current method. Such instructions are:
1777 // (1): Instructions that require an environment, as calling the runtime requires
1778 // to walk the stack and have the current method stored at a specific stack address.
1779 // (2): Object literals like classes and strings, that are loaded from the dex cache
1780 // fields of the current method.
1781 bool NeedsCurrentMethod() const {
1782 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1783 }
1784
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001785 virtual bool NeedsDexCache() const { return false; }
1786
Mark Mendellc4701932015-04-10 13:18:51 -04001787 // Does this instruction have any use in an environment before
1788 // control flow hits 'other'?
1789 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1790
1791 // Remove all references to environment uses of this instruction.
1792 // The caller must ensure that this is safe to do.
1793 void RemoveEnvironmentUsers();
1794
David Brazdil1abb4192015-02-17 18:33:36 +00001795 protected:
1796 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1797 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1798
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001799 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001800 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1801
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001802 HInstruction* previous_;
1803 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001804 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001805
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001806 // An instruction gets an id when it is added to the graph.
1807 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001808 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001809 int id_;
1810
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001811 // When doing liveness analysis, instructions that have uses get an SSA index.
1812 int ssa_index_;
1813
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001814 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001815 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001816
1817 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001818 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001819
Nicolas Geoffray39468442014-09-02 15:17:15 +01001820 // The environment associated with this instruction. Not null if the instruction
1821 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001822 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001823
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001824 // Set by the code generator.
1825 LocationSummary* locations_;
1826
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001827 // Set by the liveness analysis.
1828 LiveInterval* live_interval_;
1829
1830 // Set by the liveness analysis, this is the position in a linear
1831 // order of blocks where this instruction's live interval start.
1832 size_t lifetime_position_;
1833
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001834 const SideEffects side_effects_;
1835
Calin Juravleacf735c2015-02-12 15:25:22 +00001836 // TODO: for primitive types this should be marked as invalid.
1837 ReferenceTypeInfo reference_type_info_;
1838
David Brazdil1abb4192015-02-17 18:33:36 +00001839 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001840 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001841 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001842 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001843 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001844
1845 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1846};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001847std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001848
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001849class HInputIterator : public ValueObject {
1850 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001851 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001852
1853 bool Done() const { return index_ == instruction_->InputCount(); }
1854 HInstruction* Current() const { return instruction_->InputAt(index_); }
1855 void Advance() { index_++; }
1856
1857 private:
1858 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001859 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001860
1861 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1862};
1863
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001864class HInstructionIterator : public ValueObject {
1865 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001866 explicit HInstructionIterator(const HInstructionList& instructions)
1867 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001868 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001869 }
1870
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001871 bool Done() const { return instruction_ == nullptr; }
1872 HInstruction* Current() const { return instruction_; }
1873 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001874 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001875 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001876 }
1877
1878 private:
1879 HInstruction* instruction_;
1880 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001881
1882 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001883};
1884
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001885class HBackwardInstructionIterator : public ValueObject {
1886 public:
1887 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1888 : instruction_(instructions.last_instruction_) {
1889 next_ = Done() ? nullptr : instruction_->GetPrevious();
1890 }
1891
1892 bool Done() const { return instruction_ == nullptr; }
1893 HInstruction* Current() const { return instruction_; }
1894 void Advance() {
1895 instruction_ = next_;
1896 next_ = Done() ? nullptr : instruction_->GetPrevious();
1897 }
1898
1899 private:
1900 HInstruction* instruction_;
1901 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001902
1903 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001904};
1905
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001906// An embedded container with N elements of type T. Used (with partial
1907// specialization for N=0) because embedded arrays cannot have size 0.
1908template<typename T, intptr_t N>
1909class EmbeddedArray {
1910 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001911 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001912
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001913 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001914
1915 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001916 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001917 return elements_[i];
1918 }
1919
1920 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001921 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001922 return elements_[i];
1923 }
1924
1925 const T& At(intptr_t i) const {
1926 return (*this)[i];
1927 }
1928
1929 void SetAt(intptr_t i, const T& val) {
1930 (*this)[i] = val;
1931 }
1932
1933 private:
1934 T elements_[N];
1935};
1936
1937template<typename T>
1938class EmbeddedArray<T, 0> {
1939 public:
1940 intptr_t length() const { return 0; }
1941 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001942 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001943 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001944 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001945 }
1946 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001947 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001948 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001949 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001950 }
1951};
1952
1953template<intptr_t N>
1954class HTemplateInstruction: public HInstruction {
1955 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001956 HTemplateInstruction<N>(SideEffects side_effects)
1957 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001958 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001959
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001960 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001961
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001962 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001963 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1964
1965 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1966 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001967 }
1968
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001969 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001970 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001971
1972 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001973};
1974
Dave Allison20dfc792014-06-16 20:44:29 -07001975template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001976class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001977 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001978 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1979 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001980 virtual ~HExpression() {}
1981
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001982 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001983
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001984 protected:
1985 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001986};
1987
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001988// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1989// instruction that branches to the exit block.
1990class HReturnVoid : public HTemplateInstruction<0> {
1991 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001992 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001993
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001994 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001995
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001996 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001997
1998 private:
1999 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2000};
2001
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002002// Represents dex's RETURN opcodes. A HReturn is a control flow
2003// instruction that branches to the exit block.
2004class HReturn : public HTemplateInstruction<1> {
2005 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002006 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002007 SetRawInputAt(0, value);
2008 }
2009
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002010 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002011
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002012 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002013
2014 private:
2015 DISALLOW_COPY_AND_ASSIGN(HReturn);
2016};
2017
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002018// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002019// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002020// exit block.
2021class HExit : public HTemplateInstruction<0> {
2022 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002023 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002024
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002025 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002026
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002027 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002028
2029 private:
2030 DISALLOW_COPY_AND_ASSIGN(HExit);
2031};
2032
2033// Jumps from one block to another.
2034class HGoto : public HTemplateInstruction<0> {
2035 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002036 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2037
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002038 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002039
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002040 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002041 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002042 }
2043
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002044 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002045
2046 private:
2047 DISALLOW_COPY_AND_ASSIGN(HGoto);
2048};
2049
Dave Allison20dfc792014-06-16 20:44:29 -07002050
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002051// Conditional branch. A block ending with an HIf instruction must have
2052// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002053class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002054 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002055 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002056 SetRawInputAt(0, input);
2057 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002058
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002059 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002060
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002061 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002062 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002063 }
2064
2065 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002066 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002067 }
2068
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002069 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002070
2071 private:
2072 DISALLOW_COPY_AND_ASSIGN(HIf);
2073};
2074
David Brazdilfc6a86a2015-06-26 10:33:45 +00002075
2076// Abstract instruction which marks the beginning and/or end of a try block and
2077// links it to the respective exception handlers. Behaves the same as a Goto in
2078// non-exceptional control flow.
2079// Normal-flow successor is stored at index zero, exception handlers under
2080// higher indices in no particular order.
2081class HTryBoundary : public HTemplateInstruction<0> {
2082 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002083 enum BoundaryKind {
2084 kEntry,
2085 kExit,
2086 };
2087
2088 explicit HTryBoundary(BoundaryKind kind)
2089 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002090
2091 bool IsControlFlow() const OVERRIDE { return true; }
2092
2093 // Returns the block's non-exceptional successor (index zero).
2094 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2095
2096 // Returns whether `handler` is among its exception handlers (non-zero index
2097 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002098 bool HasExceptionHandler(const HBasicBlock& handler) const {
2099 DCHECK(handler.IsCatchBlock());
2100 return GetBlock()->GetSuccessors().Contains(
2101 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002102 }
2103
2104 // If not present already, adds `handler` to its block's list of exception
2105 // handlers.
2106 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002107 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002108 GetBlock()->AddSuccessor(handler);
2109 }
2110 }
2111
David Brazdil56e1acc2015-06-30 15:41:36 +01002112 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002113
David Brazdilffee3d32015-07-06 11:48:53 +01002114 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2115
David Brazdilfc6a86a2015-06-26 10:33:45 +00002116 DECLARE_INSTRUCTION(TryBoundary);
2117
2118 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002119 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002120
2121 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2122};
2123
David Brazdilffee3d32015-07-06 11:48:53 +01002124// Iterator over exception handlers of a given HTryBoundary, i.e. over
2125// exceptional successors of its basic block.
2126class HExceptionHandlerIterator : public ValueObject {
2127 public:
2128 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2129 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2130
2131 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2132 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
2133 size_t CurrentSuccessorIndex() const { return index_; }
2134 void Advance() { ++index_; }
2135
2136 private:
2137 const HBasicBlock& block_;
2138 size_t index_;
2139
2140 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2141};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002142
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002143// Deoptimize to interpreter, upon checking a condition.
2144class HDeoptimize : public HTemplateInstruction<1> {
2145 public:
2146 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2147 : HTemplateInstruction(SideEffects::None()),
2148 dex_pc_(dex_pc) {
2149 SetRawInputAt(0, cond);
2150 }
2151
2152 bool NeedsEnvironment() const OVERRIDE { return true; }
2153 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002154 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002155
2156 DECLARE_INSTRUCTION(Deoptimize);
2157
2158 private:
2159 uint32_t dex_pc_;
2160
2161 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2162};
2163
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002164// Represents the ArtMethod that was passed as a first argument to
2165// the method. It is used by instructions that depend on it, like
2166// instructions that work with the dex cache.
2167class HCurrentMethod : public HExpression<0> {
2168 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002169 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002170
2171 DECLARE_INSTRUCTION(CurrentMethod);
2172
2173 private:
2174 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2175};
2176
Roland Levillain88cb1752014-10-20 16:36:47 +01002177class HUnaryOperation : public HExpression<1> {
2178 public:
2179 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2180 : HExpression(result_type, SideEffects::None()) {
2181 SetRawInputAt(0, input);
2182 }
2183
2184 HInstruction* GetInput() const { return InputAt(0); }
2185 Primitive::Type GetResultType() const { return GetType(); }
2186
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002187 bool CanBeMoved() const OVERRIDE { return true; }
2188 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002189 UNUSED(other);
2190 return true;
2191 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002192
Roland Levillain9240d6a2014-10-20 16:47:04 +01002193 // Try to statically evaluate `operation` and return a HConstant
2194 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002195 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002196 HConstant* TryStaticEvaluation() const;
2197
2198 // Apply this operation to `x`.
2199 virtual int32_t Evaluate(int32_t x) const = 0;
2200 virtual int64_t Evaluate(int64_t x) const = 0;
2201
Roland Levillain88cb1752014-10-20 16:36:47 +01002202 DECLARE_INSTRUCTION(UnaryOperation);
2203
2204 private:
2205 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2206};
2207
Dave Allison20dfc792014-06-16 20:44:29 -07002208class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002209 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002210 HBinaryOperation(Primitive::Type result_type,
2211 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002212 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002213 SetRawInputAt(0, left);
2214 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002215 }
2216
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002217 HInstruction* GetLeft() const { return InputAt(0); }
2218 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002219 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002220
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002221 virtual bool IsCommutative() const { return false; }
2222
2223 // Put constant on the right.
2224 // Returns whether order is changed.
2225 bool OrderInputsWithConstantOnTheRight() {
2226 HInstruction* left = InputAt(0);
2227 HInstruction* right = InputAt(1);
2228 if (left->IsConstant() && !right->IsConstant()) {
2229 ReplaceInput(right, 0);
2230 ReplaceInput(left, 1);
2231 return true;
2232 }
2233 return false;
2234 }
2235
2236 // Order inputs by instruction id, but favor constant on the right side.
2237 // This helps GVN for commutative ops.
2238 void OrderInputs() {
2239 DCHECK(IsCommutative());
2240 HInstruction* left = InputAt(0);
2241 HInstruction* right = InputAt(1);
2242 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2243 return;
2244 }
2245 if (OrderInputsWithConstantOnTheRight()) {
2246 return;
2247 }
2248 // Order according to instruction id.
2249 if (left->GetId() > right->GetId()) {
2250 ReplaceInput(right, 0);
2251 ReplaceInput(left, 1);
2252 }
2253 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002254
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002255 bool CanBeMoved() const OVERRIDE { return true; }
2256 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002257 UNUSED(other);
2258 return true;
2259 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002260
Roland Levillain9240d6a2014-10-20 16:47:04 +01002261 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002262 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002263 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002264 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002265
2266 // Apply this operation to `x` and `y`.
2267 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2268 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2269
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002270 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002271 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002272 HConstant* GetConstantRight() const;
2273
2274 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002275 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002276 HInstruction* GetLeastConstantLeft() const;
2277
Roland Levillainccc07a92014-09-16 14:48:16 +01002278 DECLARE_INSTRUCTION(BinaryOperation);
2279
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002280 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002281 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2282};
2283
Mark Mendellc4701932015-04-10 13:18:51 -04002284// The comparison bias applies for floating point operations and indicates how NaN
2285// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002286enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002287 kNoBias, // bias is not applicable (i.e. for long operation)
2288 kGtBias, // return 1 for NaN comparisons
2289 kLtBias, // return -1 for NaN comparisons
2290};
2291
Dave Allison20dfc792014-06-16 20:44:29 -07002292class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002293 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002294 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002295 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002296 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002297 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002298
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002299 bool NeedsMaterialization() const { return needs_materialization_; }
2300 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002301
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002302 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002303 // `instruction`, and disregard moves in between.
2304 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002305
Dave Allison20dfc792014-06-16 20:44:29 -07002306 DECLARE_INSTRUCTION(Condition);
2307
2308 virtual IfCondition GetCondition() const = 0;
2309
Mark Mendellc4701932015-04-10 13:18:51 -04002310 virtual IfCondition GetOppositeCondition() const = 0;
2311
Roland Levillain4fa13f62015-07-06 18:11:54 +01002312 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002313
2314 void SetBias(ComparisonBias bias) { bias_ = bias; }
2315
2316 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2317 return bias_ == other->AsCondition()->bias_;
2318 }
2319
Roland Levillain4fa13f62015-07-06 18:11:54 +01002320 bool IsFPConditionTrueIfNaN() const {
2321 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2322 IfCondition if_cond = GetCondition();
2323 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2324 }
2325
2326 bool IsFPConditionFalseIfNaN() const {
2327 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2328 IfCondition if_cond = GetCondition();
2329 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2330 }
2331
Dave Allison20dfc792014-06-16 20:44:29 -07002332 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002333 // For register allocation purposes, returns whether this instruction needs to be
2334 // materialized (that is, not just be in the processor flags).
2335 bool needs_materialization_;
2336
Mark Mendellc4701932015-04-10 13:18:51 -04002337 // Needed if we merge a HCompare into a HCondition.
2338 ComparisonBias bias_;
2339
Dave Allison20dfc792014-06-16 20:44:29 -07002340 DISALLOW_COPY_AND_ASSIGN(HCondition);
2341};
2342
2343// Instruction to check if two inputs are equal to each other.
2344class HEqual : public HCondition {
2345 public:
2346 HEqual(HInstruction* first, HInstruction* second)
2347 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002348
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002349 bool IsCommutative() const OVERRIDE { return true; }
2350
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002351 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002352 return x == y ? 1 : 0;
2353 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002354 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002355 return x == y ? 1 : 0;
2356 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002357
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002358 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002359
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002360 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002361 return kCondEQ;
2362 }
2363
Mark Mendellc4701932015-04-10 13:18:51 -04002364 IfCondition GetOppositeCondition() const OVERRIDE {
2365 return kCondNE;
2366 }
2367
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002368 private:
2369 DISALLOW_COPY_AND_ASSIGN(HEqual);
2370};
2371
Dave Allison20dfc792014-06-16 20:44:29 -07002372class HNotEqual : public HCondition {
2373 public:
2374 HNotEqual(HInstruction* first, HInstruction* second)
2375 : HCondition(first, second) {}
2376
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002377 bool IsCommutative() const OVERRIDE { return true; }
2378
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002379 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002380 return x != y ? 1 : 0;
2381 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002382 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002383 return x != y ? 1 : 0;
2384 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002385
Dave Allison20dfc792014-06-16 20:44:29 -07002386 DECLARE_INSTRUCTION(NotEqual);
2387
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002388 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002389 return kCondNE;
2390 }
2391
Mark Mendellc4701932015-04-10 13:18:51 -04002392 IfCondition GetOppositeCondition() const OVERRIDE {
2393 return kCondEQ;
2394 }
2395
Dave Allison20dfc792014-06-16 20:44:29 -07002396 private:
2397 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2398};
2399
2400class HLessThan : public HCondition {
2401 public:
2402 HLessThan(HInstruction* first, HInstruction* second)
2403 : HCondition(first, second) {}
2404
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002405 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002406 return x < y ? 1 : 0;
2407 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002408 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002409 return x < y ? 1 : 0;
2410 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002411
Dave Allison20dfc792014-06-16 20:44:29 -07002412 DECLARE_INSTRUCTION(LessThan);
2413
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002414 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002415 return kCondLT;
2416 }
2417
Mark Mendellc4701932015-04-10 13:18:51 -04002418 IfCondition GetOppositeCondition() const OVERRIDE {
2419 return kCondGE;
2420 }
2421
Dave Allison20dfc792014-06-16 20:44:29 -07002422 private:
2423 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2424};
2425
2426class HLessThanOrEqual : public HCondition {
2427 public:
2428 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2429 : HCondition(first, second) {}
2430
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002431 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002432 return x <= y ? 1 : 0;
2433 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002434 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002435 return x <= y ? 1 : 0;
2436 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002437
Dave Allison20dfc792014-06-16 20:44:29 -07002438 DECLARE_INSTRUCTION(LessThanOrEqual);
2439
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002440 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002441 return kCondLE;
2442 }
2443
Mark Mendellc4701932015-04-10 13:18:51 -04002444 IfCondition GetOppositeCondition() const OVERRIDE {
2445 return kCondGT;
2446 }
2447
Dave Allison20dfc792014-06-16 20:44:29 -07002448 private:
2449 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2450};
2451
2452class HGreaterThan : public HCondition {
2453 public:
2454 HGreaterThan(HInstruction* first, HInstruction* second)
2455 : HCondition(first, second) {}
2456
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002457 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002458 return x > y ? 1 : 0;
2459 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002460 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002461 return x > y ? 1 : 0;
2462 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002463
Dave Allison20dfc792014-06-16 20:44:29 -07002464 DECLARE_INSTRUCTION(GreaterThan);
2465
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002466 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002467 return kCondGT;
2468 }
2469
Mark Mendellc4701932015-04-10 13:18:51 -04002470 IfCondition GetOppositeCondition() const OVERRIDE {
2471 return kCondLE;
2472 }
2473
Dave Allison20dfc792014-06-16 20:44:29 -07002474 private:
2475 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2476};
2477
2478class HGreaterThanOrEqual : public HCondition {
2479 public:
2480 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2481 : HCondition(first, second) {}
2482
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002483 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002484 return x >= y ? 1 : 0;
2485 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002486 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002487 return x >= y ? 1 : 0;
2488 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002489
Dave Allison20dfc792014-06-16 20:44:29 -07002490 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2491
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002492 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002493 return kCondGE;
2494 }
2495
Mark Mendellc4701932015-04-10 13:18:51 -04002496 IfCondition GetOppositeCondition() const OVERRIDE {
2497 return kCondLT;
2498 }
2499
Dave Allison20dfc792014-06-16 20:44:29 -07002500 private:
2501 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2502};
2503
2504
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002505// Instruction to check how two inputs compare to each other.
2506// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2507class HCompare : public HBinaryOperation {
2508 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002509 HCompare(Primitive::Type type,
2510 HInstruction* first,
2511 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002512 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002513 uint32_t dex_pc)
2514 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002515 DCHECK_EQ(type, first->GetType());
2516 DCHECK_EQ(type, second->GetType());
2517 }
2518
Calin Juravleddb7df22014-11-25 20:56:51 +00002519 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002520 return
2521 x == y ? 0 :
2522 x > y ? 1 :
2523 -1;
2524 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002525
2526 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002527 return
2528 x == y ? 0 :
2529 x > y ? 1 :
2530 -1;
2531 }
2532
Calin Juravleddb7df22014-11-25 20:56:51 +00002533 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2534 return bias_ == other->AsCompare()->bias_;
2535 }
2536
Mark Mendellc4701932015-04-10 13:18:51 -04002537 ComparisonBias GetBias() const { return bias_; }
2538
Roland Levillain4fa13f62015-07-06 18:11:54 +01002539 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002540
Alexey Frunze4dda3372015-06-01 18:31:49 -07002541 uint32_t GetDexPc() const { return dex_pc_; }
2542
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002543 DECLARE_INSTRUCTION(Compare);
2544
2545 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002546 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002547 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002548
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002549 DISALLOW_COPY_AND_ASSIGN(HCompare);
2550};
2551
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002552// A local in the graph. Corresponds to a Dex register.
2553class HLocal : public HTemplateInstruction<0> {
2554 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002555 explicit HLocal(uint16_t reg_number)
2556 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002557
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002558 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002559
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002560 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002561
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002562 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002563 // The Dex register number.
2564 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002565
2566 DISALLOW_COPY_AND_ASSIGN(HLocal);
2567};
2568
2569// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002570class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002571 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002572 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002573 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002574 SetRawInputAt(0, local);
2575 }
2576
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002577 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2578
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002579 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002580
2581 private:
2582 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2583};
2584
2585// Store a value in a given local. This instruction has two inputs: the value
2586// and the local.
2587class HStoreLocal : public HTemplateInstruction<2> {
2588 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002589 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002590 SetRawInputAt(0, local);
2591 SetRawInputAt(1, value);
2592 }
2593
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002594 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2595
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002596 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002597
2598 private:
2599 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2600};
2601
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002602class HConstant : public HExpression<0> {
2603 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002604 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2605
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002606 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002607
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002608 virtual bool IsMinusOne() const { return false; }
2609 virtual bool IsZero() const { return false; }
2610 virtual bool IsOne() const { return false; }
2611
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002612 DECLARE_INSTRUCTION(Constant);
2613
2614 private:
2615 DISALLOW_COPY_AND_ASSIGN(HConstant);
2616};
2617
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002618class HFloatConstant : public HConstant {
2619 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002620 float GetValue() const { return value_; }
2621
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002622 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002623 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2624 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002625 }
2626
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002627 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002628
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002629 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002630 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002631 }
2632 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002633 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002634 }
2635 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002636 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2637 }
2638 bool IsNaN() const {
2639 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002640 }
2641
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002642 DECLARE_INSTRUCTION(FloatConstant);
2643
2644 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002645 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002646 explicit HFloatConstant(int32_t value)
2647 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002648
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002649 const float value_;
2650
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002651 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002652 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002653 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002654 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2655};
2656
2657class HDoubleConstant : public HConstant {
2658 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002659 double GetValue() const { return value_; }
2660
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002661 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002662 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2663 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002664 }
2665
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002666 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002667
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002668 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002669 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002670 }
2671 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002672 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002673 }
2674 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002675 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2676 }
2677 bool IsNaN() const {
2678 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002679 }
2680
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002681 DECLARE_INSTRUCTION(DoubleConstant);
2682
2683 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002684 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002685 explicit HDoubleConstant(int64_t value)
2686 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002687
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002688 const double value_;
2689
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002690 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002691 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002692 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002693 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2694};
2695
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002696class HNullConstant : public HConstant {
2697 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002698 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2699 return true;
2700 }
2701
2702 size_t ComputeHashCode() const OVERRIDE { return 0; }
2703
2704 DECLARE_INSTRUCTION(NullConstant);
2705
2706 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002707 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2708
2709 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002710 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2711};
2712
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002713// Constants of the type int. Those can be from Dex instructions, or
2714// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002715class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002716 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002717 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002718
Calin Juravle61d544b2015-02-23 16:46:57 +00002719 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002720 return other->AsIntConstant()->value_ == value_;
2721 }
2722
Calin Juravle61d544b2015-02-23 16:46:57 +00002723 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2724
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002725 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2726 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2727 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2728
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002729 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002730
2731 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002732 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2733
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002734 const int32_t value_;
2735
David Brazdil8d5b8b22015-03-24 10:51:52 +00002736 friend class HGraph;
2737 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002738 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002739 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2740};
2741
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002742class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002743 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002744 int64_t GetValue() const { return value_; }
2745
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002746 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002747 return other->AsLongConstant()->value_ == value_;
2748 }
2749
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002750 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002751
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002752 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2753 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2754 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2755
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002756 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002757
2758 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002759 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2760
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002761 const int64_t value_;
2762
David Brazdil8d5b8b22015-03-24 10:51:52 +00002763 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002764 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2765};
2766
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002767enum class Intrinsics {
2768#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2769#include "intrinsics_list.h"
2770 kNone,
2771 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2772#undef INTRINSICS_LIST
2773#undef OPTIMIZING_INTRINSICS
2774};
2775std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2776
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002777class HInvoke : public HInstruction {
2778 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002779 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002780
2781 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2782 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002783 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002784
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002785 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002786 SetRawInputAt(index, argument);
2787 }
2788
Roland Levillain3e3d7332015-04-28 11:00:54 +01002789 // Return the number of arguments. This number can be lower than
2790 // the number of inputs returned by InputCount(), as some invoke
2791 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2792 // inputs at the end of their list of inputs.
2793 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2794
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002795 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002796
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002797 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002798
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002799 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002800 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002801
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002802 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2803
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002804 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002805 return intrinsic_;
2806 }
2807
2808 void SetIntrinsic(Intrinsics intrinsic) {
2809 intrinsic_ = intrinsic;
2810 }
2811
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002812 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002813 return GetEnvironment()->GetParent() != nullptr;
2814 }
2815
2816 bool CanThrow() const OVERRIDE { return true; }
2817
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002818 DECLARE_INSTRUCTION(Invoke);
2819
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002820 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002821 HInvoke(ArenaAllocator* arena,
2822 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002823 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002824 Primitive::Type return_type,
2825 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002826 uint32_t dex_method_index,
2827 InvokeType original_invoke_type)
Aart Bik854a02b2015-07-14 16:07:00 -07002828 : HInstruction(SideEffects::All()), // assume write/read on all fields/arrays
Roland Levillain3e3d7332015-04-28 11:00:54 +01002829 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002830 inputs_(arena, number_of_arguments),
2831 return_type_(return_type),
2832 dex_pc_(dex_pc),
2833 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002834 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002835 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002836 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2837 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002838 }
2839
David Brazdil1abb4192015-02-17 18:33:36 +00002840 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2841 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2842 inputs_.Put(index, input);
2843 }
2844
Roland Levillain3e3d7332015-04-28 11:00:54 +01002845 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002846 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002847 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002848 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002849 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002850 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002851 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002852
2853 private:
2854 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2855};
2856
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002857class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002858 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002859 // Requirements of this method call regarding the class
2860 // initialization (clinit) check of its declaring class.
2861 enum class ClinitCheckRequirement {
2862 kNone, // Class already initialized.
2863 kExplicit, // Static call having explicit clinit check as last input.
2864 kImplicit, // Static call implicitly requiring a clinit check.
2865 };
2866
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002867 HInvokeStaticOrDirect(ArenaAllocator* arena,
2868 uint32_t number_of_arguments,
2869 Primitive::Type return_type,
2870 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002871 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002872 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002873 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002874 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002875 InvokeType invoke_type,
2876 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002877 : HInvoke(arena,
2878 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002879 // There is one extra argument for the HCurrentMethod node, and
2880 // potentially one other if the clinit check is explicit, and one other
2881 // if the method is a string factory.
2882 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
2883 + (string_init_offset ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002884 return_type,
2885 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002886 dex_method_index,
2887 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002888 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002889 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002890 clinit_check_requirement_(clinit_check_requirement),
2891 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002892
Calin Juravle641547a2015-04-21 22:08:51 +01002893 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2894 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002895 // We access the method via the dex cache so we can't do an implicit null check.
2896 // TODO: for intrinsics we can generate implicit null checks.
2897 return false;
2898 }
2899
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002900 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002901 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002902 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002903 bool IsStringInit() const { return string_init_offset_ != 0; }
2904 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002905 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002906
Roland Levillain4c0eb422015-04-24 16:43:49 +01002907 // Is this instruction a call to a static method?
2908 bool IsStatic() const {
2909 return GetInvokeType() == kStatic;
2910 }
2911
Roland Levillain3e3d7332015-04-28 11:00:54 +01002912 // Remove the art::HLoadClass instruction set as last input by
2913 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2914 // the initial art::HClinitCheck instruction (only relevant for
2915 // static calls with explicit clinit check).
2916 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002917 DCHECK(IsStaticWithExplicitClinitCheck());
2918 size_t last_input_index = InputCount() - 1;
2919 HInstruction* last_input = InputAt(last_input_index);
2920 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002921 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002922 RemoveAsUserOfInput(last_input_index);
2923 inputs_.DeleteAt(last_input_index);
2924 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2925 DCHECK(IsStaticWithImplicitClinitCheck());
2926 }
2927
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002928 bool IsStringFactoryFor(HFakeString* str) const {
2929 if (!IsStringInit()) return false;
2930 // +1 for the current method.
2931 if (InputCount() == (number_of_arguments_ + 1)) return false;
2932 return InputAt(InputCount() - 1)->AsFakeString() == str;
2933 }
2934
2935 void RemoveFakeStringArgumentAsLastInput() {
2936 DCHECK(IsStringInit());
2937 size_t last_input_index = InputCount() - 1;
2938 HInstruction* last_input = InputAt(last_input_index);
2939 DCHECK(last_input != nullptr);
2940 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
2941 RemoveAsUserOfInput(last_input_index);
2942 inputs_.DeleteAt(last_input_index);
2943 }
2944
Roland Levillain4c0eb422015-04-24 16:43:49 +01002945 // Is this a call to a static method whose declaring class has an
2946 // explicit intialization check in the graph?
2947 bool IsStaticWithExplicitClinitCheck() const {
2948 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2949 }
2950
2951 // Is this a call to a static method whose declaring class has an
2952 // implicit intialization check requirement?
2953 bool IsStaticWithImplicitClinitCheck() const {
2954 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2955 }
2956
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002957 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002958
Roland Levillain4c0eb422015-04-24 16:43:49 +01002959 protected:
2960 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2961 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2962 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2963 HInstruction* input = input_record.GetInstruction();
2964 // `input` is the last input of a static invoke marked as having
2965 // an explicit clinit check. It must either be:
2966 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2967 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2968 DCHECK(input != nullptr);
2969 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2970 }
2971 return input_record;
2972 }
2973
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002974 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002975 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002976 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002977 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002978 // Thread entrypoint offset for string init method if this is a string init invoke.
2979 // Note that there are multiple string init methods, each having its own offset.
2980 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002981
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002982 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002983};
2984
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002985class HInvokeVirtual : public HInvoke {
2986 public:
2987 HInvokeVirtual(ArenaAllocator* arena,
2988 uint32_t number_of_arguments,
2989 Primitive::Type return_type,
2990 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002991 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002992 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002993 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002994 vtable_index_(vtable_index) {}
2995
Calin Juravle641547a2015-04-21 22:08:51 +01002996 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002997 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002998 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002999 }
3000
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003001 uint32_t GetVTableIndex() const { return vtable_index_; }
3002
3003 DECLARE_INSTRUCTION(InvokeVirtual);
3004
3005 private:
3006 const uint32_t vtable_index_;
3007
3008 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3009};
3010
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003011class HInvokeInterface : public HInvoke {
3012 public:
3013 HInvokeInterface(ArenaAllocator* arena,
3014 uint32_t number_of_arguments,
3015 Primitive::Type return_type,
3016 uint32_t dex_pc,
3017 uint32_t dex_method_index,
3018 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003019 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003020 imt_index_(imt_index) {}
3021
Calin Juravle641547a2015-04-21 22:08:51 +01003022 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003023 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003024 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003025 }
3026
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003027 uint32_t GetImtIndex() const { return imt_index_; }
3028 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3029
3030 DECLARE_INSTRUCTION(InvokeInterface);
3031
3032 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003033 const uint32_t imt_index_;
3034
3035 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3036};
3037
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003038class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003039 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003040 HNewInstance(HCurrentMethod* current_method,
3041 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003042 uint16_t type_index,
3043 const DexFile& dex_file,
3044 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003045 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3046 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003047 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003048 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003049 entrypoint_(entrypoint) {
3050 SetRawInputAt(0, current_method);
3051 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003052
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003053 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003054 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003055 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003056
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003057 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003058 bool NeedsEnvironment() const OVERRIDE { return true; }
3059 // It may throw when called on:
3060 // - interfaces
3061 // - abstract/innaccessible/unknown classes
3062 // TODO: optimize when possible.
3063 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003064
Calin Juravle10e244f2015-01-26 18:54:32 +00003065 bool CanBeNull() const OVERRIDE { return false; }
3066
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003067 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3068
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003069 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003070
3071 private:
3072 const uint32_t dex_pc_;
3073 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003074 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003075 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003076
3077 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3078};
3079
Roland Levillain88cb1752014-10-20 16:36:47 +01003080class HNeg : public HUnaryOperation {
3081 public:
3082 explicit HNeg(Primitive::Type result_type, HInstruction* input)
3083 : HUnaryOperation(result_type, input) {}
3084
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003085 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
3086 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003087
Roland Levillain88cb1752014-10-20 16:36:47 +01003088 DECLARE_INSTRUCTION(Neg);
3089
3090 private:
3091 DISALLOW_COPY_AND_ASSIGN(HNeg);
3092};
3093
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003094class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003095 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003096 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003097 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003098 uint32_t dex_pc,
3099 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003100 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003101 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003102 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3103 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003104 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003105 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003106 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003107 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003108 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003109 }
3110
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003111 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003112 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003113 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003114
3115 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003116 bool NeedsEnvironment() const OVERRIDE { return true; }
3117
Mingyao Yang0c365e62015-03-31 15:09:29 -07003118 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3119 bool CanThrow() const OVERRIDE { return true; }
3120
Calin Juravle10e244f2015-01-26 18:54:32 +00003121 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003122
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003123 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3124
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003125 DECLARE_INSTRUCTION(NewArray);
3126
3127 private:
3128 const uint32_t dex_pc_;
3129 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003130 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003131 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003132
3133 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3134};
3135
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003136class HAdd : public HBinaryOperation {
3137 public:
3138 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3139 : HBinaryOperation(result_type, left, right) {}
3140
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003141 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003142
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003143 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003144 return x + y;
3145 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003146 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003147 return x + y;
3148 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003149
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003150 DECLARE_INSTRUCTION(Add);
3151
3152 private:
3153 DISALLOW_COPY_AND_ASSIGN(HAdd);
3154};
3155
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003156class HSub : public HBinaryOperation {
3157 public:
3158 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3159 : HBinaryOperation(result_type, left, right) {}
3160
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003161 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003162 return x - y;
3163 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003164 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003165 return x - y;
3166 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003167
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003168 DECLARE_INSTRUCTION(Sub);
3169
3170 private:
3171 DISALLOW_COPY_AND_ASSIGN(HSub);
3172};
3173
Calin Juravle34bacdf2014-10-07 20:23:36 +01003174class HMul : public HBinaryOperation {
3175 public:
3176 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3177 : HBinaryOperation(result_type, left, right) {}
3178
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003179 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003180
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003181 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
3182 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003183
3184 DECLARE_INSTRUCTION(Mul);
3185
3186 private:
3187 DISALLOW_COPY_AND_ASSIGN(HMul);
3188};
3189
Calin Juravle7c4954d2014-10-28 16:57:40 +00003190class HDiv : public HBinaryOperation {
3191 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003192 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3193 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003194
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003195 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003196 // Our graph structure ensures we never have 0 for `y` during constant folding.
3197 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003198 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003199 return (y == -1) ? -x : x / y;
3200 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003201
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003202 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003203 DCHECK_NE(y, 0);
3204 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3205 return (y == -1) ? -x : x / y;
3206 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003207
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003208 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003209
Calin Juravle7c4954d2014-10-28 16:57:40 +00003210 DECLARE_INSTRUCTION(Div);
3211
3212 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003213 const uint32_t dex_pc_;
3214
Calin Juravle7c4954d2014-10-28 16:57:40 +00003215 DISALLOW_COPY_AND_ASSIGN(HDiv);
3216};
3217
Calin Juravlebacfec32014-11-14 15:54:36 +00003218class HRem : public HBinaryOperation {
3219 public:
3220 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3221 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
3222
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003223 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003224 DCHECK_NE(y, 0);
3225 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3226 return (y == -1) ? 0 : x % y;
3227 }
3228
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003229 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003230 DCHECK_NE(y, 0);
3231 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3232 return (y == -1) ? 0 : x % y;
3233 }
3234
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003235 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003236
3237 DECLARE_INSTRUCTION(Rem);
3238
3239 private:
3240 const uint32_t dex_pc_;
3241
3242 DISALLOW_COPY_AND_ASSIGN(HRem);
3243};
3244
Calin Juravled0d48522014-11-04 16:40:20 +00003245class HDivZeroCheck : public HExpression<1> {
3246 public:
3247 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3248 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3249 SetRawInputAt(0, value);
3250 }
3251
3252 bool CanBeMoved() const OVERRIDE { return true; }
3253
3254 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3255 UNUSED(other);
3256 return true;
3257 }
3258
3259 bool NeedsEnvironment() const OVERRIDE { return true; }
3260 bool CanThrow() const OVERRIDE { return true; }
3261
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003262 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003263
3264 DECLARE_INSTRUCTION(DivZeroCheck);
3265
3266 private:
3267 const uint32_t dex_pc_;
3268
3269 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3270};
3271
Calin Juravle9aec02f2014-11-18 23:06:35 +00003272class HShl : public HBinaryOperation {
3273 public:
3274 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3275 : HBinaryOperation(result_type, left, right) {}
3276
3277 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
3278 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
3279
3280 DECLARE_INSTRUCTION(Shl);
3281
3282 private:
3283 DISALLOW_COPY_AND_ASSIGN(HShl);
3284};
3285
3286class HShr : public HBinaryOperation {
3287 public:
3288 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3289 : HBinaryOperation(result_type, left, right) {}
3290
3291 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
3292 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
3293
3294 DECLARE_INSTRUCTION(Shr);
3295
3296 private:
3297 DISALLOW_COPY_AND_ASSIGN(HShr);
3298};
3299
3300class HUShr : public HBinaryOperation {
3301 public:
3302 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3303 : HBinaryOperation(result_type, left, right) {}
3304
3305 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
3306 uint32_t ux = static_cast<uint32_t>(x);
3307 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
3308 return static_cast<int32_t>(ux >> uy);
3309 }
3310
3311 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
3312 uint64_t ux = static_cast<uint64_t>(x);
3313 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
3314 return static_cast<int64_t>(ux >> uy);
3315 }
3316
3317 DECLARE_INSTRUCTION(UShr);
3318
3319 private:
3320 DISALLOW_COPY_AND_ASSIGN(HUShr);
3321};
3322
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003323class HAnd : public HBinaryOperation {
3324 public:
3325 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3326 : HBinaryOperation(result_type, left, right) {}
3327
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003328 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003329
3330 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3331 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3332
3333 DECLARE_INSTRUCTION(And);
3334
3335 private:
3336 DISALLOW_COPY_AND_ASSIGN(HAnd);
3337};
3338
3339class HOr : public HBinaryOperation {
3340 public:
3341 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3342 : HBinaryOperation(result_type, left, right) {}
3343
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003344 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003345
3346 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3347 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3348
3349 DECLARE_INSTRUCTION(Or);
3350
3351 private:
3352 DISALLOW_COPY_AND_ASSIGN(HOr);
3353};
3354
3355class HXor : public HBinaryOperation {
3356 public:
3357 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3358 : HBinaryOperation(result_type, left, right) {}
3359
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003360 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003361
3362 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3363 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3364
3365 DECLARE_INSTRUCTION(Xor);
3366
3367 private:
3368 DISALLOW_COPY_AND_ASSIGN(HXor);
3369};
3370
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003371// The value of a parameter in this method. Its location depends on
3372// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003373class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003374 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003375 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3376 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003377
3378 uint8_t GetIndex() const { return index_; }
3379
Calin Juravle10e244f2015-01-26 18:54:32 +00003380 bool CanBeNull() const OVERRIDE { return !is_this_; }
3381
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003382 bool IsThis() const { return is_this_; }
3383
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003384 DECLARE_INSTRUCTION(ParameterValue);
3385
3386 private:
3387 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003388 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003389 const uint8_t index_;
3390
Calin Juravle10e244f2015-01-26 18:54:32 +00003391 // Whether or not the parameter value corresponds to 'this' argument.
3392 const bool is_this_;
3393
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003394 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3395};
3396
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003397class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003398 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003399 explicit HNot(Primitive::Type result_type, HInstruction* input)
3400 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003401
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003402 bool CanBeMoved() const OVERRIDE { return true; }
3403 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003404 UNUSED(other);
3405 return true;
3406 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003407
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003408 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3409 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003410
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003411 DECLARE_INSTRUCTION(Not);
3412
3413 private:
3414 DISALLOW_COPY_AND_ASSIGN(HNot);
3415};
3416
David Brazdil66d126e2015-04-03 16:02:44 +01003417class HBooleanNot : public HUnaryOperation {
3418 public:
3419 explicit HBooleanNot(HInstruction* input)
3420 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3421
3422 bool CanBeMoved() const OVERRIDE { return true; }
3423 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3424 UNUSED(other);
3425 return true;
3426 }
3427
3428 int32_t Evaluate(int32_t x) const OVERRIDE {
3429 DCHECK(IsUint<1>(x));
3430 return !x;
3431 }
3432
3433 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3434 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3435 UNREACHABLE();
3436 }
3437
3438 DECLARE_INSTRUCTION(BooleanNot);
3439
3440 private:
3441 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3442};
3443
Roland Levillaindff1f282014-11-05 14:15:05 +00003444class HTypeConversion : public HExpression<1> {
3445 public:
3446 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003447 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3448 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003449 SetRawInputAt(0, input);
3450 DCHECK_NE(input->GetType(), result_type);
3451 }
3452
3453 HInstruction* GetInput() const { return InputAt(0); }
3454 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3455 Primitive::Type GetResultType() const { return GetType(); }
3456
Roland Levillain624279f2014-12-04 11:54:28 +00003457 // Required by the x86 and ARM code generators when producing calls
3458 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003459 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003460
Roland Levillaindff1f282014-11-05 14:15:05 +00003461 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003462 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003463
Mark Mendelle82549b2015-05-06 10:55:34 -04003464 // Try to statically evaluate the conversion and return a HConstant
3465 // containing the result. If the input cannot be converted, return nullptr.
3466 HConstant* TryStaticEvaluation() const;
3467
Roland Levillaindff1f282014-11-05 14:15:05 +00003468 DECLARE_INSTRUCTION(TypeConversion);
3469
3470 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003471 const uint32_t dex_pc_;
3472
Roland Levillaindff1f282014-11-05 14:15:05 +00003473 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3474};
3475
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003476static constexpr uint32_t kNoRegNumber = -1;
3477
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003478class HPhi : public HInstruction {
3479 public:
3480 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003481 : HInstruction(SideEffects::None()),
3482 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003483 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003484 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003485 is_live_(false),
3486 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003487 inputs_.SetSize(number_of_inputs);
3488 }
3489
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003490 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3491 static Primitive::Type ToPhiType(Primitive::Type type) {
3492 switch (type) {
3493 case Primitive::kPrimBoolean:
3494 case Primitive::kPrimByte:
3495 case Primitive::kPrimShort:
3496 case Primitive::kPrimChar:
3497 return Primitive::kPrimInt;
3498 default:
3499 return type;
3500 }
3501 }
3502
David Brazdilffee3d32015-07-06 11:48:53 +01003503 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3504
Calin Juravle10e244f2015-01-26 18:54:32 +00003505 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003506
3507 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003508 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003509
Calin Juravle10e244f2015-01-26 18:54:32 +00003510 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003511 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003512
Calin Juravle10e244f2015-01-26 18:54:32 +00003513 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3514 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3515
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003516 uint32_t GetRegNumber() const { return reg_number_; }
3517
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003518 void SetDead() { is_live_ = false; }
3519 void SetLive() { is_live_ = true; }
3520 bool IsDead() const { return !is_live_; }
3521 bool IsLive() const { return is_live_; }
3522
Calin Juravlea4f88312015-04-16 12:57:19 +01003523 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3524 // An equivalent phi is a phi having the same dex register and type.
3525 // It assumes that phis with the same dex register are adjacent.
3526 HPhi* GetNextEquivalentPhiWithSameType() {
3527 HInstruction* next = GetNext();
3528 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3529 if (next->GetType() == GetType()) {
3530 return next->AsPhi();
3531 }
3532 next = next->GetNext();
3533 }
3534 return nullptr;
3535 }
3536
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003537 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003538
David Brazdil1abb4192015-02-17 18:33:36 +00003539 protected:
3540 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3541
3542 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3543 inputs_.Put(index, input);
3544 }
3545
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003546 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003547 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003548 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003549 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003550 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003551 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003552
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003553 DISALLOW_COPY_AND_ASSIGN(HPhi);
3554};
3555
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003556class HNullCheck : public HExpression<1> {
3557 public:
3558 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003559 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003560 SetRawInputAt(0, value);
3561 }
3562
Calin Juravle10e244f2015-01-26 18:54:32 +00003563 bool CanBeMoved() const OVERRIDE { return true; }
3564 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003565 UNUSED(other);
3566 return true;
3567 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003568
Calin Juravle10e244f2015-01-26 18:54:32 +00003569 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003570
Calin Juravle10e244f2015-01-26 18:54:32 +00003571 bool CanThrow() const OVERRIDE { return true; }
3572
3573 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003574
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003575 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003576
3577 DECLARE_INSTRUCTION(NullCheck);
3578
3579 private:
3580 const uint32_t dex_pc_;
3581
3582 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3583};
3584
3585class FieldInfo : public ValueObject {
3586 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003587 FieldInfo(MemberOffset field_offset,
3588 Primitive::Type field_type,
3589 bool is_volatile,
3590 uint32_t index,
3591 const DexFile& dex_file)
3592 : field_offset_(field_offset),
3593 field_type_(field_type),
3594 is_volatile_(is_volatile),
3595 index_(index),
3596 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003597
3598 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003599 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003600 uint32_t GetFieldIndex() const { return index_; }
3601 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003602 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003603
3604 private:
3605 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003606 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003607 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003608 uint32_t index_;
3609 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003610};
3611
3612class HInstanceFieldGet : public HExpression<1> {
3613 public:
3614 HInstanceFieldGet(HInstruction* value,
3615 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003616 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003617 bool is_volatile,
3618 uint32_t field_idx,
3619 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003620 : HExpression(
3621 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01003622 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003623 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003624 SetRawInputAt(0, value);
3625 }
3626
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003627 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003628
3629 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3630 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3631 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003632 }
3633
Calin Juravle641547a2015-04-21 22:08:51 +01003634 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3635 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003636 }
3637
3638 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003639 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3640 }
3641
Calin Juravle52c48962014-12-16 17:02:57 +00003642 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003643 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003644 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003645 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003646
3647 DECLARE_INSTRUCTION(InstanceFieldGet);
3648
3649 private:
3650 const FieldInfo field_info_;
3651
3652 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3653};
3654
3655class HInstanceFieldSet : public HTemplateInstruction<2> {
3656 public:
3657 HInstanceFieldSet(HInstruction* object,
3658 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003659 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003660 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003661 bool is_volatile,
3662 uint32_t field_idx,
3663 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003664 : HTemplateInstruction(
3665 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003666 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003667 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003668 SetRawInputAt(0, object);
3669 SetRawInputAt(1, value);
3670 }
3671
Calin Juravle641547a2015-04-21 22:08:51 +01003672 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3673 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003674 }
3675
Calin Juravle52c48962014-12-16 17:02:57 +00003676 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003677 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003678 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003679 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003680 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003681 bool GetValueCanBeNull() const { return value_can_be_null_; }
3682 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003683
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003684 DECLARE_INSTRUCTION(InstanceFieldSet);
3685
3686 private:
3687 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003688 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003689
3690 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3691};
3692
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003693class HArrayGet : public HExpression<2> {
3694 public:
3695 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07003696 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003697 SetRawInputAt(0, array);
3698 SetRawInputAt(1, index);
3699 }
3700
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003701 bool CanBeMoved() const OVERRIDE { return true; }
3702 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003703 UNUSED(other);
3704 return true;
3705 }
Calin Juravle641547a2015-04-21 22:08:51 +01003706 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3707 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003708 // TODO: We can be smarter here.
3709 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3710 // which generates the implicit null check. There are cases when these can be removed
3711 // to produce better code. If we ever add optimizations to do so we should allow an
3712 // implicit check here (as long as the address falls in the first page).
3713 return false;
3714 }
3715
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003716 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003717
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003718 HInstruction* GetArray() const { return InputAt(0); }
3719 HInstruction* GetIndex() const { return InputAt(1); }
3720
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003721 DECLARE_INSTRUCTION(ArrayGet);
3722
3723 private:
3724 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3725};
3726
3727class HArraySet : public HTemplateInstruction<3> {
3728 public:
3729 HArraySet(HInstruction* array,
3730 HInstruction* index,
3731 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003732 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003733 uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07003734 : HTemplateInstruction(SideEffects::ArrayWriteOfType(expected_component_type)),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003735 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003736 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003737 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3738 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003739 SetRawInputAt(0, array);
3740 SetRawInputAt(1, index);
3741 SetRawInputAt(2, value);
3742 }
3743
Calin Juravle77520bc2015-01-12 18:45:46 +00003744 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003745 // We currently always call a runtime method to catch array store
3746 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003747 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003748 }
3749
Mingyao Yang81014cb2015-06-02 03:16:27 -07003750 // Can throw ArrayStoreException.
3751 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3752
Calin Juravle641547a2015-04-21 22:08:51 +01003753 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3754 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003755 // TODO: Same as for ArrayGet.
3756 return false;
3757 }
3758
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003759 void ClearNeedsTypeCheck() {
3760 needs_type_check_ = false;
3761 }
3762
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003763 void ClearValueCanBeNull() {
3764 value_can_be_null_ = false;
3765 }
3766
3767 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003768 bool NeedsTypeCheck() const { return needs_type_check_; }
3769
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003770 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003771
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003772 HInstruction* GetArray() const { return InputAt(0); }
3773 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003774 HInstruction* GetValue() const { return InputAt(2); }
3775
3776 Primitive::Type GetComponentType() const {
3777 // The Dex format does not type floating point index operations. Since the
3778 // `expected_component_type_` is set during building and can therefore not
3779 // be correct, we also check what is the value type. If it is a floating
3780 // point type, we must use that type.
3781 Primitive::Type value_type = GetValue()->GetType();
3782 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3783 ? value_type
3784 : expected_component_type_;
3785 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003786
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003787 DECLARE_INSTRUCTION(ArraySet);
3788
3789 private:
3790 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003791 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003792 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003793 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003794
3795 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3796};
3797
3798class HArrayLength : public HExpression<1> {
3799 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003800 explicit HArrayLength(HInstruction* array)
3801 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3802 // Note that arrays do not change length, so the instruction does not
3803 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003804 SetRawInputAt(0, array);
3805 }
3806
Calin Juravle77520bc2015-01-12 18:45:46 +00003807 bool CanBeMoved() const OVERRIDE { return true; }
3808 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003809 UNUSED(other);
3810 return true;
3811 }
Calin Juravle641547a2015-04-21 22:08:51 +01003812 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3813 return obj == InputAt(0);
3814 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003815
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003816 DECLARE_INSTRUCTION(ArrayLength);
3817
3818 private:
3819 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3820};
3821
3822class HBoundsCheck : public HExpression<2> {
3823 public:
3824 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003825 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003826 DCHECK(index->GetType() == Primitive::kPrimInt);
3827 SetRawInputAt(0, index);
3828 SetRawInputAt(1, length);
3829 }
3830
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003831 bool CanBeMoved() const OVERRIDE { return true; }
3832 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003833 UNUSED(other);
3834 return true;
3835 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003836
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003837 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003838
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003839 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003840
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003841 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003842
3843 DECLARE_INSTRUCTION(BoundsCheck);
3844
3845 private:
3846 const uint32_t dex_pc_;
3847
3848 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3849};
3850
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003851/**
3852 * Some DEX instructions are folded into multiple HInstructions that need
3853 * to stay live until the last HInstruction. This class
3854 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003855 * HInstruction stays live. `index` represents the stack location index of the
3856 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003857 */
3858class HTemporary : public HTemplateInstruction<0> {
3859 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003860 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003861
3862 size_t GetIndex() const { return index_; }
3863
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003864 Primitive::Type GetType() const OVERRIDE {
3865 // The previous instruction is the one that will be stored in the temporary location.
3866 DCHECK(GetPrevious() != nullptr);
3867 return GetPrevious()->GetType();
3868 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003869
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003870 DECLARE_INSTRUCTION(Temporary);
3871
3872 private:
3873 const size_t index_;
3874
3875 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3876};
3877
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003878class HSuspendCheck : public HTemplateInstruction<0> {
3879 public:
3880 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003881 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003882
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003883 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003884 return true;
3885 }
3886
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003887 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003888 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3889 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003890
3891 DECLARE_INSTRUCTION(SuspendCheck);
3892
3893 private:
3894 const uint32_t dex_pc_;
3895
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003896 // Only used for code generation, in order to share the same slow path between back edges
3897 // of a same loop.
3898 SlowPathCode* slow_path_;
3899
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003900 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3901};
3902
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003903/**
3904 * Instruction to load a Class object.
3905 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003906class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003907 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003908 HLoadClass(HCurrentMethod* current_method,
3909 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003910 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003911 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003912 uint32_t dex_pc)
3913 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3914 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003915 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003916 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003917 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003918 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00003919 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003920 SetRawInputAt(0, current_method);
3921 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003922
3923 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003924
3925 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3926 return other->AsLoadClass()->type_index_ == type_index_;
3927 }
3928
3929 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3930
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003931 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003932 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003933 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01003934 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003935
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003936 bool NeedsEnvironment() const OVERRIDE {
3937 // Will call runtime and load the class if the class is not loaded yet.
3938 // TODO: finer grain decision.
3939 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003940 }
3941
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003942 bool MustGenerateClinitCheck() const {
3943 return generate_clinit_check_;
3944 }
3945
Calin Juravle0ba218d2015-05-19 18:46:01 +01003946 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3947 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003948 }
3949
3950 bool CanCallRuntime() const {
3951 return MustGenerateClinitCheck() || !is_referrers_class_;
3952 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003953
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003954 bool CanThrow() const OVERRIDE {
3955 // May call runtime and and therefore can throw.
3956 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003957 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003958 }
3959
Calin Juravleacf735c2015-02-12 15:25:22 +00003960 ReferenceTypeInfo GetLoadedClassRTI() {
3961 return loaded_class_rti_;
3962 }
3963
3964 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3965 // Make sure we only set exact types (the loaded class should never be merged).
3966 DCHECK(rti.IsExact());
3967 loaded_class_rti_ = rti;
3968 }
3969
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003970 const DexFile& GetDexFile() { return dex_file_; }
3971
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003972 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3973
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003974 DECLARE_INSTRUCTION(LoadClass);
3975
3976 private:
3977 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003978 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003979 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003980 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003981 // Whether this instruction must generate the initialization check.
3982 // Used for code generation.
3983 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003984
Calin Juravleacf735c2015-02-12 15:25:22 +00003985 ReferenceTypeInfo loaded_class_rti_;
3986
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003987 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3988};
3989
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003990class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003991 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003992 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003993 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3994 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003995 dex_pc_(dex_pc) {
3996 SetRawInputAt(0, current_method);
3997 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003998
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003999 bool CanBeMoved() const OVERRIDE { return true; }
4000
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004001 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4002 return other->AsLoadString()->string_index_ == string_index_;
4003 }
4004
4005 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4006
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004007 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004008 uint32_t GetStringIndex() const { return string_index_; }
4009
4010 // TODO: Can we deopt or debug when we resolve a string?
4011 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004012 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004013
4014 DECLARE_INSTRUCTION(LoadString);
4015
4016 private:
4017 const uint32_t string_index_;
4018 const uint32_t dex_pc_;
4019
4020 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4021};
4022
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004023/**
4024 * Performs an initialization check on its Class object input.
4025 */
4026class HClinitCheck : public HExpression<1> {
4027 public:
4028 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004029 : HExpression(
4030 Primitive::kPrimNot,
Aart Bik34c3ba92015-07-20 14:08:59 -07004031 SideEffects::AllWrites()), // assume write on all fields/arrays
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004032 dex_pc_(dex_pc) {
4033 SetRawInputAt(0, constant);
4034 }
4035
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004036 bool CanBeMoved() const OVERRIDE { return true; }
4037 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4038 UNUSED(other);
4039 return true;
4040 }
4041
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004042 bool NeedsEnvironment() const OVERRIDE {
4043 // May call runtime to initialize the class.
4044 return true;
4045 }
4046
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004047 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004048
4049 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4050
4051 DECLARE_INSTRUCTION(ClinitCheck);
4052
4053 private:
4054 const uint32_t dex_pc_;
4055
4056 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4057};
4058
4059class HStaticFieldGet : public HExpression<1> {
4060 public:
4061 HStaticFieldGet(HInstruction* cls,
4062 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004063 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004064 bool is_volatile,
4065 uint32_t field_idx,
4066 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004067 : HExpression(
4068 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004069 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004070 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004071 SetRawInputAt(0, cls);
4072 }
4073
Calin Juravle52c48962014-12-16 17:02:57 +00004074
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004075 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004076
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004077 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004078 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4079 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004080 }
4081
4082 size_t ComputeHashCode() const OVERRIDE {
4083 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4084 }
4085
Calin Juravle52c48962014-12-16 17:02:57 +00004086 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004087 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4088 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004089 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004090
4091 DECLARE_INSTRUCTION(StaticFieldGet);
4092
4093 private:
4094 const FieldInfo field_info_;
4095
4096 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4097};
4098
4099class HStaticFieldSet : public HTemplateInstruction<2> {
4100 public:
4101 HStaticFieldSet(HInstruction* cls,
4102 HInstruction* value,
4103 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004104 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004105 bool is_volatile,
4106 uint32_t field_idx,
4107 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004108 : HTemplateInstruction(
4109 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004110 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004111 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004112 SetRawInputAt(0, cls);
4113 SetRawInputAt(1, value);
4114 }
4115
Calin Juravle52c48962014-12-16 17:02:57 +00004116 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004117 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4118 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004119 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004120
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004121 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004122 bool GetValueCanBeNull() const { return value_can_be_null_; }
4123 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004124
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004125 DECLARE_INSTRUCTION(StaticFieldSet);
4126
4127 private:
4128 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004129 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004130
4131 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4132};
4133
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004134// Implement the move-exception DEX instruction.
4135class HLoadException : public HExpression<0> {
4136 public:
4137 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4138
4139 DECLARE_INSTRUCTION(LoadException);
4140
4141 private:
4142 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4143};
4144
4145class HThrow : public HTemplateInstruction<1> {
4146 public:
4147 HThrow(HInstruction* exception, uint32_t dex_pc)
4148 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
4149 SetRawInputAt(0, exception);
4150 }
4151
4152 bool IsControlFlow() const OVERRIDE { return true; }
4153
4154 bool NeedsEnvironment() const OVERRIDE { return true; }
4155
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004156 bool CanThrow() const OVERRIDE { return true; }
4157
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004158 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004159
4160 DECLARE_INSTRUCTION(Throw);
4161
4162 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004163 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004164
4165 DISALLOW_COPY_AND_ASSIGN(HThrow);
4166};
4167
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004168class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004169 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004170 HInstanceOf(HInstruction* object,
4171 HLoadClass* constant,
4172 bool class_is_final,
4173 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004174 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
4175 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004176 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004177 dex_pc_(dex_pc) {
4178 SetRawInputAt(0, object);
4179 SetRawInputAt(1, constant);
4180 }
4181
4182 bool CanBeMoved() const OVERRIDE { return true; }
4183
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004184 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004185 return true;
4186 }
4187
4188 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004189 return false;
4190 }
4191
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004192 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004193
4194 bool IsClassFinal() const { return class_is_final_; }
4195
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004196 // Used only in code generation.
4197 bool MustDoNullCheck() const { return must_do_null_check_; }
4198 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4199
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004200 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004201
4202 private:
4203 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004204 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004205 const uint32_t dex_pc_;
4206
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004207 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4208};
4209
Calin Juravleb1498f62015-02-16 13:13:29 +00004210class HBoundType : public HExpression<1> {
4211 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004212 // Constructs an HBoundType with the given upper_bound.
4213 // Ensures that the upper_bound is valid.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004214 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004215 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004216 upper_bound_(upper_bound),
4217 upper_can_be_null_(upper_can_be_null),
4218 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004219 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004220 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004221 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004222 }
4223
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004224 // GetUpper* should only be used in reference type propagation.
4225 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4226 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004227
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004228 void SetCanBeNull(bool can_be_null) {
4229 DCHECK(upper_can_be_null_ || !can_be_null);
4230 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004231 }
4232
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004233 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4234
Calin Juravleb1498f62015-02-16 13:13:29 +00004235 DECLARE_INSTRUCTION(BoundType);
4236
4237 private:
4238 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004239 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4240 // It is used to bound the type in cases like:
4241 // if (x instanceof ClassX) {
4242 // // uper_bound_ will be ClassX
4243 // }
4244 const ReferenceTypeInfo upper_bound_;
4245 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4246 // is false then can_be_null_ cannot be true).
4247 const bool upper_can_be_null_;
4248 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004249
4250 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4251};
4252
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004253class HCheckCast : public HTemplateInstruction<2> {
4254 public:
4255 HCheckCast(HInstruction* object,
4256 HLoadClass* constant,
4257 bool class_is_final,
4258 uint32_t dex_pc)
4259 : HTemplateInstruction(SideEffects::None()),
4260 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004261 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004262 dex_pc_(dex_pc) {
4263 SetRawInputAt(0, object);
4264 SetRawInputAt(1, constant);
4265 }
4266
4267 bool CanBeMoved() const OVERRIDE { return true; }
4268
4269 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4270 return true;
4271 }
4272
4273 bool NeedsEnvironment() const OVERRIDE {
4274 // Instruction may throw a CheckCastError.
4275 return true;
4276 }
4277
4278 bool CanThrow() const OVERRIDE { return true; }
4279
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004280 bool MustDoNullCheck() const { return must_do_null_check_; }
4281 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4282
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004283 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004284
4285 bool IsClassFinal() const { return class_is_final_; }
4286
4287 DECLARE_INSTRUCTION(CheckCast);
4288
4289 private:
4290 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004291 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004292 const uint32_t dex_pc_;
4293
4294 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004295};
4296
Calin Juravle27df7582015-04-17 19:12:31 +01004297class HMemoryBarrier : public HTemplateInstruction<0> {
4298 public:
4299 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004300 : HTemplateInstruction(
4301 SideEffects::All()), // assume write/read on all fields/arrays
Calin Juravle27df7582015-04-17 19:12:31 +01004302 barrier_kind_(barrier_kind) {}
4303
4304 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4305
4306 DECLARE_INSTRUCTION(MemoryBarrier);
4307
4308 private:
4309 const MemBarrierKind barrier_kind_;
4310
4311 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4312};
4313
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004314class HMonitorOperation : public HTemplateInstruction<1> {
4315 public:
4316 enum OperationKind {
4317 kEnter,
4318 kExit,
4319 };
4320
4321 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004322 : HTemplateInstruction(SideEffects::All()), // assume write/read on all fields/arrays
4323 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004324 SetRawInputAt(0, object);
4325 }
4326
4327 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004328 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4329
4330 bool CanThrow() const OVERRIDE {
4331 // Verifier guarantees that monitor-exit cannot throw.
4332 // This is important because it allows the HGraphBuilder to remove
4333 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4334 return IsEnter();
4335 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004336
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004337 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004338
4339 bool IsEnter() const { return kind_ == kEnter; }
4340
4341 DECLARE_INSTRUCTION(MonitorOperation);
4342
Calin Juravle52c48962014-12-16 17:02:57 +00004343 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004344 const OperationKind kind_;
4345 const uint32_t dex_pc_;
4346
4347 private:
4348 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4349};
4350
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004351/**
4352 * A HInstruction used as a marker for the replacement of new + <init>
4353 * of a String to a call to a StringFactory. Only baseline will see
4354 * the node at code generation, where it will be be treated as null.
4355 * When compiling non-baseline, `HFakeString` instructions are being removed
4356 * in the instruction simplifier.
4357 */
4358class HFakeString : public HTemplateInstruction<0> {
4359 public:
4360 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4361
4362 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4363
4364 DECLARE_INSTRUCTION(FakeString);
4365
4366 private:
4367 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4368};
4369
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004370class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004371 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004372 MoveOperands(Location source,
4373 Location destination,
4374 Primitive::Type type,
4375 HInstruction* instruction)
4376 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004377
4378 Location GetSource() const { return source_; }
4379 Location GetDestination() const { return destination_; }
4380
4381 void SetSource(Location value) { source_ = value; }
4382 void SetDestination(Location value) { destination_ = value; }
4383
4384 // The parallel move resolver marks moves as "in-progress" by clearing the
4385 // destination (but not the source).
4386 Location MarkPending() {
4387 DCHECK(!IsPending());
4388 Location dest = destination_;
4389 destination_ = Location::NoLocation();
4390 return dest;
4391 }
4392
4393 void ClearPending(Location dest) {
4394 DCHECK(IsPending());
4395 destination_ = dest;
4396 }
4397
4398 bool IsPending() const {
4399 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4400 return destination_.IsInvalid() && !source_.IsInvalid();
4401 }
4402
4403 // True if this blocks a move from the given location.
4404 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004405 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004406 }
4407
4408 // A move is redundant if it's been eliminated, if its source and
4409 // destination are the same, or if its destination is unneeded.
4410 bool IsRedundant() const {
4411 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4412 }
4413
4414 // We clear both operands to indicate move that's been eliminated.
4415 void Eliminate() {
4416 source_ = destination_ = Location::NoLocation();
4417 }
4418
4419 bool IsEliminated() const {
4420 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4421 return source_.IsInvalid();
4422 }
4423
Alexey Frunze4dda3372015-06-01 18:31:49 -07004424 Primitive::Type GetType() const { return type_; }
4425
Nicolas Geoffray90218252015-04-15 11:56:51 +01004426 bool Is64BitMove() const {
4427 return Primitive::Is64BitType(type_);
4428 }
4429
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004430 HInstruction* GetInstruction() const { return instruction_; }
4431
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004432 private:
4433 Location source_;
4434 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004435 // The type this move is for.
4436 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004437 // The instruction this move is assocatied with. Null when this move is
4438 // for moving an input in the expected locations of user (including a phi user).
4439 // This is only used in debug mode, to ensure we do not connect interval siblings
4440 // in the same parallel move.
4441 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004442};
4443
4444static constexpr size_t kDefaultNumberOfMoves = 4;
4445
4446class HParallelMove : public HTemplateInstruction<0> {
4447 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004448 explicit HParallelMove(ArenaAllocator* arena)
4449 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004450
Nicolas Geoffray90218252015-04-15 11:56:51 +01004451 void AddMove(Location source,
4452 Location destination,
4453 Primitive::Type type,
4454 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004455 DCHECK(source.IsValid());
4456 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004457 if (kIsDebugBuild) {
4458 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004459 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004460 if (moves_.Get(i).GetInstruction() == instruction) {
4461 // Special case the situation where the move is for the spill slot
4462 // of the instruction.
4463 if ((GetPrevious() == instruction)
4464 || ((GetPrevious() == nullptr)
4465 && instruction->IsPhi()
4466 && instruction->GetBlock() == GetBlock())) {
4467 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4468 << "Doing parallel moves for the same instruction.";
4469 } else {
4470 DCHECK(false) << "Doing parallel moves for the same instruction.";
4471 }
4472 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004473 }
4474 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004475 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004476 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004477 << "Overlapped destination for two moves in a parallel move: "
4478 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4479 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004480 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004481 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004482 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004483 }
4484
4485 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004486 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004487 }
4488
4489 size_t NumMoves() const { return moves_.Size(); }
4490
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004491 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004492
4493 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004494 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004495
4496 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4497};
4498
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004499class HGraphVisitor : public ValueObject {
4500 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004501 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4502 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004503
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004504 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004505 virtual void VisitBasicBlock(HBasicBlock* block);
4506
Roland Levillain633021e2014-10-01 14:12:25 +01004507 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004508 void VisitInsertionOrder();
4509
Roland Levillain633021e2014-10-01 14:12:25 +01004510 // Visit the graph following dominator tree reverse post-order.
4511 void VisitReversePostOrder();
4512
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004513 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004514
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004515 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004516#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004517 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4518
4519 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4520
4521#undef DECLARE_VISIT_INSTRUCTION
4522
4523 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004524 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004525
4526 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4527};
4528
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004529class HGraphDelegateVisitor : public HGraphVisitor {
4530 public:
4531 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4532 virtual ~HGraphDelegateVisitor() {}
4533
4534 // Visit functions that delegate to to super class.
4535#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004536 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004537
4538 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4539
4540#undef DECLARE_VISIT_INSTRUCTION
4541
4542 private:
4543 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4544};
4545
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004546class HInsertionOrderIterator : public ValueObject {
4547 public:
4548 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4549
4550 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4551 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4552 void Advance() { ++index_; }
4553
4554 private:
4555 const HGraph& graph_;
4556 size_t index_;
4557
4558 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4559};
4560
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004561class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004562 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004563 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4564 // Check that reverse post order of the graph has been built.
4565 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4566 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004567
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004568 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4569 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004570 void Advance() { ++index_; }
4571
4572 private:
4573 const HGraph& graph_;
4574 size_t index_;
4575
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004576 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004577};
4578
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004579class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004580 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004581 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004582 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4583 // Check that reverse post order of the graph has been built.
4584 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4585 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004586
4587 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004588 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004589 void Advance() { --index_; }
4590
4591 private:
4592 const HGraph& graph_;
4593 size_t index_;
4594
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004595 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004596};
4597
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004598class HLinearPostOrderIterator : public ValueObject {
4599 public:
4600 explicit HLinearPostOrderIterator(const HGraph& graph)
4601 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4602
4603 bool Done() const { return index_ == 0; }
4604
4605 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4606
4607 void Advance() {
4608 --index_;
4609 DCHECK_GE(index_, 0U);
4610 }
4611
4612 private:
4613 const GrowableArray<HBasicBlock*>& order_;
4614 size_t index_;
4615
4616 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4617};
4618
4619class HLinearOrderIterator : public ValueObject {
4620 public:
4621 explicit HLinearOrderIterator(const HGraph& graph)
4622 : order_(graph.GetLinearOrder()), index_(0) {}
4623
4624 bool Done() const { return index_ == order_.Size(); }
4625 HBasicBlock* Current() const { return order_.Get(index_); }
4626 void Advance() { ++index_; }
4627
4628 private:
4629 const GrowableArray<HBasicBlock*>& order_;
4630 size_t index_;
4631
4632 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4633};
4634
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004635// Iterator over the blocks that art part of the loop. Includes blocks part
4636// of an inner loop. The order in which the blocks are iterated is on their
4637// block id.
4638class HBlocksInLoopIterator : public ValueObject {
4639 public:
4640 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4641 : blocks_in_loop_(info.GetBlocks()),
4642 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4643 index_(0) {
4644 if (!blocks_in_loop_.IsBitSet(index_)) {
4645 Advance();
4646 }
4647 }
4648
4649 bool Done() const { return index_ == blocks_.Size(); }
4650 HBasicBlock* Current() const { return blocks_.Get(index_); }
4651 void Advance() {
4652 ++index_;
4653 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4654 if (blocks_in_loop_.IsBitSet(index_)) {
4655 break;
4656 }
4657 }
4658 }
4659
4660 private:
4661 const BitVector& blocks_in_loop_;
4662 const GrowableArray<HBasicBlock*>& blocks_;
4663 size_t index_;
4664
4665 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4666};
4667
Mingyao Yang3584bce2015-05-19 16:01:59 -07004668// Iterator over the blocks that art part of the loop. Includes blocks part
4669// of an inner loop. The order in which the blocks are iterated is reverse
4670// post order.
4671class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4672 public:
4673 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4674 : blocks_in_loop_(info.GetBlocks()),
4675 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4676 index_(0) {
4677 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4678 Advance();
4679 }
4680 }
4681
4682 bool Done() const { return index_ == blocks_.Size(); }
4683 HBasicBlock* Current() const { return blocks_.Get(index_); }
4684 void Advance() {
4685 ++index_;
4686 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4687 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4688 break;
4689 }
4690 }
4691 }
4692
4693 private:
4694 const BitVector& blocks_in_loop_;
4695 const GrowableArray<HBasicBlock*>& blocks_;
4696 size_t index_;
4697
4698 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4699};
4700
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004701inline int64_t Int64FromConstant(HConstant* constant) {
4702 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4703 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4704 : constant->AsLongConstant()->GetValue();
4705}
4706
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004707} // namespace art
4708
4709#endif // ART_COMPILER_OPTIMIZING_NODES_H_