blob: 003900c8a6d5de5afe18f3aeeb639fa4e92da8d5 [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;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010061static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000062static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Calin Juravle9aec02f2014-11-18 23:06:35 +000064static constexpr uint32_t kMaxIntShiftValue = 0x1f;
65static constexpr uint64_t kMaxLongShiftValue = 0x3f;
66
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010067static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
68
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010069static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
70
Dave Allison20dfc792014-06-16 20:44:29 -070071enum IfCondition {
72 kCondEQ,
73 kCondNE,
74 kCondLT,
75 kCondLE,
76 kCondGT,
77 kCondGE,
78};
79
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010080class HInstructionList {
81 public:
82 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
83
84 void AddInstruction(HInstruction* instruction);
85 void RemoveInstruction(HInstruction* instruction);
86
David Brazdilc3d743f2015-04-22 13:40:50 +010087 // Insert `instruction` before/after an existing instruction `cursor`.
88 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
89 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
90
Roland Levillain6b469232014-09-25 10:10:38 +010091 // Return true if this list contains `instruction`.
92 bool Contains(HInstruction* instruction) const;
93
Roland Levillainccc07a92014-09-16 14:48:16 +010094 // Return true if `instruction1` is found before `instruction2` in
95 // this instruction list and false otherwise. Abort if none
96 // of these instructions is found.
97 bool FoundBefore(const HInstruction* instruction1,
98 const HInstruction* instruction2) const;
99
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000100 bool IsEmpty() const { return first_instruction_ == nullptr; }
101 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
102
103 // Update the block of all instructions to be `block`.
104 void SetBlockOfInstructions(HBasicBlock* block) const;
105
106 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
107 void Add(const HInstructionList& instruction_list);
108
David Brazdil2d7352b2015-04-20 14:52:42 +0100109 // Return the number of instructions in the list. This is an expensive operation.
110 size_t CountSize() const;
111
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100112 private:
113 HInstruction* first_instruction_;
114 HInstruction* last_instruction_;
115
116 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000117 friend class HGraph;
118 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100119 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100120 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100121
122 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
123};
124
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000125// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700126class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000127 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100128 HGraph(ArenaAllocator* arena,
129 const DexFile& dex_file,
130 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100131 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700132 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100133 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100134 bool debuggable = false,
135 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000136 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000137 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100138 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100139 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700140 entry_block_(nullptr),
141 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100142 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100143 number_of_vregs_(0),
144 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000145 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400146 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000147 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000148 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100149 dex_file_(dex_file),
150 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100151 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100152 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100153 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700154 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000155 cached_null_constant_(nullptr),
156 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000157 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
158 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100159 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
160 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000161
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000162 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100163 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100164 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000165
David Brazdil69ba7b72015-06-23 18:27:30 +0100166 bool IsInSsaForm() const { return in_ssa_form_; }
167
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000168 HBasicBlock* GetEntryBlock() const { return entry_block_; }
169 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100170 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000171
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000172 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
173 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000174
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000175 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 // Try building the SSA form of this graph, with dominance computation and loop
178 // recognition. Returns whether it was successful in doing all these steps.
179 bool TryBuildingSsa() {
180 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000181 // The SSA builder requires loops to all be natural. Specifically, the dead phi
182 // elimination phase checks the consistency of the graph when doing a post-order
183 // visit for eliminating dead phis: a dead phi can only have loop header phi
184 // users remaining when being visited.
185 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100186 // Precompute per-block try membership before entering the SSA builder,
187 // which needs the information to build catch block phis from values of
188 // locals at throwing instructions inside try blocks.
189 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000190 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100191 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000192 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000193 }
194
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100195 void ComputeDominanceInformation();
196 void ClearDominanceInformation();
197
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000198 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100200 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100201 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000202
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000203 // Analyze all natural loops in this graph. Returns false if one
204 // loop is not natural, that is the header does not dominate the
205 // back edge.
206 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100207
David Brazdilffee3d32015-07-06 11:48:53 +0100208 // Iterate over blocks to compute try block membership. Needs reverse post
209 // order and loop information.
210 void ComputeTryBlockInformation();
211
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000213 // Returns the instruction used to replace the invoke expression or null if the
214 // invoke is for a void method.
215 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000216
Mingyao Yang3584bce2015-05-19 16:01:59 -0700217 // Need to add a couple of blocks to test if the loop body is entered and
218 // put deoptimization instructions, etc.
219 void TransformLoopHeaderForBCE(HBasicBlock* header);
220
David Brazdil2d7352b2015-04-20 14:52:42 +0100221 // Removes `block` from the graph.
222 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000223
David Brazdilfc6a86a2015-06-26 10:33:45 +0000224 // Splits the edge between `block` and `successor` while preserving the
225 // indices in the predecessor/successor lists. If there are multiple edges
226 // between the blocks, the lowest indices are used.
227 // Returns the new block which is empty and has the same dex pc as `successor`.
228 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
229
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100230 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
231 void SimplifyLoop(HBasicBlock* header);
232
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000233 int32_t GetNextInstructionId() {
234 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000235 return current_instruction_id_++;
236 }
237
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000238 int32_t GetCurrentInstructionId() const {
239 return current_instruction_id_;
240 }
241
242 void SetCurrentInstructionId(int32_t id) {
243 current_instruction_id_ = id;
244 }
245
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100246 uint16_t GetMaximumNumberOfOutVRegs() const {
247 return maximum_number_of_out_vregs_;
248 }
249
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000250 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
251 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100252 }
253
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100254 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
255 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
256 }
257
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000258 void UpdateTemporariesVRegSlots(size_t slots) {
259 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100260 }
261
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000262 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100263 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000264 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100265 }
266
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100267 void SetNumberOfVRegs(uint16_t number_of_vregs) {
268 number_of_vregs_ = number_of_vregs;
269 }
270
271 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100272 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100273 return number_of_vregs_;
274 }
275
276 void SetNumberOfInVRegs(uint16_t value) {
277 number_of_in_vregs_ = value;
278 }
279
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100280 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100281 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100282 return number_of_vregs_ - number_of_in_vregs_;
283 }
284
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100285 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
286 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100287 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100288
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100289 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
290 return linear_order_;
291 }
292
Mark Mendell1152c922015-04-24 17:06:35 -0400293 bool HasBoundsChecks() const {
294 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800295 }
296
Mark Mendell1152c922015-04-24 17:06:35 -0400297 void SetHasBoundsChecks(bool value) {
298 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800299 }
300
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100301 bool ShouldGenerateConstructorBarrier() const {
302 return should_generate_constructor_barrier_;
303 }
304
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000305 bool IsDebuggable() const { return debuggable_; }
306
David Brazdil8d5b8b22015-03-24 10:51:52 +0000307 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000308 // already, it is created and inserted into the graph. This method is only for
309 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000310 HConstant* GetConstant(Primitive::Type type, int64_t value);
Calin Juravle2e768302015-07-28 14:41:11 +0000311
312 // TODO: This is problematic for the consistency of reference type propagation
313 // because it can be created anytime after the pass and thus it will be left
314 // with an invalid type.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000315 HNullConstant* GetNullConstant();
Calin Juravle2e768302015-07-28 14:41:11 +0000316
David Brazdil8d5b8b22015-03-24 10:51:52 +0000317 HIntConstant* GetIntConstant(int32_t value) {
318 return CreateConstant(value, &cached_int_constants_);
319 }
320 HLongConstant* GetLongConstant(int64_t value) {
321 return CreateConstant(value, &cached_long_constants_);
322 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000323 HFloatConstant* GetFloatConstant(float value) {
324 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
325 }
326 HDoubleConstant* GetDoubleConstant(double value) {
327 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
328 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000329
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100330 HCurrentMethod* GetCurrentMethod();
331
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000332 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100333
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100334 const DexFile& GetDexFile() const {
335 return dex_file_;
336 }
337
338 uint32_t GetMethodIdx() const {
339 return method_idx_;
340 }
341
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100342 InvokeType GetInvokeType() const {
343 return invoke_type_;
344 }
345
Mark Mendellc4701932015-04-10 13:18:51 -0400346 InstructionSet GetInstructionSet() const {
347 return instruction_set_;
348 }
349
David Brazdil2d7352b2015-04-20 14:52:42 +0100350 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000351 void VisitBlockForDominatorTree(HBasicBlock* block,
352 HBasicBlock* predecessor,
353 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100354 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000355 void VisitBlockForBackEdges(HBasicBlock* block,
356 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100357 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000358 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100359 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000360
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000361 template <class InstructionType, typename ValueType>
362 InstructionType* CreateConstant(ValueType value,
363 ArenaSafeMap<ValueType, InstructionType*>* cache) {
364 // Try to find an existing constant of the given value.
365 InstructionType* constant = nullptr;
366 auto cached_constant = cache->find(value);
367 if (cached_constant != cache->end()) {
368 constant = cached_constant->second;
369 }
370
371 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100372 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000373 if (constant == nullptr || constant->GetBlock() == nullptr) {
374 constant = new (arena_) InstructionType(value);
375 cache->Overwrite(value, constant);
376 InsertConstant(constant);
377 }
378 return constant;
379 }
380
David Brazdil8d5b8b22015-03-24 10:51:52 +0000381 void InsertConstant(HConstant* instruction);
382
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000383 // Cache a float constant into the graph. This method should only be
384 // called by the SsaBuilder when creating "equivalent" instructions.
385 void CacheFloatConstant(HFloatConstant* constant);
386
387 // See CacheFloatConstant comment.
388 void CacheDoubleConstant(HDoubleConstant* constant);
389
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000390 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000391
392 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000393 GrowableArray<HBasicBlock*> blocks_;
394
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100395 // List of blocks to perform a reverse post order tree traversal.
396 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000397
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100398 // List of blocks to perform a linear order tree traversal.
399 GrowableArray<HBasicBlock*> linear_order_;
400
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000401 HBasicBlock* entry_block_;
402 HBasicBlock* exit_block_;
403
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100404 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100405 uint16_t maximum_number_of_out_vregs_;
406
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100407 // The number of virtual registers in this method. Contains the parameters.
408 uint16_t number_of_vregs_;
409
410 // The number of virtual registers used by parameters of this method.
411 uint16_t number_of_in_vregs_;
412
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000413 // Number of vreg size slots that the temporaries use (used in baseline compiler).
414 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100415
Mark Mendell1152c922015-04-24 17:06:35 -0400416 // Has bounds checks. We can totally skip BCE if it's false.
417 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800418
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000419 // Indicates whether the graph should be compiled in a way that
420 // ensures full debuggability. If false, we can apply more
421 // aggressive optimizations that may limit the level of debugging.
422 const bool debuggable_;
423
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000424 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000425 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000426
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100427 // The dex file from which the method is from.
428 const DexFile& dex_file_;
429
430 // The method index in the dex file.
431 const uint32_t method_idx_;
432
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100433 // If inlined, this encodes how the callee is being invoked.
434 const InvokeType invoke_type_;
435
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100436 // Whether the graph has been transformed to SSA form. Only used
437 // in debug mode to ensure we are not using properties only valid
438 // for non-SSA form (like the number of temporaries).
439 bool in_ssa_form_;
440
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100441 const bool should_generate_constructor_barrier_;
442
Mathieu Chartiere401d142015-04-22 13:56:20 -0700443 const InstructionSet instruction_set_;
444
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000445 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000446 HNullConstant* cached_null_constant_;
447 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000448 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000449 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000450 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000451
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100452 HCurrentMethod* cached_current_method_;
453
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000454 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100455 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000456 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000457 DISALLOW_COPY_AND_ASSIGN(HGraph);
458};
459
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700460class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000461 public:
462 HLoopInformation(HBasicBlock* header, HGraph* graph)
463 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100464 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100465 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100466 // Make bit vector growable, as the number of blocks may change.
467 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100468
469 HBasicBlock* GetHeader() const {
470 return header_;
471 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000472
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000473 void SetHeader(HBasicBlock* block) {
474 header_ = block;
475 }
476
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100477 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
478 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
479 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
480
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000481 void AddBackEdge(HBasicBlock* back_edge) {
482 back_edges_.Add(back_edge);
483 }
484
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100485 void RemoveBackEdge(HBasicBlock* back_edge) {
486 back_edges_.Delete(back_edge);
487 }
488
David Brazdil46e2a392015-03-16 17:31:52 +0000489 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100490 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000491 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100492 }
493 return false;
494 }
495
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000496 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000497 return back_edges_.Size();
498 }
499
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100500 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100501
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100502 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
503 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100504 }
505
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100506 // Returns the lifetime position of the back edge that has the
507 // greatest lifetime position.
508 size_t GetLifetimeEnd() const;
509
510 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
511 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
512 if (back_edges_.Get(i) == existing) {
513 back_edges_.Put(i, new_back_edge);
514 return;
515 }
516 }
517 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100518 }
519
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100520 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100521 // that is the header dominates the back edge.
522 bool Populate();
523
David Brazdila4b8c212015-05-07 09:59:30 +0100524 // Reanalyzes the loop by removing loop info from its blocks and re-running
525 // Populate(). If there are no back edges left, the loop info is completely
526 // removed as well as its SuspendCheck instruction. It must be run on nested
527 // inner loops first.
528 void Update();
529
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100530 // Returns whether this loop information contains `block`.
531 // Note that this loop information *must* be populated before entering this function.
532 bool Contains(const HBasicBlock& block) const;
533
534 // Returns whether this loop information is an inner loop of `other`.
535 // Note that `other` *must* be populated before entering this function.
536 bool IsIn(const HLoopInformation& other) const;
537
538 const ArenaBitVector& GetBlocks() const { return blocks_; }
539
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000540 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000541 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000542
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000543 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100544 // Internal recursive implementation of `Populate`.
545 void PopulateRecursive(HBasicBlock* block);
546
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000547 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100548 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000549 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100550 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000551
552 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
553};
554
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100555static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100556static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100557
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000558// A block in a method. Contains the list of instructions represented
559// as a double linked list. Each block knows its predecessors and
560// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100561
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700562class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000563 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100564 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000565 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000566 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
567 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000568 loop_information_(nullptr),
569 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100570 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100571 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100572 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100573 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000574 lifetime_end_(kNoLifetime),
575 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000576
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100577 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
578 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000579 }
580
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100581 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
582 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000583 }
584
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100585 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
586 return dominated_blocks_;
587 }
588
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100589 bool IsEntryBlock() const {
590 return graph_->GetEntryBlock() == this;
591 }
592
593 bool IsExitBlock() const {
594 return graph_->GetExitBlock() == this;
595 }
596
David Brazdil46e2a392015-03-16 17:31:52 +0000597 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000598 bool IsSingleTryBoundary() const;
599
600 // Returns true if this block emits nothing but a jump.
601 bool IsSingleJump() const {
602 HLoopInformation* loop_info = GetLoopInformation();
603 return (IsSingleGoto() || IsSingleTryBoundary())
604 // Back edges generate a suspend check.
605 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
606 }
David Brazdil46e2a392015-03-16 17:31:52 +0000607
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000608 void AddBackEdge(HBasicBlock* back_edge) {
609 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000610 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000611 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100612 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000613 loop_information_->AddBackEdge(back_edge);
614 }
615
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000616 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000617 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000618
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000619 int GetBlockId() const { return block_id_; }
620 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000621
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000622 HBasicBlock* GetDominator() const { return dominator_; }
623 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100624 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100625 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000626 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
627 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
628 if (dominated_blocks_.Get(i) == existing) {
629 dominated_blocks_.Put(i, new_block);
630 return;
631 }
632 }
633 LOG(FATAL) << "Unreachable";
634 UNREACHABLE();
635 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100636 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000637
638 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100639 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000640 }
641
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100642 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
643 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100644 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100645 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100646 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
647 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000648
649 void AddSuccessor(HBasicBlock* block) {
650 successors_.Add(block);
651 block->predecessors_.Add(this);
652 }
653
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100654 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
655 size_t successor_index = GetSuccessorIndexOf(existing);
656 DCHECK_NE(successor_index, static_cast<size_t>(-1));
657 existing->RemovePredecessor(this);
658 new_block->predecessors_.Add(this);
659 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000660 }
661
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000662 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
663 size_t predecessor_index = GetPredecessorIndexOf(existing);
664 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
665 existing->RemoveSuccessor(this);
666 new_block->successors_.Add(this);
667 predecessors_.Put(predecessor_index, new_block);
668 }
669
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100670 // Insert `this` between `predecessor` and `successor. This method
671 // preserves the indicies, and will update the first edge found between
672 // `predecessor` and `successor`.
673 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
674 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
675 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
676 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
677 DCHECK_NE(successor_index, static_cast<size_t>(-1));
678 successor->predecessors_.Put(predecessor_index, this);
679 predecessor->successors_.Put(successor_index, this);
680 successors_.Add(successor);
681 predecessors_.Add(predecessor);
682 }
683
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100684 void RemovePredecessor(HBasicBlock* block) {
685 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100686 }
687
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000688 void RemoveSuccessor(HBasicBlock* block) {
689 successors_.Delete(block);
690 }
691
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100692 void ClearAllPredecessors() {
693 predecessors_.Reset();
694 }
695
696 void AddPredecessor(HBasicBlock* block) {
697 predecessors_.Add(block);
698 block->successors_.Add(this);
699 }
700
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100701 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100702 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100703 HBasicBlock* temp = predecessors_.Get(0);
704 predecessors_.Put(0, predecessors_.Get(1));
705 predecessors_.Put(1, temp);
706 }
707
David Brazdil769c9e52015-04-27 13:54:09 +0100708 void SwapSuccessors() {
709 DCHECK_EQ(successors_.Size(), 2u);
710 HBasicBlock* temp = successors_.Get(0);
711 successors_.Put(0, successors_.Get(1));
712 successors_.Put(1, temp);
713 }
714
David Brazdilfc6a86a2015-06-26 10:33:45 +0000715 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100716 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
717 if (predecessors_.Get(i) == predecessor) {
718 return i;
719 }
720 }
721 return -1;
722 }
723
David Brazdilfc6a86a2015-06-26 10:33:45 +0000724 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100725 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
726 if (successors_.Get(i) == successor) {
727 return i;
728 }
729 }
730 return -1;
731 }
732
David Brazdilfc6a86a2015-06-26 10:33:45 +0000733 HBasicBlock* GetSinglePredecessor() const {
734 DCHECK_EQ(GetPredecessors().Size(), 1u);
735 return GetPredecessors().Get(0);
736 }
737
738 HBasicBlock* GetSingleSuccessor() const {
739 DCHECK_EQ(GetSuccessors().Size(), 1u);
740 return GetSuccessors().Get(0);
741 }
742
743 // Returns whether the first occurrence of `predecessor` in the list of
744 // predecessors is at index `idx`.
745 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
746 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
747 return GetPredecessorIndexOf(predecessor) == idx;
748 }
749
David Brazdilffee3d32015-07-06 11:48:53 +0100750 // Returns the number of non-exceptional successors. SsaChecker ensures that
751 // these are stored at the beginning of the successor list.
752 size_t NumberOfNormalSuccessors() const {
753 return EndsWithTryBoundary() ? 1 : GetSuccessors().Size();
754 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000755
756 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100757 // created, latter block. Note that this method will add the block to the
758 // graph, create a Goto at the end of the former block and will create an edge
759 // between the blocks. It will not, however, update the reverse post order or
760 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000761 HBasicBlock* SplitBefore(HInstruction* cursor);
762
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000763 // Split the block into two blocks just after `cursor`. Returns the newly
764 // created block. Note that this method just updates raw block information,
765 // like predecessors, successors, dominators, and instruction list. It does not
766 // update the graph, reverse post order, loop information, nor make sure the
767 // blocks are consistent (for example ending with a control flow instruction).
768 HBasicBlock* SplitAfter(HInstruction* cursor);
769
770 // Merge `other` at the end of `this`. Successors and dominated blocks of
771 // `other` are changed to be successors and dominated blocks of `this`. Note
772 // that this method does not update the graph, reverse post order, loop
773 // information, nor make sure the blocks are consistent (for example ending
774 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100775 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000776
777 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
778 // of `this` are moved to `other`.
779 // Note that this method does not update the graph, reverse post order, loop
780 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000781 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000782 void ReplaceWith(HBasicBlock* other);
783
David Brazdil2d7352b2015-04-20 14:52:42 +0100784 // Merge `other` at the end of `this`. This method updates loops, reverse post
785 // order, links to predecessors, successors, dominators and deletes the block
786 // from the graph. The two blocks must be successive, i.e. `this` the only
787 // predecessor of `other` and vice versa.
788 void MergeWith(HBasicBlock* other);
789
790 // Disconnects `this` from all its predecessors, successors and dominator,
791 // removes it from all loops it is included in and eventually from the graph.
792 // The block must not dominate any other block. Predecessors and successors
793 // are safely updated.
794 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000795
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000796 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100797 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100798 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100799 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100800 // Replace instruction `initial` with `replacement` within this block.
801 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
802 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100803 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100804 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000805 // RemoveInstruction and RemovePhi delete a given instruction from the respective
806 // instruction list. With 'ensure_safety' set to true, it verifies that the
807 // instruction is not in use and removes it from the use lists of its inputs.
808 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
809 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100810 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100811
812 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100813 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100814 }
815
Roland Levillain6b879dd2014-09-22 17:13:44 +0100816 bool IsLoopPreHeaderFirstPredecessor() const {
817 DCHECK(IsLoopHeader());
818 DCHECK(!GetPredecessors().IsEmpty());
819 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
820 }
821
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100822 HLoopInformation* GetLoopInformation() const {
823 return loop_information_;
824 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000825
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000826 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100827 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000828 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100829 void SetInLoop(HLoopInformation* info) {
830 if (IsLoopHeader()) {
831 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100832 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100833 loop_information_ = info;
834 } else if (loop_information_->Contains(*info->GetHeader())) {
835 // Block is currently part of an outer loop. Make it part of this inner loop.
836 // Note that a non loop header having a loop information means this loop information
837 // has already been populated
838 loop_information_ = info;
839 } else {
840 // Block is part of an inner loop. Do not update the loop information.
841 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
842 // at this point, because this method is being called while populating `info`.
843 }
844 }
845
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000846 // Raw update of the loop information.
847 void SetLoopInformation(HLoopInformation* info) {
848 loop_information_ = info;
849 }
850
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100851 bool IsInLoop() const { return loop_information_ != nullptr; }
852
David Brazdilffee3d32015-07-06 11:48:53 +0100853 HTryBoundary* GetTryEntry() const { return try_entry_; }
854 void SetTryEntry(HTryBoundary* try_entry) { try_entry_ = try_entry; }
855 bool IsInTry() const { return try_entry_ != nullptr; }
856
857 // Returns the try entry that this block's successors should have. They will
858 // be in the same try, unless the block ends in a try boundary. In that case,
859 // the appropriate try entry will be returned.
860 HTryBoundary* ComputeTryEntryOfSuccessors() const;
861
David Brazdila4b8c212015-05-07 09:59:30 +0100862 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100863 bool Dominates(HBasicBlock* block) const;
864
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100865 size_t GetLifetimeStart() const { return lifetime_start_; }
866 size_t GetLifetimeEnd() const { return lifetime_end_; }
867
868 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
869 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
870
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100871 uint32_t GetDexPc() const { return dex_pc_; }
872
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000873 bool IsCatchBlock() const { return is_catch_block_; }
874 void SetIsCatchBlock() { is_catch_block_ = true; }
875
David Brazdil8d5b8b22015-03-24 10:51:52 +0000876 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000877 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100878 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000879 bool HasSinglePhi() const;
880
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000881 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000882 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000883 GrowableArray<HBasicBlock*> predecessors_;
884 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100885 HInstructionList instructions_;
886 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000887 HLoopInformation* loop_information_;
888 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100889 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000890 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100891 // The dex program counter of the first instruction of this block.
892 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100893 size_t lifetime_start_;
894 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000895 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000896
David Brazdilffee3d32015-07-06 11:48:53 +0100897 // If this block is in a try block, `try_entry_` stores one of, possibly
898 // several, TryBoundary instructions entering it.
899 HTryBoundary* try_entry_;
900
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000901 friend class HGraph;
902 friend class HInstruction;
903
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000904 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
905};
906
David Brazdilb2bd1c52015-03-25 11:17:37 +0000907// Iterates over the LoopInformation of all loops which contain 'block'
908// from the innermost to the outermost.
909class HLoopInformationOutwardIterator : public ValueObject {
910 public:
911 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
912 : current_(block.GetLoopInformation()) {}
913
914 bool Done() const { return current_ == nullptr; }
915
916 void Advance() {
917 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100918 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000919 }
920
921 HLoopInformation* Current() const {
922 DCHECK(!Done());
923 return current_;
924 }
925
926 private:
927 HLoopInformation* current_;
928
929 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
930};
931
Alexandre Ramesef20f712015-06-09 10:29:30 +0100932#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100933 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000934 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000935 M(ArrayGet, Instruction) \
936 M(ArrayLength, Instruction) \
937 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100938 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000939 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000940 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000941 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100942 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000943 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100944 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100945 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700946 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000947 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000948 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000949 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100950 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000951 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100952 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000953 M(FloatConstant, Constant) \
954 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100955 M(GreaterThan, Condition) \
956 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100957 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000958 M(InstanceFieldGet, Instruction) \
959 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000960 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100961 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000962 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000963 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100964 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000965 M(LessThan, Condition) \
966 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000967 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000968 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100969 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000970 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100971 M(Local, Instruction) \
972 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100973 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000974 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000975 M(Mul, BinaryOperation) \
976 M(Neg, UnaryOperation) \
977 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100978 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100979 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000980 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000981 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000982 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000983 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100984 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000985 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100986 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000987 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100988 M(Return, Instruction) \
989 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000990 M(Shl, BinaryOperation) \
991 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100992 M(StaticFieldGet, Instruction) \
993 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100994 M(StoreLocal, Instruction) \
995 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100996 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000997 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000998 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +0000999 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001000 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001001 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001002 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001003
Alexandre Ramesef20f712015-06-09 10:29:30 +01001004#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1005
1006#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1007
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001008#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1009
Alexandre Ramesef20f712015-06-09 10:29:30 +01001010#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1011
1012#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1013
1014#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1015 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1016 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1017 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001018 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001019 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1020 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1021
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001022#define FOR_EACH_INSTRUCTION(M) \
1023 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1024 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001025 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001026 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001027 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001028
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001029#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001030FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1031#undef FORWARD_DECLARATION
1032
Roland Levillainccc07a92014-09-16 14:48:16 +01001033#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001034 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1035 const char* DebugName() const OVERRIDE { return #type; } \
1036 const H##type* As##type() const OVERRIDE { return this; } \
1037 H##type* As##type() OVERRIDE { return this; } \
1038 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001039 return other->Is##type(); \
1040 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001041 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001042
David Brazdiled596192015-01-23 10:39:45 +00001043template <typename T> class HUseList;
1044
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001045template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001046class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001047 public:
David Brazdiled596192015-01-23 10:39:45 +00001048 HUseListNode* GetPrevious() const { return prev_; }
1049 HUseListNode* GetNext() const { return next_; }
1050 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001051 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001052 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001053
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001054 private:
David Brazdiled596192015-01-23 10:39:45 +00001055 HUseListNode(T user, size_t index)
1056 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1057
1058 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001059 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001060 HUseListNode<T>* prev_;
1061 HUseListNode<T>* next_;
1062
1063 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001064
1065 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1066};
1067
David Brazdiled596192015-01-23 10:39:45 +00001068template <typename T>
1069class HUseList : public ValueObject {
1070 public:
1071 HUseList() : first_(nullptr) {}
1072
1073 void Clear() {
1074 first_ = nullptr;
1075 }
1076
1077 // Adds a new entry at the beginning of the use list and returns
1078 // the newly created node.
1079 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001080 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001081 if (IsEmpty()) {
1082 first_ = new_node;
1083 } else {
1084 first_->prev_ = new_node;
1085 new_node->next_ = first_;
1086 first_ = new_node;
1087 }
1088 return new_node;
1089 }
1090
1091 HUseListNode<T>* GetFirst() const {
1092 return first_;
1093 }
1094
1095 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001096 DCHECK(node != nullptr);
1097 DCHECK(Contains(node));
1098
David Brazdiled596192015-01-23 10:39:45 +00001099 if (node->prev_ != nullptr) {
1100 node->prev_->next_ = node->next_;
1101 }
1102 if (node->next_ != nullptr) {
1103 node->next_->prev_ = node->prev_;
1104 }
1105 if (node == first_) {
1106 first_ = node->next_;
1107 }
1108 }
1109
David Brazdil1abb4192015-02-17 18:33:36 +00001110 bool Contains(const HUseListNode<T>* node) const {
1111 if (node == nullptr) {
1112 return false;
1113 }
1114 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1115 if (current == node) {
1116 return true;
1117 }
1118 }
1119 return false;
1120 }
1121
David Brazdiled596192015-01-23 10:39:45 +00001122 bool IsEmpty() const {
1123 return first_ == nullptr;
1124 }
1125
1126 bool HasOnlyOneUse() const {
1127 return first_ != nullptr && first_->next_ == nullptr;
1128 }
1129
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001130 size_t SizeSlow() const {
1131 size_t count = 0;
1132 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1133 ++count;
1134 }
1135 return count;
1136 }
1137
David Brazdiled596192015-01-23 10:39:45 +00001138 private:
1139 HUseListNode<T>* first_;
1140};
1141
1142template<typename T>
1143class HUseIterator : public ValueObject {
1144 public:
1145 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1146
1147 bool Done() const { return current_ == nullptr; }
1148
1149 void Advance() {
1150 DCHECK(!Done());
1151 current_ = current_->GetNext();
1152 }
1153
1154 HUseListNode<T>* Current() const {
1155 DCHECK(!Done());
1156 return current_;
1157 }
1158
1159 private:
1160 HUseListNode<T>* current_;
1161
1162 friend class HValue;
1163};
1164
David Brazdil1abb4192015-02-17 18:33:36 +00001165// This class is used by HEnvironment and HInstruction classes to record the
1166// instructions they use and pointers to the corresponding HUseListNodes kept
1167// by the used instructions.
1168template <typename T>
1169class HUserRecord : public ValueObject {
1170 public:
1171 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1172 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1173
1174 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1175 : instruction_(old_record.instruction_), use_node_(use_node) {
1176 DCHECK(instruction_ != nullptr);
1177 DCHECK(use_node_ != nullptr);
1178 DCHECK(old_record.use_node_ == nullptr);
1179 }
1180
1181 HInstruction* GetInstruction() const { return instruction_; }
1182 HUseListNode<T>* GetUseNode() const { return use_node_; }
1183
1184 private:
1185 // Instruction used by the user.
1186 HInstruction* instruction_;
1187
1188 // Corresponding entry in the use list kept by 'instruction_'.
1189 HUseListNode<T>* use_node_;
1190};
1191
Aart Bik854a02b2015-07-14 16:07:00 -07001192/**
1193 * Side-effects representation for write/read dependences on fields/arrays.
1194 *
1195 * The dependence analysis uses type disambiguation (e.g. a float field write
1196 * cannot modify the value of an integer field read) and the access type (e.g.
1197 * a reference array write cannot modify the value of a reference field read
1198 * [although it may modify the reference fetch prior to reading the field,
1199 * which is represented by its own write/read dependence]). The analysis
1200 * makes conservative points-to assumptions on reference types (e.g. two same
1201 * typed arrays are assumed to be the same, and any reference read depends
1202 * on any reference read without further regard of its type).
1203 *
1204 * The internal representation uses the following 36-bit flags assignments:
1205 *
1206 * |ARRAY-R |FIELD-R |ARRAY-W |FIELD-W |
1207 * +---------+---------+---------+---------+
1208 * |543210987|654321098|765432109|876543210|
1209 * |DFJISCBZL|DFJISCBZL|DFJISCBZL|DFJISCBZL|
1210 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001211class SideEffects : public ValueObject {
1212 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001213 SideEffects() : flags_(0) {}
1214
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001215 static SideEffects None() {
1216 return SideEffects(0);
1217 }
1218
1219 static SideEffects All() {
Aart Bik854a02b2015-07-14 16:07:00 -07001220 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001221 }
1222
Aart Bik34c3ba92015-07-20 14:08:59 -07001223 static SideEffects AllWrites() {
1224 return SideEffects(kAllWrites);
1225 }
1226
1227 static SideEffects AllReads() {
1228 return SideEffects(kAllReads);
1229 }
1230
1231 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1232 return is_volatile
1233 ? All()
1234 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001235 }
1236
Aart Bik854a02b2015-07-14 16:07:00 -07001237 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1238 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001239 }
1240
Aart Bik34c3ba92015-07-20 14:08:59 -07001241 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1242 return is_volatile
1243 ? All()
1244 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001245 }
1246
1247 static SideEffects ArrayReadOfType(Primitive::Type type) {
1248 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1249 }
1250
1251 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001252 SideEffects Union(SideEffects other) const {
1253 return SideEffects(flags_ | other.flags_);
1254 }
1255
Aart Bik854a02b2015-07-14 16:07:00 -07001256 // Returns true if something is written.
1257 bool DoesAnyWrite() const {
1258 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001259 }
1260
Aart Bik854a02b2015-07-14 16:07:00 -07001261 // Returns true if something is read.
1262 bool DoesAnyRead() const {
1263 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001264 }
1265
Aart Bik854a02b2015-07-14 16:07:00 -07001266 // Returns true if nothing is written or read.
1267 bool DoesNothing() const {
1268 return flags_ == 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001269 }
1270
Aart Bik854a02b2015-07-14 16:07:00 -07001271 // Returns true if potentially everything is written and read
1272 // (every type and every kind of access).
1273 bool DoesAll() const {
1274 return flags_ == (kAllWrites | kAllReads);
1275 }
1276
1277 // Returns true if this may read something written by other.
1278 bool MayDependOn(SideEffects other) const {
1279 const uint64_t reads = (flags_ & kAllReads) >> kFieldReadOffset;
1280 return (other.flags_ & reads);
1281 }
1282
1283 // Returns string representation of flags (for debugging only).
1284 // Format: |DFJISCBZL|DFJISCBZL|DFJISCBZL|DFJISCBZL|
1285 std::string ToString() const {
1286 static const char *kDebug = "LZBCSIJFD";
1287 std::string flags = "|";
1288 for (int s = 35; s >= 0; s--) {
1289 const int t = s % kBits;
1290 if ((flags_ >> s) & 1)
1291 flags += kDebug[t];
1292 if (t == 0)
1293 flags += "|";
1294 }
1295 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001296 }
1297
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001298 private:
Aart Bik854a02b2015-07-14 16:07:00 -07001299 static constexpr int kBits = 9;
1300 static constexpr int kFieldWriteOffset = 0 * kBits;
1301 static constexpr int kArrayWriteOffset = 1 * kBits;
1302 static constexpr int kFieldReadOffset = 2 * kBits;
1303 static constexpr int kArrayReadOffset = 3 * kBits;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001304
Aart Bik854a02b2015-07-14 16:07:00 -07001305 static constexpr uint64_t kAllWrites = 0x0003ffff;
1306 static constexpr uint64_t kAllReads = kAllWrites << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001307
Aart Bik854a02b2015-07-14 16:07:00 -07001308 // Work around the fact that HIR aliases I/F and J/D.
1309 // TODO: remove this interceptor once HIR types are clean
1310 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1311 switch (type) {
1312 case Primitive::kPrimInt:
1313 case Primitive::kPrimFloat:
1314 return TypeFlag(Primitive::kPrimInt, offset) |
1315 TypeFlag(Primitive::kPrimFloat, offset);
1316 case Primitive::kPrimLong:
1317 case Primitive::kPrimDouble:
1318 return TypeFlag(Primitive::kPrimLong, offset) |
1319 TypeFlag(Primitive::kPrimDouble, offset);
1320 default:
1321 return TypeFlag(type, offset);
1322 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001323 }
1324
Aart Bik854a02b2015-07-14 16:07:00 -07001325 // Translates type to bit flag.
1326 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1327 CHECK_NE(type, Primitive::kPrimVoid);
1328 const uint64_t one = 1;
1329 const int shift = type; // 0-based consecutive enum
1330 DCHECK_LE(kFieldWriteOffset, shift);
1331 DCHECK_LT(shift, kArrayWriteOffset);
1332 return one << (type + offset);
1333 }
1334
1335 // Private constructor on direct flags value.
1336 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1337
1338 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001339};
1340
David Brazdiled596192015-01-23 10:39:45 +00001341// A HEnvironment object contains the values of virtual registers at a given location.
1342class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1343 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001344 HEnvironment(ArenaAllocator* arena,
1345 size_t number_of_vregs,
1346 const DexFile& dex_file,
1347 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001348 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001349 InvokeType invoke_type,
1350 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001351 : vregs_(arena, number_of_vregs),
1352 locations_(arena, number_of_vregs),
1353 parent_(nullptr),
1354 dex_file_(dex_file),
1355 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001356 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001357 invoke_type_(invoke_type),
1358 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001359 vregs_.SetSize(number_of_vregs);
1360 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001361 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001362 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001363
1364 locations_.SetSize(number_of_vregs);
1365 for (size_t i = 0; i < number_of_vregs; ++i) {
1366 locations_.Put(i, Location());
1367 }
1368 }
1369
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001370 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001371 : HEnvironment(arena,
1372 to_copy.Size(),
1373 to_copy.GetDexFile(),
1374 to_copy.GetMethodIdx(),
1375 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001376 to_copy.GetInvokeType(),
1377 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001378
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001379 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001380 if (parent_ != nullptr) {
1381 parent_->SetAndCopyParentChain(allocator, parent);
1382 } else {
1383 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1384 parent_->CopyFrom(parent);
1385 if (parent->GetParent() != nullptr) {
1386 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1387 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001388 }
David Brazdiled596192015-01-23 10:39:45 +00001389 }
1390
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001391 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1392 void CopyFrom(HEnvironment* environment);
1393
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001394 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1395 // input to the loop phi instead. This is for inserting instructions that
1396 // require an environment (like HDeoptimization) in the loop pre-header.
1397 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001398
1399 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001400 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001401 }
1402
1403 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001404 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001405 }
1406
David Brazdil1abb4192015-02-17 18:33:36 +00001407 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001408
1409 size_t Size() const { return vregs_.Size(); }
1410
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001411 HEnvironment* GetParent() const { return parent_; }
1412
1413 void SetLocationAt(size_t index, Location location) {
1414 locations_.Put(index, location);
1415 }
1416
1417 Location GetLocationAt(size_t index) const {
1418 return locations_.Get(index);
1419 }
1420
1421 uint32_t GetDexPc() const {
1422 return dex_pc_;
1423 }
1424
1425 uint32_t GetMethodIdx() const {
1426 return method_idx_;
1427 }
1428
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001429 InvokeType GetInvokeType() const {
1430 return invoke_type_;
1431 }
1432
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001433 const DexFile& GetDexFile() const {
1434 return dex_file_;
1435 }
1436
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001437 HInstruction* GetHolder() const {
1438 return holder_;
1439 }
1440
David Brazdiled596192015-01-23 10:39:45 +00001441 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001442 // Record instructions' use entries of this environment for constant-time removal.
1443 // It should only be called by HInstruction when a new environment use is added.
1444 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1445 DCHECK(env_use->GetUser() == this);
1446 size_t index = env_use->GetIndex();
1447 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1448 }
David Brazdiled596192015-01-23 10:39:45 +00001449
David Brazdil1abb4192015-02-17 18:33:36 +00001450 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001451 GrowableArray<Location> locations_;
1452 HEnvironment* parent_;
1453 const DexFile& dex_file_;
1454 const uint32_t method_idx_;
1455 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001456 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001457
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001458 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001459 HInstruction* const holder_;
1460
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001461 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001462
1463 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1464};
1465
Calin Juravleacf735c2015-02-12 15:25:22 +00001466class ReferenceTypeInfo : ValueObject {
1467 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001468 typedef Handle<mirror::Class> TypeHandle;
1469
Calin Juravle2e768302015-07-28 14:41:11 +00001470 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1471 // The constructor will check that the type_handle is valid.
1472 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001473 }
1474
Calin Juravle2e768302015-07-28 14:41:11 +00001475 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1476
1477 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1478 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001479 }
1480
Calin Juravle2e768302015-07-28 14:41:11 +00001481 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1482 return IsValidHandle(type_handle_);
1483 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001484 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001485
1486 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1487 DCHECK(IsValid());
1488 return GetTypeHandle()->IsObjectClass();
1489 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001490 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001491 DCHECK(IsValid());
1492 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001493 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001494
1495 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1496
Mathieu Chartier90443472015-07-16 20:32:27 -07001497 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001498 DCHECK(IsValid());
1499 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001500 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1501 }
1502
1503 // Returns true if the type information provide the same amount of details.
1504 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001505 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001506 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001507 if (!IsValid() && !rti.IsValid()) {
1508 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001509 return true;
1510 }
Calin Juravle2e768302015-07-28 14:41:11 +00001511 if (!IsValid() || !rti.IsValid()) {
1512 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001513 return false;
1514 }
Calin Juravle2e768302015-07-28 14:41:11 +00001515 return IsExact() == rti.IsExact()
1516 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001517 }
1518
1519 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001520 ReferenceTypeInfo();
1521 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001522
Calin Juravleacf735c2015-02-12 15:25:22 +00001523 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001524 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001525 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001526 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001527 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001528};
1529
1530std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1531
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001532class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001533 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001534 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001535 : previous_(nullptr),
1536 next_(nullptr),
1537 block_(nullptr),
1538 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001539 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001540 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001541 locations_(nullptr),
1542 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001543 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001544 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001545 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001546
Dave Allison20dfc792014-06-16 20:44:29 -07001547 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001548
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001549#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001550 enum InstructionKind {
1551 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1552 };
1553#undef DECLARE_KIND
1554
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001555 HInstruction* GetNext() const { return next_; }
1556 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001557
Calin Juravle77520bc2015-01-12 18:45:46 +00001558 HInstruction* GetNextDisregardingMoves() const;
1559 HInstruction* GetPreviousDisregardingMoves() const;
1560
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001561 HBasicBlock* GetBlock() const { return block_; }
1562 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001563 bool IsInBlock() const { return block_ != nullptr; }
1564 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001565 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001566
Roland Levillain6b879dd2014-09-22 17:13:44 +01001567 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001568 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001569
1570 virtual void Accept(HGraphVisitor* visitor) = 0;
1571 virtual const char* DebugName() const = 0;
1572
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001573 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001574 void SetRawInputAt(size_t index, HInstruction* input) {
1575 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1576 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001577
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001578 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001579 virtual uint32_t GetDexPc() const {
1580 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1581 " does not need an environment";
1582 UNREACHABLE();
1583 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001584 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001585 virtual bool CanThrow() const { return false; }
Aart Bik854a02b2015-07-14 16:07:00 -07001586
1587 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001588
Calin Juravle10e244f2015-01-26 18:54:32 +00001589 // Does not apply for all instructions, but having this at top level greatly
1590 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001591 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001592 virtual bool CanBeNull() const {
1593 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1594 return true;
1595 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001596
Calin Juravle641547a2015-04-21 22:08:51 +01001597 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1598 UNUSED(obj);
1599 return false;
1600 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001601
Calin Juravle2e768302015-07-28 14:41:11 +00001602 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001603
Calin Juravle61d544b2015-02-23 16:46:57 +00001604 ReferenceTypeInfo GetReferenceTypeInfo() const {
1605 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1606 return reference_type_info_;
1607 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001608
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001609 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001610 DCHECK(user != nullptr);
1611 HUseListNode<HInstruction*>* use =
1612 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1613 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001614 }
1615
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001616 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001617 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001618 HUseListNode<HEnvironment*>* env_use =
1619 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1620 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001621 }
1622
David Brazdil1abb4192015-02-17 18:33:36 +00001623 void RemoveAsUserOfInput(size_t input) {
1624 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1625 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1626 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001627
David Brazdil1abb4192015-02-17 18:33:36 +00001628 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1629 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001630
David Brazdiled596192015-01-23 10:39:45 +00001631 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1632 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001633 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001634 bool HasOnlyOneNonEnvironmentUse() const {
1635 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1636 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001637
Roland Levillain6c82d402014-10-13 16:10:27 +01001638 // Does this instruction strictly dominate `other_instruction`?
1639 // Returns false if this instruction and `other_instruction` are the same.
1640 // Aborts if this instruction and `other_instruction` are both phis.
1641 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001642
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001643 int GetId() const { return id_; }
1644 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001645
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001646 int GetSsaIndex() const { return ssa_index_; }
1647 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1648 bool HasSsaIndex() const { return ssa_index_ != -1; }
1649
1650 bool HasEnvironment() const { return environment_ != nullptr; }
1651 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001652 // Set the `environment_` field. Raw because this method does not
1653 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001654 void SetRawEnvironment(HEnvironment* environment) {
1655 DCHECK(environment_ == nullptr);
1656 DCHECK_EQ(environment->GetHolder(), this);
1657 environment_ = environment;
1658 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001659
1660 // Set the environment of this instruction, copying it from `environment`. While
1661 // copying, the uses lists are being updated.
1662 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001663 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001664 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001665 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001666 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001667 if (environment->GetParent() != nullptr) {
1668 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1669 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001670 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001671
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001672 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1673 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001674 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001675 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001676 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001677 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001678 if (environment->GetParent() != nullptr) {
1679 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1680 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001681 }
1682
Nicolas Geoffray39468442014-09-02 15:17:15 +01001683 // Returns the number of entries in the environment. Typically, that is the
1684 // number of dex registers in a method. It could be more in case of inlining.
1685 size_t EnvironmentSize() const;
1686
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001687 LocationSummary* GetLocations() const { return locations_; }
1688 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001689
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001690 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001691 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001692
Alexandre Rames188d4312015-04-09 18:30:21 +01001693 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1694 // uses of this instruction by `other` are *not* updated.
1695 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1696 ReplaceWith(other);
1697 other->ReplaceInput(this, use_index);
1698 }
1699
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001700 // Move `this` instruction before `cursor`.
1701 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001702
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001703#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001704 bool Is##type() const { return (As##type() != nullptr); } \
1705 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001706 virtual H##type* As##type() { return nullptr; }
1707
1708 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1709#undef INSTRUCTION_TYPE_CHECK
1710
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001711 // Returns whether the instruction can be moved within the graph.
1712 virtual bool CanBeMoved() const { return false; }
1713
1714 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001715 virtual bool InstructionTypeEquals(HInstruction* other) const {
1716 UNUSED(other);
1717 return false;
1718 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001719
1720 // Returns whether any data encoded in the two instructions is equal.
1721 // This method does not look at the inputs. Both instructions must be
1722 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001723 virtual bool InstructionDataEquals(HInstruction* other) const {
1724 UNUSED(other);
1725 return false;
1726 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001727
1728 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001729 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001730 // 2) Their inputs are identical.
1731 bool Equals(HInstruction* other) const;
1732
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001733 virtual InstructionKind GetKind() const = 0;
1734
1735 virtual size_t ComputeHashCode() const {
1736 size_t result = GetKind();
1737 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1738 result = (result * 31) + InputAt(i)->GetId();
1739 }
1740 return result;
1741 }
1742
1743 SideEffects GetSideEffects() const { return side_effects_; }
1744
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001745 size_t GetLifetimePosition() const { return lifetime_position_; }
1746 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1747 LiveInterval* GetLiveInterval() const { return live_interval_; }
1748 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1749 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1750
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001751 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1752
1753 // Returns whether the code generation of the instruction will require to have access
1754 // to the current method. Such instructions are:
1755 // (1): Instructions that require an environment, as calling the runtime requires
1756 // to walk the stack and have the current method stored at a specific stack address.
1757 // (2): Object literals like classes and strings, that are loaded from the dex cache
1758 // fields of the current method.
1759 bool NeedsCurrentMethod() const {
1760 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1761 }
1762
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001763 virtual bool NeedsDexCache() const { return false; }
1764
Mark Mendellc4701932015-04-10 13:18:51 -04001765 // Does this instruction have any use in an environment before
1766 // control flow hits 'other'?
1767 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1768
1769 // Remove all references to environment uses of this instruction.
1770 // The caller must ensure that this is safe to do.
1771 void RemoveEnvironmentUsers();
1772
David Brazdil1abb4192015-02-17 18:33:36 +00001773 protected:
1774 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1775 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1776
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001777 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001778 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1779
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001780 HInstruction* previous_;
1781 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001782 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001783
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001784 // An instruction gets an id when it is added to the graph.
1785 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001786 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001787 int id_;
1788
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001789 // When doing liveness analysis, instructions that have uses get an SSA index.
1790 int ssa_index_;
1791
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001792 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001793 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001794
1795 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001796 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001797
Nicolas Geoffray39468442014-09-02 15:17:15 +01001798 // The environment associated with this instruction. Not null if the instruction
1799 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001800 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001801
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001802 // Set by the code generator.
1803 LocationSummary* locations_;
1804
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001805 // Set by the liveness analysis.
1806 LiveInterval* live_interval_;
1807
1808 // Set by the liveness analysis, this is the position in a linear
1809 // order of blocks where this instruction's live interval start.
1810 size_t lifetime_position_;
1811
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001812 const SideEffects side_effects_;
1813
Calin Juravleacf735c2015-02-12 15:25:22 +00001814 // TODO: for primitive types this should be marked as invalid.
1815 ReferenceTypeInfo reference_type_info_;
1816
David Brazdil1abb4192015-02-17 18:33:36 +00001817 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001818 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001819 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001820 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001821 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001822
1823 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1824};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001825std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001826
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001827class HInputIterator : public ValueObject {
1828 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001829 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001830
1831 bool Done() const { return index_ == instruction_->InputCount(); }
1832 HInstruction* Current() const { return instruction_->InputAt(index_); }
1833 void Advance() { index_++; }
1834
1835 private:
1836 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001837 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001838
1839 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1840};
1841
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001842class HInstructionIterator : public ValueObject {
1843 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001844 explicit HInstructionIterator(const HInstructionList& instructions)
1845 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001846 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001847 }
1848
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001849 bool Done() const { return instruction_ == nullptr; }
1850 HInstruction* Current() const { return instruction_; }
1851 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001852 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001853 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001854 }
1855
1856 private:
1857 HInstruction* instruction_;
1858 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001859
1860 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001861};
1862
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001863class HBackwardInstructionIterator : public ValueObject {
1864 public:
1865 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1866 : instruction_(instructions.last_instruction_) {
1867 next_ = Done() ? nullptr : instruction_->GetPrevious();
1868 }
1869
1870 bool Done() const { return instruction_ == nullptr; }
1871 HInstruction* Current() const { return instruction_; }
1872 void Advance() {
1873 instruction_ = next_;
1874 next_ = Done() ? nullptr : instruction_->GetPrevious();
1875 }
1876
1877 private:
1878 HInstruction* instruction_;
1879 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001880
1881 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001882};
1883
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001884// An embedded container with N elements of type T. Used (with partial
1885// specialization for N=0) because embedded arrays cannot have size 0.
1886template<typename T, intptr_t N>
1887class EmbeddedArray {
1888 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001889 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001890
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001891 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001892
1893 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001894 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001895 return elements_[i];
1896 }
1897
1898 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001899 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001900 return elements_[i];
1901 }
1902
1903 const T& At(intptr_t i) const {
1904 return (*this)[i];
1905 }
1906
1907 void SetAt(intptr_t i, const T& val) {
1908 (*this)[i] = val;
1909 }
1910
1911 private:
1912 T elements_[N];
1913};
1914
1915template<typename T>
1916class EmbeddedArray<T, 0> {
1917 public:
1918 intptr_t length() const { return 0; }
1919 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001920 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001921 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001922 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001923 }
1924 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001925 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001926 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001927 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001928 }
1929};
1930
1931template<intptr_t N>
1932class HTemplateInstruction: public HInstruction {
1933 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001934 HTemplateInstruction<N>(SideEffects side_effects)
1935 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001936 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001937
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001938 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001939
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001940 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001941 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1942
1943 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1944 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001945 }
1946
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001947 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001948 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001949
1950 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001951};
1952
Dave Allison20dfc792014-06-16 20:44:29 -07001953template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001954class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001955 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001956 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1957 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001958 virtual ~HExpression() {}
1959
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001960 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001961
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001962 protected:
1963 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001964};
1965
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001966// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1967// instruction that branches to the exit block.
1968class HReturnVoid : public HTemplateInstruction<0> {
1969 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001970 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001971
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001972 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001973
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001974 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001975
1976 private:
1977 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1978};
1979
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001980// Represents dex's RETURN opcodes. A HReturn is a control flow
1981// instruction that branches to the exit block.
1982class HReturn : public HTemplateInstruction<1> {
1983 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001984 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001985 SetRawInputAt(0, value);
1986 }
1987
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001988 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001989
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001990 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001991
1992 private:
1993 DISALLOW_COPY_AND_ASSIGN(HReturn);
1994};
1995
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001996// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001997// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001998// exit block.
1999class HExit : public HTemplateInstruction<0> {
2000 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002001 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002002
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002003 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002004
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002005 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002006
2007 private:
2008 DISALLOW_COPY_AND_ASSIGN(HExit);
2009};
2010
2011// Jumps from one block to another.
2012class HGoto : public HTemplateInstruction<0> {
2013 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002014 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2015
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002016 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002017
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002018 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002019 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002020 }
2021
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002022 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002023
2024 private:
2025 DISALLOW_COPY_AND_ASSIGN(HGoto);
2026};
2027
Dave Allison20dfc792014-06-16 20:44:29 -07002028
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002029// Conditional branch. A block ending with an HIf instruction must have
2030// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002031class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002032 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002033 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002034 SetRawInputAt(0, input);
2035 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002036
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002037 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002038
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002039 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002040 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002041 }
2042
2043 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002044 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002045 }
2046
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002047 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002048
2049 private:
2050 DISALLOW_COPY_AND_ASSIGN(HIf);
2051};
2052
David Brazdilfc6a86a2015-06-26 10:33:45 +00002053
2054// Abstract instruction which marks the beginning and/or end of a try block and
2055// links it to the respective exception handlers. Behaves the same as a Goto in
2056// non-exceptional control flow.
2057// Normal-flow successor is stored at index zero, exception handlers under
2058// higher indices in no particular order.
2059class HTryBoundary : public HTemplateInstruction<0> {
2060 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002061 enum BoundaryKind {
2062 kEntry,
2063 kExit,
2064 };
2065
2066 explicit HTryBoundary(BoundaryKind kind)
2067 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002068
2069 bool IsControlFlow() const OVERRIDE { return true; }
2070
2071 // Returns the block's non-exceptional successor (index zero).
2072 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2073
2074 // Returns whether `handler` is among its exception handlers (non-zero index
2075 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002076 bool HasExceptionHandler(const HBasicBlock& handler) const {
2077 DCHECK(handler.IsCatchBlock());
2078 return GetBlock()->GetSuccessors().Contains(
2079 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002080 }
2081
2082 // If not present already, adds `handler` to its block's list of exception
2083 // handlers.
2084 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002085 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002086 GetBlock()->AddSuccessor(handler);
2087 }
2088 }
2089
David Brazdil56e1acc2015-06-30 15:41:36 +01002090 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002091
David Brazdilffee3d32015-07-06 11:48:53 +01002092 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2093
David Brazdilfc6a86a2015-06-26 10:33:45 +00002094 DECLARE_INSTRUCTION(TryBoundary);
2095
2096 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002097 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002098
2099 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2100};
2101
David Brazdilffee3d32015-07-06 11:48:53 +01002102// Iterator over exception handlers of a given HTryBoundary, i.e. over
2103// exceptional successors of its basic block.
2104class HExceptionHandlerIterator : public ValueObject {
2105 public:
2106 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2107 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2108
2109 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2110 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
2111 size_t CurrentSuccessorIndex() const { return index_; }
2112 void Advance() { ++index_; }
2113
2114 private:
2115 const HBasicBlock& block_;
2116 size_t index_;
2117
2118 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2119};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002120
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002121// Deoptimize to interpreter, upon checking a condition.
2122class HDeoptimize : public HTemplateInstruction<1> {
2123 public:
2124 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2125 : HTemplateInstruction(SideEffects::None()),
2126 dex_pc_(dex_pc) {
2127 SetRawInputAt(0, cond);
2128 }
2129
2130 bool NeedsEnvironment() const OVERRIDE { return true; }
2131 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002132 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002133
2134 DECLARE_INSTRUCTION(Deoptimize);
2135
2136 private:
2137 uint32_t dex_pc_;
2138
2139 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2140};
2141
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002142// Represents the ArtMethod that was passed as a first argument to
2143// the method. It is used by instructions that depend on it, like
2144// instructions that work with the dex cache.
2145class HCurrentMethod : public HExpression<0> {
2146 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002147 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002148
2149 DECLARE_INSTRUCTION(CurrentMethod);
2150
2151 private:
2152 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2153};
2154
Roland Levillain88cb1752014-10-20 16:36:47 +01002155class HUnaryOperation : public HExpression<1> {
2156 public:
2157 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2158 : HExpression(result_type, SideEffects::None()) {
2159 SetRawInputAt(0, input);
2160 }
2161
2162 HInstruction* GetInput() const { return InputAt(0); }
2163 Primitive::Type GetResultType() const { return GetType(); }
2164
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002165 bool CanBeMoved() const OVERRIDE { return true; }
2166 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002167 UNUSED(other);
2168 return true;
2169 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002170
Roland Levillain9240d6a2014-10-20 16:47:04 +01002171 // Try to statically evaluate `operation` and return a HConstant
2172 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002173 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002174 HConstant* TryStaticEvaluation() const;
2175
2176 // Apply this operation to `x`.
2177 virtual int32_t Evaluate(int32_t x) const = 0;
2178 virtual int64_t Evaluate(int64_t x) const = 0;
2179
Roland Levillain88cb1752014-10-20 16:36:47 +01002180 DECLARE_INSTRUCTION(UnaryOperation);
2181
2182 private:
2183 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2184};
2185
Dave Allison20dfc792014-06-16 20:44:29 -07002186class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002187 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002188 HBinaryOperation(Primitive::Type result_type,
2189 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002190 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002191 SetRawInputAt(0, left);
2192 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002193 }
2194
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002195 HInstruction* GetLeft() const { return InputAt(0); }
2196 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002197 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002198
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002199 virtual bool IsCommutative() const { return false; }
2200
2201 // Put constant on the right.
2202 // Returns whether order is changed.
2203 bool OrderInputsWithConstantOnTheRight() {
2204 HInstruction* left = InputAt(0);
2205 HInstruction* right = InputAt(1);
2206 if (left->IsConstant() && !right->IsConstant()) {
2207 ReplaceInput(right, 0);
2208 ReplaceInput(left, 1);
2209 return true;
2210 }
2211 return false;
2212 }
2213
2214 // Order inputs by instruction id, but favor constant on the right side.
2215 // This helps GVN for commutative ops.
2216 void OrderInputs() {
2217 DCHECK(IsCommutative());
2218 HInstruction* left = InputAt(0);
2219 HInstruction* right = InputAt(1);
2220 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2221 return;
2222 }
2223 if (OrderInputsWithConstantOnTheRight()) {
2224 return;
2225 }
2226 // Order according to instruction id.
2227 if (left->GetId() > right->GetId()) {
2228 ReplaceInput(right, 0);
2229 ReplaceInput(left, 1);
2230 }
2231 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002232
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002233 bool CanBeMoved() const OVERRIDE { return true; }
2234 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002235 UNUSED(other);
2236 return true;
2237 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002238
Roland Levillain9240d6a2014-10-20 16:47:04 +01002239 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002240 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002241 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002242 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002243
2244 // Apply this operation to `x` and `y`.
2245 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2246 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2247
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002248 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002249 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002250 HConstant* GetConstantRight() const;
2251
2252 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002253 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002254 HInstruction* GetLeastConstantLeft() const;
2255
Roland Levillainccc07a92014-09-16 14:48:16 +01002256 DECLARE_INSTRUCTION(BinaryOperation);
2257
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002258 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002259 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2260};
2261
Mark Mendellc4701932015-04-10 13:18:51 -04002262// The comparison bias applies for floating point operations and indicates how NaN
2263// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002264enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002265 kNoBias, // bias is not applicable (i.e. for long operation)
2266 kGtBias, // return 1 for NaN comparisons
2267 kLtBias, // return -1 for NaN comparisons
2268};
2269
Dave Allison20dfc792014-06-16 20:44:29 -07002270class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002271 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002272 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002273 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002274 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002275 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002276
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002277 bool NeedsMaterialization() const { return needs_materialization_; }
2278 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002279
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002280 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002281 // `instruction`, and disregard moves in between.
2282 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002283
Dave Allison20dfc792014-06-16 20:44:29 -07002284 DECLARE_INSTRUCTION(Condition);
2285
2286 virtual IfCondition GetCondition() const = 0;
2287
Mark Mendellc4701932015-04-10 13:18:51 -04002288 virtual IfCondition GetOppositeCondition() const = 0;
2289
Roland Levillain4fa13f62015-07-06 18:11:54 +01002290 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002291
2292 void SetBias(ComparisonBias bias) { bias_ = bias; }
2293
2294 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2295 return bias_ == other->AsCondition()->bias_;
2296 }
2297
Roland Levillain4fa13f62015-07-06 18:11:54 +01002298 bool IsFPConditionTrueIfNaN() const {
2299 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2300 IfCondition if_cond = GetCondition();
2301 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2302 }
2303
2304 bool IsFPConditionFalseIfNaN() const {
2305 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2306 IfCondition if_cond = GetCondition();
2307 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2308 }
2309
Dave Allison20dfc792014-06-16 20:44:29 -07002310 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002311 // For register allocation purposes, returns whether this instruction needs to be
2312 // materialized (that is, not just be in the processor flags).
2313 bool needs_materialization_;
2314
Mark Mendellc4701932015-04-10 13:18:51 -04002315 // Needed if we merge a HCompare into a HCondition.
2316 ComparisonBias bias_;
2317
Dave Allison20dfc792014-06-16 20:44:29 -07002318 DISALLOW_COPY_AND_ASSIGN(HCondition);
2319};
2320
2321// Instruction to check if two inputs are equal to each other.
2322class HEqual : public HCondition {
2323 public:
2324 HEqual(HInstruction* first, HInstruction* second)
2325 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002326
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002327 bool IsCommutative() const OVERRIDE { return true; }
2328
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002329 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002330 return x == y ? 1 : 0;
2331 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002332 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002333 return x == y ? 1 : 0;
2334 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002335
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002336 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002337
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002338 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002339 return kCondEQ;
2340 }
2341
Mark Mendellc4701932015-04-10 13:18:51 -04002342 IfCondition GetOppositeCondition() const OVERRIDE {
2343 return kCondNE;
2344 }
2345
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002346 private:
2347 DISALLOW_COPY_AND_ASSIGN(HEqual);
2348};
2349
Dave Allison20dfc792014-06-16 20:44:29 -07002350class HNotEqual : public HCondition {
2351 public:
2352 HNotEqual(HInstruction* first, HInstruction* second)
2353 : HCondition(first, second) {}
2354
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002355 bool IsCommutative() const OVERRIDE { return true; }
2356
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002357 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002358 return x != y ? 1 : 0;
2359 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002360 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002361 return x != y ? 1 : 0;
2362 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002363
Dave Allison20dfc792014-06-16 20:44:29 -07002364 DECLARE_INSTRUCTION(NotEqual);
2365
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002366 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002367 return kCondNE;
2368 }
2369
Mark Mendellc4701932015-04-10 13:18:51 -04002370 IfCondition GetOppositeCondition() const OVERRIDE {
2371 return kCondEQ;
2372 }
2373
Dave Allison20dfc792014-06-16 20:44:29 -07002374 private:
2375 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2376};
2377
2378class HLessThan : public HCondition {
2379 public:
2380 HLessThan(HInstruction* first, HInstruction* second)
2381 : HCondition(first, second) {}
2382
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002383 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002384 return x < y ? 1 : 0;
2385 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002386 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002387 return x < y ? 1 : 0;
2388 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002389
Dave Allison20dfc792014-06-16 20:44:29 -07002390 DECLARE_INSTRUCTION(LessThan);
2391
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002392 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002393 return kCondLT;
2394 }
2395
Mark Mendellc4701932015-04-10 13:18:51 -04002396 IfCondition GetOppositeCondition() const OVERRIDE {
2397 return kCondGE;
2398 }
2399
Dave Allison20dfc792014-06-16 20:44:29 -07002400 private:
2401 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2402};
2403
2404class HLessThanOrEqual : public HCondition {
2405 public:
2406 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2407 : HCondition(first, second) {}
2408
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002409 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002410 return x <= y ? 1 : 0;
2411 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002412 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002413 return x <= y ? 1 : 0;
2414 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002415
Dave Allison20dfc792014-06-16 20:44:29 -07002416 DECLARE_INSTRUCTION(LessThanOrEqual);
2417
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002418 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002419 return kCondLE;
2420 }
2421
Mark Mendellc4701932015-04-10 13:18:51 -04002422 IfCondition GetOppositeCondition() const OVERRIDE {
2423 return kCondGT;
2424 }
2425
Dave Allison20dfc792014-06-16 20:44:29 -07002426 private:
2427 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2428};
2429
2430class HGreaterThan : public HCondition {
2431 public:
2432 HGreaterThan(HInstruction* first, HInstruction* second)
2433 : HCondition(first, second) {}
2434
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002435 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002436 return x > y ? 1 : 0;
2437 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002438 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002439 return x > y ? 1 : 0;
2440 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002441
Dave Allison20dfc792014-06-16 20:44:29 -07002442 DECLARE_INSTRUCTION(GreaterThan);
2443
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002444 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002445 return kCondGT;
2446 }
2447
Mark Mendellc4701932015-04-10 13:18:51 -04002448 IfCondition GetOppositeCondition() const OVERRIDE {
2449 return kCondLE;
2450 }
2451
Dave Allison20dfc792014-06-16 20:44:29 -07002452 private:
2453 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2454};
2455
2456class HGreaterThanOrEqual : public HCondition {
2457 public:
2458 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2459 : HCondition(first, second) {}
2460
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002461 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002462 return x >= y ? 1 : 0;
2463 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002464 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002465 return x >= y ? 1 : 0;
2466 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002467
Dave Allison20dfc792014-06-16 20:44:29 -07002468 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2469
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002470 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002471 return kCondGE;
2472 }
2473
Mark Mendellc4701932015-04-10 13:18:51 -04002474 IfCondition GetOppositeCondition() const OVERRIDE {
2475 return kCondLT;
2476 }
2477
Dave Allison20dfc792014-06-16 20:44:29 -07002478 private:
2479 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2480};
2481
2482
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002483// Instruction to check how two inputs compare to each other.
2484// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2485class HCompare : public HBinaryOperation {
2486 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002487 HCompare(Primitive::Type type,
2488 HInstruction* first,
2489 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002490 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002491 uint32_t dex_pc)
2492 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002493 DCHECK_EQ(type, first->GetType());
2494 DCHECK_EQ(type, second->GetType());
2495 }
2496
Calin Juravleddb7df22014-11-25 20:56:51 +00002497 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002498 return
2499 x == y ? 0 :
2500 x > y ? 1 :
2501 -1;
2502 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002503
2504 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002505 return
2506 x == y ? 0 :
2507 x > y ? 1 :
2508 -1;
2509 }
2510
Calin Juravleddb7df22014-11-25 20:56:51 +00002511 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2512 return bias_ == other->AsCompare()->bias_;
2513 }
2514
Mark Mendellc4701932015-04-10 13:18:51 -04002515 ComparisonBias GetBias() const { return bias_; }
2516
Roland Levillain4fa13f62015-07-06 18:11:54 +01002517 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002518
Alexey Frunze4dda3372015-06-01 18:31:49 -07002519 uint32_t GetDexPc() const { return dex_pc_; }
2520
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002521 DECLARE_INSTRUCTION(Compare);
2522
2523 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002524 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002525 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002526
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002527 DISALLOW_COPY_AND_ASSIGN(HCompare);
2528};
2529
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002530// A local in the graph. Corresponds to a Dex register.
2531class HLocal : public HTemplateInstruction<0> {
2532 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002533 explicit HLocal(uint16_t reg_number)
2534 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002535
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002536 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002537
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002538 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002539
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002540 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002541 // The Dex register number.
2542 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002543
2544 DISALLOW_COPY_AND_ASSIGN(HLocal);
2545};
2546
2547// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002548class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002549 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002550 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002551 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002552 SetRawInputAt(0, local);
2553 }
2554
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002555 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2556
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002557 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002558
2559 private:
2560 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2561};
2562
2563// Store a value in a given local. This instruction has two inputs: the value
2564// and the local.
2565class HStoreLocal : public HTemplateInstruction<2> {
2566 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002567 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002568 SetRawInputAt(0, local);
2569 SetRawInputAt(1, value);
2570 }
2571
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002572 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2573
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002574 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002575
2576 private:
2577 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2578};
2579
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002580class HConstant : public HExpression<0> {
2581 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002582 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2583
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002584 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002585
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002586 virtual bool IsMinusOne() const { return false; }
2587 virtual bool IsZero() const { return false; }
2588 virtual bool IsOne() const { return false; }
2589
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002590 DECLARE_INSTRUCTION(Constant);
2591
2592 private:
2593 DISALLOW_COPY_AND_ASSIGN(HConstant);
2594};
2595
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002596class HFloatConstant : public HConstant {
2597 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002598 float GetValue() const { return value_; }
2599
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002600 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002601 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2602 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002603 }
2604
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002605 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002606
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002607 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002608 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002609 }
2610 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002611 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002612 }
2613 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002614 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2615 }
2616 bool IsNaN() const {
2617 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002618 }
2619
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002620 DECLARE_INSTRUCTION(FloatConstant);
2621
2622 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002623 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002624 explicit HFloatConstant(int32_t value)
2625 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002626
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002627 const float value_;
2628
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002629 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002630 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002631 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002632 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2633};
2634
2635class HDoubleConstant : public HConstant {
2636 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002637 double GetValue() const { return value_; }
2638
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002639 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002640 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2641 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002642 }
2643
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002644 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002645
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002646 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002647 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002648 }
2649 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002650 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002651 }
2652 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002653 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2654 }
2655 bool IsNaN() const {
2656 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002657 }
2658
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002659 DECLARE_INSTRUCTION(DoubleConstant);
2660
2661 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002662 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002663 explicit HDoubleConstant(int64_t value)
2664 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002665
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002666 const double value_;
2667
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002668 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002669 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002670 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002671 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2672};
2673
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002674class HNullConstant : public HConstant {
2675 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002676 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2677 return true;
2678 }
2679
2680 size_t ComputeHashCode() const OVERRIDE { return 0; }
2681
2682 DECLARE_INSTRUCTION(NullConstant);
2683
2684 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002685 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2686
2687 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002688 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2689};
2690
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002691// Constants of the type int. Those can be from Dex instructions, or
2692// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002693class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002694 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002695 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002696
Calin Juravle61d544b2015-02-23 16:46:57 +00002697 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002698 return other->AsIntConstant()->value_ == value_;
2699 }
2700
Calin Juravle61d544b2015-02-23 16:46:57 +00002701 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2702
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002703 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2704 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2705 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2706
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002707 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002708
2709 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002710 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2711
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002712 const int32_t value_;
2713
David Brazdil8d5b8b22015-03-24 10:51:52 +00002714 friend class HGraph;
2715 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002716 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002717 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2718};
2719
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002720class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002721 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002722 int64_t GetValue() const { return value_; }
2723
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002724 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002725 return other->AsLongConstant()->value_ == value_;
2726 }
2727
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002728 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002729
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002730 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2731 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2732 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2733
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002734 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002735
2736 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002737 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2738
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002739 const int64_t value_;
2740
David Brazdil8d5b8b22015-03-24 10:51:52 +00002741 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002742 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2743};
2744
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002745enum class Intrinsics {
2746#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2747#include "intrinsics_list.h"
2748 kNone,
2749 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2750#undef INTRINSICS_LIST
2751#undef OPTIMIZING_INTRINSICS
2752};
2753std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2754
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002755class HInvoke : public HInstruction {
2756 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002757 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002758
2759 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2760 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002761 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002762
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002763 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002764 SetRawInputAt(index, argument);
2765 }
2766
Roland Levillain3e3d7332015-04-28 11:00:54 +01002767 // Return the number of arguments. This number can be lower than
2768 // the number of inputs returned by InputCount(), as some invoke
2769 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2770 // inputs at the end of their list of inputs.
2771 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2772
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002773 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002774
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002775 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002776
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002777 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002778 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002779
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002780 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2781
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002782 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002783 return intrinsic_;
2784 }
2785
2786 void SetIntrinsic(Intrinsics intrinsic) {
2787 intrinsic_ = intrinsic;
2788 }
2789
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002790 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002791 return GetEnvironment()->GetParent() != nullptr;
2792 }
2793
2794 bool CanThrow() const OVERRIDE { return true; }
2795
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002796 DECLARE_INSTRUCTION(Invoke);
2797
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002798 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002799 HInvoke(ArenaAllocator* arena,
2800 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002801 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002802 Primitive::Type return_type,
2803 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002804 uint32_t dex_method_index,
2805 InvokeType original_invoke_type)
Aart Bik854a02b2015-07-14 16:07:00 -07002806 : HInstruction(SideEffects::All()), // assume write/read on all fields/arrays
Roland Levillain3e3d7332015-04-28 11:00:54 +01002807 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002808 inputs_(arena, number_of_arguments),
2809 return_type_(return_type),
2810 dex_pc_(dex_pc),
2811 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002812 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002813 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002814 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2815 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002816 }
2817
David Brazdil1abb4192015-02-17 18:33:36 +00002818 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2819 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2820 inputs_.Put(index, input);
2821 }
2822
Roland Levillain3e3d7332015-04-28 11:00:54 +01002823 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002824 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002825 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002826 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002827 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002828 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002829 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002830
2831 private:
2832 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2833};
2834
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002835class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002836 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002837 // Requirements of this method call regarding the class
2838 // initialization (clinit) check of its declaring class.
2839 enum class ClinitCheckRequirement {
2840 kNone, // Class already initialized.
2841 kExplicit, // Static call having explicit clinit check as last input.
2842 kImplicit, // Static call implicitly requiring a clinit check.
2843 };
2844
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002845 HInvokeStaticOrDirect(ArenaAllocator* arena,
2846 uint32_t number_of_arguments,
2847 Primitive::Type return_type,
2848 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002849 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002850 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002851 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002852 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002853 InvokeType invoke_type,
2854 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002855 : HInvoke(arena,
2856 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002857 // There is one extra argument for the HCurrentMethod node, and
2858 // potentially one other if the clinit check is explicit, and one other
2859 // if the method is a string factory.
2860 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
2861 + (string_init_offset ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002862 return_type,
2863 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002864 dex_method_index,
2865 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002866 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002867 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002868 clinit_check_requirement_(clinit_check_requirement),
2869 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002870
Calin Juravle641547a2015-04-21 22:08:51 +01002871 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2872 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002873 // We access the method via the dex cache so we can't do an implicit null check.
2874 // TODO: for intrinsics we can generate implicit null checks.
2875 return false;
2876 }
2877
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002878 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002879 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002880 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002881 bool IsStringInit() const { return string_init_offset_ != 0; }
2882 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002883 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002884
Roland Levillain4c0eb422015-04-24 16:43:49 +01002885 // Is this instruction a call to a static method?
2886 bool IsStatic() const {
2887 return GetInvokeType() == kStatic;
2888 }
2889
Roland Levillain3e3d7332015-04-28 11:00:54 +01002890 // Remove the art::HLoadClass instruction set as last input by
2891 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2892 // the initial art::HClinitCheck instruction (only relevant for
2893 // static calls with explicit clinit check).
2894 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002895 DCHECK(IsStaticWithExplicitClinitCheck());
2896 size_t last_input_index = InputCount() - 1;
2897 HInstruction* last_input = InputAt(last_input_index);
2898 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002899 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002900 RemoveAsUserOfInput(last_input_index);
2901 inputs_.DeleteAt(last_input_index);
2902 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2903 DCHECK(IsStaticWithImplicitClinitCheck());
2904 }
2905
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002906 bool IsStringFactoryFor(HFakeString* str) const {
2907 if (!IsStringInit()) return false;
2908 // +1 for the current method.
2909 if (InputCount() == (number_of_arguments_ + 1)) return false;
2910 return InputAt(InputCount() - 1)->AsFakeString() == str;
2911 }
2912
2913 void RemoveFakeStringArgumentAsLastInput() {
2914 DCHECK(IsStringInit());
2915 size_t last_input_index = InputCount() - 1;
2916 HInstruction* last_input = InputAt(last_input_index);
2917 DCHECK(last_input != nullptr);
2918 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
2919 RemoveAsUserOfInput(last_input_index);
2920 inputs_.DeleteAt(last_input_index);
2921 }
2922
Roland Levillain4c0eb422015-04-24 16:43:49 +01002923 // Is this a call to a static method whose declaring class has an
2924 // explicit intialization check in the graph?
2925 bool IsStaticWithExplicitClinitCheck() const {
2926 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2927 }
2928
2929 // Is this a call to a static method whose declaring class has an
2930 // implicit intialization check requirement?
2931 bool IsStaticWithImplicitClinitCheck() const {
2932 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2933 }
2934
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002935 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002936
Roland Levillain4c0eb422015-04-24 16:43:49 +01002937 protected:
2938 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2939 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2940 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2941 HInstruction* input = input_record.GetInstruction();
2942 // `input` is the last input of a static invoke marked as having
2943 // an explicit clinit check. It must either be:
2944 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2945 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2946 DCHECK(input != nullptr);
2947 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2948 }
2949 return input_record;
2950 }
2951
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002952 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002953 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002954 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002955 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002956 // Thread entrypoint offset for string init method if this is a string init invoke.
2957 // Note that there are multiple string init methods, each having its own offset.
2958 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002959
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002960 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002961};
2962
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002963class HInvokeVirtual : public HInvoke {
2964 public:
2965 HInvokeVirtual(ArenaAllocator* arena,
2966 uint32_t number_of_arguments,
2967 Primitive::Type return_type,
2968 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002969 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002970 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002971 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002972 vtable_index_(vtable_index) {}
2973
Calin Juravle641547a2015-04-21 22:08:51 +01002974 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002975 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002976 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002977 }
2978
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002979 uint32_t GetVTableIndex() const { return vtable_index_; }
2980
2981 DECLARE_INSTRUCTION(InvokeVirtual);
2982
2983 private:
2984 const uint32_t vtable_index_;
2985
2986 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2987};
2988
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002989class HInvokeInterface : public HInvoke {
2990 public:
2991 HInvokeInterface(ArenaAllocator* arena,
2992 uint32_t number_of_arguments,
2993 Primitive::Type return_type,
2994 uint32_t dex_pc,
2995 uint32_t dex_method_index,
2996 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002997 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002998 imt_index_(imt_index) {}
2999
Calin Juravle641547a2015-04-21 22:08:51 +01003000 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003001 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003002 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003003 }
3004
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003005 uint32_t GetImtIndex() const { return imt_index_; }
3006 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3007
3008 DECLARE_INSTRUCTION(InvokeInterface);
3009
3010 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003011 const uint32_t imt_index_;
3012
3013 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3014};
3015
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003016class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003017 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003018 HNewInstance(HCurrentMethod* current_method,
3019 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003020 uint16_t type_index,
3021 const DexFile& dex_file,
3022 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003023 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3024 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003025 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003026 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003027 entrypoint_(entrypoint) {
3028 SetRawInputAt(0, current_method);
3029 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003030
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003031 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003032 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003033 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003034
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003035 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003036 bool NeedsEnvironment() const OVERRIDE { return true; }
3037 // It may throw when called on:
3038 // - interfaces
3039 // - abstract/innaccessible/unknown classes
3040 // TODO: optimize when possible.
3041 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003042
Calin Juravle10e244f2015-01-26 18:54:32 +00003043 bool CanBeNull() const OVERRIDE { return false; }
3044
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003045 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3046
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003047 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003048
3049 private:
3050 const uint32_t dex_pc_;
3051 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003052 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003053 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003054
3055 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3056};
3057
Roland Levillain88cb1752014-10-20 16:36:47 +01003058class HNeg : public HUnaryOperation {
3059 public:
3060 explicit HNeg(Primitive::Type result_type, HInstruction* input)
3061 : HUnaryOperation(result_type, input) {}
3062
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003063 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
3064 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003065
Roland Levillain88cb1752014-10-20 16:36:47 +01003066 DECLARE_INSTRUCTION(Neg);
3067
3068 private:
3069 DISALLOW_COPY_AND_ASSIGN(HNeg);
3070};
3071
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003072class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003073 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003074 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003075 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003076 uint32_t dex_pc,
3077 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003078 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003079 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003080 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3081 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003082 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003083 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003084 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003085 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003086 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003087 }
3088
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003089 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003090 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003091 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003092
3093 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003094 bool NeedsEnvironment() const OVERRIDE { return true; }
3095
Mingyao Yang0c365e62015-03-31 15:09:29 -07003096 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3097 bool CanThrow() const OVERRIDE { return true; }
3098
Calin Juravle10e244f2015-01-26 18:54:32 +00003099 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003100
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003101 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3102
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003103 DECLARE_INSTRUCTION(NewArray);
3104
3105 private:
3106 const uint32_t dex_pc_;
3107 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003108 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003109 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003110
3111 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3112};
3113
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003114class HAdd : public HBinaryOperation {
3115 public:
3116 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3117 : HBinaryOperation(result_type, left, right) {}
3118
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003119 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003120
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003121 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003122 return x + y;
3123 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003124 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003125 return x + y;
3126 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003127
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003128 DECLARE_INSTRUCTION(Add);
3129
3130 private:
3131 DISALLOW_COPY_AND_ASSIGN(HAdd);
3132};
3133
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003134class HSub : public HBinaryOperation {
3135 public:
3136 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3137 : HBinaryOperation(result_type, left, right) {}
3138
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003139 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003140 return x - y;
3141 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003142 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003143 return x - y;
3144 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003145
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003146 DECLARE_INSTRUCTION(Sub);
3147
3148 private:
3149 DISALLOW_COPY_AND_ASSIGN(HSub);
3150};
3151
Calin Juravle34bacdf2014-10-07 20:23:36 +01003152class HMul : public HBinaryOperation {
3153 public:
3154 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3155 : HBinaryOperation(result_type, left, right) {}
3156
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003157 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003158
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003159 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
3160 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003161
3162 DECLARE_INSTRUCTION(Mul);
3163
3164 private:
3165 DISALLOW_COPY_AND_ASSIGN(HMul);
3166};
3167
Calin Juravle7c4954d2014-10-28 16:57:40 +00003168class HDiv : public HBinaryOperation {
3169 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003170 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3171 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003172
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003173 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003174 // Our graph structure ensures we never have 0 for `y` during constant folding.
3175 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003176 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003177 return (y == -1) ? -x : x / y;
3178 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003179
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003180 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003181 DCHECK_NE(y, 0);
3182 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3183 return (y == -1) ? -x : x / y;
3184 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003185
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003186 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003187
Calin Juravle7c4954d2014-10-28 16:57:40 +00003188 DECLARE_INSTRUCTION(Div);
3189
3190 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003191 const uint32_t dex_pc_;
3192
Calin Juravle7c4954d2014-10-28 16:57:40 +00003193 DISALLOW_COPY_AND_ASSIGN(HDiv);
3194};
3195
Calin Juravlebacfec32014-11-14 15:54:36 +00003196class HRem : public HBinaryOperation {
3197 public:
3198 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3199 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
3200
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003201 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003202 DCHECK_NE(y, 0);
3203 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3204 return (y == -1) ? 0 : x % y;
3205 }
3206
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003207 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003208 DCHECK_NE(y, 0);
3209 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3210 return (y == -1) ? 0 : x % y;
3211 }
3212
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003213 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003214
3215 DECLARE_INSTRUCTION(Rem);
3216
3217 private:
3218 const uint32_t dex_pc_;
3219
3220 DISALLOW_COPY_AND_ASSIGN(HRem);
3221};
3222
Calin Juravled0d48522014-11-04 16:40:20 +00003223class HDivZeroCheck : public HExpression<1> {
3224 public:
3225 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3226 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3227 SetRawInputAt(0, value);
3228 }
3229
3230 bool CanBeMoved() const OVERRIDE { return true; }
3231
3232 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3233 UNUSED(other);
3234 return true;
3235 }
3236
3237 bool NeedsEnvironment() const OVERRIDE { return true; }
3238 bool CanThrow() const OVERRIDE { return true; }
3239
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003240 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003241
3242 DECLARE_INSTRUCTION(DivZeroCheck);
3243
3244 private:
3245 const uint32_t dex_pc_;
3246
3247 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3248};
3249
Calin Juravle9aec02f2014-11-18 23:06:35 +00003250class HShl : public HBinaryOperation {
3251 public:
3252 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3253 : HBinaryOperation(result_type, left, right) {}
3254
3255 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
3256 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
3257
3258 DECLARE_INSTRUCTION(Shl);
3259
3260 private:
3261 DISALLOW_COPY_AND_ASSIGN(HShl);
3262};
3263
3264class HShr : public HBinaryOperation {
3265 public:
3266 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3267 : HBinaryOperation(result_type, left, right) {}
3268
3269 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
3270 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
3271
3272 DECLARE_INSTRUCTION(Shr);
3273
3274 private:
3275 DISALLOW_COPY_AND_ASSIGN(HShr);
3276};
3277
3278class HUShr : public HBinaryOperation {
3279 public:
3280 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3281 : HBinaryOperation(result_type, left, right) {}
3282
3283 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
3284 uint32_t ux = static_cast<uint32_t>(x);
3285 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
3286 return static_cast<int32_t>(ux >> uy);
3287 }
3288
3289 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
3290 uint64_t ux = static_cast<uint64_t>(x);
3291 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
3292 return static_cast<int64_t>(ux >> uy);
3293 }
3294
3295 DECLARE_INSTRUCTION(UShr);
3296
3297 private:
3298 DISALLOW_COPY_AND_ASSIGN(HUShr);
3299};
3300
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003301class HAnd : public HBinaryOperation {
3302 public:
3303 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3304 : HBinaryOperation(result_type, left, right) {}
3305
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003306 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003307
3308 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3309 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3310
3311 DECLARE_INSTRUCTION(And);
3312
3313 private:
3314 DISALLOW_COPY_AND_ASSIGN(HAnd);
3315};
3316
3317class HOr : public HBinaryOperation {
3318 public:
3319 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3320 : HBinaryOperation(result_type, left, right) {}
3321
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003322 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003323
3324 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3325 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3326
3327 DECLARE_INSTRUCTION(Or);
3328
3329 private:
3330 DISALLOW_COPY_AND_ASSIGN(HOr);
3331};
3332
3333class HXor : public HBinaryOperation {
3334 public:
3335 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3336 : HBinaryOperation(result_type, left, right) {}
3337
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003338 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003339
3340 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3341 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3342
3343 DECLARE_INSTRUCTION(Xor);
3344
3345 private:
3346 DISALLOW_COPY_AND_ASSIGN(HXor);
3347};
3348
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003349// The value of a parameter in this method. Its location depends on
3350// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003351class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003352 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003353 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3354 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003355
3356 uint8_t GetIndex() const { return index_; }
3357
Calin Juravle10e244f2015-01-26 18:54:32 +00003358 bool CanBeNull() const OVERRIDE { return !is_this_; }
3359
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003360 bool IsThis() const { return is_this_; }
3361
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003362 DECLARE_INSTRUCTION(ParameterValue);
3363
3364 private:
3365 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003366 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003367 const uint8_t index_;
3368
Calin Juravle10e244f2015-01-26 18:54:32 +00003369 // Whether or not the parameter value corresponds to 'this' argument.
3370 const bool is_this_;
3371
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003372 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3373};
3374
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003375class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003376 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003377 explicit HNot(Primitive::Type result_type, HInstruction* input)
3378 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003379
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003380 bool CanBeMoved() const OVERRIDE { return true; }
3381 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003382 UNUSED(other);
3383 return true;
3384 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003385
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003386 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3387 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003388
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003389 DECLARE_INSTRUCTION(Not);
3390
3391 private:
3392 DISALLOW_COPY_AND_ASSIGN(HNot);
3393};
3394
David Brazdil66d126e2015-04-03 16:02:44 +01003395class HBooleanNot : public HUnaryOperation {
3396 public:
3397 explicit HBooleanNot(HInstruction* input)
3398 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3399
3400 bool CanBeMoved() const OVERRIDE { return true; }
3401 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3402 UNUSED(other);
3403 return true;
3404 }
3405
3406 int32_t Evaluate(int32_t x) const OVERRIDE {
3407 DCHECK(IsUint<1>(x));
3408 return !x;
3409 }
3410
3411 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3412 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3413 UNREACHABLE();
3414 }
3415
3416 DECLARE_INSTRUCTION(BooleanNot);
3417
3418 private:
3419 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3420};
3421
Roland Levillaindff1f282014-11-05 14:15:05 +00003422class HTypeConversion : public HExpression<1> {
3423 public:
3424 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003425 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3426 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003427 SetRawInputAt(0, input);
3428 DCHECK_NE(input->GetType(), result_type);
3429 }
3430
3431 HInstruction* GetInput() const { return InputAt(0); }
3432 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3433 Primitive::Type GetResultType() const { return GetType(); }
3434
Roland Levillain624279f2014-12-04 11:54:28 +00003435 // Required by the x86 and ARM code generators when producing calls
3436 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003437 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003438
Roland Levillaindff1f282014-11-05 14:15:05 +00003439 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003440 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003441
Mark Mendelle82549b2015-05-06 10:55:34 -04003442 // Try to statically evaluate the conversion and return a HConstant
3443 // containing the result. If the input cannot be converted, return nullptr.
3444 HConstant* TryStaticEvaluation() const;
3445
Roland Levillaindff1f282014-11-05 14:15:05 +00003446 DECLARE_INSTRUCTION(TypeConversion);
3447
3448 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003449 const uint32_t dex_pc_;
3450
Roland Levillaindff1f282014-11-05 14:15:05 +00003451 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3452};
3453
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003454static constexpr uint32_t kNoRegNumber = -1;
3455
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003456class HPhi : public HInstruction {
3457 public:
3458 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003459 : HInstruction(SideEffects::None()),
3460 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003461 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003462 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003463 is_live_(false),
3464 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003465 inputs_.SetSize(number_of_inputs);
3466 }
3467
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003468 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3469 static Primitive::Type ToPhiType(Primitive::Type type) {
3470 switch (type) {
3471 case Primitive::kPrimBoolean:
3472 case Primitive::kPrimByte:
3473 case Primitive::kPrimShort:
3474 case Primitive::kPrimChar:
3475 return Primitive::kPrimInt;
3476 default:
3477 return type;
3478 }
3479 }
3480
David Brazdilffee3d32015-07-06 11:48:53 +01003481 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3482
Calin Juravle10e244f2015-01-26 18:54:32 +00003483 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003484
3485 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003486 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003487
Calin Juravle10e244f2015-01-26 18:54:32 +00003488 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003489 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003490
Calin Juravle10e244f2015-01-26 18:54:32 +00003491 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3492 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3493
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003494 uint32_t GetRegNumber() const { return reg_number_; }
3495
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003496 void SetDead() { is_live_ = false; }
3497 void SetLive() { is_live_ = true; }
3498 bool IsDead() const { return !is_live_; }
3499 bool IsLive() const { return is_live_; }
3500
Calin Juravlea4f88312015-04-16 12:57:19 +01003501 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3502 // An equivalent phi is a phi having the same dex register and type.
3503 // It assumes that phis with the same dex register are adjacent.
3504 HPhi* GetNextEquivalentPhiWithSameType() {
3505 HInstruction* next = GetNext();
3506 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3507 if (next->GetType() == GetType()) {
3508 return next->AsPhi();
3509 }
3510 next = next->GetNext();
3511 }
3512 return nullptr;
3513 }
3514
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003515 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003516
David Brazdil1abb4192015-02-17 18:33:36 +00003517 protected:
3518 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3519
3520 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3521 inputs_.Put(index, input);
3522 }
3523
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003524 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003525 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003526 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003527 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003528 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003529 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003530
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003531 DISALLOW_COPY_AND_ASSIGN(HPhi);
3532};
3533
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003534class HNullCheck : public HExpression<1> {
3535 public:
3536 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003537 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003538 SetRawInputAt(0, value);
3539 }
3540
Calin Juravle10e244f2015-01-26 18:54:32 +00003541 bool CanBeMoved() const OVERRIDE { return true; }
3542 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003543 UNUSED(other);
3544 return true;
3545 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003546
Calin Juravle10e244f2015-01-26 18:54:32 +00003547 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003548
Calin Juravle10e244f2015-01-26 18:54:32 +00003549 bool CanThrow() const OVERRIDE { return true; }
3550
3551 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003552
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003553 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003554
3555 DECLARE_INSTRUCTION(NullCheck);
3556
3557 private:
3558 const uint32_t dex_pc_;
3559
3560 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3561};
3562
3563class FieldInfo : public ValueObject {
3564 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003565 FieldInfo(MemberOffset field_offset,
3566 Primitive::Type field_type,
3567 bool is_volatile,
3568 uint32_t index,
3569 const DexFile& dex_file)
3570 : field_offset_(field_offset),
3571 field_type_(field_type),
3572 is_volatile_(is_volatile),
3573 index_(index),
3574 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003575
3576 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003577 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003578 uint32_t GetFieldIndex() const { return index_; }
3579 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003580 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003581
3582 private:
3583 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003584 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003585 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003586 uint32_t index_;
3587 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003588};
3589
3590class HInstanceFieldGet : public HExpression<1> {
3591 public:
3592 HInstanceFieldGet(HInstruction* value,
3593 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003594 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003595 bool is_volatile,
3596 uint32_t field_idx,
3597 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003598 : HExpression(
3599 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01003600 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003601 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003602 SetRawInputAt(0, value);
3603 }
3604
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003605 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003606
3607 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3608 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3609 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003610 }
3611
Calin Juravle641547a2015-04-21 22:08:51 +01003612 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3613 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003614 }
3615
3616 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003617 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3618 }
3619
Calin Juravle52c48962014-12-16 17:02:57 +00003620 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003621 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003622 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003623 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003624
3625 DECLARE_INSTRUCTION(InstanceFieldGet);
3626
3627 private:
3628 const FieldInfo field_info_;
3629
3630 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3631};
3632
3633class HInstanceFieldSet : public HTemplateInstruction<2> {
3634 public:
3635 HInstanceFieldSet(HInstruction* object,
3636 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003637 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003638 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003639 bool is_volatile,
3640 uint32_t field_idx,
3641 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003642 : HTemplateInstruction(
3643 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003644 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003645 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003646 SetRawInputAt(0, object);
3647 SetRawInputAt(1, value);
3648 }
3649
Calin Juravle641547a2015-04-21 22:08:51 +01003650 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3651 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003652 }
3653
Calin Juravle52c48962014-12-16 17:02:57 +00003654 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003655 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003656 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003657 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003658 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003659 bool GetValueCanBeNull() const { return value_can_be_null_; }
3660 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003661
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003662 DECLARE_INSTRUCTION(InstanceFieldSet);
3663
3664 private:
3665 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003666 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003667
3668 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3669};
3670
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003671class HArrayGet : public HExpression<2> {
3672 public:
3673 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07003674 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003675 SetRawInputAt(0, array);
3676 SetRawInputAt(1, index);
3677 }
3678
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003679 bool CanBeMoved() const OVERRIDE { return true; }
3680 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003681 UNUSED(other);
3682 return true;
3683 }
Calin Juravle641547a2015-04-21 22:08:51 +01003684 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3685 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003686 // TODO: We can be smarter here.
3687 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3688 // which generates the implicit null check. There are cases when these can be removed
3689 // to produce better code. If we ever add optimizations to do so we should allow an
3690 // implicit check here (as long as the address falls in the first page).
3691 return false;
3692 }
3693
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003694 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003695
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003696 HInstruction* GetArray() const { return InputAt(0); }
3697 HInstruction* GetIndex() const { return InputAt(1); }
3698
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003699 DECLARE_INSTRUCTION(ArrayGet);
3700
3701 private:
3702 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3703};
3704
3705class HArraySet : public HTemplateInstruction<3> {
3706 public:
3707 HArraySet(HInstruction* array,
3708 HInstruction* index,
3709 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003710 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003711 uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07003712 : HTemplateInstruction(SideEffects::ArrayWriteOfType(expected_component_type)),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003713 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003714 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003715 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3716 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003717 SetRawInputAt(0, array);
3718 SetRawInputAt(1, index);
3719 SetRawInputAt(2, value);
3720 }
3721
Calin Juravle77520bc2015-01-12 18:45:46 +00003722 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003723 // We currently always call a runtime method to catch array store
3724 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003725 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003726 }
3727
Mingyao Yang81014cb2015-06-02 03:16:27 -07003728 // Can throw ArrayStoreException.
3729 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3730
Calin Juravle641547a2015-04-21 22:08:51 +01003731 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3732 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003733 // TODO: Same as for ArrayGet.
3734 return false;
3735 }
3736
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003737 void ClearNeedsTypeCheck() {
3738 needs_type_check_ = false;
3739 }
3740
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003741 void ClearValueCanBeNull() {
3742 value_can_be_null_ = false;
3743 }
3744
3745 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003746 bool NeedsTypeCheck() const { return needs_type_check_; }
3747
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003748 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003749
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003750 HInstruction* GetArray() const { return InputAt(0); }
3751 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003752 HInstruction* GetValue() const { return InputAt(2); }
3753
3754 Primitive::Type GetComponentType() const {
3755 // The Dex format does not type floating point index operations. Since the
3756 // `expected_component_type_` is set during building and can therefore not
3757 // be correct, we also check what is the value type. If it is a floating
3758 // point type, we must use that type.
3759 Primitive::Type value_type = GetValue()->GetType();
3760 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3761 ? value_type
3762 : expected_component_type_;
3763 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003764
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003765 DECLARE_INSTRUCTION(ArraySet);
3766
3767 private:
3768 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003769 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003770 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003771 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003772
3773 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3774};
3775
3776class HArrayLength : public HExpression<1> {
3777 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003778 explicit HArrayLength(HInstruction* array)
3779 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3780 // Note that arrays do not change length, so the instruction does not
3781 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003782 SetRawInputAt(0, array);
3783 }
3784
Calin Juravle77520bc2015-01-12 18:45:46 +00003785 bool CanBeMoved() const OVERRIDE { return true; }
3786 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003787 UNUSED(other);
3788 return true;
3789 }
Calin Juravle641547a2015-04-21 22:08:51 +01003790 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3791 return obj == InputAt(0);
3792 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003793
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003794 DECLARE_INSTRUCTION(ArrayLength);
3795
3796 private:
3797 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3798};
3799
3800class HBoundsCheck : public HExpression<2> {
3801 public:
3802 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003803 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003804 DCHECK(index->GetType() == Primitive::kPrimInt);
3805 SetRawInputAt(0, index);
3806 SetRawInputAt(1, length);
3807 }
3808
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003809 bool CanBeMoved() const OVERRIDE { return true; }
3810 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003811 UNUSED(other);
3812 return true;
3813 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003814
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003815 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003816
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003817 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003818
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003819 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003820
3821 DECLARE_INSTRUCTION(BoundsCheck);
3822
3823 private:
3824 const uint32_t dex_pc_;
3825
3826 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3827};
3828
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003829/**
3830 * Some DEX instructions are folded into multiple HInstructions that need
3831 * to stay live until the last HInstruction. This class
3832 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003833 * HInstruction stays live. `index` represents the stack location index of the
3834 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003835 */
3836class HTemporary : public HTemplateInstruction<0> {
3837 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003838 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003839
3840 size_t GetIndex() const { return index_; }
3841
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003842 Primitive::Type GetType() const OVERRIDE {
3843 // The previous instruction is the one that will be stored in the temporary location.
3844 DCHECK(GetPrevious() != nullptr);
3845 return GetPrevious()->GetType();
3846 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003847
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003848 DECLARE_INSTRUCTION(Temporary);
3849
3850 private:
3851 const size_t index_;
3852
3853 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3854};
3855
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003856class HSuspendCheck : public HTemplateInstruction<0> {
3857 public:
3858 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003859 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003860
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003861 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003862 return true;
3863 }
3864
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003865 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003866 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3867 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003868
3869 DECLARE_INSTRUCTION(SuspendCheck);
3870
3871 private:
3872 const uint32_t dex_pc_;
3873
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003874 // Only used for code generation, in order to share the same slow path between back edges
3875 // of a same loop.
3876 SlowPathCode* slow_path_;
3877
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003878 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3879};
3880
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003881/**
3882 * Instruction to load a Class object.
3883 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003884class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003885 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003886 HLoadClass(HCurrentMethod* current_method,
3887 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003888 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003889 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003890 uint32_t dex_pc)
3891 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3892 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003893 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003894 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003895 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003896 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00003897 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003898 SetRawInputAt(0, current_method);
3899 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003900
3901 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003902
3903 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3904 return other->AsLoadClass()->type_index_ == type_index_;
3905 }
3906
3907 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3908
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003909 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003910 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003911 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01003912 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003913
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003914 bool NeedsEnvironment() const OVERRIDE {
3915 // Will call runtime and load the class if the class is not loaded yet.
3916 // TODO: finer grain decision.
3917 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003918 }
3919
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003920 bool MustGenerateClinitCheck() const {
3921 return generate_clinit_check_;
3922 }
3923
Calin Juravle0ba218d2015-05-19 18:46:01 +01003924 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3925 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003926 }
3927
3928 bool CanCallRuntime() const {
3929 return MustGenerateClinitCheck() || !is_referrers_class_;
3930 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003931
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003932 bool CanThrow() const OVERRIDE {
3933 // May call runtime and and therefore can throw.
3934 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003935 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003936 }
3937
Calin Juravleacf735c2015-02-12 15:25:22 +00003938 ReferenceTypeInfo GetLoadedClassRTI() {
3939 return loaded_class_rti_;
3940 }
3941
3942 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3943 // Make sure we only set exact types (the loaded class should never be merged).
3944 DCHECK(rti.IsExact());
3945 loaded_class_rti_ = rti;
3946 }
3947
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003948 const DexFile& GetDexFile() { return dex_file_; }
3949
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003950 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3951
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003952 DECLARE_INSTRUCTION(LoadClass);
3953
3954 private:
3955 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003956 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003957 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003958 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003959 // Whether this instruction must generate the initialization check.
3960 // Used for code generation.
3961 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003962
Calin Juravleacf735c2015-02-12 15:25:22 +00003963 ReferenceTypeInfo loaded_class_rti_;
3964
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003965 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3966};
3967
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003968class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003969 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003970 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003971 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3972 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003973 dex_pc_(dex_pc) {
3974 SetRawInputAt(0, current_method);
3975 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003976
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003977 bool CanBeMoved() const OVERRIDE { return true; }
3978
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003979 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3980 return other->AsLoadString()->string_index_ == string_index_;
3981 }
3982
3983 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3984
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003985 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003986 uint32_t GetStringIndex() const { return string_index_; }
3987
3988 // TODO: Can we deopt or debug when we resolve a string?
3989 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003990 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003991
3992 DECLARE_INSTRUCTION(LoadString);
3993
3994 private:
3995 const uint32_t string_index_;
3996 const uint32_t dex_pc_;
3997
3998 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3999};
4000
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004001/**
4002 * Performs an initialization check on its Class object input.
4003 */
4004class HClinitCheck : public HExpression<1> {
4005 public:
4006 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004007 : HExpression(
4008 Primitive::kPrimNot,
Aart Bik34c3ba92015-07-20 14:08:59 -07004009 SideEffects::AllWrites()), // assume write on all fields/arrays
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004010 dex_pc_(dex_pc) {
4011 SetRawInputAt(0, constant);
4012 }
4013
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004014 bool CanBeMoved() const OVERRIDE { return true; }
4015 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4016 UNUSED(other);
4017 return true;
4018 }
4019
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004020 bool NeedsEnvironment() const OVERRIDE {
4021 // May call runtime to initialize the class.
4022 return true;
4023 }
4024
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004025 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004026
4027 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4028
4029 DECLARE_INSTRUCTION(ClinitCheck);
4030
4031 private:
4032 const uint32_t dex_pc_;
4033
4034 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4035};
4036
4037class HStaticFieldGet : public HExpression<1> {
4038 public:
4039 HStaticFieldGet(HInstruction* cls,
4040 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004041 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004042 bool is_volatile,
4043 uint32_t field_idx,
4044 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004045 : HExpression(
4046 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004047 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004048 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004049 SetRawInputAt(0, cls);
4050 }
4051
Calin Juravle52c48962014-12-16 17:02:57 +00004052
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004053 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004054
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004055 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004056 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4057 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004058 }
4059
4060 size_t ComputeHashCode() const OVERRIDE {
4061 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4062 }
4063
Calin Juravle52c48962014-12-16 17:02:57 +00004064 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004065 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4066 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004067 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004068
4069 DECLARE_INSTRUCTION(StaticFieldGet);
4070
4071 private:
4072 const FieldInfo field_info_;
4073
4074 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4075};
4076
4077class HStaticFieldSet : public HTemplateInstruction<2> {
4078 public:
4079 HStaticFieldSet(HInstruction* cls,
4080 HInstruction* value,
4081 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004082 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004083 bool is_volatile,
4084 uint32_t field_idx,
4085 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004086 : HTemplateInstruction(
4087 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004088 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004089 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004090 SetRawInputAt(0, cls);
4091 SetRawInputAt(1, value);
4092 }
4093
Calin Juravle52c48962014-12-16 17:02:57 +00004094 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004095 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4096 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004097 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004098
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004099 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004100 bool GetValueCanBeNull() const { return value_can_be_null_; }
4101 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004102
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004103 DECLARE_INSTRUCTION(StaticFieldSet);
4104
4105 private:
4106 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004107 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004108
4109 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4110};
4111
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004112// Implement the move-exception DEX instruction.
4113class HLoadException : public HExpression<0> {
4114 public:
4115 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4116
4117 DECLARE_INSTRUCTION(LoadException);
4118
4119 private:
4120 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4121};
4122
4123class HThrow : public HTemplateInstruction<1> {
4124 public:
4125 HThrow(HInstruction* exception, uint32_t dex_pc)
4126 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
4127 SetRawInputAt(0, exception);
4128 }
4129
4130 bool IsControlFlow() const OVERRIDE { return true; }
4131
4132 bool NeedsEnvironment() const OVERRIDE { return true; }
4133
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004134 bool CanThrow() const OVERRIDE { return true; }
4135
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004136 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004137
4138 DECLARE_INSTRUCTION(Throw);
4139
4140 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004141 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004142
4143 DISALLOW_COPY_AND_ASSIGN(HThrow);
4144};
4145
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004146class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004147 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004148 HInstanceOf(HInstruction* object,
4149 HLoadClass* constant,
4150 bool class_is_final,
4151 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004152 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
4153 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004154 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004155 dex_pc_(dex_pc) {
4156 SetRawInputAt(0, object);
4157 SetRawInputAt(1, constant);
4158 }
4159
4160 bool CanBeMoved() const OVERRIDE { return true; }
4161
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004162 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004163 return true;
4164 }
4165
4166 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004167 return false;
4168 }
4169
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004170 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004171
4172 bool IsClassFinal() const { return class_is_final_; }
4173
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004174 // Used only in code generation.
4175 bool MustDoNullCheck() const { return must_do_null_check_; }
4176 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4177
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004178 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004179
4180 private:
4181 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004182 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004183 const uint32_t dex_pc_;
4184
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004185 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4186};
4187
Calin Juravleb1498f62015-02-16 13:13:29 +00004188class HBoundType : public HExpression<1> {
4189 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004190 // Constructs an HBoundType with the given upper_bound.
4191 // Ensures that the upper_bound is valid.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004192 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004193 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004194 upper_bound_(upper_bound),
4195 upper_can_be_null_(upper_can_be_null),
4196 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004197 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004198 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004199 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004200 }
4201
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004202 // GetUpper* should only be used in reference type propagation.
4203 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4204 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004205
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004206 void SetCanBeNull(bool can_be_null) {
4207 DCHECK(upper_can_be_null_ || !can_be_null);
4208 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004209 }
4210
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004211 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4212
Calin Juravleb1498f62015-02-16 13:13:29 +00004213 DECLARE_INSTRUCTION(BoundType);
4214
4215 private:
4216 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004217 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4218 // It is used to bound the type in cases like:
4219 // if (x instanceof ClassX) {
4220 // // uper_bound_ will be ClassX
4221 // }
4222 const ReferenceTypeInfo upper_bound_;
4223 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4224 // is false then can_be_null_ cannot be true).
4225 const bool upper_can_be_null_;
4226 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004227
4228 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4229};
4230
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004231class HCheckCast : public HTemplateInstruction<2> {
4232 public:
4233 HCheckCast(HInstruction* object,
4234 HLoadClass* constant,
4235 bool class_is_final,
4236 uint32_t dex_pc)
4237 : HTemplateInstruction(SideEffects::None()),
4238 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004239 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004240 dex_pc_(dex_pc) {
4241 SetRawInputAt(0, object);
4242 SetRawInputAt(1, constant);
4243 }
4244
4245 bool CanBeMoved() const OVERRIDE { return true; }
4246
4247 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4248 return true;
4249 }
4250
4251 bool NeedsEnvironment() const OVERRIDE {
4252 // Instruction may throw a CheckCastError.
4253 return true;
4254 }
4255
4256 bool CanThrow() const OVERRIDE { return true; }
4257
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004258 bool MustDoNullCheck() const { return must_do_null_check_; }
4259 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4260
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004261 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004262
4263 bool IsClassFinal() const { return class_is_final_; }
4264
4265 DECLARE_INSTRUCTION(CheckCast);
4266
4267 private:
4268 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004269 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004270 const uint32_t dex_pc_;
4271
4272 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004273};
4274
Calin Juravle27df7582015-04-17 19:12:31 +01004275class HMemoryBarrier : public HTemplateInstruction<0> {
4276 public:
4277 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004278 : HTemplateInstruction(
4279 SideEffects::All()), // assume write/read on all fields/arrays
Calin Juravle27df7582015-04-17 19:12:31 +01004280 barrier_kind_(barrier_kind) {}
4281
4282 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4283
4284 DECLARE_INSTRUCTION(MemoryBarrier);
4285
4286 private:
4287 const MemBarrierKind barrier_kind_;
4288
4289 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4290};
4291
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004292class HMonitorOperation : public HTemplateInstruction<1> {
4293 public:
4294 enum OperationKind {
4295 kEnter,
4296 kExit,
4297 };
4298
4299 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004300 : HTemplateInstruction(SideEffects::All()), // assume write/read on all fields/arrays
4301 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004302 SetRawInputAt(0, object);
4303 }
4304
4305 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004306 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4307
4308 bool CanThrow() const OVERRIDE {
4309 // Verifier guarantees that monitor-exit cannot throw.
4310 // This is important because it allows the HGraphBuilder to remove
4311 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4312 return IsEnter();
4313 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004314
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004315 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004316
4317 bool IsEnter() const { return kind_ == kEnter; }
4318
4319 DECLARE_INSTRUCTION(MonitorOperation);
4320
Calin Juravle52c48962014-12-16 17:02:57 +00004321 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004322 const OperationKind kind_;
4323 const uint32_t dex_pc_;
4324
4325 private:
4326 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4327};
4328
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004329/**
4330 * A HInstruction used as a marker for the replacement of new + <init>
4331 * of a String to a call to a StringFactory. Only baseline will see
4332 * the node at code generation, where it will be be treated as null.
4333 * When compiling non-baseline, `HFakeString` instructions are being removed
4334 * in the instruction simplifier.
4335 */
4336class HFakeString : public HTemplateInstruction<0> {
4337 public:
4338 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4339
4340 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4341
4342 DECLARE_INSTRUCTION(FakeString);
4343
4344 private:
4345 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4346};
4347
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004348class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004349 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004350 MoveOperands(Location source,
4351 Location destination,
4352 Primitive::Type type,
4353 HInstruction* instruction)
4354 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004355
4356 Location GetSource() const { return source_; }
4357 Location GetDestination() const { return destination_; }
4358
4359 void SetSource(Location value) { source_ = value; }
4360 void SetDestination(Location value) { destination_ = value; }
4361
4362 // The parallel move resolver marks moves as "in-progress" by clearing the
4363 // destination (but not the source).
4364 Location MarkPending() {
4365 DCHECK(!IsPending());
4366 Location dest = destination_;
4367 destination_ = Location::NoLocation();
4368 return dest;
4369 }
4370
4371 void ClearPending(Location dest) {
4372 DCHECK(IsPending());
4373 destination_ = dest;
4374 }
4375
4376 bool IsPending() const {
4377 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4378 return destination_.IsInvalid() && !source_.IsInvalid();
4379 }
4380
4381 // True if this blocks a move from the given location.
4382 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004383 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004384 }
4385
4386 // A move is redundant if it's been eliminated, if its source and
4387 // destination are the same, or if its destination is unneeded.
4388 bool IsRedundant() const {
4389 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4390 }
4391
4392 // We clear both operands to indicate move that's been eliminated.
4393 void Eliminate() {
4394 source_ = destination_ = Location::NoLocation();
4395 }
4396
4397 bool IsEliminated() const {
4398 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4399 return source_.IsInvalid();
4400 }
4401
Alexey Frunze4dda3372015-06-01 18:31:49 -07004402 Primitive::Type GetType() const { return type_; }
4403
Nicolas Geoffray90218252015-04-15 11:56:51 +01004404 bool Is64BitMove() const {
4405 return Primitive::Is64BitType(type_);
4406 }
4407
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004408 HInstruction* GetInstruction() const { return instruction_; }
4409
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004410 private:
4411 Location source_;
4412 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004413 // The type this move is for.
4414 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004415 // The instruction this move is assocatied with. Null when this move is
4416 // for moving an input in the expected locations of user (including a phi user).
4417 // This is only used in debug mode, to ensure we do not connect interval siblings
4418 // in the same parallel move.
4419 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004420};
4421
4422static constexpr size_t kDefaultNumberOfMoves = 4;
4423
4424class HParallelMove : public HTemplateInstruction<0> {
4425 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004426 explicit HParallelMove(ArenaAllocator* arena)
4427 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004428
Nicolas Geoffray90218252015-04-15 11:56:51 +01004429 void AddMove(Location source,
4430 Location destination,
4431 Primitive::Type type,
4432 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004433 DCHECK(source.IsValid());
4434 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004435 if (kIsDebugBuild) {
4436 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004437 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004438 if (moves_.Get(i).GetInstruction() == instruction) {
4439 // Special case the situation where the move is for the spill slot
4440 // of the instruction.
4441 if ((GetPrevious() == instruction)
4442 || ((GetPrevious() == nullptr)
4443 && instruction->IsPhi()
4444 && instruction->GetBlock() == GetBlock())) {
4445 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4446 << "Doing parallel moves for the same instruction.";
4447 } else {
4448 DCHECK(false) << "Doing parallel moves for the same instruction.";
4449 }
4450 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004451 }
4452 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004453 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004454 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004455 << "Overlapped destination for two moves in a parallel move: "
4456 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4457 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004458 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004459 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004460 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004461 }
4462
4463 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004464 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004465 }
4466
4467 size_t NumMoves() const { return moves_.Size(); }
4468
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004469 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004470
4471 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004472 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004473
4474 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4475};
4476
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004477class HGraphVisitor : public ValueObject {
4478 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004479 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4480 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004481
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004482 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004483 virtual void VisitBasicBlock(HBasicBlock* block);
4484
Roland Levillain633021e2014-10-01 14:12:25 +01004485 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004486 void VisitInsertionOrder();
4487
Roland Levillain633021e2014-10-01 14:12:25 +01004488 // Visit the graph following dominator tree reverse post-order.
4489 void VisitReversePostOrder();
4490
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004491 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004492
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004493 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004494#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004495 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4496
4497 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4498
4499#undef DECLARE_VISIT_INSTRUCTION
4500
4501 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004502 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004503
4504 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4505};
4506
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004507class HGraphDelegateVisitor : public HGraphVisitor {
4508 public:
4509 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4510 virtual ~HGraphDelegateVisitor() {}
4511
4512 // Visit functions that delegate to to super class.
4513#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004514 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004515
4516 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4517
4518#undef DECLARE_VISIT_INSTRUCTION
4519
4520 private:
4521 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4522};
4523
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004524class HInsertionOrderIterator : public ValueObject {
4525 public:
4526 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4527
4528 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4529 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4530 void Advance() { ++index_; }
4531
4532 private:
4533 const HGraph& graph_;
4534 size_t index_;
4535
4536 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4537};
4538
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004539class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004540 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004541 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4542 // Check that reverse post order of the graph has been built.
4543 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4544 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004545
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004546 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4547 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004548 void Advance() { ++index_; }
4549
4550 private:
4551 const HGraph& graph_;
4552 size_t index_;
4553
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004554 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004555};
4556
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004557class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004558 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004559 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004560 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4561 // Check that reverse post order of the graph has been built.
4562 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4563 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004564
4565 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004566 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004567 void Advance() { --index_; }
4568
4569 private:
4570 const HGraph& graph_;
4571 size_t index_;
4572
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004573 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004574};
4575
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004576class HLinearPostOrderIterator : public ValueObject {
4577 public:
4578 explicit HLinearPostOrderIterator(const HGraph& graph)
4579 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4580
4581 bool Done() const { return index_ == 0; }
4582
4583 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4584
4585 void Advance() {
4586 --index_;
4587 DCHECK_GE(index_, 0U);
4588 }
4589
4590 private:
4591 const GrowableArray<HBasicBlock*>& order_;
4592 size_t index_;
4593
4594 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4595};
4596
4597class HLinearOrderIterator : public ValueObject {
4598 public:
4599 explicit HLinearOrderIterator(const HGraph& graph)
4600 : order_(graph.GetLinearOrder()), index_(0) {}
4601
4602 bool Done() const { return index_ == order_.Size(); }
4603 HBasicBlock* Current() const { return order_.Get(index_); }
4604 void Advance() { ++index_; }
4605
4606 private:
4607 const GrowableArray<HBasicBlock*>& order_;
4608 size_t index_;
4609
4610 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4611};
4612
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004613// Iterator over the blocks that art part of the loop. Includes blocks part
4614// of an inner loop. The order in which the blocks are iterated is on their
4615// block id.
4616class HBlocksInLoopIterator : public ValueObject {
4617 public:
4618 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4619 : blocks_in_loop_(info.GetBlocks()),
4620 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4621 index_(0) {
4622 if (!blocks_in_loop_.IsBitSet(index_)) {
4623 Advance();
4624 }
4625 }
4626
4627 bool Done() const { return index_ == blocks_.Size(); }
4628 HBasicBlock* Current() const { return blocks_.Get(index_); }
4629 void Advance() {
4630 ++index_;
4631 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4632 if (blocks_in_loop_.IsBitSet(index_)) {
4633 break;
4634 }
4635 }
4636 }
4637
4638 private:
4639 const BitVector& blocks_in_loop_;
4640 const GrowableArray<HBasicBlock*>& blocks_;
4641 size_t index_;
4642
4643 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4644};
4645
Mingyao Yang3584bce2015-05-19 16:01:59 -07004646// Iterator over the blocks that art part of the loop. Includes blocks part
4647// of an inner loop. The order in which the blocks are iterated is reverse
4648// post order.
4649class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4650 public:
4651 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4652 : blocks_in_loop_(info.GetBlocks()),
4653 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4654 index_(0) {
4655 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4656 Advance();
4657 }
4658 }
4659
4660 bool Done() const { return index_ == blocks_.Size(); }
4661 HBasicBlock* Current() const { return blocks_.Get(index_); }
4662 void Advance() {
4663 ++index_;
4664 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4665 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4666 break;
4667 }
4668 }
4669 }
4670
4671 private:
4672 const BitVector& blocks_in_loop_;
4673 const GrowableArray<HBasicBlock*>& blocks_;
4674 size_t index_;
4675
4676 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4677};
4678
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004679inline int64_t Int64FromConstant(HConstant* constant) {
4680 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4681 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4682 : constant->AsLongConstant()->GetValue();
4683}
4684
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004685} // namespace art
4686
4687#endif // ART_COMPILER_OPTIMIZING_NODES_H_