blob: c4f64b4ebeb1dc54ca22b217c34c3443e2954c70 [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
Roland Levillain9867bc72015-08-05 10:21:34 +010020#include <type_traits>
21
David Brazdil8d5b8b22015-03-24 10:51:52 +000022#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080023#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010024#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000025#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026#include "handle.h"
27#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000028#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010029#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010031#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070032#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000033#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000034#include "utils/growable_array.h"
35
36namespace art {
37
David Brazdil1abb4192015-02-17 18:33:36 +000038class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010040class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000041class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010042class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010043class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000044class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000045class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000046class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000047class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000048class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000049class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000051class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010052class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010053class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010054class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010055class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000056class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010057class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000058class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000059
60static const int kDefaultNumberOfBlocks = 8;
61static const int kDefaultNumberOfSuccessors = 2;
62static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010063static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010064static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000065static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000066
Calin Juravle9aec02f2014-11-18 23:06:35 +000067static constexpr uint32_t kMaxIntShiftValue = 0x1f;
68static constexpr uint64_t kMaxLongShiftValue = 0x3f;
69
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010070static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
71
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010072static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
73
Dave Allison20dfc792014-06-16 20:44:29 -070074enum IfCondition {
75 kCondEQ,
76 kCondNE,
77 kCondLT,
78 kCondLE,
79 kCondGT,
80 kCondGE,
81};
82
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010083class HInstructionList {
84 public:
85 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
86
87 void AddInstruction(HInstruction* instruction);
88 void RemoveInstruction(HInstruction* instruction);
89
David Brazdilc3d743f2015-04-22 13:40:50 +010090 // Insert `instruction` before/after an existing instruction `cursor`.
91 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
92 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
93
Roland Levillain6b469232014-09-25 10:10:38 +010094 // Return true if this list contains `instruction`.
95 bool Contains(HInstruction* instruction) const;
96
Roland Levillainccc07a92014-09-16 14:48:16 +010097 // Return true if `instruction1` is found before `instruction2` in
98 // this instruction list and false otherwise. Abort if none
99 // of these instructions is found.
100 bool FoundBefore(const HInstruction* instruction1,
101 const HInstruction* instruction2) const;
102
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000103 bool IsEmpty() const { return first_instruction_ == nullptr; }
104 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
105
106 // Update the block of all instructions to be `block`.
107 void SetBlockOfInstructions(HBasicBlock* block) const;
108
109 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
110 void Add(const HInstructionList& instruction_list);
111
David Brazdil2d7352b2015-04-20 14:52:42 +0100112 // Return the number of instructions in the list. This is an expensive operation.
113 size_t CountSize() const;
114
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100115 private:
116 HInstruction* first_instruction_;
117 HInstruction* last_instruction_;
118
119 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000120 friend class HGraph;
121 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100122 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100123 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100124
125 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
126};
127
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000128// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700129class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000130 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100131 HGraph(ArenaAllocator* arena,
132 const DexFile& dex_file,
133 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100134 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100136 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100137 bool debuggable = false,
138 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000139 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000140 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100141 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100142 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700143 entry_block_(nullptr),
144 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100145 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100146 number_of_vregs_(0),
147 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000148 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400149 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000150 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000151 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100152 dex_file_(dex_file),
153 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100154 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100155 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100156 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700157 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000158 cached_null_constant_(nullptr),
159 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000160 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
161 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100162 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
163 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000164
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000165 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100166 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100167 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000168
David Brazdil69ba7b72015-06-23 18:27:30 +0100169 bool IsInSsaForm() const { return in_ssa_form_; }
170
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000171 HBasicBlock* GetEntryBlock() const { return entry_block_; }
172 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100173 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000174
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000175 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
176 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000177
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000178 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100179
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000180 // Try building the SSA form of this graph, with dominance computation and loop
181 // recognition. Returns whether it was successful in doing all these steps.
182 bool TryBuildingSsa() {
183 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000184 // The SSA builder requires loops to all be natural. Specifically, the dead phi
185 // elimination phase checks the consistency of the graph when doing a post-order
186 // visit for eliminating dead phis: a dead phi can only have loop header phi
187 // users remaining when being visited.
188 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100189 // Precompute per-block try membership before entering the SSA builder,
190 // which needs the information to build catch block phis from values of
191 // locals at throwing instructions inside try blocks.
192 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000193 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100194 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000195 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000196 }
197
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100198 void ComputeDominanceInformation();
199 void ClearDominanceInformation();
200
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000201 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000202 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100203 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100204 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000205
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000206 // Analyze all natural loops in this graph. Returns false if one
207 // loop is not natural, that is the header does not dominate the
208 // back edge.
209 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100210
David Brazdilffee3d32015-07-06 11:48:53 +0100211 // Iterate over blocks to compute try block membership. Needs reverse post
212 // order and loop information.
213 void ComputeTryBlockInformation();
214
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000216 // Returns the instruction used to replace the invoke expression or null if the
217 // invoke is for a void method.
218 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000219
Mingyao Yang3584bce2015-05-19 16:01:59 -0700220 // Need to add a couple of blocks to test if the loop body is entered and
221 // put deoptimization instructions, etc.
222 void TransformLoopHeaderForBCE(HBasicBlock* header);
223
David Brazdil2d7352b2015-04-20 14:52:42 +0100224 // Removes `block` from the graph.
225 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000226
David Brazdilfc6a86a2015-06-26 10:33:45 +0000227 // Splits the edge between `block` and `successor` while preserving the
228 // indices in the predecessor/successor lists. If there are multiple edges
229 // between the blocks, the lowest indices are used.
230 // Returns the new block which is empty and has the same dex pc as `successor`.
231 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
232
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100233 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
234 void SimplifyLoop(HBasicBlock* header);
235
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000236 int32_t GetNextInstructionId() {
237 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000238 return current_instruction_id_++;
239 }
240
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000241 int32_t GetCurrentInstructionId() const {
242 return current_instruction_id_;
243 }
244
245 void SetCurrentInstructionId(int32_t id) {
246 current_instruction_id_ = id;
247 }
248
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100249 uint16_t GetMaximumNumberOfOutVRegs() const {
250 return maximum_number_of_out_vregs_;
251 }
252
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000253 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
254 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100255 }
256
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100257 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
258 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
259 }
260
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000261 void UpdateTemporariesVRegSlots(size_t slots) {
262 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100263 }
264
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000265 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100266 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000267 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100268 }
269
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100270 void SetNumberOfVRegs(uint16_t number_of_vregs) {
271 number_of_vregs_ = number_of_vregs;
272 }
273
274 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100275 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100276 return number_of_vregs_;
277 }
278
279 void SetNumberOfInVRegs(uint16_t value) {
280 number_of_in_vregs_ = value;
281 }
282
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100283 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100284 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100285 return number_of_vregs_ - number_of_in_vregs_;
286 }
287
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100288 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
289 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100290 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100291
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100292 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
293 return linear_order_;
294 }
295
Mark Mendell1152c922015-04-24 17:06:35 -0400296 bool HasBoundsChecks() const {
297 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800298 }
299
Mark Mendell1152c922015-04-24 17:06:35 -0400300 void SetHasBoundsChecks(bool value) {
301 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800302 }
303
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100304 bool ShouldGenerateConstructorBarrier() const {
305 return should_generate_constructor_barrier_;
306 }
307
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000308 bool IsDebuggable() const { return debuggable_; }
309
David Brazdil8d5b8b22015-03-24 10:51:52 +0000310 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000311 // already, it is created and inserted into the graph. This method is only for
312 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000313 HConstant* GetConstant(Primitive::Type type, int64_t value);
Calin Juravle2e768302015-07-28 14:41:11 +0000314
315 // TODO: This is problematic for the consistency of reference type propagation
316 // because it can be created anytime after the pass and thus it will be left
317 // with an invalid type.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000318 HNullConstant* GetNullConstant();
Calin Juravle2e768302015-07-28 14:41:11 +0000319
David Brazdil8d5b8b22015-03-24 10:51:52 +0000320 HIntConstant* GetIntConstant(int32_t value) {
321 return CreateConstant(value, &cached_int_constants_);
322 }
323 HLongConstant* GetLongConstant(int64_t value) {
324 return CreateConstant(value, &cached_long_constants_);
325 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000326 HFloatConstant* GetFloatConstant(float value) {
327 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
328 }
329 HDoubleConstant* GetDoubleConstant(double value) {
330 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
331 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000332
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100333 HCurrentMethod* GetCurrentMethod();
334
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000335 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100336
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100337 const DexFile& GetDexFile() const {
338 return dex_file_;
339 }
340
341 uint32_t GetMethodIdx() const {
342 return method_idx_;
343 }
344
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100345 InvokeType GetInvokeType() const {
346 return invoke_type_;
347 }
348
Mark Mendellc4701932015-04-10 13:18:51 -0400349 InstructionSet GetInstructionSet() const {
350 return instruction_set_;
351 }
352
David Brazdil2d7352b2015-04-20 14:52:42 +0100353 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000354 void VisitBlockForDominatorTree(HBasicBlock* block,
355 HBasicBlock* predecessor,
356 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100357 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000358 void VisitBlockForBackEdges(HBasicBlock* block,
359 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100360 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000361 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100362 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000363
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000364 template <class InstructionType, typename ValueType>
365 InstructionType* CreateConstant(ValueType value,
366 ArenaSafeMap<ValueType, InstructionType*>* cache) {
367 // Try to find an existing constant of the given value.
368 InstructionType* constant = nullptr;
369 auto cached_constant = cache->find(value);
370 if (cached_constant != cache->end()) {
371 constant = cached_constant->second;
372 }
373
374 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100375 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000376 if (constant == nullptr || constant->GetBlock() == nullptr) {
377 constant = new (arena_) InstructionType(value);
378 cache->Overwrite(value, constant);
379 InsertConstant(constant);
380 }
381 return constant;
382 }
383
David Brazdil8d5b8b22015-03-24 10:51:52 +0000384 void InsertConstant(HConstant* instruction);
385
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000386 // Cache a float constant into the graph. This method should only be
387 // called by the SsaBuilder when creating "equivalent" instructions.
388 void CacheFloatConstant(HFloatConstant* constant);
389
390 // See CacheFloatConstant comment.
391 void CacheDoubleConstant(HDoubleConstant* constant);
392
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000393 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000394
395 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000396 GrowableArray<HBasicBlock*> blocks_;
397
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100398 // List of blocks to perform a reverse post order tree traversal.
399 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000400
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100401 // List of blocks to perform a linear order tree traversal.
402 GrowableArray<HBasicBlock*> linear_order_;
403
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000404 HBasicBlock* entry_block_;
405 HBasicBlock* exit_block_;
406
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100407 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100408 uint16_t maximum_number_of_out_vregs_;
409
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100410 // The number of virtual registers in this method. Contains the parameters.
411 uint16_t number_of_vregs_;
412
413 // The number of virtual registers used by parameters of this method.
414 uint16_t number_of_in_vregs_;
415
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000416 // Number of vreg size slots that the temporaries use (used in baseline compiler).
417 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100418
Mark Mendell1152c922015-04-24 17:06:35 -0400419 // Has bounds checks. We can totally skip BCE if it's false.
420 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800421
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000422 // Indicates whether the graph should be compiled in a way that
423 // ensures full debuggability. If false, we can apply more
424 // aggressive optimizations that may limit the level of debugging.
425 const bool debuggable_;
426
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000427 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000428 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000429
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100430 // The dex file from which the method is from.
431 const DexFile& dex_file_;
432
433 // The method index in the dex file.
434 const uint32_t method_idx_;
435
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100436 // If inlined, this encodes how the callee is being invoked.
437 const InvokeType invoke_type_;
438
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100439 // Whether the graph has been transformed to SSA form. Only used
440 // in debug mode to ensure we are not using properties only valid
441 // for non-SSA form (like the number of temporaries).
442 bool in_ssa_form_;
443
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100444 const bool should_generate_constructor_barrier_;
445
Mathieu Chartiere401d142015-04-22 13:56:20 -0700446 const InstructionSet instruction_set_;
447
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000448 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000449 HNullConstant* cached_null_constant_;
450 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000451 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000452 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000453 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000454
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100455 HCurrentMethod* cached_current_method_;
456
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000457 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100458 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000459 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000460 DISALLOW_COPY_AND_ASSIGN(HGraph);
461};
462
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700463class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000464 public:
465 HLoopInformation(HBasicBlock* header, HGraph* graph)
466 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100467 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100468 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100469 // Make bit vector growable, as the number of blocks may change.
470 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100471
472 HBasicBlock* GetHeader() const {
473 return header_;
474 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000475
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000476 void SetHeader(HBasicBlock* block) {
477 header_ = block;
478 }
479
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100480 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
481 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
482 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
483
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000484 void AddBackEdge(HBasicBlock* back_edge) {
485 back_edges_.Add(back_edge);
486 }
487
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100488 void RemoveBackEdge(HBasicBlock* back_edge) {
489 back_edges_.Delete(back_edge);
490 }
491
David Brazdil46e2a392015-03-16 17:31:52 +0000492 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100493 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000494 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100495 }
496 return false;
497 }
498
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000499 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000500 return back_edges_.Size();
501 }
502
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100503 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100504
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100505 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
506 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100507 }
508
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100509 // Returns the lifetime position of the back edge that has the
510 // greatest lifetime position.
511 size_t GetLifetimeEnd() const;
512
513 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
514 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
515 if (back_edges_.Get(i) == existing) {
516 back_edges_.Put(i, new_back_edge);
517 return;
518 }
519 }
520 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100521 }
522
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100523 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100524 // that is the header dominates the back edge.
525 bool Populate();
526
David Brazdila4b8c212015-05-07 09:59:30 +0100527 // Reanalyzes the loop by removing loop info from its blocks and re-running
528 // Populate(). If there are no back edges left, the loop info is completely
529 // removed as well as its SuspendCheck instruction. It must be run on nested
530 // inner loops first.
531 void Update();
532
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100533 // Returns whether this loop information contains `block`.
534 // Note that this loop information *must* be populated before entering this function.
535 bool Contains(const HBasicBlock& block) const;
536
537 // Returns whether this loop information is an inner loop of `other`.
538 // Note that `other` *must* be populated before entering this function.
539 bool IsIn(const HLoopInformation& other) const;
540
541 const ArenaBitVector& GetBlocks() const { return blocks_; }
542
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000543 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000544 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000545
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000546 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100547 // Internal recursive implementation of `Populate`.
548 void PopulateRecursive(HBasicBlock* block);
549
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000550 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100551 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000552 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100553 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000554
555 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
556};
557
David Brazdilec16f792015-08-19 15:04:01 +0100558// Stores try/catch information for basic blocks.
559// Note that HGraph is constructed so that catch blocks cannot simultaneously
560// be try blocks.
561class TryCatchInformation : public ArenaObject<kArenaAllocMisc> {
562 public:
563 // Try block information constructor.
564 explicit TryCatchInformation(const HTryBoundary& try_entry)
565 : try_entry_(&try_entry),
566 catch_dex_file_(nullptr),
567 catch_type_index_(DexFile::kDexNoIndex16) {
568 DCHECK(try_entry_ != nullptr);
569 }
570
571 // Catch block information constructor.
572 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
573 : try_entry_(nullptr),
574 catch_dex_file_(&dex_file),
575 catch_type_index_(catch_type_index) {}
576
577 bool IsTryBlock() const { return try_entry_ != nullptr; }
578
579 const HTryBoundary& GetTryEntry() const {
580 DCHECK(IsTryBlock());
581 return *try_entry_;
582 }
583
584 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
585
586 bool IsCatchAllTypeIndex() const {
587 DCHECK(IsCatchBlock());
588 return catch_type_index_ == DexFile::kDexNoIndex16;
589 }
590
591 uint16_t GetCatchTypeIndex() const {
592 DCHECK(IsCatchBlock());
593 return catch_type_index_;
594 }
595
596 const DexFile& GetCatchDexFile() const {
597 DCHECK(IsCatchBlock());
598 return *catch_dex_file_;
599 }
600
601 private:
602 // One of possibly several TryBoundary instructions entering the block's try.
603 // Only set for try blocks.
604 const HTryBoundary* try_entry_;
605
606 // Exception type information. Only set for catch blocks.
607 const DexFile* catch_dex_file_;
608 const uint16_t catch_type_index_;
609};
610
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100611static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100612static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100613
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000614// A block in a method. Contains the list of instructions represented
615// as a double linked list. Each block knows its predecessors and
616// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100617
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700618class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000619 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100620 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000621 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000622 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
623 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000624 loop_information_(nullptr),
625 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100626 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100627 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100628 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100629 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000630 lifetime_end_(kNoLifetime),
David Brazdilec16f792015-08-19 15:04:01 +0100631 try_catch_information_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100633 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
634 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000635 }
636
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100637 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
638 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000639 }
640
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100641 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
642 return dominated_blocks_;
643 }
644
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100645 bool IsEntryBlock() const {
646 return graph_->GetEntryBlock() == this;
647 }
648
649 bool IsExitBlock() const {
650 return graph_->GetExitBlock() == this;
651 }
652
David Brazdil46e2a392015-03-16 17:31:52 +0000653 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000654 bool IsSingleTryBoundary() const;
655
656 // Returns true if this block emits nothing but a jump.
657 bool IsSingleJump() const {
658 HLoopInformation* loop_info = GetLoopInformation();
659 return (IsSingleGoto() || IsSingleTryBoundary())
660 // Back edges generate a suspend check.
661 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
662 }
David Brazdil46e2a392015-03-16 17:31:52 +0000663
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000664 void AddBackEdge(HBasicBlock* back_edge) {
665 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000666 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000667 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100668 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000669 loop_information_->AddBackEdge(back_edge);
670 }
671
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000672 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000673 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000674
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000675 int GetBlockId() const { return block_id_; }
676 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000677
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000678 HBasicBlock* GetDominator() const { return dominator_; }
679 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100680 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100681 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000682 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
683 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
684 if (dominated_blocks_.Get(i) == existing) {
685 dominated_blocks_.Put(i, new_block);
686 return;
687 }
688 }
689 LOG(FATAL) << "Unreachable";
690 UNREACHABLE();
691 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100692 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000693
694 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100695 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000696 }
697
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100698 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
699 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100700 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100701 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100702 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
703 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000704
705 void AddSuccessor(HBasicBlock* block) {
706 successors_.Add(block);
707 block->predecessors_.Add(this);
708 }
709
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100710 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
711 size_t successor_index = GetSuccessorIndexOf(existing);
712 DCHECK_NE(successor_index, static_cast<size_t>(-1));
713 existing->RemovePredecessor(this);
714 new_block->predecessors_.Add(this);
715 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000716 }
717
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000718 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
719 size_t predecessor_index = GetPredecessorIndexOf(existing);
720 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
721 existing->RemoveSuccessor(this);
722 new_block->successors_.Add(this);
723 predecessors_.Put(predecessor_index, new_block);
724 }
725
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100726 // Insert `this` between `predecessor` and `successor. This method
727 // preserves the indicies, and will update the first edge found between
728 // `predecessor` and `successor`.
729 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
730 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
731 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
732 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
733 DCHECK_NE(successor_index, static_cast<size_t>(-1));
734 successor->predecessors_.Put(predecessor_index, this);
735 predecessor->successors_.Put(successor_index, this);
736 successors_.Add(successor);
737 predecessors_.Add(predecessor);
738 }
739
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100740 void RemovePredecessor(HBasicBlock* block) {
741 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100742 }
743
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000744 void RemoveSuccessor(HBasicBlock* block) {
745 successors_.Delete(block);
746 }
747
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100748 void ClearAllPredecessors() {
749 predecessors_.Reset();
750 }
751
752 void AddPredecessor(HBasicBlock* block) {
753 predecessors_.Add(block);
754 block->successors_.Add(this);
755 }
756
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100757 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100758 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100759 HBasicBlock* temp = predecessors_.Get(0);
760 predecessors_.Put(0, predecessors_.Get(1));
761 predecessors_.Put(1, temp);
762 }
763
David Brazdil769c9e52015-04-27 13:54:09 +0100764 void SwapSuccessors() {
765 DCHECK_EQ(successors_.Size(), 2u);
766 HBasicBlock* temp = successors_.Get(0);
767 successors_.Put(0, successors_.Get(1));
768 successors_.Put(1, temp);
769 }
770
David Brazdilfc6a86a2015-06-26 10:33:45 +0000771 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100772 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
773 if (predecessors_.Get(i) == predecessor) {
774 return i;
775 }
776 }
777 return -1;
778 }
779
David Brazdilfc6a86a2015-06-26 10:33:45 +0000780 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100781 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
782 if (successors_.Get(i) == successor) {
783 return i;
784 }
785 }
786 return -1;
787 }
788
David Brazdilfc6a86a2015-06-26 10:33:45 +0000789 HBasicBlock* GetSinglePredecessor() const {
790 DCHECK_EQ(GetPredecessors().Size(), 1u);
791 return GetPredecessors().Get(0);
792 }
793
794 HBasicBlock* GetSingleSuccessor() const {
795 DCHECK_EQ(GetSuccessors().Size(), 1u);
796 return GetSuccessors().Get(0);
797 }
798
799 // Returns whether the first occurrence of `predecessor` in the list of
800 // predecessors is at index `idx`.
801 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
802 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
803 return GetPredecessorIndexOf(predecessor) == idx;
804 }
805
David Brazdilffee3d32015-07-06 11:48:53 +0100806 // Returns the number of non-exceptional successors. SsaChecker ensures that
807 // these are stored at the beginning of the successor list.
808 size_t NumberOfNormalSuccessors() const {
809 return EndsWithTryBoundary() ? 1 : GetSuccessors().Size();
810 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000811
812 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100813 // created, latter block. Note that this method will add the block to the
814 // graph, create a Goto at the end of the former block and will create an edge
815 // between the blocks. It will not, however, update the reverse post order or
816 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000817 HBasicBlock* SplitBefore(HInstruction* cursor);
818
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000819 // Split the block into two blocks just after `cursor`. Returns the newly
820 // created block. Note that this method just updates raw block information,
821 // like predecessors, successors, dominators, and instruction list. It does not
822 // update the graph, reverse post order, loop information, nor make sure the
823 // blocks are consistent (for example ending with a control flow instruction).
824 HBasicBlock* SplitAfter(HInstruction* cursor);
825
826 // Merge `other` at the end of `this`. Successors and dominated blocks of
827 // `other` are changed to be successors and dominated blocks of `this`. Note
828 // that this method does not update the graph, reverse post order, loop
829 // information, nor make sure the blocks are consistent (for example ending
830 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100831 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000832
833 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
834 // of `this` are moved to `other`.
835 // Note that this method does not update the graph, reverse post order, loop
836 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000837 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000838 void ReplaceWith(HBasicBlock* other);
839
David Brazdil2d7352b2015-04-20 14:52:42 +0100840 // Merge `other` at the end of `this`. This method updates loops, reverse post
841 // order, links to predecessors, successors, dominators and deletes the block
842 // from the graph. The two blocks must be successive, i.e. `this` the only
843 // predecessor of `other` and vice versa.
844 void MergeWith(HBasicBlock* other);
845
846 // Disconnects `this` from all its predecessors, successors and dominator,
847 // removes it from all loops it is included in and eventually from the graph.
848 // The block must not dominate any other block. Predecessors and successors
849 // are safely updated.
850 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000851
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000852 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100853 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100854 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100855 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100856 // Replace instruction `initial` with `replacement` within this block.
857 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
858 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100859 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100860 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000861 // RemoveInstruction and RemovePhi delete a given instruction from the respective
862 // instruction list. With 'ensure_safety' set to true, it verifies that the
863 // instruction is not in use and removes it from the use lists of its inputs.
864 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
865 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100866 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100867
868 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100869 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100870 }
871
Roland Levillain6b879dd2014-09-22 17:13:44 +0100872 bool IsLoopPreHeaderFirstPredecessor() const {
873 DCHECK(IsLoopHeader());
874 DCHECK(!GetPredecessors().IsEmpty());
875 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
876 }
877
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100878 HLoopInformation* GetLoopInformation() const {
879 return loop_information_;
880 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000881
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000882 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100883 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000884 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100885 void SetInLoop(HLoopInformation* info) {
886 if (IsLoopHeader()) {
887 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100888 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100889 loop_information_ = info;
890 } else if (loop_information_->Contains(*info->GetHeader())) {
891 // Block is currently part of an outer loop. Make it part of this inner loop.
892 // Note that a non loop header having a loop information means this loop information
893 // has already been populated
894 loop_information_ = info;
895 } else {
896 // Block is part of an inner loop. Do not update the loop information.
897 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
898 // at this point, because this method is being called while populating `info`.
899 }
900 }
901
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000902 // Raw update of the loop information.
903 void SetLoopInformation(HLoopInformation* info) {
904 loop_information_ = info;
905 }
906
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100907 bool IsInLoop() const { return loop_information_ != nullptr; }
908
David Brazdilec16f792015-08-19 15:04:01 +0100909 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
910
911 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
912 try_catch_information_ = try_catch_information;
913 }
914
915 bool IsTryBlock() const {
916 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
917 }
918
919 bool IsCatchBlock() const {
920 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
921 }
David Brazdilffee3d32015-07-06 11:48:53 +0100922
923 // Returns the try entry that this block's successors should have. They will
924 // be in the same try, unless the block ends in a try boundary. In that case,
925 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100926 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100927
David Brazdila4b8c212015-05-07 09:59:30 +0100928 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100929 bool Dominates(HBasicBlock* block) const;
930
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100931 size_t GetLifetimeStart() const { return lifetime_start_; }
932 size_t GetLifetimeEnd() const { return lifetime_end_; }
933
934 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
935 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
936
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100937 uint32_t GetDexPc() const { return dex_pc_; }
938
David Brazdil8d5b8b22015-03-24 10:51:52 +0000939 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000940 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100941 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000942 bool HasSinglePhi() const;
943
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000944 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000945 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000946 GrowableArray<HBasicBlock*> predecessors_;
947 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100948 HInstructionList instructions_;
949 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000950 HLoopInformation* loop_information_;
951 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100952 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000953 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100954 // The dex program counter of the first instruction of this block.
955 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100956 size_t lifetime_start_;
957 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100958 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100959
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000960 friend class HGraph;
961 friend class HInstruction;
962
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000963 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
964};
965
David Brazdilb2bd1c52015-03-25 11:17:37 +0000966// Iterates over the LoopInformation of all loops which contain 'block'
967// from the innermost to the outermost.
968class HLoopInformationOutwardIterator : public ValueObject {
969 public:
970 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
971 : current_(block.GetLoopInformation()) {}
972
973 bool Done() const { return current_ == nullptr; }
974
975 void Advance() {
976 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100977 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000978 }
979
980 HLoopInformation* Current() const {
981 DCHECK(!Done());
982 return current_;
983 }
984
985 private:
986 HLoopInformation* current_;
987
988 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
989};
990
Alexandre Ramesef20f712015-06-09 10:29:30 +0100991#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100992 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000993 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000994 M(ArrayGet, Instruction) \
995 M(ArrayLength, Instruction) \
996 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100997 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000998 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000999 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001000 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001001 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001002 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001003 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001004 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001005 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001006 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001007 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001008 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001009 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001010 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001011 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001012 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001013 M(FloatConstant, Constant) \
1014 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001015 M(GreaterThan, Condition) \
1016 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001017 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001018 M(InstanceFieldGet, Instruction) \
1019 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001020 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001021 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001022 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001023 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001024 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001025 M(LessThan, Condition) \
1026 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001027 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001028 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001029 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001030 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001031 M(Local, Instruction) \
1032 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001033 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001034 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001035 M(Mul, BinaryOperation) \
1036 M(Neg, UnaryOperation) \
1037 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001038 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001039 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001040 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001041 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001042 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001043 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001044 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001045 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001046 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001047 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001048 M(Return, Instruction) \
1049 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001050 M(Shl, BinaryOperation) \
1051 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001052 M(StaticFieldGet, Instruction) \
1053 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001054 M(StoreLocal, Instruction) \
1055 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001056 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001057 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001058 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001059 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001060 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001061 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001062 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001063
Alexandre Ramesef20f712015-06-09 10:29:30 +01001064#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1065
1066#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1067
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001068#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1069
Alexandre Ramesef20f712015-06-09 10:29:30 +01001070#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1071
1072#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1073
1074#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1075 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1076 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1077 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001078 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001079 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1080 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1081
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001082#define FOR_EACH_INSTRUCTION(M) \
1083 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1084 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001085 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001086 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001087 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001088
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001089#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001090FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1091#undef FORWARD_DECLARATION
1092
Roland Levillainccc07a92014-09-16 14:48:16 +01001093#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001094 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1095 const char* DebugName() const OVERRIDE { return #type; } \
1096 const H##type* As##type() const OVERRIDE { return this; } \
1097 H##type* As##type() OVERRIDE { return this; } \
1098 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001099 return other->Is##type(); \
1100 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001101 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001102
David Brazdiled596192015-01-23 10:39:45 +00001103template <typename T> class HUseList;
1104
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001105template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001106class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001107 public:
David Brazdiled596192015-01-23 10:39:45 +00001108 HUseListNode* GetPrevious() const { return prev_; }
1109 HUseListNode* GetNext() const { return next_; }
1110 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001111 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001112 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001113
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001114 private:
David Brazdiled596192015-01-23 10:39:45 +00001115 HUseListNode(T user, size_t index)
1116 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1117
1118 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001119 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001120 HUseListNode<T>* prev_;
1121 HUseListNode<T>* next_;
1122
1123 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001124
1125 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1126};
1127
David Brazdiled596192015-01-23 10:39:45 +00001128template <typename T>
1129class HUseList : public ValueObject {
1130 public:
1131 HUseList() : first_(nullptr) {}
1132
1133 void Clear() {
1134 first_ = nullptr;
1135 }
1136
1137 // Adds a new entry at the beginning of the use list and returns
1138 // the newly created node.
1139 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001140 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001141 if (IsEmpty()) {
1142 first_ = new_node;
1143 } else {
1144 first_->prev_ = new_node;
1145 new_node->next_ = first_;
1146 first_ = new_node;
1147 }
1148 return new_node;
1149 }
1150
1151 HUseListNode<T>* GetFirst() const {
1152 return first_;
1153 }
1154
1155 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001156 DCHECK(node != nullptr);
1157 DCHECK(Contains(node));
1158
David Brazdiled596192015-01-23 10:39:45 +00001159 if (node->prev_ != nullptr) {
1160 node->prev_->next_ = node->next_;
1161 }
1162 if (node->next_ != nullptr) {
1163 node->next_->prev_ = node->prev_;
1164 }
1165 if (node == first_) {
1166 first_ = node->next_;
1167 }
1168 }
1169
David Brazdil1abb4192015-02-17 18:33:36 +00001170 bool Contains(const HUseListNode<T>* node) const {
1171 if (node == nullptr) {
1172 return false;
1173 }
1174 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1175 if (current == node) {
1176 return true;
1177 }
1178 }
1179 return false;
1180 }
1181
David Brazdiled596192015-01-23 10:39:45 +00001182 bool IsEmpty() const {
1183 return first_ == nullptr;
1184 }
1185
1186 bool HasOnlyOneUse() const {
1187 return first_ != nullptr && first_->next_ == nullptr;
1188 }
1189
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001190 size_t SizeSlow() const {
1191 size_t count = 0;
1192 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1193 ++count;
1194 }
1195 return count;
1196 }
1197
David Brazdiled596192015-01-23 10:39:45 +00001198 private:
1199 HUseListNode<T>* first_;
1200};
1201
1202template<typename T>
1203class HUseIterator : public ValueObject {
1204 public:
1205 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1206
1207 bool Done() const { return current_ == nullptr; }
1208
1209 void Advance() {
1210 DCHECK(!Done());
1211 current_ = current_->GetNext();
1212 }
1213
1214 HUseListNode<T>* Current() const {
1215 DCHECK(!Done());
1216 return current_;
1217 }
1218
1219 private:
1220 HUseListNode<T>* current_;
1221
1222 friend class HValue;
1223};
1224
David Brazdil1abb4192015-02-17 18:33:36 +00001225// This class is used by HEnvironment and HInstruction classes to record the
1226// instructions they use and pointers to the corresponding HUseListNodes kept
1227// by the used instructions.
1228template <typename T>
1229class HUserRecord : public ValueObject {
1230 public:
1231 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1232 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1233
1234 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1235 : instruction_(old_record.instruction_), use_node_(use_node) {
1236 DCHECK(instruction_ != nullptr);
1237 DCHECK(use_node_ != nullptr);
1238 DCHECK(old_record.use_node_ == nullptr);
1239 }
1240
1241 HInstruction* GetInstruction() const { return instruction_; }
1242 HUseListNode<T>* GetUseNode() const { return use_node_; }
1243
1244 private:
1245 // Instruction used by the user.
1246 HInstruction* instruction_;
1247
1248 // Corresponding entry in the use list kept by 'instruction_'.
1249 HUseListNode<T>* use_node_;
1250};
1251
Aart Bik854a02b2015-07-14 16:07:00 -07001252/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001253 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001254 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001255 * For write/read dependences on fields/arrays, the dependence analysis uses
1256 * type disambiguation (e.g. a float field write cannot modify the value of an
1257 * integer field read) and the access type (e.g. a reference array write cannot
1258 * modify the value of a reference field read [although it may modify the
1259 * reference fetch prior to reading the field, which is represented by its own
1260 * write/read dependence]). The analysis makes conservative points-to
1261 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1262 * the same, and any reference read depends on any reference read without
1263 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001264 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001265 * The internal representation uses 38-bit and is described in the table below.
1266 * The first line indicates the side effect, and for field/array accesses the
1267 * second line indicates the type of the access (in the order of the
1268 * Primitive::Type enum).
1269 * The two numbered lines below indicate the bit position in the bitfield (read
1270 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001271 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001272 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1273 * +-------------+---------+---------+--------------+---------+---------+
1274 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1275 * | 3 |333333322|222222221| 1 |111111110|000000000|
1276 * | 7 |654321098|765432109| 8 |765432109|876543210|
1277 *
1278 * Note that, to ease the implementation, 'changes' bits are least significant
1279 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001280 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001281class SideEffects : public ValueObject {
1282 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001283 SideEffects() : flags_(0) {}
1284
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001285 static SideEffects None() {
1286 return SideEffects(0);
1287 }
1288
1289 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001290 return SideEffects(kAllChangeBits | kAllDependOnBits);
1291 }
1292
1293 static SideEffects AllChanges() {
1294 return SideEffects(kAllChangeBits);
1295 }
1296
1297 static SideEffects AllDependencies() {
1298 return SideEffects(kAllDependOnBits);
1299 }
1300
1301 static SideEffects AllExceptGCDependency() {
1302 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1303 }
1304
1305 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001306 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001307 }
1308
Aart Bik34c3ba92015-07-20 14:08:59 -07001309 static SideEffects AllWrites() {
1310 return SideEffects(kAllWrites);
1311 }
1312
1313 static SideEffects AllReads() {
1314 return SideEffects(kAllReads);
1315 }
1316
1317 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1318 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001319 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001320 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001321 }
1322
Aart Bik854a02b2015-07-14 16:07:00 -07001323 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1324 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001325 }
1326
Aart Bik34c3ba92015-07-20 14:08:59 -07001327 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1328 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001329 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001330 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001331 }
1332
1333 static SideEffects ArrayReadOfType(Primitive::Type type) {
1334 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1335 }
1336
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001337 static SideEffects CanTriggerGC() {
1338 return SideEffects(1ULL << kCanTriggerGCBit);
1339 }
1340
1341 static SideEffects DependsOnGC() {
1342 return SideEffects(1ULL << kDependsOnGCBit);
1343 }
1344
Aart Bik854a02b2015-07-14 16:07:00 -07001345 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001346 SideEffects Union(SideEffects other) const {
1347 return SideEffects(flags_ | other.flags_);
1348 }
1349
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001350 SideEffects Exclusion(SideEffects other) const {
1351 return SideEffects(flags_ & ~other.flags_);
1352 }
1353
1354 bool Includes(SideEffects other) const {
1355 return (other.flags_ & flags_) == other.flags_;
1356 }
1357
1358 bool HasSideEffects() const {
1359 return (flags_ & kAllChangeBits);
1360 }
1361
1362 bool HasDependencies() const {
1363 return (flags_ & kAllDependOnBits);
1364 }
1365
1366 // Returns true if there are no side effects or dependencies.
1367 bool DoesNothing() const {
1368 return flags_ == 0;
1369 }
1370
Aart Bik854a02b2015-07-14 16:07:00 -07001371 // Returns true if something is written.
1372 bool DoesAnyWrite() const {
1373 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001374 }
1375
Aart Bik854a02b2015-07-14 16:07:00 -07001376 // Returns true if something is read.
1377 bool DoesAnyRead() const {
1378 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001379 }
1380
Aart Bik854a02b2015-07-14 16:07:00 -07001381 // Returns true if potentially everything is written and read
1382 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001383 bool DoesAllReadWrite() const {
1384 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1385 }
1386
Aart Bik854a02b2015-07-14 16:07:00 -07001387 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001388 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001389 }
1390
1391 // Returns true if this may read something written by other.
1392 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001393 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1394 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001395 }
1396
1397 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001398 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001399 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001400 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001401 for (int s = kLastBit; s >= 0; s--) {
1402 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1403 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1404 // This is a bit for the GC side effect.
1405 if (current_bit_is_set) {
1406 flags += "GC";
1407 }
Aart Bik854a02b2015-07-14 16:07:00 -07001408 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001409 } else {
1410 // This is a bit for the array/field analysis.
1411 // The underscore character stands for the 'can trigger GC' bit.
1412 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1413 if (current_bit_is_set) {
1414 flags += kDebug[s];
1415 }
1416 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1417 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1418 flags += "|";
1419 }
1420 }
Aart Bik854a02b2015-07-14 16:07:00 -07001421 }
1422 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001423 }
1424
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001425 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001426
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001427 private:
1428 static constexpr int kFieldArrayAnalysisBits = 9;
1429
1430 static constexpr int kFieldWriteOffset = 0;
1431 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1432 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1433 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1434
1435 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1436
1437 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1438 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1439 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1440 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1441
1442 static constexpr int kLastBit = kDependsOnGCBit;
1443 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1444
1445 // Aliases.
1446
1447 static_assert(kChangeBits == kDependOnBits,
1448 "the 'change' bits should match the 'depend on' bits.");
1449
1450 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1451 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1452 static constexpr uint64_t kAllWrites =
1453 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1454 static constexpr uint64_t kAllReads =
1455 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001456
Aart Bik854a02b2015-07-14 16:07:00 -07001457 // Work around the fact that HIR aliases I/F and J/D.
1458 // TODO: remove this interceptor once HIR types are clean
1459 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1460 switch (type) {
1461 case Primitive::kPrimInt:
1462 case Primitive::kPrimFloat:
1463 return TypeFlag(Primitive::kPrimInt, offset) |
1464 TypeFlag(Primitive::kPrimFloat, offset);
1465 case Primitive::kPrimLong:
1466 case Primitive::kPrimDouble:
1467 return TypeFlag(Primitive::kPrimLong, offset) |
1468 TypeFlag(Primitive::kPrimDouble, offset);
1469 default:
1470 return TypeFlag(type, offset);
1471 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001472 }
1473
Aart Bik854a02b2015-07-14 16:07:00 -07001474 // Translates type to bit flag.
1475 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1476 CHECK_NE(type, Primitive::kPrimVoid);
1477 const uint64_t one = 1;
1478 const int shift = type; // 0-based consecutive enum
1479 DCHECK_LE(kFieldWriteOffset, shift);
1480 DCHECK_LT(shift, kArrayWriteOffset);
1481 return one << (type + offset);
1482 }
1483
1484 // Private constructor on direct flags value.
1485 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1486
1487 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001488};
1489
David Brazdiled596192015-01-23 10:39:45 +00001490// A HEnvironment object contains the values of virtual registers at a given location.
1491class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1492 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001493 HEnvironment(ArenaAllocator* arena,
1494 size_t number_of_vregs,
1495 const DexFile& dex_file,
1496 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001497 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001498 InvokeType invoke_type,
1499 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001500 : vregs_(arena, number_of_vregs),
1501 locations_(arena, number_of_vregs),
1502 parent_(nullptr),
1503 dex_file_(dex_file),
1504 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001505 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001506 invoke_type_(invoke_type),
1507 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001508 vregs_.SetSize(number_of_vregs);
1509 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001510 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001511 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001512
1513 locations_.SetSize(number_of_vregs);
1514 for (size_t i = 0; i < number_of_vregs; ++i) {
1515 locations_.Put(i, Location());
1516 }
1517 }
1518
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001519 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001520 : HEnvironment(arena,
1521 to_copy.Size(),
1522 to_copy.GetDexFile(),
1523 to_copy.GetMethodIdx(),
1524 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001525 to_copy.GetInvokeType(),
1526 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001527
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001528 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001529 if (parent_ != nullptr) {
1530 parent_->SetAndCopyParentChain(allocator, parent);
1531 } else {
1532 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1533 parent_->CopyFrom(parent);
1534 if (parent->GetParent() != nullptr) {
1535 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1536 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001537 }
David Brazdiled596192015-01-23 10:39:45 +00001538 }
1539
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001540 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1541 void CopyFrom(HEnvironment* environment);
1542
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001543 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1544 // input to the loop phi instead. This is for inserting instructions that
1545 // require an environment (like HDeoptimization) in the loop pre-header.
1546 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001547
1548 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001549 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001550 }
1551
1552 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001553 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001554 }
1555
David Brazdil1abb4192015-02-17 18:33:36 +00001556 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001557
1558 size_t Size() const { return vregs_.Size(); }
1559
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001560 HEnvironment* GetParent() const { return parent_; }
1561
1562 void SetLocationAt(size_t index, Location location) {
1563 locations_.Put(index, location);
1564 }
1565
1566 Location GetLocationAt(size_t index) const {
1567 return locations_.Get(index);
1568 }
1569
1570 uint32_t GetDexPc() const {
1571 return dex_pc_;
1572 }
1573
1574 uint32_t GetMethodIdx() const {
1575 return method_idx_;
1576 }
1577
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001578 InvokeType GetInvokeType() const {
1579 return invoke_type_;
1580 }
1581
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001582 const DexFile& GetDexFile() const {
1583 return dex_file_;
1584 }
1585
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001586 HInstruction* GetHolder() const {
1587 return holder_;
1588 }
1589
David Brazdiled596192015-01-23 10:39:45 +00001590 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001591 // Record instructions' use entries of this environment for constant-time removal.
1592 // It should only be called by HInstruction when a new environment use is added.
1593 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1594 DCHECK(env_use->GetUser() == this);
1595 size_t index = env_use->GetIndex();
1596 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1597 }
David Brazdiled596192015-01-23 10:39:45 +00001598
David Brazdil1abb4192015-02-17 18:33:36 +00001599 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001600 GrowableArray<Location> locations_;
1601 HEnvironment* parent_;
1602 const DexFile& dex_file_;
1603 const uint32_t method_idx_;
1604 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001605 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001606
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001607 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001608 HInstruction* const holder_;
1609
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001610 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001611
1612 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1613};
1614
Calin Juravleacf735c2015-02-12 15:25:22 +00001615class ReferenceTypeInfo : ValueObject {
1616 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001617 typedef Handle<mirror::Class> TypeHandle;
1618
Calin Juravle2e768302015-07-28 14:41:11 +00001619 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1620 // The constructor will check that the type_handle is valid.
1621 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001622 }
1623
Calin Juravle2e768302015-07-28 14:41:11 +00001624 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1625
1626 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1627 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001628 }
1629
Calin Juravle2e768302015-07-28 14:41:11 +00001630 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1631 return IsValidHandle(type_handle_);
1632 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001633 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001634
1635 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1636 DCHECK(IsValid());
1637 return GetTypeHandle()->IsObjectClass();
1638 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001639 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001640 DCHECK(IsValid());
1641 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001642 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001643
1644 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1645
Mathieu Chartier90443472015-07-16 20:32:27 -07001646 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001647 DCHECK(IsValid());
1648 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001649 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1650 }
1651
1652 // Returns true if the type information provide the same amount of details.
1653 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001654 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001655 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001656 if (!IsValid() && !rti.IsValid()) {
1657 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001658 return true;
1659 }
Calin Juravle2e768302015-07-28 14:41:11 +00001660 if (!IsValid() || !rti.IsValid()) {
1661 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001662 return false;
1663 }
Calin Juravle2e768302015-07-28 14:41:11 +00001664 return IsExact() == rti.IsExact()
1665 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001666 }
1667
1668 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001669 ReferenceTypeInfo();
1670 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001671
Calin Juravleacf735c2015-02-12 15:25:22 +00001672 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001673 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001674 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001675 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001676 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001677};
1678
1679std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1680
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001681class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001682 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001683 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001684 : previous_(nullptr),
1685 next_(nullptr),
1686 block_(nullptr),
1687 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001688 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001689 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001690 locations_(nullptr),
1691 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001692 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001693 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001694 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001695
Dave Allison20dfc792014-06-16 20:44:29 -07001696 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001697
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001698#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001699 enum InstructionKind {
1700 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1701 };
1702#undef DECLARE_KIND
1703
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001704 HInstruction* GetNext() const { return next_; }
1705 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001706
Calin Juravle77520bc2015-01-12 18:45:46 +00001707 HInstruction* GetNextDisregardingMoves() const;
1708 HInstruction* GetPreviousDisregardingMoves() const;
1709
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001710 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001711 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001712 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001713 bool IsInBlock() const { return block_ != nullptr; }
1714 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001715 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001716
Roland Levillain6b879dd2014-09-22 17:13:44 +01001717 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001718 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001719
1720 virtual void Accept(HGraphVisitor* visitor) = 0;
1721 virtual const char* DebugName() const = 0;
1722
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001723 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001724 void SetRawInputAt(size_t index, HInstruction* input) {
1725 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1726 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001727
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001728 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001729 virtual uint32_t GetDexPc() const {
1730 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1731 " does not need an environment";
1732 UNREACHABLE();
1733 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001734 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001735
Roland Levillaine161a2a2014-10-03 12:45:18 +01001736 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001737 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001738
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001739 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001740 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001741
Calin Juravle10e244f2015-01-26 18:54:32 +00001742 // Does not apply for all instructions, but having this at top level greatly
1743 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001744 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001745 virtual bool CanBeNull() const {
1746 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1747 return true;
1748 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001749
Calin Juravle641547a2015-04-21 22:08:51 +01001750 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1751 UNUSED(obj);
1752 return false;
1753 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001754
Calin Juravle2e768302015-07-28 14:41:11 +00001755 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001756
Calin Juravle61d544b2015-02-23 16:46:57 +00001757 ReferenceTypeInfo GetReferenceTypeInfo() const {
1758 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1759 return reference_type_info_;
1760 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001761
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001762 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001763 DCHECK(user != nullptr);
1764 HUseListNode<HInstruction*>* use =
1765 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1766 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001767 }
1768
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001769 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001770 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001771 HUseListNode<HEnvironment*>* env_use =
1772 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1773 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001774 }
1775
David Brazdil1abb4192015-02-17 18:33:36 +00001776 void RemoveAsUserOfInput(size_t input) {
1777 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1778 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1779 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001780
David Brazdil1abb4192015-02-17 18:33:36 +00001781 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1782 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001783
David Brazdiled596192015-01-23 10:39:45 +00001784 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1785 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001786 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001787 bool HasOnlyOneNonEnvironmentUse() const {
1788 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1789 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001790
Roland Levillain6c82d402014-10-13 16:10:27 +01001791 // Does this instruction strictly dominate `other_instruction`?
1792 // Returns false if this instruction and `other_instruction` are the same.
1793 // Aborts if this instruction and `other_instruction` are both phis.
1794 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001795
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001796 int GetId() const { return id_; }
1797 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001798
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001799 int GetSsaIndex() const { return ssa_index_; }
1800 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1801 bool HasSsaIndex() const { return ssa_index_ != -1; }
1802
1803 bool HasEnvironment() const { return environment_ != nullptr; }
1804 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001805 // Set the `environment_` field. Raw because this method does not
1806 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001807 void SetRawEnvironment(HEnvironment* environment) {
1808 DCHECK(environment_ == nullptr);
1809 DCHECK_EQ(environment->GetHolder(), this);
1810 environment_ = environment;
1811 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001812
1813 // Set the environment of this instruction, copying it from `environment`. While
1814 // copying, the uses lists are being updated.
1815 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001816 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001817 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001818 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001819 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001820 if (environment->GetParent() != nullptr) {
1821 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1822 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001823 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001824
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001825 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1826 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001827 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001828 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001829 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001830 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001831 if (environment->GetParent() != nullptr) {
1832 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1833 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001834 }
1835
Nicolas Geoffray39468442014-09-02 15:17:15 +01001836 // Returns the number of entries in the environment. Typically, that is the
1837 // number of dex registers in a method. It could be more in case of inlining.
1838 size_t EnvironmentSize() const;
1839
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001840 LocationSummary* GetLocations() const { return locations_; }
1841 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001842
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001843 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001844 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001845
Alexandre Rames188d4312015-04-09 18:30:21 +01001846 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1847 // uses of this instruction by `other` are *not* updated.
1848 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1849 ReplaceWith(other);
1850 other->ReplaceInput(this, use_index);
1851 }
1852
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001853 // Move `this` instruction before `cursor`.
1854 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001855
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001856#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001857 bool Is##type() const { return (As##type() != nullptr); } \
1858 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001859 virtual H##type* As##type() { return nullptr; }
1860
1861 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1862#undef INSTRUCTION_TYPE_CHECK
1863
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001864 // Returns whether the instruction can be moved within the graph.
1865 virtual bool CanBeMoved() const { return false; }
1866
1867 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001868 virtual bool InstructionTypeEquals(HInstruction* other) const {
1869 UNUSED(other);
1870 return false;
1871 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001872
1873 // Returns whether any data encoded in the two instructions is equal.
1874 // This method does not look at the inputs. Both instructions must be
1875 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001876 virtual bool InstructionDataEquals(HInstruction* other) const {
1877 UNUSED(other);
1878 return false;
1879 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001880
1881 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001882 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001883 // 2) Their inputs are identical.
1884 bool Equals(HInstruction* other) const;
1885
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001886 virtual InstructionKind GetKind() const = 0;
1887
1888 virtual size_t ComputeHashCode() const {
1889 size_t result = GetKind();
1890 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1891 result = (result * 31) + InputAt(i)->GetId();
1892 }
1893 return result;
1894 }
1895
1896 SideEffects GetSideEffects() const { return side_effects_; }
1897
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001898 size_t GetLifetimePosition() const { return lifetime_position_; }
1899 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1900 LiveInterval* GetLiveInterval() const { return live_interval_; }
1901 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1902 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1903
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001904 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1905
1906 // Returns whether the code generation of the instruction will require to have access
1907 // to the current method. Such instructions are:
1908 // (1): Instructions that require an environment, as calling the runtime requires
1909 // to walk the stack and have the current method stored at a specific stack address.
1910 // (2): Object literals like classes and strings, that are loaded from the dex cache
1911 // fields of the current method.
1912 bool NeedsCurrentMethod() const {
1913 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1914 }
1915
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001916 virtual bool NeedsDexCache() const { return false; }
1917
Mark Mendellc4701932015-04-10 13:18:51 -04001918 // Does this instruction have any use in an environment before
1919 // control flow hits 'other'?
1920 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1921
1922 // Remove all references to environment uses of this instruction.
1923 // The caller must ensure that this is safe to do.
1924 void RemoveEnvironmentUsers();
1925
David Brazdil1abb4192015-02-17 18:33:36 +00001926 protected:
1927 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1928 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1929
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001930 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001931 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1932
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001933 HInstruction* previous_;
1934 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001935 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001936
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001937 // An instruction gets an id when it is added to the graph.
1938 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001939 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001940 int id_;
1941
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001942 // When doing liveness analysis, instructions that have uses get an SSA index.
1943 int ssa_index_;
1944
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001945 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001946 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001947
1948 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001949 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001950
Nicolas Geoffray39468442014-09-02 15:17:15 +01001951 // The environment associated with this instruction. Not null if the instruction
1952 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001953 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001954
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001955 // Set by the code generator.
1956 LocationSummary* locations_;
1957
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001958 // Set by the liveness analysis.
1959 LiveInterval* live_interval_;
1960
1961 // Set by the liveness analysis, this is the position in a linear
1962 // order of blocks where this instruction's live interval start.
1963 size_t lifetime_position_;
1964
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001965 const SideEffects side_effects_;
1966
Calin Juravleacf735c2015-02-12 15:25:22 +00001967 // TODO: for primitive types this should be marked as invalid.
1968 ReferenceTypeInfo reference_type_info_;
1969
David Brazdil1abb4192015-02-17 18:33:36 +00001970 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001971 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001972 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001973 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001974 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001975
1976 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1977};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001978std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001979
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001980class HInputIterator : public ValueObject {
1981 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001982 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001983
1984 bool Done() const { return index_ == instruction_->InputCount(); }
1985 HInstruction* Current() const { return instruction_->InputAt(index_); }
1986 void Advance() { index_++; }
1987
1988 private:
1989 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001990 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001991
1992 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1993};
1994
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001995class HInstructionIterator : public ValueObject {
1996 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001997 explicit HInstructionIterator(const HInstructionList& instructions)
1998 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001999 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002000 }
2001
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002002 bool Done() const { return instruction_ == nullptr; }
2003 HInstruction* Current() const { return instruction_; }
2004 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002005 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002006 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002007 }
2008
2009 private:
2010 HInstruction* instruction_;
2011 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002012
2013 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002014};
2015
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002016class HBackwardInstructionIterator : public ValueObject {
2017 public:
2018 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2019 : instruction_(instructions.last_instruction_) {
2020 next_ = Done() ? nullptr : instruction_->GetPrevious();
2021 }
2022
2023 bool Done() const { return instruction_ == nullptr; }
2024 HInstruction* Current() const { return instruction_; }
2025 void Advance() {
2026 instruction_ = next_;
2027 next_ = Done() ? nullptr : instruction_->GetPrevious();
2028 }
2029
2030 private:
2031 HInstruction* instruction_;
2032 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002033
2034 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002035};
2036
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002037// An embedded container with N elements of type T. Used (with partial
2038// specialization for N=0) because embedded arrays cannot have size 0.
2039template<typename T, intptr_t N>
2040class EmbeddedArray {
2041 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002042 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002043
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002044 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002045
2046 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002047 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002048 return elements_[i];
2049 }
2050
2051 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002052 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002053 return elements_[i];
2054 }
2055
2056 const T& At(intptr_t i) const {
2057 return (*this)[i];
2058 }
2059
2060 void SetAt(intptr_t i, const T& val) {
2061 (*this)[i] = val;
2062 }
2063
2064 private:
2065 T elements_[N];
2066};
2067
2068template<typename T>
2069class EmbeddedArray<T, 0> {
2070 public:
2071 intptr_t length() const { return 0; }
2072 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002073 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002074 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002075 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002076 }
2077 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002078 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002079 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002080 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002081 }
2082};
2083
2084template<intptr_t N>
2085class HTemplateInstruction: public HInstruction {
2086 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002087 HTemplateInstruction<N>(SideEffects side_effects)
2088 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002089 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002090
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002091 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002092
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002093 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00002094 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
2095
2096 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
2097 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002098 }
2099
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002100 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002101 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002102
2103 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002104};
2105
Dave Allison20dfc792014-06-16 20:44:29 -07002106template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002107class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002108 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002109 HExpression<N>(Primitive::Type type, SideEffects side_effects)
2110 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002111 virtual ~HExpression() {}
2112
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002113 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002114
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002115 protected:
2116 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002117};
2118
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002119// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2120// instruction that branches to the exit block.
2121class HReturnVoid : public HTemplateInstruction<0> {
2122 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002123 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002124
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002125 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002126
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002127 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002128
2129 private:
2130 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2131};
2132
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002133// Represents dex's RETURN opcodes. A HReturn is a control flow
2134// instruction that branches to the exit block.
2135class HReturn : public HTemplateInstruction<1> {
2136 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002137 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002138 SetRawInputAt(0, value);
2139 }
2140
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002141 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002142
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002143 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002144
2145 private:
2146 DISALLOW_COPY_AND_ASSIGN(HReturn);
2147};
2148
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002149// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002150// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002151// exit block.
2152class HExit : public HTemplateInstruction<0> {
2153 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002154 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002155
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002156 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002157
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002158 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002159
2160 private:
2161 DISALLOW_COPY_AND_ASSIGN(HExit);
2162};
2163
2164// Jumps from one block to another.
2165class HGoto : public HTemplateInstruction<0> {
2166 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002167 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2168
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002169 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002170
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002171 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002172 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002173 }
2174
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002175 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002176
2177 private:
2178 DISALLOW_COPY_AND_ASSIGN(HGoto);
2179};
2180
Roland Levillain9867bc72015-08-05 10:21:34 +01002181class HConstant : public HExpression<0> {
2182 public:
2183 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2184
2185 bool CanBeMoved() const OVERRIDE { return true; }
2186
2187 virtual bool IsMinusOne() const { return false; }
2188 virtual bool IsZero() const { return false; }
2189 virtual bool IsOne() const { return false; }
2190
2191 DECLARE_INSTRUCTION(Constant);
2192
2193 private:
2194 DISALLOW_COPY_AND_ASSIGN(HConstant);
2195};
2196
2197class HNullConstant : public HConstant {
2198 public:
2199 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2200 return true;
2201 }
2202
2203 size_t ComputeHashCode() const OVERRIDE { return 0; }
2204
2205 DECLARE_INSTRUCTION(NullConstant);
2206
2207 private:
2208 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2209
2210 friend class HGraph;
2211 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2212};
2213
2214// Constants of the type int. Those can be from Dex instructions, or
2215// synthesized (for example with the if-eqz instruction).
2216class HIntConstant : public HConstant {
2217 public:
2218 int32_t GetValue() const { return value_; }
2219
2220 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2221 DCHECK(other->IsIntConstant());
2222 return other->AsIntConstant()->value_ == value_;
2223 }
2224
2225 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2226
2227 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2228 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2229 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2230
2231 DECLARE_INSTRUCTION(IntConstant);
2232
2233 private:
2234 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2235 explicit HIntConstant(bool value) : HConstant(Primitive::kPrimInt), value_(value ? 1 : 0) {}
2236
2237 const int32_t value_;
2238
2239 friend class HGraph;
2240 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2241 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2242 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2243};
2244
2245class HLongConstant : public HConstant {
2246 public:
2247 int64_t GetValue() const { return value_; }
2248
2249 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2250 DCHECK(other->IsLongConstant());
2251 return other->AsLongConstant()->value_ == value_;
2252 }
2253
2254 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2255
2256 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2257 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2258 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2259
2260 DECLARE_INSTRUCTION(LongConstant);
2261
2262 private:
2263 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2264
2265 const int64_t value_;
2266
2267 friend class HGraph;
2268 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2269};
Dave Allison20dfc792014-06-16 20:44:29 -07002270
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002271// Conditional branch. A block ending with an HIf instruction must have
2272// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002273class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002274 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002275 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002276 SetRawInputAt(0, input);
2277 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002278
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002279 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002280
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002281 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002282 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002283 }
2284
2285 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002286 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002287 }
2288
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002289 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002290
2291 private:
2292 DISALLOW_COPY_AND_ASSIGN(HIf);
2293};
2294
David Brazdilfc6a86a2015-06-26 10:33:45 +00002295
2296// Abstract instruction which marks the beginning and/or end of a try block and
2297// links it to the respective exception handlers. Behaves the same as a Goto in
2298// non-exceptional control flow.
2299// Normal-flow successor is stored at index zero, exception handlers under
2300// higher indices in no particular order.
2301class HTryBoundary : public HTemplateInstruction<0> {
2302 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002303 enum BoundaryKind {
2304 kEntry,
2305 kExit,
2306 };
2307
2308 explicit HTryBoundary(BoundaryKind kind)
2309 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002310
2311 bool IsControlFlow() const OVERRIDE { return true; }
2312
2313 // Returns the block's non-exceptional successor (index zero).
2314 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2315
2316 // Returns whether `handler` is among its exception handlers (non-zero index
2317 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002318 bool HasExceptionHandler(const HBasicBlock& handler) const {
2319 DCHECK(handler.IsCatchBlock());
2320 return GetBlock()->GetSuccessors().Contains(
2321 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002322 }
2323
2324 // If not present already, adds `handler` to its block's list of exception
2325 // handlers.
2326 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002327 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002328 GetBlock()->AddSuccessor(handler);
2329 }
2330 }
2331
David Brazdil56e1acc2015-06-30 15:41:36 +01002332 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002333
David Brazdilffee3d32015-07-06 11:48:53 +01002334 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2335
David Brazdilfc6a86a2015-06-26 10:33:45 +00002336 DECLARE_INSTRUCTION(TryBoundary);
2337
2338 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002339 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002340
2341 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2342};
2343
David Brazdilffee3d32015-07-06 11:48:53 +01002344// Iterator over exception handlers of a given HTryBoundary, i.e. over
2345// exceptional successors of its basic block.
2346class HExceptionHandlerIterator : public ValueObject {
2347 public:
2348 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2349 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2350
2351 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2352 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
2353 size_t CurrentSuccessorIndex() const { return index_; }
2354 void Advance() { ++index_; }
2355
2356 private:
2357 const HBasicBlock& block_;
2358 size_t index_;
2359
2360 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2361};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002362
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002363// Deoptimize to interpreter, upon checking a condition.
2364class HDeoptimize : public HTemplateInstruction<1> {
2365 public:
2366 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2367 : HTemplateInstruction(SideEffects::None()),
2368 dex_pc_(dex_pc) {
2369 SetRawInputAt(0, cond);
2370 }
2371
2372 bool NeedsEnvironment() const OVERRIDE { return true; }
2373 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002374 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002375
2376 DECLARE_INSTRUCTION(Deoptimize);
2377
2378 private:
2379 uint32_t dex_pc_;
2380
2381 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2382};
2383
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002384// Represents the ArtMethod that was passed as a first argument to
2385// the method. It is used by instructions that depend on it, like
2386// instructions that work with the dex cache.
2387class HCurrentMethod : public HExpression<0> {
2388 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002389 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002390
2391 DECLARE_INSTRUCTION(CurrentMethod);
2392
2393 private:
2394 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2395};
2396
Roland Levillain88cb1752014-10-20 16:36:47 +01002397class HUnaryOperation : public HExpression<1> {
2398 public:
2399 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2400 : HExpression(result_type, SideEffects::None()) {
2401 SetRawInputAt(0, input);
2402 }
2403
2404 HInstruction* GetInput() const { return InputAt(0); }
2405 Primitive::Type GetResultType() const { return GetType(); }
2406
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002407 bool CanBeMoved() const OVERRIDE { return true; }
2408 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002409 UNUSED(other);
2410 return true;
2411 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002412
Roland Levillain9240d6a2014-10-20 16:47:04 +01002413 // Try to statically evaluate `operation` and return a HConstant
2414 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002415 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002416 HConstant* TryStaticEvaluation() const;
2417
2418 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002419 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2420 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002421
Roland Levillain88cb1752014-10-20 16:36:47 +01002422 DECLARE_INSTRUCTION(UnaryOperation);
2423
2424 private:
2425 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2426};
2427
Dave Allison20dfc792014-06-16 20:44:29 -07002428class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002429 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002430 HBinaryOperation(Primitive::Type result_type,
2431 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002432 HInstruction* right,
2433 SideEffects side_effects = SideEffects::None())
2434 : HExpression(result_type, side_effects) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002435 SetRawInputAt(0, left);
2436 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002437 }
2438
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002439 HInstruction* GetLeft() const { return InputAt(0); }
2440 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002441 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002442
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002443 virtual bool IsCommutative() const { return false; }
2444
2445 // Put constant on the right.
2446 // Returns whether order is changed.
2447 bool OrderInputsWithConstantOnTheRight() {
2448 HInstruction* left = InputAt(0);
2449 HInstruction* right = InputAt(1);
2450 if (left->IsConstant() && !right->IsConstant()) {
2451 ReplaceInput(right, 0);
2452 ReplaceInput(left, 1);
2453 return true;
2454 }
2455 return false;
2456 }
2457
2458 // Order inputs by instruction id, but favor constant on the right side.
2459 // This helps GVN for commutative ops.
2460 void OrderInputs() {
2461 DCHECK(IsCommutative());
2462 HInstruction* left = InputAt(0);
2463 HInstruction* right = InputAt(1);
2464 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2465 return;
2466 }
2467 if (OrderInputsWithConstantOnTheRight()) {
2468 return;
2469 }
2470 // Order according to instruction id.
2471 if (left->GetId() > right->GetId()) {
2472 ReplaceInput(right, 0);
2473 ReplaceInput(left, 1);
2474 }
2475 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002476
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002477 bool CanBeMoved() const OVERRIDE { return true; }
2478 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002479 UNUSED(other);
2480 return true;
2481 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002482
Roland Levillain9240d6a2014-10-20 16:47:04 +01002483 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002484 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002485 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002486 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002487
2488 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002489 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2490 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2491 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2492 HLongConstant* y ATTRIBUTE_UNUSED) const {
2493 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2494 return nullptr;
2495 }
2496 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2497 HIntConstant* y ATTRIBUTE_UNUSED) const {
2498 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2499 return nullptr;
2500 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002501
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002502 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002503 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002504 HConstant* GetConstantRight() const;
2505
2506 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002507 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002508 HInstruction* GetLeastConstantLeft() const;
2509
Roland Levillainccc07a92014-09-16 14:48:16 +01002510 DECLARE_INSTRUCTION(BinaryOperation);
2511
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002512 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002513 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2514};
2515
Mark Mendellc4701932015-04-10 13:18:51 -04002516// The comparison bias applies for floating point operations and indicates how NaN
2517// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002518enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002519 kNoBias, // bias is not applicable (i.e. for long operation)
2520 kGtBias, // return 1 for NaN comparisons
2521 kLtBias, // return -1 for NaN comparisons
2522};
2523
Dave Allison20dfc792014-06-16 20:44:29 -07002524class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002525 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002526 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002527 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002528 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002529 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002530
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002531 bool NeedsMaterialization() const { return needs_materialization_; }
2532 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002533
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002534 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002535 // `instruction`, and disregard moves in between.
2536 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002537
Dave Allison20dfc792014-06-16 20:44:29 -07002538 DECLARE_INSTRUCTION(Condition);
2539
2540 virtual IfCondition GetCondition() const = 0;
2541
Mark Mendellc4701932015-04-10 13:18:51 -04002542 virtual IfCondition GetOppositeCondition() const = 0;
2543
Roland Levillain4fa13f62015-07-06 18:11:54 +01002544 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002545
2546 void SetBias(ComparisonBias bias) { bias_ = bias; }
2547
2548 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2549 return bias_ == other->AsCondition()->bias_;
2550 }
2551
Roland Levillain4fa13f62015-07-06 18:11:54 +01002552 bool IsFPConditionTrueIfNaN() const {
2553 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2554 IfCondition if_cond = GetCondition();
2555 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2556 }
2557
2558 bool IsFPConditionFalseIfNaN() const {
2559 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2560 IfCondition if_cond = GetCondition();
2561 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2562 }
2563
Dave Allison20dfc792014-06-16 20:44:29 -07002564 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002565 // For register allocation purposes, returns whether this instruction needs to be
2566 // materialized (that is, not just be in the processor flags).
2567 bool needs_materialization_;
2568
Mark Mendellc4701932015-04-10 13:18:51 -04002569 // Needed if we merge a HCompare into a HCondition.
2570 ComparisonBias bias_;
2571
Dave Allison20dfc792014-06-16 20:44:29 -07002572 DISALLOW_COPY_AND_ASSIGN(HCondition);
2573};
2574
2575// Instruction to check if two inputs are equal to each other.
2576class HEqual : public HCondition {
2577 public:
2578 HEqual(HInstruction* first, HInstruction* second)
2579 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002580
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002581 bool IsCommutative() const OVERRIDE { return true; }
2582
Roland Levillain9867bc72015-08-05 10:21:34 +01002583 template <typename T> bool Compute(T x, T y) const { return x == y; }
2584
2585 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2586 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002587 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002588 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2589 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002590 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002591
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002592 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002593
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002594 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002595 return kCondEQ;
2596 }
2597
Mark Mendellc4701932015-04-10 13:18:51 -04002598 IfCondition GetOppositeCondition() const OVERRIDE {
2599 return kCondNE;
2600 }
2601
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002602 private:
2603 DISALLOW_COPY_AND_ASSIGN(HEqual);
2604};
2605
Dave Allison20dfc792014-06-16 20:44:29 -07002606class HNotEqual : public HCondition {
2607 public:
2608 HNotEqual(HInstruction* first, HInstruction* second)
2609 : HCondition(first, second) {}
2610
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002611 bool IsCommutative() const OVERRIDE { return true; }
2612
Roland Levillain9867bc72015-08-05 10:21:34 +01002613 template <typename T> bool Compute(T x, T y) const { return x != y; }
2614
2615 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2616 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002617 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002618 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2619 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002620 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002621
Dave Allison20dfc792014-06-16 20:44:29 -07002622 DECLARE_INSTRUCTION(NotEqual);
2623
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002624 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002625 return kCondNE;
2626 }
2627
Mark Mendellc4701932015-04-10 13:18:51 -04002628 IfCondition GetOppositeCondition() const OVERRIDE {
2629 return kCondEQ;
2630 }
2631
Dave Allison20dfc792014-06-16 20:44:29 -07002632 private:
2633 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2634};
2635
2636class HLessThan : public HCondition {
2637 public:
2638 HLessThan(HInstruction* first, HInstruction* second)
2639 : HCondition(first, second) {}
2640
Roland Levillain9867bc72015-08-05 10:21:34 +01002641 template <typename T> bool Compute(T x, T y) const { return x < y; }
2642
2643 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2644 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002645 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002646 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2647 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002648 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002649
Dave Allison20dfc792014-06-16 20:44:29 -07002650 DECLARE_INSTRUCTION(LessThan);
2651
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002652 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002653 return kCondLT;
2654 }
2655
Mark Mendellc4701932015-04-10 13:18:51 -04002656 IfCondition GetOppositeCondition() const OVERRIDE {
2657 return kCondGE;
2658 }
2659
Dave Allison20dfc792014-06-16 20:44:29 -07002660 private:
2661 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2662};
2663
2664class HLessThanOrEqual : public HCondition {
2665 public:
2666 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2667 : HCondition(first, second) {}
2668
Roland Levillain9867bc72015-08-05 10:21:34 +01002669 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2670
2671 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2672 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002673 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002674 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2675 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002676 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002677
Dave Allison20dfc792014-06-16 20:44:29 -07002678 DECLARE_INSTRUCTION(LessThanOrEqual);
2679
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002680 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002681 return kCondLE;
2682 }
2683
Mark Mendellc4701932015-04-10 13:18:51 -04002684 IfCondition GetOppositeCondition() const OVERRIDE {
2685 return kCondGT;
2686 }
2687
Dave Allison20dfc792014-06-16 20:44:29 -07002688 private:
2689 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2690};
2691
2692class HGreaterThan : public HCondition {
2693 public:
2694 HGreaterThan(HInstruction* first, HInstruction* second)
2695 : HCondition(first, second) {}
2696
Roland Levillain9867bc72015-08-05 10:21:34 +01002697 template <typename T> bool Compute(T x, T y) const { return x > y; }
2698
2699 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2700 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002701 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002702 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2703 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002704 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002705
Dave Allison20dfc792014-06-16 20:44:29 -07002706 DECLARE_INSTRUCTION(GreaterThan);
2707
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002708 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002709 return kCondGT;
2710 }
2711
Mark Mendellc4701932015-04-10 13:18:51 -04002712 IfCondition GetOppositeCondition() const OVERRIDE {
2713 return kCondLE;
2714 }
2715
Dave Allison20dfc792014-06-16 20:44:29 -07002716 private:
2717 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2718};
2719
2720class HGreaterThanOrEqual : public HCondition {
2721 public:
2722 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2723 : HCondition(first, second) {}
2724
Roland Levillain9867bc72015-08-05 10:21:34 +01002725 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2726
2727 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2728 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002729 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002730 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2731 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002732 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002733
Dave Allison20dfc792014-06-16 20:44:29 -07002734 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2735
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002736 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002737 return kCondGE;
2738 }
2739
Mark Mendellc4701932015-04-10 13:18:51 -04002740 IfCondition GetOppositeCondition() const OVERRIDE {
2741 return kCondLT;
2742 }
2743
Dave Allison20dfc792014-06-16 20:44:29 -07002744 private:
2745 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2746};
2747
2748
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002749// Instruction to check how two inputs compare to each other.
2750// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2751class HCompare : public HBinaryOperation {
2752 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002753 HCompare(Primitive::Type type,
2754 HInstruction* first,
2755 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002756 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002757 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002758 : HBinaryOperation(Primitive::kPrimInt, first, second, SideEffectsForArchRuntimeCalls(type)),
2759 bias_(bias),
2760 dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002761 DCHECK_EQ(type, first->GetType());
2762 DCHECK_EQ(type, second->GetType());
2763 }
2764
Roland Levillain9867bc72015-08-05 10:21:34 +01002765 template <typename T>
2766 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002767
Roland Levillain9867bc72015-08-05 10:21:34 +01002768 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2769 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
2770 }
2771 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2772 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain556c3d12014-09-18 15:25:07 +01002773 }
2774
Calin Juravleddb7df22014-11-25 20:56:51 +00002775 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2776 return bias_ == other->AsCompare()->bias_;
2777 }
2778
Mark Mendellc4701932015-04-10 13:18:51 -04002779 ComparisonBias GetBias() const { return bias_; }
2780
Roland Levillain4fa13f62015-07-06 18:11:54 +01002781 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002782
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002783 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
2784
2785 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2786 // MIPS64 uses a runtime call for FP comparisons.
2787 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2788 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002789
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002790 DECLARE_INSTRUCTION(Compare);
2791
2792 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002793 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002794 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002795
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002796 DISALLOW_COPY_AND_ASSIGN(HCompare);
2797};
2798
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002799// A local in the graph. Corresponds to a Dex register.
2800class HLocal : public HTemplateInstruction<0> {
2801 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002802 explicit HLocal(uint16_t reg_number)
2803 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002804
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002805 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002806
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002807 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002808
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002809 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002810 // The Dex register number.
2811 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002812
2813 DISALLOW_COPY_AND_ASSIGN(HLocal);
2814};
2815
2816// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002817class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002818 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002819 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002820 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002821 SetRawInputAt(0, local);
2822 }
2823
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002824 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2825
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002826 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002827
2828 private:
2829 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2830};
2831
2832// Store a value in a given local. This instruction has two inputs: the value
2833// and the local.
2834class HStoreLocal : public HTemplateInstruction<2> {
2835 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002836 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002837 SetRawInputAt(0, local);
2838 SetRawInputAt(1, value);
2839 }
2840
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002841 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2842
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002843 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002844
2845 private:
2846 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2847};
2848
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002849class HFloatConstant : public HConstant {
2850 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002851 float GetValue() const { return value_; }
2852
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002853 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002854 DCHECK(other->IsFloatConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002855 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2856 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002857 }
2858
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002859 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002860
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002861 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002862 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002863 }
2864 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002865 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002866 }
2867 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002868 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2869 }
2870 bool IsNaN() const {
2871 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002872 }
2873
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002874 DECLARE_INSTRUCTION(FloatConstant);
2875
2876 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002877 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002878 explicit HFloatConstant(int32_t value)
2879 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002880
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002881 const float value_;
2882
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002883 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002884 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002885 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002886 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2887};
2888
2889class HDoubleConstant : public HConstant {
2890 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002891 double GetValue() const { return value_; }
2892
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002893 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002894 DCHECK(other->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002895 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2896 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002897 }
2898
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002899 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002900
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002901 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002902 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002903 }
2904 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002905 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002906 }
2907 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002908 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2909 }
2910 bool IsNaN() const {
2911 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002912 }
2913
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002914 DECLARE_INSTRUCTION(DoubleConstant);
2915
2916 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002917 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002918 explicit HDoubleConstant(int64_t value)
2919 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002920
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002921 const double value_;
2922
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002923 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002924 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002925 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002926 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2927};
2928
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002929enum class Intrinsics {
agicsaki57b81ec2015-08-11 17:39:37 -07002930#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironment) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002931#include "intrinsics_list.h"
2932 kNone,
2933 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2934#undef INTRINSICS_LIST
2935#undef OPTIMIZING_INTRINSICS
2936};
2937std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2938
agicsaki57b81ec2015-08-11 17:39:37 -07002939enum IntrinsicNeedsEnvironment {
2940 kNoEnvironment, // Intrinsic does not require an environment.
2941 kNeedsEnvironment // Intrinsic requires an environment.
2942};
2943
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002944class HInvoke : public HInstruction {
2945 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002946 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002947
2948 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2949 // know their environment.
agicsaki57b81ec2015-08-11 17:39:37 -07002950 bool NeedsEnvironment() const OVERRIDE { return needs_environment_ == kNeedsEnvironment; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002951
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002952 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002953 SetRawInputAt(index, argument);
2954 }
2955
Roland Levillain3e3d7332015-04-28 11:00:54 +01002956 // Return the number of arguments. This number can be lower than
2957 // the number of inputs returned by InputCount(), as some invoke
2958 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2959 // inputs at the end of their list of inputs.
2960 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2961
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002962 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002963
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002964 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002965
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002966 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002967 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002968
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002969 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2970
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002971 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002972 return intrinsic_;
2973 }
2974
agicsaki57b81ec2015-08-11 17:39:37 -07002975 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironment needs_environment) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002976 intrinsic_ = intrinsic;
agicsaki57b81ec2015-08-11 17:39:37 -07002977 needs_environment_ = needs_environment;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002978 }
2979
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002980 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002981 return GetEnvironment()->GetParent() != nullptr;
2982 }
2983
2984 bool CanThrow() const OVERRIDE { return true; }
2985
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002986 DECLARE_INSTRUCTION(Invoke);
2987
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002988 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002989 HInvoke(ArenaAllocator* arena,
2990 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002991 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002992 Primitive::Type return_type,
2993 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002994 uint32_t dex_method_index,
2995 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002996 : HInstruction(
2997 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01002998 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002999 inputs_(arena, number_of_arguments),
3000 return_type_(return_type),
3001 dex_pc_(dex_pc),
3002 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003003 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003004 intrinsic_(Intrinsics::kNone),
3005 needs_environment_(kNeedsEnvironment) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003006 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
3007 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003008 }
3009
David Brazdil1abb4192015-02-17 18:33:36 +00003010 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3011 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3012 inputs_.Put(index, input);
3013 }
3014
Roland Levillain3e3d7332015-04-28 11:00:54 +01003015 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00003016 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003017 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003018 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003019 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003020 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003021 Intrinsics intrinsic_;
agicsaki57b81ec2015-08-11 17:39:37 -07003022 IntrinsicNeedsEnvironment needs_environment_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003023
3024 private:
3025 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3026};
3027
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003028class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003029 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003030 // Requirements of this method call regarding the class
3031 // initialization (clinit) check of its declaring class.
3032 enum class ClinitCheckRequirement {
3033 kNone, // Class already initialized.
3034 kExplicit, // Static call having explicit clinit check as last input.
3035 kImplicit, // Static call implicitly requiring a clinit check.
3036 };
3037
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003038 HInvokeStaticOrDirect(ArenaAllocator* arena,
3039 uint32_t number_of_arguments,
3040 Primitive::Type return_type,
3041 uint32_t dex_pc,
Vladimir Markob2c431e2015-08-19 12:45:42 +00003042 uint32_t dex_method_index,
3043 bool is_recursive,
3044 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003045 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003046 InvokeType invoke_type,
3047 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003048 : HInvoke(arena,
3049 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003050 // There is one extra argument for the HCurrentMethod node, and
3051 // potentially one other if the clinit check is explicit, and one other
3052 // if the method is a string factory.
3053 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Markob2c431e2015-08-19 12:45:42 +00003054 + (string_init_offset ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003055 return_type,
3056 dex_pc,
Vladimir Markob2c431e2015-08-19 12:45:42 +00003057 dex_method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003058 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003059 invoke_type_(invoke_type),
Vladimir Markob2c431e2015-08-19 12:45:42 +00003060 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08003061 clinit_check_requirement_(clinit_check_requirement),
Vladimir Markob2c431e2015-08-19 12:45:42 +00003062 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003063
Calin Juravle641547a2015-04-21 22:08:51 +01003064 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3065 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003066 // We access the method via the dex cache so we can't do an implicit null check.
3067 // TODO: for intrinsics we can generate implicit null checks.
3068 return false;
3069 }
3070
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003071 bool CanBeNull() const OVERRIDE {
3072 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3073 }
3074
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003075 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003076 bool IsRecursive() const { return is_recursive_; }
3077 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
3078 bool IsStringInit() const { return string_init_offset_ != 0; }
3079 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003080 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003081
Roland Levillain4c0eb422015-04-24 16:43:49 +01003082 // Is this instruction a call to a static method?
3083 bool IsStatic() const {
3084 return GetInvokeType() == kStatic;
3085 }
3086
Roland Levillain3e3d7332015-04-28 11:00:54 +01003087 // Remove the art::HLoadClass instruction set as last input by
3088 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3089 // the initial art::HClinitCheck instruction (only relevant for
3090 // static calls with explicit clinit check).
3091 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003092 DCHECK(IsStaticWithExplicitClinitCheck());
3093 size_t last_input_index = InputCount() - 1;
3094 HInstruction* last_input = InputAt(last_input_index);
3095 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003096 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003097 RemoveAsUserOfInput(last_input_index);
3098 inputs_.DeleteAt(last_input_index);
3099 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3100 DCHECK(IsStaticWithImplicitClinitCheck());
3101 }
3102
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003103 bool IsStringFactoryFor(HFakeString* str) const {
3104 if (!IsStringInit()) return false;
3105 // +1 for the current method.
3106 if (InputCount() == (number_of_arguments_ + 1)) return false;
3107 return InputAt(InputCount() - 1)->AsFakeString() == str;
3108 }
3109
3110 void RemoveFakeStringArgumentAsLastInput() {
3111 DCHECK(IsStringInit());
3112 size_t last_input_index = InputCount() - 1;
3113 HInstruction* last_input = InputAt(last_input_index);
3114 DCHECK(last_input != nullptr);
3115 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3116 RemoveAsUserOfInput(last_input_index);
3117 inputs_.DeleteAt(last_input_index);
3118 }
3119
Roland Levillain4c0eb422015-04-24 16:43:49 +01003120 // Is this a call to a static method whose declaring class has an
3121 // explicit intialization check in the graph?
3122 bool IsStaticWithExplicitClinitCheck() const {
3123 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3124 }
3125
3126 // Is this a call to a static method whose declaring class has an
3127 // implicit intialization check requirement?
3128 bool IsStaticWithImplicitClinitCheck() const {
3129 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3130 }
3131
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003132 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003133
Roland Levillain4c0eb422015-04-24 16:43:49 +01003134 protected:
3135 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3136 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3137 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3138 HInstruction* input = input_record.GetInstruction();
3139 // `input` is the last input of a static invoke marked as having
3140 // an explicit clinit check. It must either be:
3141 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3142 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3143 DCHECK(input != nullptr);
3144 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3145 }
3146 return input_record;
3147 }
3148
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003149 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003150 const InvokeType invoke_type_;
Vladimir Markob2c431e2015-08-19 12:45:42 +00003151 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003152 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Markob2c431e2015-08-19 12:45:42 +00003153 // Thread entrypoint offset for string init method if this is a string init invoke.
3154 // Note that there are multiple string init methods, each having its own offset.
3155 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003156
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003157 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003158};
3159
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003160class HInvokeVirtual : public HInvoke {
3161 public:
3162 HInvokeVirtual(ArenaAllocator* arena,
3163 uint32_t number_of_arguments,
3164 Primitive::Type return_type,
3165 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003166 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003167 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003168 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003169 vtable_index_(vtable_index) {}
3170
Calin Juravle641547a2015-04-21 22:08:51 +01003171 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003172 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003173 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003174 }
3175
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003176 uint32_t GetVTableIndex() const { return vtable_index_; }
3177
3178 DECLARE_INSTRUCTION(InvokeVirtual);
3179
3180 private:
3181 const uint32_t vtable_index_;
3182
3183 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3184};
3185
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003186class HInvokeInterface : public HInvoke {
3187 public:
3188 HInvokeInterface(ArenaAllocator* arena,
3189 uint32_t number_of_arguments,
3190 Primitive::Type return_type,
3191 uint32_t dex_pc,
3192 uint32_t dex_method_index,
3193 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003194 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003195 imt_index_(imt_index) {}
3196
Calin Juravle641547a2015-04-21 22:08:51 +01003197 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003198 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003199 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003200 }
3201
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003202 uint32_t GetImtIndex() const { return imt_index_; }
3203 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3204
3205 DECLARE_INSTRUCTION(InvokeInterface);
3206
3207 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003208 const uint32_t imt_index_;
3209
3210 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3211};
3212
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003213class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003214 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003215 HNewInstance(HCurrentMethod* current_method,
3216 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003217 uint16_t type_index,
3218 const DexFile& dex_file,
3219 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003220 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003221 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003222 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003223 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003224 entrypoint_(entrypoint) {
3225 SetRawInputAt(0, current_method);
3226 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003227
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003228 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003229 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003230 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003231
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003232 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003233 bool NeedsEnvironment() const OVERRIDE { return true; }
3234 // It may throw when called on:
3235 // - interfaces
3236 // - abstract/innaccessible/unknown classes
3237 // TODO: optimize when possible.
3238 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003239
Calin Juravle10e244f2015-01-26 18:54:32 +00003240 bool CanBeNull() const OVERRIDE { return false; }
3241
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003242 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3243
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003244 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003245
3246 private:
3247 const uint32_t dex_pc_;
3248 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003249 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003250 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003251
3252 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3253};
3254
Roland Levillain88cb1752014-10-20 16:36:47 +01003255class HNeg : public HUnaryOperation {
3256 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003257 HNeg(Primitive::Type result_type, HInstruction* input)
Roland Levillain88cb1752014-10-20 16:36:47 +01003258 : HUnaryOperation(result_type, input) {}
3259
Roland Levillain9867bc72015-08-05 10:21:34 +01003260 template <typename T> T Compute(T x) const { return -x; }
3261
3262 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3263 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3264 }
3265 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3266 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3267 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003268
Roland Levillain88cb1752014-10-20 16:36:47 +01003269 DECLARE_INSTRUCTION(Neg);
3270
3271 private:
3272 DISALLOW_COPY_AND_ASSIGN(HNeg);
3273};
3274
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003275class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003276 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003277 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003278 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003279 uint32_t dex_pc,
3280 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003281 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003282 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003283 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003284 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003285 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003286 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003287 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003288 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003289 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003290 }
3291
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003292 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003293 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003294 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003295
3296 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003297 bool NeedsEnvironment() const OVERRIDE { return true; }
3298
Mingyao Yang0c365e62015-03-31 15:09:29 -07003299 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3300 bool CanThrow() const OVERRIDE { return true; }
3301
Calin Juravle10e244f2015-01-26 18:54:32 +00003302 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003303
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003304 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3305
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003306 DECLARE_INSTRUCTION(NewArray);
3307
3308 private:
3309 const uint32_t dex_pc_;
3310 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003311 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003312 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003313
3314 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3315};
3316
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003317class HAdd : public HBinaryOperation {
3318 public:
3319 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3320 : HBinaryOperation(result_type, left, right) {}
3321
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003322 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003323
Roland Levillain9867bc72015-08-05 10:21:34 +01003324 template <typename T> T Compute(T x, T y) const { return x + y; }
3325
3326 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3327 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003328 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003329 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3330 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003331 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003332
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003333 DECLARE_INSTRUCTION(Add);
3334
3335 private:
3336 DISALLOW_COPY_AND_ASSIGN(HAdd);
3337};
3338
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003339class HSub : public HBinaryOperation {
3340 public:
3341 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3342 : HBinaryOperation(result_type, left, right) {}
3343
Roland Levillain9867bc72015-08-05 10:21:34 +01003344 template <typename T> T Compute(T x, T y) const { return x - y; }
3345
3346 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3347 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003348 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003349 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3350 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003351 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003352
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003353 DECLARE_INSTRUCTION(Sub);
3354
3355 private:
3356 DISALLOW_COPY_AND_ASSIGN(HSub);
3357};
3358
Calin Juravle34bacdf2014-10-07 20:23:36 +01003359class HMul : public HBinaryOperation {
3360 public:
3361 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3362 : HBinaryOperation(result_type, left, right) {}
3363
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003364 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003365
Roland Levillain9867bc72015-08-05 10:21:34 +01003366 template <typename T> T Compute(T x, T y) const { return x * y; }
3367
3368 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3369 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3370 }
3371 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3372 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3373 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003374
3375 DECLARE_INSTRUCTION(Mul);
3376
3377 private:
3378 DISALLOW_COPY_AND_ASSIGN(HMul);
3379};
3380
Calin Juravle7c4954d2014-10-28 16:57:40 +00003381class HDiv : public HBinaryOperation {
3382 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003383 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003384 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3385 dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003386
Roland Levillain9867bc72015-08-05 10:21:34 +01003387 template <typename T>
3388 T Compute(T x, T y) const {
3389 // Our graph structure ensures we never have 0 for `y` during
3390 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003391 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003392 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003393 return (y == -1) ? -x : x / y;
3394 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003395
Roland Levillain9867bc72015-08-05 10:21:34 +01003396 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3397 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3398 }
3399 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3400 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003401 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003402
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003403 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003404
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003405 static SideEffects SideEffectsForArchRuntimeCalls() {
3406 // The generated code can use a runtime call.
3407 return SideEffects::CanTriggerGC();
3408 }
3409
Calin Juravle7c4954d2014-10-28 16:57:40 +00003410 DECLARE_INSTRUCTION(Div);
3411
3412 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003413 const uint32_t dex_pc_;
3414
Calin Juravle7c4954d2014-10-28 16:57:40 +00003415 DISALLOW_COPY_AND_ASSIGN(HDiv);
3416};
3417
Calin Juravlebacfec32014-11-14 15:54:36 +00003418class HRem : public HBinaryOperation {
3419 public:
3420 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003421 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3422 dex_pc_(dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003423
Roland Levillain9867bc72015-08-05 10:21:34 +01003424 template <typename T>
3425 T Compute(T x, T y) const {
3426 // Our graph structure ensures we never have 0 for `y` during
3427 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003428 DCHECK_NE(y, 0);
3429 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3430 return (y == -1) ? 0 : x % y;
3431 }
3432
Roland Levillain9867bc72015-08-05 10:21:34 +01003433 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3434 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3435 }
3436 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3437 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003438 }
3439
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003440 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003441
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003442 static SideEffects SideEffectsForArchRuntimeCalls() {
3443 return SideEffects::CanTriggerGC();
3444 }
3445
Calin Juravlebacfec32014-11-14 15:54:36 +00003446 DECLARE_INSTRUCTION(Rem);
3447
3448 private:
3449 const uint32_t dex_pc_;
3450
3451 DISALLOW_COPY_AND_ASSIGN(HRem);
3452};
3453
Calin Juravled0d48522014-11-04 16:40:20 +00003454class HDivZeroCheck : public HExpression<1> {
3455 public:
3456 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3457 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3458 SetRawInputAt(0, value);
3459 }
3460
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003461 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3462
Calin Juravled0d48522014-11-04 16:40:20 +00003463 bool CanBeMoved() const OVERRIDE { return true; }
3464
3465 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3466 UNUSED(other);
3467 return true;
3468 }
3469
3470 bool NeedsEnvironment() const OVERRIDE { return true; }
3471 bool CanThrow() const OVERRIDE { return true; }
3472
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003473 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003474
3475 DECLARE_INSTRUCTION(DivZeroCheck);
3476
3477 private:
3478 const uint32_t dex_pc_;
3479
3480 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3481};
3482
Calin Juravle9aec02f2014-11-18 23:06:35 +00003483class HShl : public HBinaryOperation {
3484 public:
3485 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3486 : HBinaryOperation(result_type, left, right) {}
3487
Roland Levillain9867bc72015-08-05 10:21:34 +01003488 template <typename T, typename U, typename V>
3489 T Compute(T x, U y, V max_shift_value) const {
3490 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3491 "V is not the unsigned integer type corresponding to T");
3492 return x << (y & max_shift_value);
3493 }
3494
3495 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3496 return GetBlock()->GetGraph()->GetIntConstant(
3497 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3498 }
3499 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3500 // case is handled as `x << static_cast<int>(y)`.
3501 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3502 return GetBlock()->GetGraph()->GetLongConstant(
3503 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3504 }
3505 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3506 return GetBlock()->GetGraph()->GetLongConstant(
3507 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3508 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003509
3510 DECLARE_INSTRUCTION(Shl);
3511
3512 private:
3513 DISALLOW_COPY_AND_ASSIGN(HShl);
3514};
3515
3516class HShr : public HBinaryOperation {
3517 public:
3518 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3519 : HBinaryOperation(result_type, left, right) {}
3520
Roland Levillain9867bc72015-08-05 10:21:34 +01003521 template <typename T, typename U, typename V>
3522 T Compute(T x, U y, V max_shift_value) const {
3523 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3524 "V is not the unsigned integer type corresponding to T");
3525 return x >> (y & max_shift_value);
3526 }
3527
3528 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3529 return GetBlock()->GetGraph()->GetIntConstant(
3530 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3531 }
3532 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3533 // case is handled as `x >> static_cast<int>(y)`.
3534 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3535 return GetBlock()->GetGraph()->GetLongConstant(
3536 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3537 }
3538 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3539 return GetBlock()->GetGraph()->GetLongConstant(
3540 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3541 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003542
3543 DECLARE_INSTRUCTION(Shr);
3544
3545 private:
3546 DISALLOW_COPY_AND_ASSIGN(HShr);
3547};
3548
3549class HUShr : public HBinaryOperation {
3550 public:
3551 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3552 : HBinaryOperation(result_type, left, right) {}
3553
Roland Levillain9867bc72015-08-05 10:21:34 +01003554 template <typename T, typename U, typename V>
3555 T Compute(T x, U y, V max_shift_value) const {
3556 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3557 "V is not the unsigned integer type corresponding to T");
3558 V ux = static_cast<V>(x);
3559 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003560 }
3561
Roland Levillain9867bc72015-08-05 10:21:34 +01003562 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3563 return GetBlock()->GetGraph()->GetIntConstant(
3564 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3565 }
3566 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3567 // case is handled as `x >>> static_cast<int>(y)`.
3568 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3569 return GetBlock()->GetGraph()->GetLongConstant(
3570 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3571 }
3572 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3573 return GetBlock()->GetGraph()->GetLongConstant(
3574 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003575 }
3576
3577 DECLARE_INSTRUCTION(UShr);
3578
3579 private:
3580 DISALLOW_COPY_AND_ASSIGN(HUShr);
3581};
3582
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003583class HAnd : public HBinaryOperation {
3584 public:
3585 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3586 : HBinaryOperation(result_type, left, right) {}
3587
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003588 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003589
Roland Levillain9867bc72015-08-05 10:21:34 +01003590 template <typename T, typename U>
3591 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3592
3593 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3594 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3595 }
3596 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3597 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3598 }
3599 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3600 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3601 }
3602 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3603 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3604 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003605
3606 DECLARE_INSTRUCTION(And);
3607
3608 private:
3609 DISALLOW_COPY_AND_ASSIGN(HAnd);
3610};
3611
3612class HOr : public HBinaryOperation {
3613 public:
3614 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3615 : HBinaryOperation(result_type, left, right) {}
3616
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003617 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003618
Roland Levillain9867bc72015-08-05 10:21:34 +01003619 template <typename T, typename U>
3620 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3621
3622 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3623 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3624 }
3625 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3626 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3627 }
3628 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3629 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3630 }
3631 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3632 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3633 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003634
3635 DECLARE_INSTRUCTION(Or);
3636
3637 private:
3638 DISALLOW_COPY_AND_ASSIGN(HOr);
3639};
3640
3641class HXor : public HBinaryOperation {
3642 public:
3643 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3644 : HBinaryOperation(result_type, left, right) {}
3645
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003646 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003647
Roland Levillain9867bc72015-08-05 10:21:34 +01003648 template <typename T, typename U>
3649 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3650
3651 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3652 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3653 }
3654 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3655 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3656 }
3657 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3658 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3659 }
3660 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3661 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3662 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003663
3664 DECLARE_INSTRUCTION(Xor);
3665
3666 private:
3667 DISALLOW_COPY_AND_ASSIGN(HXor);
3668};
3669
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003670// The value of a parameter in this method. Its location depends on
3671// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003672class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003673 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003674 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3675 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003676
3677 uint8_t GetIndex() const { return index_; }
3678
Calin Juravle10e244f2015-01-26 18:54:32 +00003679 bool CanBeNull() const OVERRIDE { return !is_this_; }
3680
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003681 bool IsThis() const { return is_this_; }
3682
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003683 DECLARE_INSTRUCTION(ParameterValue);
3684
3685 private:
3686 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003687 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003688 const uint8_t index_;
3689
Calin Juravle10e244f2015-01-26 18:54:32 +00003690 // Whether or not the parameter value corresponds to 'this' argument.
3691 const bool is_this_;
3692
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003693 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3694};
3695
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003696class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003697 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003698 HNot(Primitive::Type result_type, HInstruction* input)
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003699 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003700
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003701 bool CanBeMoved() const OVERRIDE { return true; }
3702 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003703 UNUSED(other);
3704 return true;
3705 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003706
Roland Levillain9867bc72015-08-05 10:21:34 +01003707 template <typename T> T Compute(T x) const { return ~x; }
3708
3709 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3710 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3711 }
3712 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3713 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3714 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003715
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003716 DECLARE_INSTRUCTION(Not);
3717
3718 private:
3719 DISALLOW_COPY_AND_ASSIGN(HNot);
3720};
3721
David Brazdil66d126e2015-04-03 16:02:44 +01003722class HBooleanNot : public HUnaryOperation {
3723 public:
3724 explicit HBooleanNot(HInstruction* input)
3725 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3726
3727 bool CanBeMoved() const OVERRIDE { return true; }
3728 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3729 UNUSED(other);
3730 return true;
3731 }
3732
Roland Levillain9867bc72015-08-05 10:21:34 +01003733 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003734 DCHECK(IsUint<1>(x));
3735 return !x;
3736 }
3737
Roland Levillain9867bc72015-08-05 10:21:34 +01003738 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3739 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3740 }
3741 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3742 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003743 UNREACHABLE();
3744 }
3745
3746 DECLARE_INSTRUCTION(BooleanNot);
3747
3748 private:
3749 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3750};
3751
Roland Levillaindff1f282014-11-05 14:15:05 +00003752class HTypeConversion : public HExpression<1> {
3753 public:
3754 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003755 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003756 : HExpression(result_type, SideEffectsForArchRuntimeCalls(input->GetType(), result_type)),
3757 dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003758 SetRawInputAt(0, input);
3759 DCHECK_NE(input->GetType(), result_type);
3760 }
3761
3762 HInstruction* GetInput() const { return InputAt(0); }
3763 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3764 Primitive::Type GetResultType() const { return GetType(); }
3765
Roland Levillain624279f2014-12-04 11:54:28 +00003766 // Required by the x86 and ARM code generators when producing calls
3767 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003768 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003769
Roland Levillaindff1f282014-11-05 14:15:05 +00003770 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003771 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003772
Mark Mendelle82549b2015-05-06 10:55:34 -04003773 // Try to statically evaluate the conversion and return a HConstant
3774 // containing the result. If the input cannot be converted, return nullptr.
3775 HConstant* TryStaticEvaluation() const;
3776
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003777 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3778 Primitive::Type result_type) {
3779 // Some architectures may not require the 'GC' side effects, but at this point
3780 // in the compilation process we do not know what architecture we will
3781 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003782 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3783 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003784 return SideEffects::CanTriggerGC();
3785 }
3786 return SideEffects::None();
3787 }
3788
Roland Levillaindff1f282014-11-05 14:15:05 +00003789 DECLARE_INSTRUCTION(TypeConversion);
3790
3791 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003792 const uint32_t dex_pc_;
3793
Roland Levillaindff1f282014-11-05 14:15:05 +00003794 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3795};
3796
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003797static constexpr uint32_t kNoRegNumber = -1;
3798
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003799class HPhi : public HInstruction {
3800 public:
3801 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003802 : HInstruction(SideEffects::None()),
3803 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003804 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003805 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003806 is_live_(false),
3807 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003808 inputs_.SetSize(number_of_inputs);
3809 }
3810
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003811 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3812 static Primitive::Type ToPhiType(Primitive::Type type) {
3813 switch (type) {
3814 case Primitive::kPrimBoolean:
3815 case Primitive::kPrimByte:
3816 case Primitive::kPrimShort:
3817 case Primitive::kPrimChar:
3818 return Primitive::kPrimInt;
3819 default:
3820 return type;
3821 }
3822 }
3823
David Brazdilffee3d32015-07-06 11:48:53 +01003824 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3825
Calin Juravle10e244f2015-01-26 18:54:32 +00003826 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003827
3828 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003829 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003830
Calin Juravle10e244f2015-01-26 18:54:32 +00003831 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003832 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003833
Calin Juravle10e244f2015-01-26 18:54:32 +00003834 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3835 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3836
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003837 uint32_t GetRegNumber() const { return reg_number_; }
3838
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003839 void SetDead() { is_live_ = false; }
3840 void SetLive() { is_live_ = true; }
3841 bool IsDead() const { return !is_live_; }
3842 bool IsLive() const { return is_live_; }
3843
Calin Juravlea4f88312015-04-16 12:57:19 +01003844 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3845 // An equivalent phi is a phi having the same dex register and type.
3846 // It assumes that phis with the same dex register are adjacent.
3847 HPhi* GetNextEquivalentPhiWithSameType() {
3848 HInstruction* next = GetNext();
3849 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3850 if (next->GetType() == GetType()) {
3851 return next->AsPhi();
3852 }
3853 next = next->GetNext();
3854 }
3855 return nullptr;
3856 }
3857
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003858 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003859
David Brazdil1abb4192015-02-17 18:33:36 +00003860 protected:
3861 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3862
3863 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3864 inputs_.Put(index, input);
3865 }
3866
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003867 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003868 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003869 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003870 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003871 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003872 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003873
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003874 DISALLOW_COPY_AND_ASSIGN(HPhi);
3875};
3876
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003877class HNullCheck : public HExpression<1> {
3878 public:
3879 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003880 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003881 SetRawInputAt(0, value);
3882 }
3883
Calin Juravle10e244f2015-01-26 18:54:32 +00003884 bool CanBeMoved() const OVERRIDE { return true; }
3885 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003886 UNUSED(other);
3887 return true;
3888 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003889
Calin Juravle10e244f2015-01-26 18:54:32 +00003890 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003891
Calin Juravle10e244f2015-01-26 18:54:32 +00003892 bool CanThrow() const OVERRIDE { return true; }
3893
3894 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003895
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003896 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003897
3898 DECLARE_INSTRUCTION(NullCheck);
3899
3900 private:
3901 const uint32_t dex_pc_;
3902
3903 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3904};
3905
3906class FieldInfo : public ValueObject {
3907 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003908 FieldInfo(MemberOffset field_offset,
3909 Primitive::Type field_type,
3910 bool is_volatile,
3911 uint32_t index,
3912 const DexFile& dex_file)
3913 : field_offset_(field_offset),
3914 field_type_(field_type),
3915 is_volatile_(is_volatile),
3916 index_(index),
3917 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003918
3919 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003920 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003921 uint32_t GetFieldIndex() const { return index_; }
3922 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003923 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003924
3925 private:
3926 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003927 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003928 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003929 uint32_t index_;
3930 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003931};
3932
3933class HInstanceFieldGet : public HExpression<1> {
3934 public:
3935 HInstanceFieldGet(HInstruction* value,
3936 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003937 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003938 bool is_volatile,
3939 uint32_t field_idx,
3940 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003941 : HExpression(
3942 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01003943 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003944 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003945 SetRawInputAt(0, value);
3946 }
3947
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003948 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003949
3950 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3951 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3952 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003953 }
3954
Calin Juravle641547a2015-04-21 22:08:51 +01003955 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3956 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003957 }
3958
3959 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003960 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3961 }
3962
Calin Juravle52c48962014-12-16 17:02:57 +00003963 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003964 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003965 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003966 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003967
3968 DECLARE_INSTRUCTION(InstanceFieldGet);
3969
3970 private:
3971 const FieldInfo field_info_;
3972
3973 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3974};
3975
3976class HInstanceFieldSet : public HTemplateInstruction<2> {
3977 public:
3978 HInstanceFieldSet(HInstruction* object,
3979 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003980 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003981 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003982 bool is_volatile,
3983 uint32_t field_idx,
3984 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003985 : HTemplateInstruction(
3986 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003987 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003988 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003989 SetRawInputAt(0, object);
3990 SetRawInputAt(1, value);
3991 }
3992
Calin Juravle641547a2015-04-21 22:08:51 +01003993 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3994 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003995 }
3996
Calin Juravle52c48962014-12-16 17:02:57 +00003997 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003998 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003999 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004000 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004001 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004002 bool GetValueCanBeNull() const { return value_can_be_null_; }
4003 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004004
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004005 DECLARE_INSTRUCTION(InstanceFieldSet);
4006
4007 private:
4008 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004009 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004010
4011 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4012};
4013
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004014class HArrayGet : public HExpression<2> {
4015 public:
4016 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07004017 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004018 SetRawInputAt(0, array);
4019 SetRawInputAt(1, index);
4020 }
4021
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004022 bool CanBeMoved() const OVERRIDE { return true; }
4023 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004024 UNUSED(other);
4025 return true;
4026 }
Calin Juravle641547a2015-04-21 22:08:51 +01004027 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4028 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004029 // TODO: We can be smarter here.
4030 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4031 // which generates the implicit null check. There are cases when these can be removed
4032 // to produce better code. If we ever add optimizations to do so we should allow an
4033 // implicit check here (as long as the address falls in the first page).
4034 return false;
4035 }
4036
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004037 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004038
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004039 HInstruction* GetArray() const { return InputAt(0); }
4040 HInstruction* GetIndex() const { return InputAt(1); }
4041
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004042 DECLARE_INSTRUCTION(ArrayGet);
4043
4044 private:
4045 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4046};
4047
4048class HArraySet : public HTemplateInstruction<3> {
4049 public:
4050 HArraySet(HInstruction* array,
4051 HInstruction* index,
4052 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004053 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004054 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004055 : HTemplateInstruction(
4056 SideEffects::ArrayWriteOfType(expected_component_type).Union(
4057 SideEffectsForArchRuntimeCalls(value->GetType()))),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004058 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004059 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004060 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4061 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004062 SetRawInputAt(0, array);
4063 SetRawInputAt(1, index);
4064 SetRawInputAt(2, value);
4065 }
4066
Calin Juravle77520bc2015-01-12 18:45:46 +00004067 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004068 // We currently always call a runtime method to catch array store
4069 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004070 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004071 }
4072
Mingyao Yang81014cb2015-06-02 03:16:27 -07004073 // Can throw ArrayStoreException.
4074 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4075
Calin Juravle641547a2015-04-21 22:08:51 +01004076 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4077 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004078 // TODO: Same as for ArrayGet.
4079 return false;
4080 }
4081
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004082 void ClearNeedsTypeCheck() {
4083 needs_type_check_ = false;
4084 }
4085
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004086 void ClearValueCanBeNull() {
4087 value_can_be_null_ = false;
4088 }
4089
4090 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004091 bool NeedsTypeCheck() const { return needs_type_check_; }
4092
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004093 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004094
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004095 HInstruction* GetArray() const { return InputAt(0); }
4096 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004097 HInstruction* GetValue() const { return InputAt(2); }
4098
4099 Primitive::Type GetComponentType() const {
4100 // The Dex format does not type floating point index operations. Since the
4101 // `expected_component_type_` is set during building and can therefore not
4102 // be correct, we also check what is the value type. If it is a floating
4103 // point type, we must use that type.
4104 Primitive::Type value_type = GetValue()->GetType();
4105 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4106 ? value_type
4107 : expected_component_type_;
4108 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004109
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004110 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4111 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4112 }
4113
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004114 DECLARE_INSTRUCTION(ArraySet);
4115
4116 private:
4117 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004118 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004119 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004120 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004121
4122 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4123};
4124
4125class HArrayLength : public HExpression<1> {
4126 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004127 explicit HArrayLength(HInstruction* array)
4128 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
4129 // Note that arrays do not change length, so the instruction does not
4130 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004131 SetRawInputAt(0, array);
4132 }
4133
Calin Juravle77520bc2015-01-12 18:45:46 +00004134 bool CanBeMoved() const OVERRIDE { return true; }
4135 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004136 UNUSED(other);
4137 return true;
4138 }
Calin Juravle641547a2015-04-21 22:08:51 +01004139 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4140 return obj == InputAt(0);
4141 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004142
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004143 DECLARE_INSTRUCTION(ArrayLength);
4144
4145 private:
4146 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4147};
4148
4149class HBoundsCheck : public HExpression<2> {
4150 public:
4151 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004152 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004153 DCHECK(index->GetType() == Primitive::kPrimInt);
4154 SetRawInputAt(0, index);
4155 SetRawInputAt(1, length);
4156 }
4157
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004158 bool CanBeMoved() const OVERRIDE { return true; }
4159 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004160 UNUSED(other);
4161 return true;
4162 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004163
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004164 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004165
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004166 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004167
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004168 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004169
4170 DECLARE_INSTRUCTION(BoundsCheck);
4171
4172 private:
4173 const uint32_t dex_pc_;
4174
4175 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4176};
4177
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004178/**
4179 * Some DEX instructions are folded into multiple HInstructions that need
4180 * to stay live until the last HInstruction. This class
4181 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004182 * HInstruction stays live. `index` represents the stack location index of the
4183 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004184 */
4185class HTemporary : public HTemplateInstruction<0> {
4186 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004187 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004188
4189 size_t GetIndex() const { return index_; }
4190
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004191 Primitive::Type GetType() const OVERRIDE {
4192 // The previous instruction is the one that will be stored in the temporary location.
4193 DCHECK(GetPrevious() != nullptr);
4194 return GetPrevious()->GetType();
4195 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004196
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004197 DECLARE_INSTRUCTION(Temporary);
4198
4199 private:
4200 const size_t index_;
4201
4202 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4203};
4204
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004205class HSuspendCheck : public HTemplateInstruction<0> {
4206 public:
4207 explicit HSuspendCheck(uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004208 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004209
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004210 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004211 return true;
4212 }
4213
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004214 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004215 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4216 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004217
4218 DECLARE_INSTRUCTION(SuspendCheck);
4219
4220 private:
4221 const uint32_t dex_pc_;
4222
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004223 // Only used for code generation, in order to share the same slow path between back edges
4224 // of a same loop.
4225 SlowPathCode* slow_path_;
4226
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004227 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4228};
4229
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004230/**
4231 * Instruction to load a Class object.
4232 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004233class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004234 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004235 HLoadClass(HCurrentMethod* current_method,
4236 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004237 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004238 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004239 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004240 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004241 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004242 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004243 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004244 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00004245 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004246 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004247 SetRawInputAt(0, current_method);
4248 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004249
4250 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004251
4252 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4253 return other->AsLoadClass()->type_index_ == type_index_;
4254 }
4255
4256 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4257
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004258 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004259 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004260 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004261 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004262
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004263 bool NeedsEnvironment() const OVERRIDE {
4264 // Will call runtime and load the class if the class is not loaded yet.
4265 // TODO: finer grain decision.
4266 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004267 }
4268
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004269 bool MustGenerateClinitCheck() const {
4270 return generate_clinit_check_;
4271 }
4272
Calin Juravle0ba218d2015-05-19 18:46:01 +01004273 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4274 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004275 }
4276
4277 bool CanCallRuntime() const {
4278 return MustGenerateClinitCheck() || !is_referrers_class_;
4279 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004280
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004281 bool CanThrow() const OVERRIDE {
4282 // May call runtime and and therefore can throw.
4283 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004284 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004285 }
4286
Calin Juravleacf735c2015-02-12 15:25:22 +00004287 ReferenceTypeInfo GetLoadedClassRTI() {
4288 return loaded_class_rti_;
4289 }
4290
4291 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4292 // Make sure we only set exact types (the loaded class should never be merged).
4293 DCHECK(rti.IsExact());
4294 loaded_class_rti_ = rti;
4295 }
4296
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004297 const DexFile& GetDexFile() { return dex_file_; }
4298
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004299 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4300
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004301 static SideEffects SideEffectsForArchRuntimeCalls() {
4302 return SideEffects::CanTriggerGC();
4303 }
4304
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004305 DECLARE_INSTRUCTION(LoadClass);
4306
4307 private:
4308 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004309 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004310 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004311 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004312 // Whether this instruction must generate the initialization check.
4313 // Used for code generation.
4314 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004315
Calin Juravleacf735c2015-02-12 15:25:22 +00004316 ReferenceTypeInfo loaded_class_rti_;
4317
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004318 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4319};
4320
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004321class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004322 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004323 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004324 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004325 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004326 dex_pc_(dex_pc) {
4327 SetRawInputAt(0, current_method);
4328 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004329
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004330 bool CanBeMoved() const OVERRIDE { return true; }
4331
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004332 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4333 return other->AsLoadString()->string_index_ == string_index_;
4334 }
4335
4336 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4337
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004338 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004339 uint32_t GetStringIndex() const { return string_index_; }
4340
4341 // TODO: Can we deopt or debug when we resolve a string?
4342 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004343 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004344
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004345 static SideEffects SideEffectsForArchRuntimeCalls() {
4346 return SideEffects::CanTriggerGC();
4347 }
4348
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004349 DECLARE_INSTRUCTION(LoadString);
4350
4351 private:
4352 const uint32_t string_index_;
4353 const uint32_t dex_pc_;
4354
4355 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4356};
4357
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004358/**
4359 * Performs an initialization check on its Class object input.
4360 */
4361class HClinitCheck : public HExpression<1> {
4362 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004363 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004364 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004365 Primitive::kPrimNot,
4366 SideEffects::AllChanges()), // Assume write/read on all fields/arrays.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004367 dex_pc_(dex_pc) {
4368 SetRawInputAt(0, constant);
4369 }
4370
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004371 bool CanBeMoved() const OVERRIDE { return true; }
4372 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4373 UNUSED(other);
4374 return true;
4375 }
4376
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004377 bool NeedsEnvironment() const OVERRIDE {
4378 // May call runtime to initialize the class.
4379 return true;
4380 }
4381
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004382 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004383
4384 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4385
4386 DECLARE_INSTRUCTION(ClinitCheck);
4387
4388 private:
4389 const uint32_t dex_pc_;
4390
4391 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4392};
4393
4394class HStaticFieldGet : public HExpression<1> {
4395 public:
4396 HStaticFieldGet(HInstruction* cls,
4397 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004398 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004399 bool is_volatile,
4400 uint32_t field_idx,
4401 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004402 : HExpression(
4403 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004404 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004405 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004406 SetRawInputAt(0, cls);
4407 }
4408
Calin Juravle52c48962014-12-16 17:02:57 +00004409
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004410 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004411
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004412 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004413 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4414 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004415 }
4416
4417 size_t ComputeHashCode() const OVERRIDE {
4418 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4419 }
4420
Calin Juravle52c48962014-12-16 17:02:57 +00004421 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004422 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4423 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004424 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004425
4426 DECLARE_INSTRUCTION(StaticFieldGet);
4427
4428 private:
4429 const FieldInfo field_info_;
4430
4431 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4432};
4433
4434class HStaticFieldSet : public HTemplateInstruction<2> {
4435 public:
4436 HStaticFieldSet(HInstruction* cls,
4437 HInstruction* value,
4438 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004439 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004440 bool is_volatile,
4441 uint32_t field_idx,
4442 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004443 : HTemplateInstruction(
4444 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004445 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004446 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004447 SetRawInputAt(0, cls);
4448 SetRawInputAt(1, value);
4449 }
4450
Calin Juravle52c48962014-12-16 17:02:57 +00004451 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004452 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4453 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004454 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004455
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004456 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004457 bool GetValueCanBeNull() const { return value_can_be_null_; }
4458 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004459
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004460 DECLARE_INSTRUCTION(StaticFieldSet);
4461
4462 private:
4463 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004464 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004465
4466 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4467};
4468
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004469// Implement the move-exception DEX instruction.
4470class HLoadException : public HExpression<0> {
4471 public:
4472 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4473
4474 DECLARE_INSTRUCTION(LoadException);
4475
4476 private:
4477 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4478};
4479
David Brazdilcb1c0552015-08-04 16:22:25 +01004480// Implicit part of move-exception which clears thread-local exception storage.
4481// Must not be removed because the runtime expects the TLS to get cleared.
4482class HClearException : public HTemplateInstruction<0> {
4483 public:
4484 HClearException() : HTemplateInstruction(SideEffects::AllWrites()) {}
4485
4486 DECLARE_INSTRUCTION(ClearException);
4487
4488 private:
4489 DISALLOW_COPY_AND_ASSIGN(HClearException);
4490};
4491
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004492class HThrow : public HTemplateInstruction<1> {
4493 public:
4494 HThrow(HInstruction* exception, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004495 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004496 SetRawInputAt(0, exception);
4497 }
4498
4499 bool IsControlFlow() const OVERRIDE { return true; }
4500
4501 bool NeedsEnvironment() const OVERRIDE { return true; }
4502
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004503 bool CanThrow() const OVERRIDE { return true; }
4504
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004505 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004506
4507 DECLARE_INSTRUCTION(Throw);
4508
4509 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004510 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004511
4512 DISALLOW_COPY_AND_ASSIGN(HThrow);
4513};
4514
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004515class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004516 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004517 HInstanceOf(HInstruction* object,
4518 HLoadClass* constant,
4519 bool class_is_final,
4520 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004521 : HExpression(Primitive::kPrimBoolean, SideEffectsForArchRuntimeCalls(class_is_final)),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004522 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004523 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004524 dex_pc_(dex_pc) {
4525 SetRawInputAt(0, object);
4526 SetRawInputAt(1, constant);
4527 }
4528
4529 bool CanBeMoved() const OVERRIDE { return true; }
4530
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004531 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004532 return true;
4533 }
4534
4535 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004536 return false;
4537 }
4538
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004539 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004540
4541 bool IsClassFinal() const { return class_is_final_; }
4542
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004543 // Used only in code generation.
4544 bool MustDoNullCheck() const { return must_do_null_check_; }
4545 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4546
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004547 static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) {
4548 return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC();
4549 }
4550
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004551 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004552
4553 private:
4554 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004555 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004556 const uint32_t dex_pc_;
4557
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004558 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4559};
4560
Calin Juravleb1498f62015-02-16 13:13:29 +00004561class HBoundType : public HExpression<1> {
4562 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004563 // Constructs an HBoundType with the given upper_bound.
4564 // Ensures that the upper_bound is valid.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004565 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004566 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004567 upper_bound_(upper_bound),
4568 upper_can_be_null_(upper_can_be_null),
4569 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004570 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004571 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004572 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004573 }
4574
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004575 // GetUpper* should only be used in reference type propagation.
4576 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4577 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004578
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004579 void SetCanBeNull(bool can_be_null) {
4580 DCHECK(upper_can_be_null_ || !can_be_null);
4581 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004582 }
4583
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004584 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4585
Calin Juravleb1498f62015-02-16 13:13:29 +00004586 DECLARE_INSTRUCTION(BoundType);
4587
4588 private:
4589 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004590 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4591 // It is used to bound the type in cases like:
4592 // if (x instanceof ClassX) {
4593 // // uper_bound_ will be ClassX
4594 // }
4595 const ReferenceTypeInfo upper_bound_;
4596 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4597 // is false then can_be_null_ cannot be true).
4598 const bool upper_can_be_null_;
4599 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004600
4601 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4602};
4603
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004604class HCheckCast : public HTemplateInstruction<2> {
4605 public:
4606 HCheckCast(HInstruction* object,
4607 HLoadClass* constant,
4608 bool class_is_final,
4609 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004610 : HTemplateInstruction(SideEffects::CanTriggerGC()),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004611 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004612 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004613 dex_pc_(dex_pc) {
4614 SetRawInputAt(0, object);
4615 SetRawInputAt(1, constant);
4616 }
4617
4618 bool CanBeMoved() const OVERRIDE { return true; }
4619
4620 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4621 return true;
4622 }
4623
4624 bool NeedsEnvironment() const OVERRIDE {
4625 // Instruction may throw a CheckCastError.
4626 return true;
4627 }
4628
4629 bool CanThrow() const OVERRIDE { return true; }
4630
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004631 bool MustDoNullCheck() const { return must_do_null_check_; }
4632 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4633
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004634 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004635
4636 bool IsClassFinal() const { return class_is_final_; }
4637
4638 DECLARE_INSTRUCTION(CheckCast);
4639
4640 private:
4641 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004642 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004643 const uint32_t dex_pc_;
4644
4645 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004646};
4647
Calin Juravle27df7582015-04-17 19:12:31 +01004648class HMemoryBarrier : public HTemplateInstruction<0> {
4649 public:
4650 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004651 : HTemplateInstruction(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004652 SideEffects::AllWritesAndReads()), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004653 barrier_kind_(barrier_kind) {}
4654
4655 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4656
4657 DECLARE_INSTRUCTION(MemoryBarrier);
4658
4659 private:
4660 const MemBarrierKind barrier_kind_;
4661
4662 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4663};
4664
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004665class HMonitorOperation : public HTemplateInstruction<1> {
4666 public:
4667 enum OperationKind {
4668 kEnter,
4669 kExit,
4670 };
4671
4672 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004673 : HTemplateInstruction(
4674 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Aart Bik854a02b2015-07-14 16:07:00 -07004675 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004676 SetRawInputAt(0, object);
4677 }
4678
4679 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004680 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4681
4682 bool CanThrow() const OVERRIDE {
4683 // Verifier guarantees that monitor-exit cannot throw.
4684 // This is important because it allows the HGraphBuilder to remove
4685 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4686 return IsEnter();
4687 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004688
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004689 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004690
4691 bool IsEnter() const { return kind_ == kEnter; }
4692
4693 DECLARE_INSTRUCTION(MonitorOperation);
4694
Calin Juravle52c48962014-12-16 17:02:57 +00004695 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004696 const OperationKind kind_;
4697 const uint32_t dex_pc_;
4698
4699 private:
4700 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4701};
4702
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004703/**
4704 * A HInstruction used as a marker for the replacement of new + <init>
4705 * of a String to a call to a StringFactory. Only baseline will see
4706 * the node at code generation, where it will be be treated as null.
4707 * When compiling non-baseline, `HFakeString` instructions are being removed
4708 * in the instruction simplifier.
4709 */
4710class HFakeString : public HTemplateInstruction<0> {
4711 public:
4712 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4713
4714 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4715
4716 DECLARE_INSTRUCTION(FakeString);
4717
4718 private:
4719 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4720};
4721
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004722class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004723 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004724 MoveOperands(Location source,
4725 Location destination,
4726 Primitive::Type type,
4727 HInstruction* instruction)
4728 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004729
4730 Location GetSource() const { return source_; }
4731 Location GetDestination() const { return destination_; }
4732
4733 void SetSource(Location value) { source_ = value; }
4734 void SetDestination(Location value) { destination_ = value; }
4735
4736 // The parallel move resolver marks moves as "in-progress" by clearing the
4737 // destination (but not the source).
4738 Location MarkPending() {
4739 DCHECK(!IsPending());
4740 Location dest = destination_;
4741 destination_ = Location::NoLocation();
4742 return dest;
4743 }
4744
4745 void ClearPending(Location dest) {
4746 DCHECK(IsPending());
4747 destination_ = dest;
4748 }
4749
4750 bool IsPending() const {
4751 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4752 return destination_.IsInvalid() && !source_.IsInvalid();
4753 }
4754
4755 // True if this blocks a move from the given location.
4756 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004757 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004758 }
4759
4760 // A move is redundant if it's been eliminated, if its source and
4761 // destination are the same, or if its destination is unneeded.
4762 bool IsRedundant() const {
4763 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4764 }
4765
4766 // We clear both operands to indicate move that's been eliminated.
4767 void Eliminate() {
4768 source_ = destination_ = Location::NoLocation();
4769 }
4770
4771 bool IsEliminated() const {
4772 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4773 return source_.IsInvalid();
4774 }
4775
Alexey Frunze4dda3372015-06-01 18:31:49 -07004776 Primitive::Type GetType() const { return type_; }
4777
Nicolas Geoffray90218252015-04-15 11:56:51 +01004778 bool Is64BitMove() const {
4779 return Primitive::Is64BitType(type_);
4780 }
4781
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004782 HInstruction* GetInstruction() const { return instruction_; }
4783
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004784 private:
4785 Location source_;
4786 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004787 // The type this move is for.
4788 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004789 // The instruction this move is assocatied with. Null when this move is
4790 // for moving an input in the expected locations of user (including a phi user).
4791 // This is only used in debug mode, to ensure we do not connect interval siblings
4792 // in the same parallel move.
4793 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004794};
4795
4796static constexpr size_t kDefaultNumberOfMoves = 4;
4797
4798class HParallelMove : public HTemplateInstruction<0> {
4799 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004800 explicit HParallelMove(ArenaAllocator* arena)
4801 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004802
Nicolas Geoffray90218252015-04-15 11:56:51 +01004803 void AddMove(Location source,
4804 Location destination,
4805 Primitive::Type type,
4806 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004807 DCHECK(source.IsValid());
4808 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004809 if (kIsDebugBuild) {
4810 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004811 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004812 if (moves_.Get(i).GetInstruction() == instruction) {
4813 // Special case the situation where the move is for the spill slot
4814 // of the instruction.
4815 if ((GetPrevious() == instruction)
4816 || ((GetPrevious() == nullptr)
4817 && instruction->IsPhi()
4818 && instruction->GetBlock() == GetBlock())) {
4819 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4820 << "Doing parallel moves for the same instruction.";
4821 } else {
4822 DCHECK(false) << "Doing parallel moves for the same instruction.";
4823 }
4824 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004825 }
4826 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004827 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004828 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004829 << "Overlapped destination for two moves in a parallel move: "
4830 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4831 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004832 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004833 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004834 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004835 }
4836
4837 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004838 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004839 }
4840
4841 size_t NumMoves() const { return moves_.Size(); }
4842
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004843 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004844
4845 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004846 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004847
4848 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4849};
4850
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004851class HGraphVisitor : public ValueObject {
4852 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004853 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4854 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004855
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004856 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004857 virtual void VisitBasicBlock(HBasicBlock* block);
4858
Roland Levillain633021e2014-10-01 14:12:25 +01004859 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004860 void VisitInsertionOrder();
4861
Roland Levillain633021e2014-10-01 14:12:25 +01004862 // Visit the graph following dominator tree reverse post-order.
4863 void VisitReversePostOrder();
4864
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004865 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004866
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004867 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004868#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004869 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4870
4871 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4872
4873#undef DECLARE_VISIT_INSTRUCTION
4874
4875 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004876 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004877
4878 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4879};
4880
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004881class HGraphDelegateVisitor : public HGraphVisitor {
4882 public:
4883 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4884 virtual ~HGraphDelegateVisitor() {}
4885
4886 // Visit functions that delegate to to super class.
4887#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004888 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004889
4890 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4891
4892#undef DECLARE_VISIT_INSTRUCTION
4893
4894 private:
4895 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4896};
4897
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004898class HInsertionOrderIterator : public ValueObject {
4899 public:
4900 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4901
4902 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4903 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4904 void Advance() { ++index_; }
4905
4906 private:
4907 const HGraph& graph_;
4908 size_t index_;
4909
4910 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4911};
4912
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004913class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004914 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004915 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4916 // Check that reverse post order of the graph has been built.
4917 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4918 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004919
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004920 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4921 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004922 void Advance() { ++index_; }
4923
4924 private:
4925 const HGraph& graph_;
4926 size_t index_;
4927
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004928 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004929};
4930
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004931class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004932 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004933 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004934 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4935 // Check that reverse post order of the graph has been built.
4936 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4937 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004938
4939 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004940 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004941 void Advance() { --index_; }
4942
4943 private:
4944 const HGraph& graph_;
4945 size_t index_;
4946
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004947 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004948};
4949
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004950class HLinearPostOrderIterator : public ValueObject {
4951 public:
4952 explicit HLinearPostOrderIterator(const HGraph& graph)
4953 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4954
4955 bool Done() const { return index_ == 0; }
4956
4957 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4958
4959 void Advance() {
4960 --index_;
4961 DCHECK_GE(index_, 0U);
4962 }
4963
4964 private:
4965 const GrowableArray<HBasicBlock*>& order_;
4966 size_t index_;
4967
4968 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4969};
4970
4971class HLinearOrderIterator : public ValueObject {
4972 public:
4973 explicit HLinearOrderIterator(const HGraph& graph)
4974 : order_(graph.GetLinearOrder()), index_(0) {}
4975
4976 bool Done() const { return index_ == order_.Size(); }
4977 HBasicBlock* Current() const { return order_.Get(index_); }
4978 void Advance() { ++index_; }
4979
4980 private:
4981 const GrowableArray<HBasicBlock*>& order_;
4982 size_t index_;
4983
4984 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4985};
4986
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004987// Iterator over the blocks that art part of the loop. Includes blocks part
4988// of an inner loop. The order in which the blocks are iterated is on their
4989// block id.
4990class HBlocksInLoopIterator : public ValueObject {
4991 public:
4992 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4993 : blocks_in_loop_(info.GetBlocks()),
4994 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4995 index_(0) {
4996 if (!blocks_in_loop_.IsBitSet(index_)) {
4997 Advance();
4998 }
4999 }
5000
5001 bool Done() const { return index_ == blocks_.Size(); }
5002 HBasicBlock* Current() const { return blocks_.Get(index_); }
5003 void Advance() {
5004 ++index_;
5005 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5006 if (blocks_in_loop_.IsBitSet(index_)) {
5007 break;
5008 }
5009 }
5010 }
5011
5012 private:
5013 const BitVector& blocks_in_loop_;
5014 const GrowableArray<HBasicBlock*>& blocks_;
5015 size_t index_;
5016
5017 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5018};
5019
Mingyao Yang3584bce2015-05-19 16:01:59 -07005020// Iterator over the blocks that art part of the loop. Includes blocks part
5021// of an inner loop. The order in which the blocks are iterated is reverse
5022// post order.
5023class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5024 public:
5025 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5026 : blocks_in_loop_(info.GetBlocks()),
5027 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5028 index_(0) {
5029 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5030 Advance();
5031 }
5032 }
5033
5034 bool Done() const { return index_ == blocks_.Size(); }
5035 HBasicBlock* Current() const { return blocks_.Get(index_); }
5036 void Advance() {
5037 ++index_;
5038 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
5039 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
5040 break;
5041 }
5042 }
5043 }
5044
5045 private:
5046 const BitVector& blocks_in_loop_;
5047 const GrowableArray<HBasicBlock*>& blocks_;
5048 size_t index_;
5049
5050 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5051};
5052
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005053inline int64_t Int64FromConstant(HConstant* constant) {
5054 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5055 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5056 : constant->AsLongConstant()->GetValue();
5057}
5058
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005059} // namespace art
5060
5061#endif // ART_COMPILER_OPTIMIZING_NODES_H_