blob: efb708974dea974867753bb2cf4b4e65285d272a [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
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100558static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100559static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100560
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000561// A block in a method. Contains the list of instructions represented
562// as a double linked list. Each block knows its predecessors and
563// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100564
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700565class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000566 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100567 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000568 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000569 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
David Brazdilb618ade2015-07-29 10:31:29 +0100570 exceptional_predecessors_(graph->GetArena(), kDefaultNumberOfExceptionalPredecessors),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000571 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000572 loop_information_(nullptr),
573 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100574 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100575 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100576 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100577 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000578 lifetime_end_(kNoLifetime),
579 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000580
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100581 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
582 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000583 }
584
David Brazdilb618ade2015-07-29 10:31:29 +0100585 const GrowableArray<HInstruction*>& GetExceptionalPredecessors() const {
586 return exceptional_predecessors_;
587 }
588
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100589 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
590 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000591 }
592
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100593 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
594 return dominated_blocks_;
595 }
596
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100597 bool IsEntryBlock() const {
598 return graph_->GetEntryBlock() == this;
599 }
600
601 bool IsExitBlock() const {
602 return graph_->GetExitBlock() == this;
603 }
604
David Brazdil46e2a392015-03-16 17:31:52 +0000605 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000606 bool IsSingleTryBoundary() const;
607
608 // Returns true if this block emits nothing but a jump.
609 bool IsSingleJump() const {
610 HLoopInformation* loop_info = GetLoopInformation();
611 return (IsSingleGoto() || IsSingleTryBoundary())
612 // Back edges generate a suspend check.
613 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
614 }
David Brazdil46e2a392015-03-16 17:31:52 +0000615
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000616 void AddBackEdge(HBasicBlock* back_edge) {
617 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000618 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000619 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100620 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000621 loop_information_->AddBackEdge(back_edge);
622 }
623
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000624 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000625 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000626
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000627 int GetBlockId() const { return block_id_; }
628 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000629
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000630 HBasicBlock* GetDominator() const { return dominator_; }
631 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100632 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100633 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000634 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
635 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
636 if (dominated_blocks_.Get(i) == existing) {
637 dominated_blocks_.Put(i, new_block);
638 return;
639 }
640 }
641 LOG(FATAL) << "Unreachable";
642 UNREACHABLE();
643 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100644 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000645
646 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100647 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000648 }
649
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100650 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
651 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100652 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100653 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100654 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
655 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000656
David Brazdilb618ade2015-07-29 10:31:29 +0100657 void AddExceptionalPredecessor(HInstruction* exceptional_predecessor);
658
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000659 void AddSuccessor(HBasicBlock* block) {
660 successors_.Add(block);
661 block->predecessors_.Add(this);
662 }
663
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100664 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
665 size_t successor_index = GetSuccessorIndexOf(existing);
666 DCHECK_NE(successor_index, static_cast<size_t>(-1));
667 existing->RemovePredecessor(this);
668 new_block->predecessors_.Add(this);
669 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000670 }
671
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000672 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
673 size_t predecessor_index = GetPredecessorIndexOf(existing);
674 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
675 existing->RemoveSuccessor(this);
676 new_block->successors_.Add(this);
677 predecessors_.Put(predecessor_index, new_block);
678 }
679
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100680 // Insert `this` between `predecessor` and `successor. This method
681 // preserves the indicies, and will update the first edge found between
682 // `predecessor` and `successor`.
683 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
684 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
685 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
686 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
687 DCHECK_NE(successor_index, static_cast<size_t>(-1));
688 successor->predecessors_.Put(predecessor_index, this);
689 predecessor->successors_.Put(successor_index, this);
690 successors_.Add(successor);
691 predecessors_.Add(predecessor);
692 }
693
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100694 void RemovePredecessor(HBasicBlock* block) {
695 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100696 }
697
David Brazdilb618ade2015-07-29 10:31:29 +0100698 void RemoveExceptionalPredecessor(HInstruction* instruction) {
699 exceptional_predecessors_.Delete(instruction);
700 }
701
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000702 void RemoveSuccessor(HBasicBlock* block) {
703 successors_.Delete(block);
704 }
705
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100706 void ClearAllPredecessors() {
707 predecessors_.Reset();
708 }
709
710 void AddPredecessor(HBasicBlock* block) {
711 predecessors_.Add(block);
712 block->successors_.Add(this);
713 }
714
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100715 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100716 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100717 HBasicBlock* temp = predecessors_.Get(0);
718 predecessors_.Put(0, predecessors_.Get(1));
719 predecessors_.Put(1, temp);
720 }
721
David Brazdil769c9e52015-04-27 13:54:09 +0100722 void SwapSuccessors() {
723 DCHECK_EQ(successors_.Size(), 2u);
724 HBasicBlock* temp = successors_.Get(0);
725 successors_.Put(0, successors_.Get(1));
726 successors_.Put(1, temp);
727 }
728
David Brazdilfc6a86a2015-06-26 10:33:45 +0000729 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100730 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
731 if (predecessors_.Get(i) == predecessor) {
732 return i;
733 }
734 }
735 return -1;
736 }
737
David Brazdilb618ade2015-07-29 10:31:29 +0100738 size_t GetExceptionalPredecessorIndexOf(HInstruction* exceptional_predecessor) const {
739 for (size_t i = 0, e = exceptional_predecessors_.Size(); i < e; ++i) {
740 if (exceptional_predecessors_.Get(i) == exceptional_predecessor) {
741 return i;
742 }
743 }
744 return -1;
745 }
746
David Brazdilfc6a86a2015-06-26 10:33:45 +0000747 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100748 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
749 if (successors_.Get(i) == successor) {
750 return i;
751 }
752 }
753 return -1;
754 }
755
David Brazdilfc6a86a2015-06-26 10:33:45 +0000756 HBasicBlock* GetSinglePredecessor() const {
757 DCHECK_EQ(GetPredecessors().Size(), 1u);
758 return GetPredecessors().Get(0);
759 }
760
761 HBasicBlock* GetSingleSuccessor() const {
762 DCHECK_EQ(GetSuccessors().Size(), 1u);
763 return GetSuccessors().Get(0);
764 }
765
766 // Returns whether the first occurrence of `predecessor` in the list of
767 // predecessors is at index `idx`.
768 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
769 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
770 return GetPredecessorIndexOf(predecessor) == idx;
771 }
772
David Brazdilffee3d32015-07-06 11:48:53 +0100773 // Returns the number of non-exceptional successors. SsaChecker ensures that
774 // these are stored at the beginning of the successor list.
775 size_t NumberOfNormalSuccessors() const {
776 return EndsWithTryBoundary() ? 1 : GetSuccessors().Size();
777 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000778
779 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100780 // created, latter block. Note that this method will add the block to the
781 // graph, create a Goto at the end of the former block and will create an edge
782 // between the blocks. It will not, however, update the reverse post order or
783 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000784 HBasicBlock* SplitBefore(HInstruction* cursor);
785
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000786 // Split the block into two blocks just after `cursor`. Returns the newly
787 // created block. Note that this method just updates raw block information,
788 // like predecessors, successors, dominators, and instruction list. It does not
789 // update the graph, reverse post order, loop information, nor make sure the
790 // blocks are consistent (for example ending with a control flow instruction).
791 HBasicBlock* SplitAfter(HInstruction* cursor);
792
793 // Merge `other` at the end of `this`. Successors and dominated blocks of
794 // `other` are changed to be successors and dominated blocks of `this`. Note
795 // that this method does not update the graph, reverse post order, loop
796 // information, nor make sure the blocks are consistent (for example ending
797 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100798 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000799
800 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
801 // of `this` are moved to `other`.
802 // Note that this method does not update the graph, reverse post order, loop
803 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000804 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000805 void ReplaceWith(HBasicBlock* other);
806
David Brazdil2d7352b2015-04-20 14:52:42 +0100807 // Merge `other` at the end of `this`. This method updates loops, reverse post
808 // order, links to predecessors, successors, dominators and deletes the block
809 // from the graph. The two blocks must be successive, i.e. `this` the only
810 // predecessor of `other` and vice versa.
811 void MergeWith(HBasicBlock* other);
812
813 // Disconnects `this` from all its predecessors, successors and dominator,
814 // removes it from all loops it is included in and eventually from the graph.
815 // The block must not dominate any other block. Predecessors and successors
816 // are safely updated.
817 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000818
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000819 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100820 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100821 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100822 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100823 // Replace instruction `initial` with `replacement` within this block.
824 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
825 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100826 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100827 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000828 // RemoveInstruction and RemovePhi delete a given instruction from the respective
829 // instruction list. With 'ensure_safety' set to true, it verifies that the
830 // instruction is not in use and removes it from the use lists of its inputs.
831 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
832 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100833 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100834
835 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100836 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100837 }
838
Roland Levillain6b879dd2014-09-22 17:13:44 +0100839 bool IsLoopPreHeaderFirstPredecessor() const {
840 DCHECK(IsLoopHeader());
841 DCHECK(!GetPredecessors().IsEmpty());
842 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
843 }
844
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100845 HLoopInformation* GetLoopInformation() const {
846 return loop_information_;
847 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000848
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000849 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100850 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000851 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100852 void SetInLoop(HLoopInformation* info) {
853 if (IsLoopHeader()) {
854 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100855 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100856 loop_information_ = info;
857 } else if (loop_information_->Contains(*info->GetHeader())) {
858 // Block is currently part of an outer loop. Make it part of this inner loop.
859 // Note that a non loop header having a loop information means this loop information
860 // has already been populated
861 loop_information_ = info;
862 } else {
863 // Block is part of an inner loop. Do not update the loop information.
864 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
865 // at this point, because this method is being called while populating `info`.
866 }
867 }
868
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000869 // Raw update of the loop information.
870 void SetLoopInformation(HLoopInformation* info) {
871 loop_information_ = info;
872 }
873
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100874 bool IsInLoop() const { return loop_information_ != nullptr; }
875
David Brazdilffee3d32015-07-06 11:48:53 +0100876 HTryBoundary* GetTryEntry() const { return try_entry_; }
877 void SetTryEntry(HTryBoundary* try_entry) { try_entry_ = try_entry; }
878 bool IsInTry() const { return try_entry_ != nullptr; }
879
880 // Returns the try entry that this block's successors should have. They will
881 // be in the same try, unless the block ends in a try boundary. In that case,
882 // the appropriate try entry will be returned.
883 HTryBoundary* ComputeTryEntryOfSuccessors() const;
884
David Brazdila4b8c212015-05-07 09:59:30 +0100885 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100886 bool Dominates(HBasicBlock* block) const;
887
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100888 size_t GetLifetimeStart() const { return lifetime_start_; }
889 size_t GetLifetimeEnd() const { return lifetime_end_; }
890
891 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
892 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
893
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100894 uint32_t GetDexPc() const { return dex_pc_; }
895
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000896 bool IsCatchBlock() const { return is_catch_block_; }
897 void SetIsCatchBlock() { is_catch_block_ = true; }
898
David Brazdil8d5b8b22015-03-24 10:51:52 +0000899 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000900 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100901 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000902 bool HasSinglePhi() const;
903
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000904 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000905 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000906 GrowableArray<HBasicBlock*> predecessors_;
David Brazdilb618ade2015-07-29 10:31:29 +0100907 GrowableArray<HInstruction*> exceptional_predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000908 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100909 HInstructionList instructions_;
910 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000911 HLoopInformation* loop_information_;
912 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100913 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000914 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100915 // The dex program counter of the first instruction of this block.
916 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100917 size_t lifetime_start_;
918 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000919 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000920
David Brazdilffee3d32015-07-06 11:48:53 +0100921 // If this block is in a try block, `try_entry_` stores one of, possibly
922 // several, TryBoundary instructions entering it.
923 HTryBoundary* try_entry_;
924
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000925 friend class HGraph;
926 friend class HInstruction;
927
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000928 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
929};
930
David Brazdilb2bd1c52015-03-25 11:17:37 +0000931// Iterates over the LoopInformation of all loops which contain 'block'
932// from the innermost to the outermost.
933class HLoopInformationOutwardIterator : public ValueObject {
934 public:
935 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
936 : current_(block.GetLoopInformation()) {}
937
938 bool Done() const { return current_ == nullptr; }
939
940 void Advance() {
941 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100942 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000943 }
944
945 HLoopInformation* Current() const {
946 DCHECK(!Done());
947 return current_;
948 }
949
950 private:
951 HLoopInformation* current_;
952
953 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
954};
955
Alexandre Ramesef20f712015-06-09 10:29:30 +0100956#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100957 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000958 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000959 M(ArrayGet, Instruction) \
960 M(ArrayLength, Instruction) \
961 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100962 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000963 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000964 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000965 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +0100966 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100967 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000968 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100969 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100970 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700971 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000972 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000973 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000974 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100975 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000976 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100977 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000978 M(FloatConstant, Constant) \
979 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100980 M(GreaterThan, Condition) \
981 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100982 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000983 M(InstanceFieldGet, Instruction) \
984 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000985 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100986 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000987 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000988 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100989 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000990 M(LessThan, Condition) \
991 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000992 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000993 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100994 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000995 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100996 M(Local, Instruction) \
997 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100998 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000999 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001000 M(Mul, BinaryOperation) \
1001 M(Neg, UnaryOperation) \
1002 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001003 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001004 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001005 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001006 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001007 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001008 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001009 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001010 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001011 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001012 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001013 M(Return, Instruction) \
1014 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001015 M(Shl, BinaryOperation) \
1016 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001017 M(StaticFieldGet, Instruction) \
1018 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001019 M(StoreLocal, Instruction) \
1020 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001021 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001022 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001023 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001024 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001025 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001026 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001027 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001028
Alexandre Ramesef20f712015-06-09 10:29:30 +01001029#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1030
1031#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1032
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001033#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1034
Alexandre Ramesef20f712015-06-09 10:29:30 +01001035#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1036
1037#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1038
1039#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1040 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1041 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1042 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001043 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001044 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1045 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1046
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001047#define FOR_EACH_INSTRUCTION(M) \
1048 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1049 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001050 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001051 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001052 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001053
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001054#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001055FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1056#undef FORWARD_DECLARATION
1057
Roland Levillainccc07a92014-09-16 14:48:16 +01001058#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001059 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1060 const char* DebugName() const OVERRIDE { return #type; } \
1061 const H##type* As##type() const OVERRIDE { return this; } \
1062 H##type* As##type() OVERRIDE { return this; } \
1063 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001064 return other->Is##type(); \
1065 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001066 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001067
David Brazdiled596192015-01-23 10:39:45 +00001068template <typename T> class HUseList;
1069
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001070template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001071class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001072 public:
David Brazdiled596192015-01-23 10:39:45 +00001073 HUseListNode* GetPrevious() const { return prev_; }
1074 HUseListNode* GetNext() const { return next_; }
1075 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001076 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001077 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001078
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001079 private:
David Brazdiled596192015-01-23 10:39:45 +00001080 HUseListNode(T user, size_t index)
1081 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1082
1083 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001084 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001085 HUseListNode<T>* prev_;
1086 HUseListNode<T>* next_;
1087
1088 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001089
1090 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1091};
1092
David Brazdiled596192015-01-23 10:39:45 +00001093template <typename T>
1094class HUseList : public ValueObject {
1095 public:
1096 HUseList() : first_(nullptr) {}
1097
1098 void Clear() {
1099 first_ = nullptr;
1100 }
1101
1102 // Adds a new entry at the beginning of the use list and returns
1103 // the newly created node.
1104 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001105 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001106 if (IsEmpty()) {
1107 first_ = new_node;
1108 } else {
1109 first_->prev_ = new_node;
1110 new_node->next_ = first_;
1111 first_ = new_node;
1112 }
1113 return new_node;
1114 }
1115
1116 HUseListNode<T>* GetFirst() const {
1117 return first_;
1118 }
1119
1120 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001121 DCHECK(node != nullptr);
1122 DCHECK(Contains(node));
1123
David Brazdiled596192015-01-23 10:39:45 +00001124 if (node->prev_ != nullptr) {
1125 node->prev_->next_ = node->next_;
1126 }
1127 if (node->next_ != nullptr) {
1128 node->next_->prev_ = node->prev_;
1129 }
1130 if (node == first_) {
1131 first_ = node->next_;
1132 }
1133 }
1134
David Brazdil1abb4192015-02-17 18:33:36 +00001135 bool Contains(const HUseListNode<T>* node) const {
1136 if (node == nullptr) {
1137 return false;
1138 }
1139 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1140 if (current == node) {
1141 return true;
1142 }
1143 }
1144 return false;
1145 }
1146
David Brazdiled596192015-01-23 10:39:45 +00001147 bool IsEmpty() const {
1148 return first_ == nullptr;
1149 }
1150
1151 bool HasOnlyOneUse() const {
1152 return first_ != nullptr && first_->next_ == nullptr;
1153 }
1154
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001155 size_t SizeSlow() const {
1156 size_t count = 0;
1157 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1158 ++count;
1159 }
1160 return count;
1161 }
1162
David Brazdiled596192015-01-23 10:39:45 +00001163 private:
1164 HUseListNode<T>* first_;
1165};
1166
1167template<typename T>
1168class HUseIterator : public ValueObject {
1169 public:
1170 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1171
1172 bool Done() const { return current_ == nullptr; }
1173
1174 void Advance() {
1175 DCHECK(!Done());
1176 current_ = current_->GetNext();
1177 }
1178
1179 HUseListNode<T>* Current() const {
1180 DCHECK(!Done());
1181 return current_;
1182 }
1183
1184 private:
1185 HUseListNode<T>* current_;
1186
1187 friend class HValue;
1188};
1189
David Brazdil1abb4192015-02-17 18:33:36 +00001190// This class is used by HEnvironment and HInstruction classes to record the
1191// instructions they use and pointers to the corresponding HUseListNodes kept
1192// by the used instructions.
1193template <typename T>
1194class HUserRecord : public ValueObject {
1195 public:
1196 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1197 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1198
1199 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1200 : instruction_(old_record.instruction_), use_node_(use_node) {
1201 DCHECK(instruction_ != nullptr);
1202 DCHECK(use_node_ != nullptr);
1203 DCHECK(old_record.use_node_ == nullptr);
1204 }
1205
1206 HInstruction* GetInstruction() const { return instruction_; }
1207 HUseListNode<T>* GetUseNode() const { return use_node_; }
1208
1209 private:
1210 // Instruction used by the user.
1211 HInstruction* instruction_;
1212
1213 // Corresponding entry in the use list kept by 'instruction_'.
1214 HUseListNode<T>* use_node_;
1215};
1216
Aart Bik854a02b2015-07-14 16:07:00 -07001217/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001218 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001219 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001220 * For write/read dependences on fields/arrays, the dependence analysis uses
1221 * type disambiguation (e.g. a float field write cannot modify the value of an
1222 * integer field read) and the access type (e.g. a reference array write cannot
1223 * modify the value of a reference field read [although it may modify the
1224 * reference fetch prior to reading the field, which is represented by its own
1225 * write/read dependence]). The analysis makes conservative points-to
1226 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1227 * the same, and any reference read depends on any reference read without
1228 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001229 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001230 * The internal representation uses 38-bit and is described in the table below.
1231 * The first line indicates the side effect, and for field/array accesses the
1232 * second line indicates the type of the access (in the order of the
1233 * Primitive::Type enum).
1234 * The two numbered lines below indicate the bit position in the bitfield (read
1235 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001236 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001237 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1238 * +-------------+---------+---------+--------------+---------+---------+
1239 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1240 * | 3 |333333322|222222221| 1 |111111110|000000000|
1241 * | 7 |654321098|765432109| 8 |765432109|876543210|
1242 *
1243 * Note that, to ease the implementation, 'changes' bits are least significant
1244 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001245 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001246class SideEffects : public ValueObject {
1247 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001248 SideEffects() : flags_(0) {}
1249
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001250 static SideEffects None() {
1251 return SideEffects(0);
1252 }
1253
1254 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001255 return SideEffects(kAllChangeBits | kAllDependOnBits);
1256 }
1257
1258 static SideEffects AllChanges() {
1259 return SideEffects(kAllChangeBits);
1260 }
1261
1262 static SideEffects AllDependencies() {
1263 return SideEffects(kAllDependOnBits);
1264 }
1265
1266 static SideEffects AllExceptGCDependency() {
1267 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1268 }
1269
1270 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001271 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001272 }
1273
Aart Bik34c3ba92015-07-20 14:08:59 -07001274 static SideEffects AllWrites() {
1275 return SideEffects(kAllWrites);
1276 }
1277
1278 static SideEffects AllReads() {
1279 return SideEffects(kAllReads);
1280 }
1281
1282 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1283 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001284 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001285 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001286 }
1287
Aart Bik854a02b2015-07-14 16:07:00 -07001288 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1289 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001290 }
1291
Aart Bik34c3ba92015-07-20 14:08:59 -07001292 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1293 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001294 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001295 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001296 }
1297
1298 static SideEffects ArrayReadOfType(Primitive::Type type) {
1299 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1300 }
1301
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001302 static SideEffects CanTriggerGC() {
1303 return SideEffects(1ULL << kCanTriggerGCBit);
1304 }
1305
1306 static SideEffects DependsOnGC() {
1307 return SideEffects(1ULL << kDependsOnGCBit);
1308 }
1309
Aart Bik854a02b2015-07-14 16:07:00 -07001310 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001311 SideEffects Union(SideEffects other) const {
1312 return SideEffects(flags_ | other.flags_);
1313 }
1314
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001315 SideEffects Exclusion(SideEffects other) const {
1316 return SideEffects(flags_ & ~other.flags_);
1317 }
1318
1319 bool Includes(SideEffects other) const {
1320 return (other.flags_ & flags_) == other.flags_;
1321 }
1322
1323 bool HasSideEffects() const {
1324 return (flags_ & kAllChangeBits);
1325 }
1326
1327 bool HasDependencies() const {
1328 return (flags_ & kAllDependOnBits);
1329 }
1330
1331 // Returns true if there are no side effects or dependencies.
1332 bool DoesNothing() const {
1333 return flags_ == 0;
1334 }
1335
Aart Bik854a02b2015-07-14 16:07:00 -07001336 // Returns true if something is written.
1337 bool DoesAnyWrite() const {
1338 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001339 }
1340
Aart Bik854a02b2015-07-14 16:07:00 -07001341 // Returns true if something is read.
1342 bool DoesAnyRead() const {
1343 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001344 }
1345
Aart Bik854a02b2015-07-14 16:07:00 -07001346 // Returns true if potentially everything is written and read
1347 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001348 bool DoesAllReadWrite() const {
1349 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1350 }
1351
Aart Bik854a02b2015-07-14 16:07:00 -07001352 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001353 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001354 }
1355
1356 // Returns true if this may read something written by other.
1357 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001358 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1359 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001360 }
1361
1362 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001363 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001364 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001365 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001366 for (int s = kLastBit; s >= 0; s--) {
1367 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1368 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1369 // This is a bit for the GC side effect.
1370 if (current_bit_is_set) {
1371 flags += "GC";
1372 }
Aart Bik854a02b2015-07-14 16:07:00 -07001373 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001374 } else {
1375 // This is a bit for the array/field analysis.
1376 // The underscore character stands for the 'can trigger GC' bit.
1377 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1378 if (current_bit_is_set) {
1379 flags += kDebug[s];
1380 }
1381 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1382 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1383 flags += "|";
1384 }
1385 }
Aart Bik854a02b2015-07-14 16:07:00 -07001386 }
1387 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001388 }
1389
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001390 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001391
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001392 private:
1393 static constexpr int kFieldArrayAnalysisBits = 9;
1394
1395 static constexpr int kFieldWriteOffset = 0;
1396 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1397 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1398 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1399
1400 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1401
1402 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1403 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1404 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1405 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1406
1407 static constexpr int kLastBit = kDependsOnGCBit;
1408 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1409
1410 // Aliases.
1411
1412 static_assert(kChangeBits == kDependOnBits,
1413 "the 'change' bits should match the 'depend on' bits.");
1414
1415 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1416 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1417 static constexpr uint64_t kAllWrites =
1418 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1419 static constexpr uint64_t kAllReads =
1420 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001421
Aart Bik854a02b2015-07-14 16:07:00 -07001422 // Work around the fact that HIR aliases I/F and J/D.
1423 // TODO: remove this interceptor once HIR types are clean
1424 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1425 switch (type) {
1426 case Primitive::kPrimInt:
1427 case Primitive::kPrimFloat:
1428 return TypeFlag(Primitive::kPrimInt, offset) |
1429 TypeFlag(Primitive::kPrimFloat, offset);
1430 case Primitive::kPrimLong:
1431 case Primitive::kPrimDouble:
1432 return TypeFlag(Primitive::kPrimLong, offset) |
1433 TypeFlag(Primitive::kPrimDouble, offset);
1434 default:
1435 return TypeFlag(type, offset);
1436 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001437 }
1438
Aart Bik854a02b2015-07-14 16:07:00 -07001439 // Translates type to bit flag.
1440 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1441 CHECK_NE(type, Primitive::kPrimVoid);
1442 const uint64_t one = 1;
1443 const int shift = type; // 0-based consecutive enum
1444 DCHECK_LE(kFieldWriteOffset, shift);
1445 DCHECK_LT(shift, kArrayWriteOffset);
1446 return one << (type + offset);
1447 }
1448
1449 // Private constructor on direct flags value.
1450 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1451
1452 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001453};
1454
David Brazdiled596192015-01-23 10:39:45 +00001455// A HEnvironment object contains the values of virtual registers at a given location.
1456class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1457 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001458 HEnvironment(ArenaAllocator* arena,
1459 size_t number_of_vregs,
1460 const DexFile& dex_file,
1461 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001462 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001463 InvokeType invoke_type,
1464 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001465 : vregs_(arena, number_of_vregs),
1466 locations_(arena, number_of_vregs),
1467 parent_(nullptr),
1468 dex_file_(dex_file),
1469 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001470 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001471 invoke_type_(invoke_type),
1472 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001473 vregs_.SetSize(number_of_vregs);
1474 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001475 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001476 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001477
1478 locations_.SetSize(number_of_vregs);
1479 for (size_t i = 0; i < number_of_vregs; ++i) {
1480 locations_.Put(i, Location());
1481 }
1482 }
1483
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001484 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001485 : HEnvironment(arena,
1486 to_copy.Size(),
1487 to_copy.GetDexFile(),
1488 to_copy.GetMethodIdx(),
1489 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001490 to_copy.GetInvokeType(),
1491 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001492
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001493 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001494 if (parent_ != nullptr) {
1495 parent_->SetAndCopyParentChain(allocator, parent);
1496 } else {
1497 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1498 parent_->CopyFrom(parent);
1499 if (parent->GetParent() != nullptr) {
1500 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1501 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001502 }
David Brazdiled596192015-01-23 10:39:45 +00001503 }
1504
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001505 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1506 void CopyFrom(HEnvironment* environment);
1507
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001508 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1509 // input to the loop phi instead. This is for inserting instructions that
1510 // require an environment (like HDeoptimization) in the loop pre-header.
1511 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001512
1513 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001514 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001515 }
1516
1517 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001518 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001519 }
1520
David Brazdil1abb4192015-02-17 18:33:36 +00001521 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001522
1523 size_t Size() const { return vregs_.Size(); }
1524
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001525 HEnvironment* GetParent() const { return parent_; }
1526
1527 void SetLocationAt(size_t index, Location location) {
1528 locations_.Put(index, location);
1529 }
1530
1531 Location GetLocationAt(size_t index) const {
1532 return locations_.Get(index);
1533 }
1534
1535 uint32_t GetDexPc() const {
1536 return dex_pc_;
1537 }
1538
1539 uint32_t GetMethodIdx() const {
1540 return method_idx_;
1541 }
1542
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001543 InvokeType GetInvokeType() const {
1544 return invoke_type_;
1545 }
1546
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001547 const DexFile& GetDexFile() const {
1548 return dex_file_;
1549 }
1550
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001551 HInstruction* GetHolder() const {
1552 return holder_;
1553 }
1554
David Brazdiled596192015-01-23 10:39:45 +00001555 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001556 // Record instructions' use entries of this environment for constant-time removal.
1557 // It should only be called by HInstruction when a new environment use is added.
1558 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1559 DCHECK(env_use->GetUser() == this);
1560 size_t index = env_use->GetIndex();
1561 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1562 }
David Brazdiled596192015-01-23 10:39:45 +00001563
David Brazdil1abb4192015-02-17 18:33:36 +00001564 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001565 GrowableArray<Location> locations_;
1566 HEnvironment* parent_;
1567 const DexFile& dex_file_;
1568 const uint32_t method_idx_;
1569 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001570 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001571
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001572 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001573 HInstruction* const holder_;
1574
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001575 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001576
1577 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1578};
1579
Calin Juravleacf735c2015-02-12 15:25:22 +00001580class ReferenceTypeInfo : ValueObject {
1581 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001582 typedef Handle<mirror::Class> TypeHandle;
1583
Calin Juravle2e768302015-07-28 14:41:11 +00001584 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1585 // The constructor will check that the type_handle is valid.
1586 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001587 }
1588
Calin Juravle2e768302015-07-28 14:41:11 +00001589 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1590
1591 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1592 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001593 }
1594
Calin Juravle2e768302015-07-28 14:41:11 +00001595 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1596 return IsValidHandle(type_handle_);
1597 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001598 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001599
1600 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1601 DCHECK(IsValid());
1602 return GetTypeHandle()->IsObjectClass();
1603 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001604 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001605 DCHECK(IsValid());
1606 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001607 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001608
1609 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1610
Mathieu Chartier90443472015-07-16 20:32:27 -07001611 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001612 DCHECK(IsValid());
1613 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001614 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1615 }
1616
1617 // Returns true if the type information provide the same amount of details.
1618 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001619 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001620 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001621 if (!IsValid() && !rti.IsValid()) {
1622 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001623 return true;
1624 }
Calin Juravle2e768302015-07-28 14:41:11 +00001625 if (!IsValid() || !rti.IsValid()) {
1626 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001627 return false;
1628 }
Calin Juravle2e768302015-07-28 14:41:11 +00001629 return IsExact() == rti.IsExact()
1630 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001631 }
1632
1633 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001634 ReferenceTypeInfo();
1635 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001636
Calin Juravleacf735c2015-02-12 15:25:22 +00001637 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001638 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001639 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001640 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001641 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001642};
1643
1644std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1645
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001646class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001647 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001648 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001649 : previous_(nullptr),
1650 next_(nullptr),
1651 block_(nullptr),
1652 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001653 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001654 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001655 locations_(nullptr),
1656 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001657 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001658 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001659 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001660
Dave Allison20dfc792014-06-16 20:44:29 -07001661 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001662
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001663#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001664 enum InstructionKind {
1665 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1666 };
1667#undef DECLARE_KIND
1668
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001669 HInstruction* GetNext() const { return next_; }
1670 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001671
Calin Juravle77520bc2015-01-12 18:45:46 +00001672 HInstruction* GetNextDisregardingMoves() const;
1673 HInstruction* GetPreviousDisregardingMoves() const;
1674
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001675 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001676 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001677 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001678 bool IsInBlock() const { return block_ != nullptr; }
1679 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001680 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001681
Roland Levillain6b879dd2014-09-22 17:13:44 +01001682 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001683 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001684
1685 virtual void Accept(HGraphVisitor* visitor) = 0;
1686 virtual const char* DebugName() const = 0;
1687
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001688 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001689 void SetRawInputAt(size_t index, HInstruction* input) {
1690 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1691 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001692
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001693 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001694 virtual uint32_t GetDexPc() const {
1695 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1696 " does not need an environment";
1697 UNREACHABLE();
1698 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001699 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001700 virtual bool CanThrow() const { return false; }
Aart Bik854a02b2015-07-14 16:07:00 -07001701
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001702 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001703 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001704
Calin Juravle10e244f2015-01-26 18:54:32 +00001705 // Does not apply for all instructions, but having this at top level greatly
1706 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001707 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001708 virtual bool CanBeNull() const {
1709 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1710 return true;
1711 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001712
Calin Juravle641547a2015-04-21 22:08:51 +01001713 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1714 UNUSED(obj);
1715 return false;
1716 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001717
Calin Juravle2e768302015-07-28 14:41:11 +00001718 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001719
Calin Juravle61d544b2015-02-23 16:46:57 +00001720 ReferenceTypeInfo GetReferenceTypeInfo() const {
1721 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1722 return reference_type_info_;
1723 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001724
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001725 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001726 DCHECK(user != nullptr);
1727 HUseListNode<HInstruction*>* use =
1728 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1729 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001730 }
1731
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001732 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001733 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001734 HUseListNode<HEnvironment*>* env_use =
1735 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1736 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001737 }
1738
David Brazdil1abb4192015-02-17 18:33:36 +00001739 void RemoveAsUserOfInput(size_t input) {
1740 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1741 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1742 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001743
David Brazdil1abb4192015-02-17 18:33:36 +00001744 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1745 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001746
David Brazdiled596192015-01-23 10:39:45 +00001747 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1748 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001749 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001750 bool HasOnlyOneNonEnvironmentUse() const {
1751 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1752 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001753
Roland Levillain6c82d402014-10-13 16:10:27 +01001754 // Does this instruction strictly dominate `other_instruction`?
1755 // Returns false if this instruction and `other_instruction` are the same.
1756 // Aborts if this instruction and `other_instruction` are both phis.
1757 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001758
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001759 int GetId() const { return id_; }
1760 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001761
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001762 int GetSsaIndex() const { return ssa_index_; }
1763 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1764 bool HasSsaIndex() const { return ssa_index_ != -1; }
1765
1766 bool HasEnvironment() const { return environment_ != nullptr; }
1767 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001768 // Set the `environment_` field. Raw because this method does not
1769 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001770 void SetRawEnvironment(HEnvironment* environment) {
1771 DCHECK(environment_ == nullptr);
1772 DCHECK_EQ(environment->GetHolder(), this);
1773 environment_ = environment;
1774 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001775
1776 // Set the environment of this instruction, copying it from `environment`. While
1777 // copying, the uses lists are being updated.
1778 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001779 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001780 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001781 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001782 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001783 if (environment->GetParent() != nullptr) {
1784 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1785 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001786 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001787
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001788 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1789 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001790 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001791 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001792 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001793 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001794 if (environment->GetParent() != nullptr) {
1795 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1796 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001797 }
1798
Nicolas Geoffray39468442014-09-02 15:17:15 +01001799 // Returns the number of entries in the environment. Typically, that is the
1800 // number of dex registers in a method. It could be more in case of inlining.
1801 size_t EnvironmentSize() const;
1802
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001803 LocationSummary* GetLocations() const { return locations_; }
1804 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001805
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001806 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001807 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001808
Alexandre Rames188d4312015-04-09 18:30:21 +01001809 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1810 // uses of this instruction by `other` are *not* updated.
1811 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1812 ReplaceWith(other);
1813 other->ReplaceInput(this, use_index);
1814 }
1815
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001816 // Move `this` instruction before `cursor`.
1817 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001818
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001819#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001820 bool Is##type() const { return (As##type() != nullptr); } \
1821 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001822 virtual H##type* As##type() { return nullptr; }
1823
1824 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1825#undef INSTRUCTION_TYPE_CHECK
1826
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001827 // Returns whether the instruction can be moved within the graph.
1828 virtual bool CanBeMoved() const { return false; }
1829
1830 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001831 virtual bool InstructionTypeEquals(HInstruction* other) const {
1832 UNUSED(other);
1833 return false;
1834 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001835
1836 // Returns whether any data encoded in the two instructions is equal.
1837 // This method does not look at the inputs. Both instructions must be
1838 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001839 virtual bool InstructionDataEquals(HInstruction* other) const {
1840 UNUSED(other);
1841 return false;
1842 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001843
1844 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001845 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001846 // 2) Their inputs are identical.
1847 bool Equals(HInstruction* other) const;
1848
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001849 virtual InstructionKind GetKind() const = 0;
1850
1851 virtual size_t ComputeHashCode() const {
1852 size_t result = GetKind();
1853 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1854 result = (result * 31) + InputAt(i)->GetId();
1855 }
1856 return result;
1857 }
1858
1859 SideEffects GetSideEffects() const { return side_effects_; }
1860
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001861 size_t GetLifetimePosition() const { return lifetime_position_; }
1862 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1863 LiveInterval* GetLiveInterval() const { return live_interval_; }
1864 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1865 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1866
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001867 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1868
1869 // Returns whether the code generation of the instruction will require to have access
1870 // to the current method. Such instructions are:
1871 // (1): Instructions that require an environment, as calling the runtime requires
1872 // to walk the stack and have the current method stored at a specific stack address.
1873 // (2): Object literals like classes and strings, that are loaded from the dex cache
1874 // fields of the current method.
1875 bool NeedsCurrentMethod() const {
1876 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1877 }
1878
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001879 virtual bool NeedsDexCache() const { return false; }
1880
Mark Mendellc4701932015-04-10 13:18:51 -04001881 // Does this instruction have any use in an environment before
1882 // control flow hits 'other'?
1883 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1884
1885 // Remove all references to environment uses of this instruction.
1886 // The caller must ensure that this is safe to do.
1887 void RemoveEnvironmentUsers();
1888
David Brazdil1abb4192015-02-17 18:33:36 +00001889 protected:
1890 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1891 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1892
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001893 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001894 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1895
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001896 HInstruction* previous_;
1897 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001898 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001899
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001900 // An instruction gets an id when it is added to the graph.
1901 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001902 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001903 int id_;
1904
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001905 // When doing liveness analysis, instructions that have uses get an SSA index.
1906 int ssa_index_;
1907
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001908 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001909 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001910
1911 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001912 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001913
Nicolas Geoffray39468442014-09-02 15:17:15 +01001914 // The environment associated with this instruction. Not null if the instruction
1915 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001916 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001917
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001918 // Set by the code generator.
1919 LocationSummary* locations_;
1920
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001921 // Set by the liveness analysis.
1922 LiveInterval* live_interval_;
1923
1924 // Set by the liveness analysis, this is the position in a linear
1925 // order of blocks where this instruction's live interval start.
1926 size_t lifetime_position_;
1927
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001928 const SideEffects side_effects_;
1929
Calin Juravleacf735c2015-02-12 15:25:22 +00001930 // TODO: for primitive types this should be marked as invalid.
1931 ReferenceTypeInfo reference_type_info_;
1932
David Brazdil1abb4192015-02-17 18:33:36 +00001933 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001934 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001935 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001936 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001937 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001938
1939 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1940};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001941std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001942
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001943class HInputIterator : public ValueObject {
1944 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001945 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001946
1947 bool Done() const { return index_ == instruction_->InputCount(); }
1948 HInstruction* Current() const { return instruction_->InputAt(index_); }
1949 void Advance() { index_++; }
1950
1951 private:
1952 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001953 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001954
1955 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1956};
1957
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001958class HInstructionIterator : public ValueObject {
1959 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001960 explicit HInstructionIterator(const HInstructionList& instructions)
1961 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001962 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001963 }
1964
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001965 bool Done() const { return instruction_ == nullptr; }
1966 HInstruction* Current() const { return instruction_; }
1967 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001968 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001969 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001970 }
1971
1972 private:
1973 HInstruction* instruction_;
1974 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001975
1976 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001977};
1978
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001979class HBackwardInstructionIterator : public ValueObject {
1980 public:
1981 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1982 : instruction_(instructions.last_instruction_) {
1983 next_ = Done() ? nullptr : instruction_->GetPrevious();
1984 }
1985
1986 bool Done() const { return instruction_ == nullptr; }
1987 HInstruction* Current() const { return instruction_; }
1988 void Advance() {
1989 instruction_ = next_;
1990 next_ = Done() ? nullptr : instruction_->GetPrevious();
1991 }
1992
1993 private:
1994 HInstruction* instruction_;
1995 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001996
1997 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001998};
1999
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002000// An embedded container with N elements of type T. Used (with partial
2001// specialization for N=0) because embedded arrays cannot have size 0.
2002template<typename T, intptr_t N>
2003class EmbeddedArray {
2004 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002005 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002006
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002007 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002008
2009 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002010 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002011 return elements_[i];
2012 }
2013
2014 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002015 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002016 return elements_[i];
2017 }
2018
2019 const T& At(intptr_t i) const {
2020 return (*this)[i];
2021 }
2022
2023 void SetAt(intptr_t i, const T& val) {
2024 (*this)[i] = val;
2025 }
2026
2027 private:
2028 T elements_[N];
2029};
2030
2031template<typename T>
2032class EmbeddedArray<T, 0> {
2033 public:
2034 intptr_t length() const { return 0; }
2035 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002036 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002037 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002038 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002039 }
2040 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002041 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002042 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002043 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002044 }
2045};
2046
2047template<intptr_t N>
2048class HTemplateInstruction: public HInstruction {
2049 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002050 HTemplateInstruction<N>(SideEffects side_effects)
2051 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002052 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002053
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002054 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002055
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002056 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00002057 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
2058
2059 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
2060 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002061 }
2062
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002063 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002064 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002065
2066 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002067};
2068
Dave Allison20dfc792014-06-16 20:44:29 -07002069template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002070class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002071 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002072 HExpression<N>(Primitive::Type type, SideEffects side_effects)
2073 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002074 virtual ~HExpression() {}
2075
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002076 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002077
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002078 protected:
2079 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002080};
2081
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002082// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2083// instruction that branches to the exit block.
2084class HReturnVoid : public HTemplateInstruction<0> {
2085 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002086 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002087
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002088 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002089
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002090 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002091
2092 private:
2093 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2094};
2095
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002096// Represents dex's RETURN opcodes. A HReturn is a control flow
2097// instruction that branches to the exit block.
2098class HReturn : public HTemplateInstruction<1> {
2099 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002100 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002101 SetRawInputAt(0, value);
2102 }
2103
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002104 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002105
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002106 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002107
2108 private:
2109 DISALLOW_COPY_AND_ASSIGN(HReturn);
2110};
2111
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002112// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002113// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114// exit block.
2115class HExit : public HTemplateInstruction<0> {
2116 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002117 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002118
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002119 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002120
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002121 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002122
2123 private:
2124 DISALLOW_COPY_AND_ASSIGN(HExit);
2125};
2126
2127// Jumps from one block to another.
2128class HGoto : public HTemplateInstruction<0> {
2129 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002130 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2131
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002132 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002133
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002134 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002135 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002136 }
2137
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002138 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002139
2140 private:
2141 DISALLOW_COPY_AND_ASSIGN(HGoto);
2142};
2143
Roland Levillain9867bc72015-08-05 10:21:34 +01002144class HConstant : public HExpression<0> {
2145 public:
2146 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2147
2148 bool CanBeMoved() const OVERRIDE { return true; }
2149
2150 virtual bool IsMinusOne() const { return false; }
2151 virtual bool IsZero() const { return false; }
2152 virtual bool IsOne() const { return false; }
2153
2154 DECLARE_INSTRUCTION(Constant);
2155
2156 private:
2157 DISALLOW_COPY_AND_ASSIGN(HConstant);
2158};
2159
2160class HNullConstant : public HConstant {
2161 public:
2162 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2163 return true;
2164 }
2165
2166 size_t ComputeHashCode() const OVERRIDE { return 0; }
2167
2168 DECLARE_INSTRUCTION(NullConstant);
2169
2170 private:
2171 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2172
2173 friend class HGraph;
2174 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2175};
2176
2177// Constants of the type int. Those can be from Dex instructions, or
2178// synthesized (for example with the if-eqz instruction).
2179class HIntConstant : public HConstant {
2180 public:
2181 int32_t GetValue() const { return value_; }
2182
2183 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2184 DCHECK(other->IsIntConstant());
2185 return other->AsIntConstant()->value_ == value_;
2186 }
2187
2188 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2189
2190 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2191 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2192 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2193
2194 DECLARE_INSTRUCTION(IntConstant);
2195
2196 private:
2197 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2198 explicit HIntConstant(bool value) : HConstant(Primitive::kPrimInt), value_(value ? 1 : 0) {}
2199
2200 const int32_t value_;
2201
2202 friend class HGraph;
2203 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2204 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2205 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2206};
2207
2208class HLongConstant : public HConstant {
2209 public:
2210 int64_t GetValue() const { return value_; }
2211
2212 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2213 DCHECK(other->IsLongConstant());
2214 return other->AsLongConstant()->value_ == value_;
2215 }
2216
2217 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2218
2219 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2220 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2221 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2222
2223 DECLARE_INSTRUCTION(LongConstant);
2224
2225 private:
2226 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2227
2228 const int64_t value_;
2229
2230 friend class HGraph;
2231 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2232};
Dave Allison20dfc792014-06-16 20:44:29 -07002233
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002234// Conditional branch. A block ending with an HIf instruction must have
2235// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002236class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002237 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002238 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002239 SetRawInputAt(0, input);
2240 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002241
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002242 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002243
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002244 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002245 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002246 }
2247
2248 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002249 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002250 }
2251
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002252 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002253
2254 private:
2255 DISALLOW_COPY_AND_ASSIGN(HIf);
2256};
2257
David Brazdilfc6a86a2015-06-26 10:33:45 +00002258
2259// Abstract instruction which marks the beginning and/or end of a try block and
2260// links it to the respective exception handlers. Behaves the same as a Goto in
2261// non-exceptional control flow.
2262// Normal-flow successor is stored at index zero, exception handlers under
2263// higher indices in no particular order.
2264class HTryBoundary : public HTemplateInstruction<0> {
2265 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002266 enum BoundaryKind {
2267 kEntry,
2268 kExit,
2269 };
2270
2271 explicit HTryBoundary(BoundaryKind kind)
2272 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002273
2274 bool IsControlFlow() const OVERRIDE { return true; }
2275
2276 // Returns the block's non-exceptional successor (index zero).
2277 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2278
2279 // Returns whether `handler` is among its exception handlers (non-zero index
2280 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002281 bool HasExceptionHandler(const HBasicBlock& handler) const {
2282 DCHECK(handler.IsCatchBlock());
2283 return GetBlock()->GetSuccessors().Contains(
2284 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002285 }
2286
2287 // If not present already, adds `handler` to its block's list of exception
2288 // handlers.
2289 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002290 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002291 GetBlock()->AddSuccessor(handler);
2292 }
2293 }
2294
David Brazdil56e1acc2015-06-30 15:41:36 +01002295 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002296
David Brazdilffee3d32015-07-06 11:48:53 +01002297 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2298
David Brazdilfc6a86a2015-06-26 10:33:45 +00002299 DECLARE_INSTRUCTION(TryBoundary);
2300
2301 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002302 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002303
2304 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2305};
2306
David Brazdilffee3d32015-07-06 11:48:53 +01002307// Iterator over exception handlers of a given HTryBoundary, i.e. over
2308// exceptional successors of its basic block.
2309class HExceptionHandlerIterator : public ValueObject {
2310 public:
2311 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2312 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2313
2314 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2315 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
2316 size_t CurrentSuccessorIndex() const { return index_; }
2317 void Advance() { ++index_; }
2318
2319 private:
2320 const HBasicBlock& block_;
2321 size_t index_;
2322
2323 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2324};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002325
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002326// Deoptimize to interpreter, upon checking a condition.
2327class HDeoptimize : public HTemplateInstruction<1> {
2328 public:
2329 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2330 : HTemplateInstruction(SideEffects::None()),
2331 dex_pc_(dex_pc) {
2332 SetRawInputAt(0, cond);
2333 }
2334
2335 bool NeedsEnvironment() const OVERRIDE { return true; }
2336 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002337 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002338
2339 DECLARE_INSTRUCTION(Deoptimize);
2340
2341 private:
2342 uint32_t dex_pc_;
2343
2344 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2345};
2346
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002347// Represents the ArtMethod that was passed as a first argument to
2348// the method. It is used by instructions that depend on it, like
2349// instructions that work with the dex cache.
2350class HCurrentMethod : public HExpression<0> {
2351 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002352 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002353
2354 DECLARE_INSTRUCTION(CurrentMethod);
2355
2356 private:
2357 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2358};
2359
Roland Levillain88cb1752014-10-20 16:36:47 +01002360class HUnaryOperation : public HExpression<1> {
2361 public:
2362 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2363 : HExpression(result_type, SideEffects::None()) {
2364 SetRawInputAt(0, input);
2365 }
2366
2367 HInstruction* GetInput() const { return InputAt(0); }
2368 Primitive::Type GetResultType() const { return GetType(); }
2369
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002370 bool CanBeMoved() const OVERRIDE { return true; }
2371 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002372 UNUSED(other);
2373 return true;
2374 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002375
Roland Levillain9240d6a2014-10-20 16:47:04 +01002376 // Try to statically evaluate `operation` and return a HConstant
2377 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002378 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002379 HConstant* TryStaticEvaluation() const;
2380
2381 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002382 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2383 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002384
Roland Levillain88cb1752014-10-20 16:36:47 +01002385 DECLARE_INSTRUCTION(UnaryOperation);
2386
2387 private:
2388 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2389};
2390
Dave Allison20dfc792014-06-16 20:44:29 -07002391class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002392 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002393 HBinaryOperation(Primitive::Type result_type,
2394 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002395 HInstruction* right,
2396 SideEffects side_effects = SideEffects::None())
2397 : HExpression(result_type, side_effects) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002398 SetRawInputAt(0, left);
2399 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002400 }
2401
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002402 HInstruction* GetLeft() const { return InputAt(0); }
2403 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002404 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002405
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002406 virtual bool IsCommutative() const { return false; }
2407
2408 // Put constant on the right.
2409 // Returns whether order is changed.
2410 bool OrderInputsWithConstantOnTheRight() {
2411 HInstruction* left = InputAt(0);
2412 HInstruction* right = InputAt(1);
2413 if (left->IsConstant() && !right->IsConstant()) {
2414 ReplaceInput(right, 0);
2415 ReplaceInput(left, 1);
2416 return true;
2417 }
2418 return false;
2419 }
2420
2421 // Order inputs by instruction id, but favor constant on the right side.
2422 // This helps GVN for commutative ops.
2423 void OrderInputs() {
2424 DCHECK(IsCommutative());
2425 HInstruction* left = InputAt(0);
2426 HInstruction* right = InputAt(1);
2427 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2428 return;
2429 }
2430 if (OrderInputsWithConstantOnTheRight()) {
2431 return;
2432 }
2433 // Order according to instruction id.
2434 if (left->GetId() > right->GetId()) {
2435 ReplaceInput(right, 0);
2436 ReplaceInput(left, 1);
2437 }
2438 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002439
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002440 bool CanBeMoved() const OVERRIDE { return true; }
2441 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002442 UNUSED(other);
2443 return true;
2444 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002445
Roland Levillain9240d6a2014-10-20 16:47:04 +01002446 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002447 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002448 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002449 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002450
2451 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002452 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2453 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2454 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2455 HLongConstant* y ATTRIBUTE_UNUSED) const {
2456 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2457 return nullptr;
2458 }
2459 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2460 HIntConstant* y ATTRIBUTE_UNUSED) const {
2461 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2462 return nullptr;
2463 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002464
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002465 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002466 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002467 HConstant* GetConstantRight() const;
2468
2469 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002470 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002471 HInstruction* GetLeastConstantLeft() const;
2472
Roland Levillainccc07a92014-09-16 14:48:16 +01002473 DECLARE_INSTRUCTION(BinaryOperation);
2474
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002475 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002476 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2477};
2478
Mark Mendellc4701932015-04-10 13:18:51 -04002479// The comparison bias applies for floating point operations and indicates how NaN
2480// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002481enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002482 kNoBias, // bias is not applicable (i.e. for long operation)
2483 kGtBias, // return 1 for NaN comparisons
2484 kLtBias, // return -1 for NaN comparisons
2485};
2486
Dave Allison20dfc792014-06-16 20:44:29 -07002487class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002488 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002489 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002490 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002491 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002492 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002493
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002494 bool NeedsMaterialization() const { return needs_materialization_; }
2495 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002496
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002497 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002498 // `instruction`, and disregard moves in between.
2499 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002500
Dave Allison20dfc792014-06-16 20:44:29 -07002501 DECLARE_INSTRUCTION(Condition);
2502
2503 virtual IfCondition GetCondition() const = 0;
2504
Mark Mendellc4701932015-04-10 13:18:51 -04002505 virtual IfCondition GetOppositeCondition() const = 0;
2506
Roland Levillain4fa13f62015-07-06 18:11:54 +01002507 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002508
2509 void SetBias(ComparisonBias bias) { bias_ = bias; }
2510
2511 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2512 return bias_ == other->AsCondition()->bias_;
2513 }
2514
Roland Levillain4fa13f62015-07-06 18:11:54 +01002515 bool IsFPConditionTrueIfNaN() const {
2516 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2517 IfCondition if_cond = GetCondition();
2518 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2519 }
2520
2521 bool IsFPConditionFalseIfNaN() const {
2522 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2523 IfCondition if_cond = GetCondition();
2524 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2525 }
2526
Dave Allison20dfc792014-06-16 20:44:29 -07002527 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002528 // For register allocation purposes, returns whether this instruction needs to be
2529 // materialized (that is, not just be in the processor flags).
2530 bool needs_materialization_;
2531
Mark Mendellc4701932015-04-10 13:18:51 -04002532 // Needed if we merge a HCompare into a HCondition.
2533 ComparisonBias bias_;
2534
Dave Allison20dfc792014-06-16 20:44:29 -07002535 DISALLOW_COPY_AND_ASSIGN(HCondition);
2536};
2537
2538// Instruction to check if two inputs are equal to each other.
2539class HEqual : public HCondition {
2540 public:
2541 HEqual(HInstruction* first, HInstruction* second)
2542 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002543
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002544 bool IsCommutative() const OVERRIDE { return true; }
2545
Roland Levillain9867bc72015-08-05 10:21:34 +01002546 template <typename T> bool Compute(T x, T y) const { return x == y; }
2547
2548 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2549 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002550 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002551 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2552 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002553 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002554
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002555 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002556
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002557 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002558 return kCondEQ;
2559 }
2560
Mark Mendellc4701932015-04-10 13:18:51 -04002561 IfCondition GetOppositeCondition() const OVERRIDE {
2562 return kCondNE;
2563 }
2564
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002565 private:
2566 DISALLOW_COPY_AND_ASSIGN(HEqual);
2567};
2568
Dave Allison20dfc792014-06-16 20:44:29 -07002569class HNotEqual : public HCondition {
2570 public:
2571 HNotEqual(HInstruction* first, HInstruction* second)
2572 : HCondition(first, second) {}
2573
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002574 bool IsCommutative() const OVERRIDE { return true; }
2575
Roland Levillain9867bc72015-08-05 10:21:34 +01002576 template <typename T> bool Compute(T x, T y) const { return x != y; }
2577
2578 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2579 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002580 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002581 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2582 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002583 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002584
Dave Allison20dfc792014-06-16 20:44:29 -07002585 DECLARE_INSTRUCTION(NotEqual);
2586
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002587 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002588 return kCondNE;
2589 }
2590
Mark Mendellc4701932015-04-10 13:18:51 -04002591 IfCondition GetOppositeCondition() const OVERRIDE {
2592 return kCondEQ;
2593 }
2594
Dave Allison20dfc792014-06-16 20:44:29 -07002595 private:
2596 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2597};
2598
2599class HLessThan : public HCondition {
2600 public:
2601 HLessThan(HInstruction* first, HInstruction* second)
2602 : HCondition(first, second) {}
2603
Roland Levillain9867bc72015-08-05 10:21:34 +01002604 template <typename T> bool Compute(T x, T y) const { return x < y; }
2605
2606 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2607 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002608 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002609 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2610 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002611 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002612
Dave Allison20dfc792014-06-16 20:44:29 -07002613 DECLARE_INSTRUCTION(LessThan);
2614
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002615 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002616 return kCondLT;
2617 }
2618
Mark Mendellc4701932015-04-10 13:18:51 -04002619 IfCondition GetOppositeCondition() const OVERRIDE {
2620 return kCondGE;
2621 }
2622
Dave Allison20dfc792014-06-16 20:44:29 -07002623 private:
2624 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2625};
2626
2627class HLessThanOrEqual : public HCondition {
2628 public:
2629 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2630 : HCondition(first, second) {}
2631
Roland Levillain9867bc72015-08-05 10:21:34 +01002632 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2633
2634 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2635 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002636 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002637 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2638 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002639 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002640
Dave Allison20dfc792014-06-16 20:44:29 -07002641 DECLARE_INSTRUCTION(LessThanOrEqual);
2642
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002643 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002644 return kCondLE;
2645 }
2646
Mark Mendellc4701932015-04-10 13:18:51 -04002647 IfCondition GetOppositeCondition() const OVERRIDE {
2648 return kCondGT;
2649 }
2650
Dave Allison20dfc792014-06-16 20:44:29 -07002651 private:
2652 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2653};
2654
2655class HGreaterThan : public HCondition {
2656 public:
2657 HGreaterThan(HInstruction* first, HInstruction* second)
2658 : HCondition(first, second) {}
2659
Roland Levillain9867bc72015-08-05 10:21:34 +01002660 template <typename T> bool Compute(T x, T y) const { return x > y; }
2661
2662 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2663 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002664 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002665 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2666 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002667 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002668
Dave Allison20dfc792014-06-16 20:44:29 -07002669 DECLARE_INSTRUCTION(GreaterThan);
2670
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002671 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002672 return kCondGT;
2673 }
2674
Mark Mendellc4701932015-04-10 13:18:51 -04002675 IfCondition GetOppositeCondition() const OVERRIDE {
2676 return kCondLE;
2677 }
2678
Dave Allison20dfc792014-06-16 20:44:29 -07002679 private:
2680 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2681};
2682
2683class HGreaterThanOrEqual : public HCondition {
2684 public:
2685 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2686 : HCondition(first, second) {}
2687
Roland Levillain9867bc72015-08-05 10:21:34 +01002688 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2689
2690 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2691 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002692 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002693 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2694 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002695 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002696
Dave Allison20dfc792014-06-16 20:44:29 -07002697 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2698
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002699 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002700 return kCondGE;
2701 }
2702
Mark Mendellc4701932015-04-10 13:18:51 -04002703 IfCondition GetOppositeCondition() const OVERRIDE {
2704 return kCondLT;
2705 }
2706
Dave Allison20dfc792014-06-16 20:44:29 -07002707 private:
2708 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2709};
2710
2711
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002712// Instruction to check how two inputs compare to each other.
2713// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2714class HCompare : public HBinaryOperation {
2715 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002716 HCompare(Primitive::Type type,
2717 HInstruction* first,
2718 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002719 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002720 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002721 : HBinaryOperation(Primitive::kPrimInt, first, second, SideEffectsForArchRuntimeCalls(type)),
2722 bias_(bias),
2723 dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002724 DCHECK_EQ(type, first->GetType());
2725 DCHECK_EQ(type, second->GetType());
2726 }
2727
Roland Levillain9867bc72015-08-05 10:21:34 +01002728 template <typename T>
2729 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002730
Roland Levillain9867bc72015-08-05 10:21:34 +01002731 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2732 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
2733 }
2734 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2735 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain556c3d12014-09-18 15:25:07 +01002736 }
2737
Calin Juravleddb7df22014-11-25 20:56:51 +00002738 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2739 return bias_ == other->AsCompare()->bias_;
2740 }
2741
Mark Mendellc4701932015-04-10 13:18:51 -04002742 ComparisonBias GetBias() const { return bias_; }
2743
Roland Levillain4fa13f62015-07-06 18:11:54 +01002744 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002745
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002746 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
2747
2748 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2749 // MIPS64 uses a runtime call for FP comparisons.
2750 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2751 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002752
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002753 DECLARE_INSTRUCTION(Compare);
2754
2755 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002756 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002757 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002758
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002759 DISALLOW_COPY_AND_ASSIGN(HCompare);
2760};
2761
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002762// A local in the graph. Corresponds to a Dex register.
2763class HLocal : public HTemplateInstruction<0> {
2764 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002765 explicit HLocal(uint16_t reg_number)
2766 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002767
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002768 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002769
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002770 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002771
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002772 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002773 // The Dex register number.
2774 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002775
2776 DISALLOW_COPY_AND_ASSIGN(HLocal);
2777};
2778
2779// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002780class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002781 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002782 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002783 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002784 SetRawInputAt(0, local);
2785 }
2786
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002787 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2788
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002789 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002790
2791 private:
2792 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2793};
2794
2795// Store a value in a given local. This instruction has two inputs: the value
2796// and the local.
2797class HStoreLocal : public HTemplateInstruction<2> {
2798 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002799 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002800 SetRawInputAt(0, local);
2801 SetRawInputAt(1, value);
2802 }
2803
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002804 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2805
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002806 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002807
2808 private:
2809 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2810};
2811
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002812class HFloatConstant : public HConstant {
2813 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002814 float GetValue() const { return value_; }
2815
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002816 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002817 DCHECK(other->IsFloatConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002818 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2819 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002820 }
2821
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002822 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002823
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002824 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002825 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002826 }
2827 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002828 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002829 }
2830 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002831 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2832 }
2833 bool IsNaN() const {
2834 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002835 }
2836
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002837 DECLARE_INSTRUCTION(FloatConstant);
2838
2839 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002840 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002841 explicit HFloatConstant(int32_t value)
2842 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002843
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002844 const float value_;
2845
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002846 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002847 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002848 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002849 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2850};
2851
2852class HDoubleConstant : public HConstant {
2853 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002854 double GetValue() const { return value_; }
2855
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002856 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002857 DCHECK(other->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002858 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2859 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002860 }
2861
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002862 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002863
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002864 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002865 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002866 }
2867 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002868 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002869 }
2870 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002871 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2872 }
2873 bool IsNaN() const {
2874 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002875 }
2876
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002877 DECLARE_INSTRUCTION(DoubleConstant);
2878
2879 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002880 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002881 explicit HDoubleConstant(int64_t value)
2882 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002883
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002884 const double value_;
2885
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002886 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002887 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002888 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002889 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2890};
2891
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002892enum class Intrinsics {
2893#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2894#include "intrinsics_list.h"
2895 kNone,
2896 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2897#undef INTRINSICS_LIST
2898#undef OPTIMIZING_INTRINSICS
2899};
2900std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2901
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002902class HInvoke : public HInstruction {
2903 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002904 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002905
2906 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2907 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002908 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002909
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002910 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002911 SetRawInputAt(index, argument);
2912 }
2913
Roland Levillain3e3d7332015-04-28 11:00:54 +01002914 // Return the number of arguments. This number can be lower than
2915 // the number of inputs returned by InputCount(), as some invoke
2916 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2917 // inputs at the end of their list of inputs.
2918 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2919
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002920 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002921
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002922 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002923
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002924 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002925 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002926
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002927 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2928
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002929 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002930 return intrinsic_;
2931 }
2932
2933 void SetIntrinsic(Intrinsics intrinsic) {
2934 intrinsic_ = intrinsic;
2935 }
2936
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002937 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002938 return GetEnvironment()->GetParent() != nullptr;
2939 }
2940
2941 bool CanThrow() const OVERRIDE { return true; }
2942
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002943 DECLARE_INSTRUCTION(Invoke);
2944
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002945 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002946 HInvoke(ArenaAllocator* arena,
2947 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002948 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002949 Primitive::Type return_type,
2950 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002951 uint32_t dex_method_index,
2952 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002953 : HInstruction(
2954 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01002955 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002956 inputs_(arena, number_of_arguments),
2957 return_type_(return_type),
2958 dex_pc_(dex_pc),
2959 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002960 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002961 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002962 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2963 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002964 }
2965
David Brazdil1abb4192015-02-17 18:33:36 +00002966 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2967 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2968 inputs_.Put(index, input);
2969 }
2970
Roland Levillain3e3d7332015-04-28 11:00:54 +01002971 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002972 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002973 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002974 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002975 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002976 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002977 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002978
2979 private:
2980 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2981};
2982
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002983class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002984 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002985 // Requirements of this method call regarding the class
2986 // initialization (clinit) check of its declaring class.
2987 enum class ClinitCheckRequirement {
2988 kNone, // Class already initialized.
2989 kExplicit, // Static call having explicit clinit check as last input.
2990 kImplicit, // Static call implicitly requiring a clinit check.
2991 };
2992
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002993 HInvokeStaticOrDirect(ArenaAllocator* arena,
2994 uint32_t number_of_arguments,
2995 Primitive::Type return_type,
2996 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002997 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002998 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002999 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003000 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003001 InvokeType invoke_type,
3002 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003003 : HInvoke(arena,
3004 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003005 // There is one extra argument for the HCurrentMethod node, and
3006 // potentially one other if the clinit check is explicit, and one other
3007 // if the method is a string factory.
3008 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
3009 + (string_init_offset ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003010 return_type,
3011 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003012 dex_method_index,
3013 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003014 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01003015 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08003016 clinit_check_requirement_(clinit_check_requirement),
3017 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003018
Calin Juravle641547a2015-04-21 22:08:51 +01003019 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3020 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003021 // We access the method via the dex cache so we can't do an implicit null check.
3022 // TODO: for intrinsics we can generate implicit null checks.
3023 return false;
3024 }
3025
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003026 bool CanBeNull() const OVERRIDE {
3027 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3028 }
3029
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003030 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003031 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003032 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08003033 bool IsStringInit() const { return string_init_offset_ != 0; }
3034 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003035 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003036
Roland Levillain4c0eb422015-04-24 16:43:49 +01003037 // Is this instruction a call to a static method?
3038 bool IsStatic() const {
3039 return GetInvokeType() == kStatic;
3040 }
3041
Roland Levillain3e3d7332015-04-28 11:00:54 +01003042 // Remove the art::HLoadClass instruction set as last input by
3043 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3044 // the initial art::HClinitCheck instruction (only relevant for
3045 // static calls with explicit clinit check).
3046 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003047 DCHECK(IsStaticWithExplicitClinitCheck());
3048 size_t last_input_index = InputCount() - 1;
3049 HInstruction* last_input = InputAt(last_input_index);
3050 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003051 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003052 RemoveAsUserOfInput(last_input_index);
3053 inputs_.DeleteAt(last_input_index);
3054 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3055 DCHECK(IsStaticWithImplicitClinitCheck());
3056 }
3057
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003058 bool IsStringFactoryFor(HFakeString* str) const {
3059 if (!IsStringInit()) return false;
3060 // +1 for the current method.
3061 if (InputCount() == (number_of_arguments_ + 1)) return false;
3062 return InputAt(InputCount() - 1)->AsFakeString() == str;
3063 }
3064
3065 void RemoveFakeStringArgumentAsLastInput() {
3066 DCHECK(IsStringInit());
3067 size_t last_input_index = InputCount() - 1;
3068 HInstruction* last_input = InputAt(last_input_index);
3069 DCHECK(last_input != nullptr);
3070 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3071 RemoveAsUserOfInput(last_input_index);
3072 inputs_.DeleteAt(last_input_index);
3073 }
3074
Roland Levillain4c0eb422015-04-24 16:43:49 +01003075 // Is this a call to a static method whose declaring class has an
3076 // explicit intialization check in the graph?
3077 bool IsStaticWithExplicitClinitCheck() const {
3078 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3079 }
3080
3081 // Is this a call to a static method whose declaring class has an
3082 // implicit intialization check requirement?
3083 bool IsStaticWithImplicitClinitCheck() const {
3084 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3085 }
3086
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003087 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003088
Roland Levillain4c0eb422015-04-24 16:43:49 +01003089 protected:
3090 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3091 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3092 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3093 HInstruction* input = input_record.GetInstruction();
3094 // `input` is the last input of a static invoke marked as having
3095 // an explicit clinit check. It must either be:
3096 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3097 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3098 DCHECK(input != nullptr);
3099 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3100 }
3101 return input_record;
3102 }
3103
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003104 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003105 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003106 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003107 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08003108 // Thread entrypoint offset for string init method if this is a string init invoke.
3109 // Note that there are multiple string init methods, each having its own offset.
3110 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003111
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003112 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003113};
3114
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003115class HInvokeVirtual : public HInvoke {
3116 public:
3117 HInvokeVirtual(ArenaAllocator* arena,
3118 uint32_t number_of_arguments,
3119 Primitive::Type return_type,
3120 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003121 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003122 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003123 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003124 vtable_index_(vtable_index) {}
3125
Calin Juravle641547a2015-04-21 22:08:51 +01003126 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003127 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003128 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003129 }
3130
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003131 uint32_t GetVTableIndex() const { return vtable_index_; }
3132
3133 DECLARE_INSTRUCTION(InvokeVirtual);
3134
3135 private:
3136 const uint32_t vtable_index_;
3137
3138 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3139};
3140
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003141class HInvokeInterface : public HInvoke {
3142 public:
3143 HInvokeInterface(ArenaAllocator* arena,
3144 uint32_t number_of_arguments,
3145 Primitive::Type return_type,
3146 uint32_t dex_pc,
3147 uint32_t dex_method_index,
3148 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003149 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003150 imt_index_(imt_index) {}
3151
Calin Juravle641547a2015-04-21 22:08:51 +01003152 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003153 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003154 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003155 }
3156
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003157 uint32_t GetImtIndex() const { return imt_index_; }
3158 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3159
3160 DECLARE_INSTRUCTION(InvokeInterface);
3161
3162 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003163 const uint32_t imt_index_;
3164
3165 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3166};
3167
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003168class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003169 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003170 HNewInstance(HCurrentMethod* current_method,
3171 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003172 uint16_t type_index,
3173 const DexFile& dex_file,
3174 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003175 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003176 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003177 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003178 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003179 entrypoint_(entrypoint) {
3180 SetRawInputAt(0, current_method);
3181 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003182
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003183 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003184 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003185 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003186
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003187 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003188 bool NeedsEnvironment() const OVERRIDE { return true; }
3189 // It may throw when called on:
3190 // - interfaces
3191 // - abstract/innaccessible/unknown classes
3192 // TODO: optimize when possible.
3193 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003194
Calin Juravle10e244f2015-01-26 18:54:32 +00003195 bool CanBeNull() const OVERRIDE { return false; }
3196
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003197 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3198
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003199 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003200
3201 private:
3202 const uint32_t dex_pc_;
3203 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003204 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003205 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003206
3207 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3208};
3209
Roland Levillain88cb1752014-10-20 16:36:47 +01003210class HNeg : public HUnaryOperation {
3211 public:
3212 explicit HNeg(Primitive::Type result_type, HInstruction* input)
3213 : HUnaryOperation(result_type, input) {}
3214
Roland Levillain9867bc72015-08-05 10:21:34 +01003215 template <typename T> T Compute(T x) const { return -x; }
3216
3217 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3218 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3219 }
3220 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3221 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3222 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003223
Roland Levillain88cb1752014-10-20 16:36:47 +01003224 DECLARE_INSTRUCTION(Neg);
3225
3226 private:
3227 DISALLOW_COPY_AND_ASSIGN(HNeg);
3228};
3229
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003230class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003231 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003232 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003233 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003234 uint32_t dex_pc,
3235 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003236 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003237 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003238 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003239 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003240 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003241 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003242 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003243 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003244 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003245 }
3246
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003247 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003248 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003249 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003250
3251 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003252 bool NeedsEnvironment() const OVERRIDE { return true; }
3253
Mingyao Yang0c365e62015-03-31 15:09:29 -07003254 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3255 bool CanThrow() const OVERRIDE { return true; }
3256
Calin Juravle10e244f2015-01-26 18:54:32 +00003257 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003258
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003259 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3260
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003261 DECLARE_INSTRUCTION(NewArray);
3262
3263 private:
3264 const uint32_t dex_pc_;
3265 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003266 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003267 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003268
3269 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3270};
3271
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003272class HAdd : public HBinaryOperation {
3273 public:
3274 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3275 : HBinaryOperation(result_type, left, right) {}
3276
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003277 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003278
Roland Levillain9867bc72015-08-05 10:21:34 +01003279 template <typename T> T Compute(T x, T y) const { return x + y; }
3280
3281 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3282 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003283 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003284 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3285 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003286 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003287
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003288 DECLARE_INSTRUCTION(Add);
3289
3290 private:
3291 DISALLOW_COPY_AND_ASSIGN(HAdd);
3292};
3293
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003294class HSub : public HBinaryOperation {
3295 public:
3296 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3297 : HBinaryOperation(result_type, left, right) {}
3298
Roland Levillain9867bc72015-08-05 10:21:34 +01003299 template <typename T> T Compute(T x, T y) const { return x - y; }
3300
3301 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3302 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003303 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003304 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3305 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003306 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003307
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003308 DECLARE_INSTRUCTION(Sub);
3309
3310 private:
3311 DISALLOW_COPY_AND_ASSIGN(HSub);
3312};
3313
Calin Juravle34bacdf2014-10-07 20:23:36 +01003314class HMul : public HBinaryOperation {
3315 public:
3316 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3317 : HBinaryOperation(result_type, left, right) {}
3318
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003319 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003320
Roland Levillain9867bc72015-08-05 10:21:34 +01003321 template <typename T> T Compute(T x, T y) const { return x * y; }
3322
3323 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3324 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3325 }
3326 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3327 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3328 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003329
3330 DECLARE_INSTRUCTION(Mul);
3331
3332 private:
3333 DISALLOW_COPY_AND_ASSIGN(HMul);
3334};
3335
Calin Juravle7c4954d2014-10-28 16:57:40 +00003336class HDiv : public HBinaryOperation {
3337 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003338 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003339 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3340 dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003341
Roland Levillain9867bc72015-08-05 10:21:34 +01003342 template <typename T>
3343 T Compute(T x, T y) const {
3344 // Our graph structure ensures we never have 0 for `y` during
3345 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003346 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003347 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003348 return (y == -1) ? -x : x / y;
3349 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003350
Roland Levillain9867bc72015-08-05 10:21:34 +01003351 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3352 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3353 }
3354 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3355 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003356 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003357
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003358 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003359
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003360 static SideEffects SideEffectsForArchRuntimeCalls() {
3361 // The generated code can use a runtime call.
3362 return SideEffects::CanTriggerGC();
3363 }
3364
Calin Juravle7c4954d2014-10-28 16:57:40 +00003365 DECLARE_INSTRUCTION(Div);
3366
3367 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003368 const uint32_t dex_pc_;
3369
Calin Juravle7c4954d2014-10-28 16:57:40 +00003370 DISALLOW_COPY_AND_ASSIGN(HDiv);
3371};
3372
Calin Juravlebacfec32014-11-14 15:54:36 +00003373class HRem : public HBinaryOperation {
3374 public:
3375 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003376 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3377 dex_pc_(dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003378
Roland Levillain9867bc72015-08-05 10:21:34 +01003379 template <typename T>
3380 T Compute(T x, T y) const {
3381 // Our graph structure ensures we never have 0 for `y` during
3382 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003383 DCHECK_NE(y, 0);
3384 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3385 return (y == -1) ? 0 : x % y;
3386 }
3387
Roland Levillain9867bc72015-08-05 10:21:34 +01003388 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3389 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3390 }
3391 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3392 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003393 }
3394
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003395 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003396
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003397 static SideEffects SideEffectsForArchRuntimeCalls() {
3398 return SideEffects::CanTriggerGC();
3399 }
3400
Calin Juravlebacfec32014-11-14 15:54:36 +00003401 DECLARE_INSTRUCTION(Rem);
3402
3403 private:
3404 const uint32_t dex_pc_;
3405
3406 DISALLOW_COPY_AND_ASSIGN(HRem);
3407};
3408
Calin Juravled0d48522014-11-04 16:40:20 +00003409class HDivZeroCheck : public HExpression<1> {
3410 public:
3411 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3412 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3413 SetRawInputAt(0, value);
3414 }
3415
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003416 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3417
Calin Juravled0d48522014-11-04 16:40:20 +00003418 bool CanBeMoved() const OVERRIDE { return true; }
3419
3420 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3421 UNUSED(other);
3422 return true;
3423 }
3424
3425 bool NeedsEnvironment() const OVERRIDE { return true; }
3426 bool CanThrow() const OVERRIDE { return true; }
3427
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003428 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003429
3430 DECLARE_INSTRUCTION(DivZeroCheck);
3431
3432 private:
3433 const uint32_t dex_pc_;
3434
3435 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3436};
3437
Calin Juravle9aec02f2014-11-18 23:06:35 +00003438class HShl : public HBinaryOperation {
3439 public:
3440 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3441 : HBinaryOperation(result_type, left, right) {}
3442
Roland Levillain9867bc72015-08-05 10:21:34 +01003443 template <typename T, typename U, typename V>
3444 T Compute(T x, U y, V max_shift_value) const {
3445 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3446 "V is not the unsigned integer type corresponding to T");
3447 return x << (y & max_shift_value);
3448 }
3449
3450 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3451 return GetBlock()->GetGraph()->GetIntConstant(
3452 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3453 }
3454 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3455 // case is handled as `x << static_cast<int>(y)`.
3456 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3457 return GetBlock()->GetGraph()->GetLongConstant(
3458 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3459 }
3460 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3461 return GetBlock()->GetGraph()->GetLongConstant(
3462 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3463 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003464
3465 DECLARE_INSTRUCTION(Shl);
3466
3467 private:
3468 DISALLOW_COPY_AND_ASSIGN(HShl);
3469};
3470
3471class HShr : public HBinaryOperation {
3472 public:
3473 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3474 : HBinaryOperation(result_type, left, right) {}
3475
Roland Levillain9867bc72015-08-05 10:21:34 +01003476 template <typename T, typename U, typename V>
3477 T Compute(T x, U y, V max_shift_value) const {
3478 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3479 "V is not the unsigned integer type corresponding to T");
3480 return x >> (y & max_shift_value);
3481 }
3482
3483 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3484 return GetBlock()->GetGraph()->GetIntConstant(
3485 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3486 }
3487 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3488 // case is handled as `x >> static_cast<int>(y)`.
3489 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3490 return GetBlock()->GetGraph()->GetLongConstant(
3491 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3492 }
3493 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3494 return GetBlock()->GetGraph()->GetLongConstant(
3495 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3496 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003497
3498 DECLARE_INSTRUCTION(Shr);
3499
3500 private:
3501 DISALLOW_COPY_AND_ASSIGN(HShr);
3502};
3503
3504class HUShr : public HBinaryOperation {
3505 public:
3506 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3507 : HBinaryOperation(result_type, left, right) {}
3508
Roland Levillain9867bc72015-08-05 10:21:34 +01003509 template <typename T, typename U, typename V>
3510 T Compute(T x, U y, V max_shift_value) const {
3511 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3512 "V is not the unsigned integer type corresponding to T");
3513 V ux = static_cast<V>(x);
3514 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003515 }
3516
Roland Levillain9867bc72015-08-05 10:21:34 +01003517 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3518 return GetBlock()->GetGraph()->GetIntConstant(
3519 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3520 }
3521 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3522 // case is handled as `x >>> static_cast<int>(y)`.
3523 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3524 return GetBlock()->GetGraph()->GetLongConstant(
3525 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3526 }
3527 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3528 return GetBlock()->GetGraph()->GetLongConstant(
3529 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003530 }
3531
3532 DECLARE_INSTRUCTION(UShr);
3533
3534 private:
3535 DISALLOW_COPY_AND_ASSIGN(HUShr);
3536};
3537
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003538class HAnd : public HBinaryOperation {
3539 public:
3540 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3541 : HBinaryOperation(result_type, left, right) {}
3542
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003543 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003544
Roland Levillain9867bc72015-08-05 10:21:34 +01003545 template <typename T, typename U>
3546 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3547
3548 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3549 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3550 }
3551 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3552 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3553 }
3554 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3555 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3556 }
3557 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3558 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3559 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003560
3561 DECLARE_INSTRUCTION(And);
3562
3563 private:
3564 DISALLOW_COPY_AND_ASSIGN(HAnd);
3565};
3566
3567class HOr : public HBinaryOperation {
3568 public:
3569 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3570 : HBinaryOperation(result_type, left, right) {}
3571
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003572 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003573
Roland Levillain9867bc72015-08-05 10:21:34 +01003574 template <typename T, typename U>
3575 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3576
3577 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3578 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3579 }
3580 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3581 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3582 }
3583 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3584 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3585 }
3586 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3587 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3588 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003589
3590 DECLARE_INSTRUCTION(Or);
3591
3592 private:
3593 DISALLOW_COPY_AND_ASSIGN(HOr);
3594};
3595
3596class HXor : public HBinaryOperation {
3597 public:
3598 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3599 : HBinaryOperation(result_type, left, right) {}
3600
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003601 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003602
Roland Levillain9867bc72015-08-05 10:21:34 +01003603 template <typename T, typename U>
3604 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3605
3606 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3607 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3608 }
3609 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3610 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3611 }
3612 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3613 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3614 }
3615 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3616 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3617 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003618
3619 DECLARE_INSTRUCTION(Xor);
3620
3621 private:
3622 DISALLOW_COPY_AND_ASSIGN(HXor);
3623};
3624
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003625// The value of a parameter in this method. Its location depends on
3626// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003627class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003628 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003629 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3630 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003631
3632 uint8_t GetIndex() const { return index_; }
3633
Calin Juravle10e244f2015-01-26 18:54:32 +00003634 bool CanBeNull() const OVERRIDE { return !is_this_; }
3635
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003636 bool IsThis() const { return is_this_; }
3637
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003638 DECLARE_INSTRUCTION(ParameterValue);
3639
3640 private:
3641 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003642 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003643 const uint8_t index_;
3644
Calin Juravle10e244f2015-01-26 18:54:32 +00003645 // Whether or not the parameter value corresponds to 'this' argument.
3646 const bool is_this_;
3647
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003648 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3649};
3650
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003651class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003652 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003653 explicit HNot(Primitive::Type result_type, HInstruction* input)
3654 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003655
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003656 bool CanBeMoved() const OVERRIDE { return true; }
3657 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003658 UNUSED(other);
3659 return true;
3660 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003661
Roland Levillain9867bc72015-08-05 10:21:34 +01003662 template <typename T> T Compute(T x) const { return ~x; }
3663
3664 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3665 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3666 }
3667 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3668 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3669 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003670
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003671 DECLARE_INSTRUCTION(Not);
3672
3673 private:
3674 DISALLOW_COPY_AND_ASSIGN(HNot);
3675};
3676
David Brazdil66d126e2015-04-03 16:02:44 +01003677class HBooleanNot : public HUnaryOperation {
3678 public:
3679 explicit HBooleanNot(HInstruction* input)
3680 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3681
3682 bool CanBeMoved() const OVERRIDE { return true; }
3683 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3684 UNUSED(other);
3685 return true;
3686 }
3687
Roland Levillain9867bc72015-08-05 10:21:34 +01003688 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003689 DCHECK(IsUint<1>(x));
3690 return !x;
3691 }
3692
Roland Levillain9867bc72015-08-05 10:21:34 +01003693 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3694 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3695 }
3696 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3697 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003698 UNREACHABLE();
3699 }
3700
3701 DECLARE_INSTRUCTION(BooleanNot);
3702
3703 private:
3704 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3705};
3706
Roland Levillaindff1f282014-11-05 14:15:05 +00003707class HTypeConversion : public HExpression<1> {
3708 public:
3709 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003710 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003711 : HExpression(result_type, SideEffectsForArchRuntimeCalls(input->GetType(), result_type)),
3712 dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003713 SetRawInputAt(0, input);
3714 DCHECK_NE(input->GetType(), result_type);
3715 }
3716
3717 HInstruction* GetInput() const { return InputAt(0); }
3718 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3719 Primitive::Type GetResultType() const { return GetType(); }
3720
Roland Levillain624279f2014-12-04 11:54:28 +00003721 // Required by the x86 and ARM code generators when producing calls
3722 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003723 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003724
Roland Levillaindff1f282014-11-05 14:15:05 +00003725 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003726 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003727
Mark Mendelle82549b2015-05-06 10:55:34 -04003728 // Try to statically evaluate the conversion and return a HConstant
3729 // containing the result. If the input cannot be converted, return nullptr.
3730 HConstant* TryStaticEvaluation() const;
3731
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003732 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3733 Primitive::Type result_type) {
3734 // Some architectures may not require the 'GC' side effects, but at this point
3735 // in the compilation process we do not know what architecture we will
3736 // generate code for, so we must be conservative.
3737 if (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3738 && result_type == Primitive::kPrimLong)
3739 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat)) {
3740 return SideEffects::CanTriggerGC();
3741 }
3742 return SideEffects::None();
3743 }
3744
Roland Levillaindff1f282014-11-05 14:15:05 +00003745 DECLARE_INSTRUCTION(TypeConversion);
3746
3747 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003748 const uint32_t dex_pc_;
3749
Roland Levillaindff1f282014-11-05 14:15:05 +00003750 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3751};
3752
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003753static constexpr uint32_t kNoRegNumber = -1;
3754
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003755class HPhi : public HInstruction {
3756 public:
3757 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003758 : HInstruction(SideEffects::None()),
3759 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003760 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003761 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003762 is_live_(false),
3763 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003764 inputs_.SetSize(number_of_inputs);
3765 }
3766
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003767 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3768 static Primitive::Type ToPhiType(Primitive::Type type) {
3769 switch (type) {
3770 case Primitive::kPrimBoolean:
3771 case Primitive::kPrimByte:
3772 case Primitive::kPrimShort:
3773 case Primitive::kPrimChar:
3774 return Primitive::kPrimInt;
3775 default:
3776 return type;
3777 }
3778 }
3779
David Brazdilffee3d32015-07-06 11:48:53 +01003780 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3781
Calin Juravle10e244f2015-01-26 18:54:32 +00003782 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003783
3784 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003785 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003786
Calin Juravle10e244f2015-01-26 18:54:32 +00003787 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003788 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003789
Calin Juravle10e244f2015-01-26 18:54:32 +00003790 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3791 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3792
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003793 uint32_t GetRegNumber() const { return reg_number_; }
3794
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003795 void SetDead() { is_live_ = false; }
3796 void SetLive() { is_live_ = true; }
3797 bool IsDead() const { return !is_live_; }
3798 bool IsLive() const { return is_live_; }
3799
Calin Juravlea4f88312015-04-16 12:57:19 +01003800 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3801 // An equivalent phi is a phi having the same dex register and type.
3802 // It assumes that phis with the same dex register are adjacent.
3803 HPhi* GetNextEquivalentPhiWithSameType() {
3804 HInstruction* next = GetNext();
3805 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3806 if (next->GetType() == GetType()) {
3807 return next->AsPhi();
3808 }
3809 next = next->GetNext();
3810 }
3811 return nullptr;
3812 }
3813
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003814 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003815
David Brazdil1abb4192015-02-17 18:33:36 +00003816 protected:
3817 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3818
3819 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3820 inputs_.Put(index, input);
3821 }
3822
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003823 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003824 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003825 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003826 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003827 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003828 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003829
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003830 DISALLOW_COPY_AND_ASSIGN(HPhi);
3831};
3832
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003833class HNullCheck : public HExpression<1> {
3834 public:
3835 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003836 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003837 SetRawInputAt(0, value);
3838 }
3839
Calin Juravle10e244f2015-01-26 18:54:32 +00003840 bool CanBeMoved() const OVERRIDE { return true; }
3841 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003842 UNUSED(other);
3843 return true;
3844 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003845
Calin Juravle10e244f2015-01-26 18:54:32 +00003846 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003847
Calin Juravle10e244f2015-01-26 18:54:32 +00003848 bool CanThrow() const OVERRIDE { return true; }
3849
3850 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003851
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003852 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003853
3854 DECLARE_INSTRUCTION(NullCheck);
3855
3856 private:
3857 const uint32_t dex_pc_;
3858
3859 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3860};
3861
3862class FieldInfo : public ValueObject {
3863 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003864 FieldInfo(MemberOffset field_offset,
3865 Primitive::Type field_type,
3866 bool is_volatile,
3867 uint32_t index,
3868 const DexFile& dex_file)
3869 : field_offset_(field_offset),
3870 field_type_(field_type),
3871 is_volatile_(is_volatile),
3872 index_(index),
3873 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003874
3875 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003876 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003877 uint32_t GetFieldIndex() const { return index_; }
3878 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003879 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003880
3881 private:
3882 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003883 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003884 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003885 uint32_t index_;
3886 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003887};
3888
3889class HInstanceFieldGet : public HExpression<1> {
3890 public:
3891 HInstanceFieldGet(HInstruction* value,
3892 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003893 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003894 bool is_volatile,
3895 uint32_t field_idx,
3896 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003897 : HExpression(
3898 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01003899 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003900 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003901 SetRawInputAt(0, value);
3902 }
3903
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003904 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003905
3906 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3907 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3908 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003909 }
3910
Calin Juravle641547a2015-04-21 22:08:51 +01003911 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3912 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003913 }
3914
3915 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003916 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3917 }
3918
Calin Juravle52c48962014-12-16 17:02:57 +00003919 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003920 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003921 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003922 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003923
3924 DECLARE_INSTRUCTION(InstanceFieldGet);
3925
3926 private:
3927 const FieldInfo field_info_;
3928
3929 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3930};
3931
3932class HInstanceFieldSet : public HTemplateInstruction<2> {
3933 public:
3934 HInstanceFieldSet(HInstruction* object,
3935 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003936 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 : HTemplateInstruction(
3942 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003943 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003944 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003945 SetRawInputAt(0, object);
3946 SetRawInputAt(1, value);
3947 }
3948
Calin Juravle641547a2015-04-21 22:08:51 +01003949 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3950 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003951 }
3952
Calin Juravle52c48962014-12-16 17:02:57 +00003953 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003954 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003955 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003956 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003957 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003958 bool GetValueCanBeNull() const { return value_can_be_null_; }
3959 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003960
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003961 DECLARE_INSTRUCTION(InstanceFieldSet);
3962
3963 private:
3964 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003965 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003966
3967 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3968};
3969
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003970class HArrayGet : public HExpression<2> {
3971 public:
3972 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07003973 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003974 SetRawInputAt(0, array);
3975 SetRawInputAt(1, index);
3976 }
3977
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003978 bool CanBeMoved() const OVERRIDE { return true; }
3979 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003980 UNUSED(other);
3981 return true;
3982 }
Calin Juravle641547a2015-04-21 22:08:51 +01003983 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3984 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003985 // TODO: We can be smarter here.
3986 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3987 // which generates the implicit null check. There are cases when these can be removed
3988 // to produce better code. If we ever add optimizations to do so we should allow an
3989 // implicit check here (as long as the address falls in the first page).
3990 return false;
3991 }
3992
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003993 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003994
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003995 HInstruction* GetArray() const { return InputAt(0); }
3996 HInstruction* GetIndex() const { return InputAt(1); }
3997
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003998 DECLARE_INSTRUCTION(ArrayGet);
3999
4000 private:
4001 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4002};
4003
4004class HArraySet : public HTemplateInstruction<3> {
4005 public:
4006 HArraySet(HInstruction* array,
4007 HInstruction* index,
4008 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004009 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004010 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004011 : HTemplateInstruction(
4012 SideEffects::ArrayWriteOfType(expected_component_type).Union(
4013 SideEffectsForArchRuntimeCalls(value->GetType()))),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004014 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004015 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004016 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4017 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004018 SetRawInputAt(0, array);
4019 SetRawInputAt(1, index);
4020 SetRawInputAt(2, value);
4021 }
4022
Calin Juravle77520bc2015-01-12 18:45:46 +00004023 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004024 // We currently always call a runtime method to catch array store
4025 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004026 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004027 }
4028
Mingyao Yang81014cb2015-06-02 03:16:27 -07004029 // Can throw ArrayStoreException.
4030 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4031
Calin Juravle641547a2015-04-21 22:08:51 +01004032 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4033 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004034 // TODO: Same as for ArrayGet.
4035 return false;
4036 }
4037
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004038 void ClearNeedsTypeCheck() {
4039 needs_type_check_ = false;
4040 }
4041
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004042 void ClearValueCanBeNull() {
4043 value_can_be_null_ = false;
4044 }
4045
4046 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004047 bool NeedsTypeCheck() const { return needs_type_check_; }
4048
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004049 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004050
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004051 HInstruction* GetArray() const { return InputAt(0); }
4052 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004053 HInstruction* GetValue() const { return InputAt(2); }
4054
4055 Primitive::Type GetComponentType() const {
4056 // The Dex format does not type floating point index operations. Since the
4057 // `expected_component_type_` is set during building and can therefore not
4058 // be correct, we also check what is the value type. If it is a floating
4059 // point type, we must use that type.
4060 Primitive::Type value_type = GetValue()->GetType();
4061 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4062 ? value_type
4063 : expected_component_type_;
4064 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004065
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004066 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4067 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4068 }
4069
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004070 DECLARE_INSTRUCTION(ArraySet);
4071
4072 private:
4073 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004074 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004075 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004076 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004077
4078 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4079};
4080
4081class HArrayLength : public HExpression<1> {
4082 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004083 explicit HArrayLength(HInstruction* array)
4084 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
4085 // Note that arrays do not change length, so the instruction does not
4086 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004087 SetRawInputAt(0, array);
4088 }
4089
Calin Juravle77520bc2015-01-12 18:45:46 +00004090 bool CanBeMoved() const OVERRIDE { return true; }
4091 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004092 UNUSED(other);
4093 return true;
4094 }
Calin Juravle641547a2015-04-21 22:08:51 +01004095 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4096 return obj == InputAt(0);
4097 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004098
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004099 DECLARE_INSTRUCTION(ArrayLength);
4100
4101 private:
4102 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4103};
4104
4105class HBoundsCheck : public HExpression<2> {
4106 public:
4107 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004108 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004109 DCHECK(index->GetType() == Primitive::kPrimInt);
4110 SetRawInputAt(0, index);
4111 SetRawInputAt(1, length);
4112 }
4113
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004114 bool CanBeMoved() const OVERRIDE { return true; }
4115 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004116 UNUSED(other);
4117 return true;
4118 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004119
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004120 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004121
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004122 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004123
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004124 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004125
4126 DECLARE_INSTRUCTION(BoundsCheck);
4127
4128 private:
4129 const uint32_t dex_pc_;
4130
4131 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4132};
4133
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004134/**
4135 * Some DEX instructions are folded into multiple HInstructions that need
4136 * to stay live until the last HInstruction. This class
4137 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004138 * HInstruction stays live. `index` represents the stack location index of the
4139 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004140 */
4141class HTemporary : public HTemplateInstruction<0> {
4142 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004143 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004144
4145 size_t GetIndex() const { return index_; }
4146
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004147 Primitive::Type GetType() const OVERRIDE {
4148 // The previous instruction is the one that will be stored in the temporary location.
4149 DCHECK(GetPrevious() != nullptr);
4150 return GetPrevious()->GetType();
4151 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004152
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004153 DECLARE_INSTRUCTION(Temporary);
4154
4155 private:
4156 const size_t index_;
4157
4158 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4159};
4160
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004161class HSuspendCheck : public HTemplateInstruction<0> {
4162 public:
4163 explicit HSuspendCheck(uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004164 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004165
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004166 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004167 return true;
4168 }
4169
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004170 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004171 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4172 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004173
4174 DECLARE_INSTRUCTION(SuspendCheck);
4175
4176 private:
4177 const uint32_t dex_pc_;
4178
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004179 // Only used for code generation, in order to share the same slow path between back edges
4180 // of a same loop.
4181 SlowPathCode* slow_path_;
4182
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004183 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4184};
4185
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004186/**
4187 * Instruction to load a Class object.
4188 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004189class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004190 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004191 HLoadClass(HCurrentMethod* current_method,
4192 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004193 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004194 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004195 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004196 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004197 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004198 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004199 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004200 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00004201 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004202 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004203 SetRawInputAt(0, current_method);
4204 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004205
4206 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004207
4208 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4209 return other->AsLoadClass()->type_index_ == type_index_;
4210 }
4211
4212 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4213
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004214 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004215 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004216 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004217 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004218
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004219 bool NeedsEnvironment() const OVERRIDE {
4220 // Will call runtime and load the class if the class is not loaded yet.
4221 // TODO: finer grain decision.
4222 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004223 }
4224
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004225 bool MustGenerateClinitCheck() const {
4226 return generate_clinit_check_;
4227 }
4228
Calin Juravle0ba218d2015-05-19 18:46:01 +01004229 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4230 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004231 }
4232
4233 bool CanCallRuntime() const {
4234 return MustGenerateClinitCheck() || !is_referrers_class_;
4235 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004236
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004237 bool CanThrow() const OVERRIDE {
4238 // May call runtime and and therefore can throw.
4239 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004240 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004241 }
4242
Calin Juravleacf735c2015-02-12 15:25:22 +00004243 ReferenceTypeInfo GetLoadedClassRTI() {
4244 return loaded_class_rti_;
4245 }
4246
4247 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4248 // Make sure we only set exact types (the loaded class should never be merged).
4249 DCHECK(rti.IsExact());
4250 loaded_class_rti_ = rti;
4251 }
4252
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004253 const DexFile& GetDexFile() { return dex_file_; }
4254
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004255 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4256
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004257 static SideEffects SideEffectsForArchRuntimeCalls() {
4258 return SideEffects::CanTriggerGC();
4259 }
4260
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004261 DECLARE_INSTRUCTION(LoadClass);
4262
4263 private:
4264 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004265 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004266 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004267 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004268 // Whether this instruction must generate the initialization check.
4269 // Used for code generation.
4270 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004271
Calin Juravleacf735c2015-02-12 15:25:22 +00004272 ReferenceTypeInfo loaded_class_rti_;
4273
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004274 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4275};
4276
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004277class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004278 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004279 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004280 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004281 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004282 dex_pc_(dex_pc) {
4283 SetRawInputAt(0, current_method);
4284 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004285
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004286 bool CanBeMoved() const OVERRIDE { return true; }
4287
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004288 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4289 return other->AsLoadString()->string_index_ == string_index_;
4290 }
4291
4292 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4293
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004294 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004295 uint32_t GetStringIndex() const { return string_index_; }
4296
4297 // TODO: Can we deopt or debug when we resolve a string?
4298 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004299 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004300
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004301 static SideEffects SideEffectsForArchRuntimeCalls() {
4302 return SideEffects::CanTriggerGC();
4303 }
4304
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004305 DECLARE_INSTRUCTION(LoadString);
4306
4307 private:
4308 const uint32_t string_index_;
4309 const uint32_t dex_pc_;
4310
4311 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4312};
4313
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004314/**
4315 * Performs an initialization check on its Class object input.
4316 */
4317class HClinitCheck : public HExpression<1> {
4318 public:
4319 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004320 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004321 Primitive::kPrimNot,
4322 SideEffects::AllChanges()), // Assume write/read on all fields/arrays.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004323 dex_pc_(dex_pc) {
4324 SetRawInputAt(0, constant);
4325 }
4326
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004327 bool CanBeMoved() const OVERRIDE { return true; }
4328 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4329 UNUSED(other);
4330 return true;
4331 }
4332
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004333 bool NeedsEnvironment() const OVERRIDE {
4334 // May call runtime to initialize the class.
4335 return true;
4336 }
4337
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004338 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004339
4340 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4341
4342 DECLARE_INSTRUCTION(ClinitCheck);
4343
4344 private:
4345 const uint32_t dex_pc_;
4346
4347 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4348};
4349
4350class HStaticFieldGet : public HExpression<1> {
4351 public:
4352 HStaticFieldGet(HInstruction* cls,
4353 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004354 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004355 bool is_volatile,
4356 uint32_t field_idx,
4357 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004358 : HExpression(
4359 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004360 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004361 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004362 SetRawInputAt(0, cls);
4363 }
4364
Calin Juravle52c48962014-12-16 17:02:57 +00004365
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004366 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004367
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004368 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004369 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4370 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004371 }
4372
4373 size_t ComputeHashCode() const OVERRIDE {
4374 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4375 }
4376
Calin Juravle52c48962014-12-16 17:02:57 +00004377 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004378 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4379 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004380 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004381
4382 DECLARE_INSTRUCTION(StaticFieldGet);
4383
4384 private:
4385 const FieldInfo field_info_;
4386
4387 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4388};
4389
4390class HStaticFieldSet : public HTemplateInstruction<2> {
4391 public:
4392 HStaticFieldSet(HInstruction* cls,
4393 HInstruction* value,
4394 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004395 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004396 bool is_volatile,
4397 uint32_t field_idx,
4398 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004399 : HTemplateInstruction(
4400 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004401 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004402 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004403 SetRawInputAt(0, cls);
4404 SetRawInputAt(1, value);
4405 }
4406
Calin Juravle52c48962014-12-16 17:02:57 +00004407 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004408 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4409 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004410 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004411
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004412 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004413 bool GetValueCanBeNull() const { return value_can_be_null_; }
4414 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004415
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004416 DECLARE_INSTRUCTION(StaticFieldSet);
4417
4418 private:
4419 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004420 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004421
4422 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4423};
4424
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004425// Implement the move-exception DEX instruction.
4426class HLoadException : public HExpression<0> {
4427 public:
4428 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4429
4430 DECLARE_INSTRUCTION(LoadException);
4431
4432 private:
4433 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4434};
4435
David Brazdilcb1c0552015-08-04 16:22:25 +01004436// Implicit part of move-exception which clears thread-local exception storage.
4437// Must not be removed because the runtime expects the TLS to get cleared.
4438class HClearException : public HTemplateInstruction<0> {
4439 public:
4440 HClearException() : HTemplateInstruction(SideEffects::AllWrites()) {}
4441
4442 DECLARE_INSTRUCTION(ClearException);
4443
4444 private:
4445 DISALLOW_COPY_AND_ASSIGN(HClearException);
4446};
4447
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004448class HThrow : public HTemplateInstruction<1> {
4449 public:
4450 HThrow(HInstruction* exception, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004451 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004452 SetRawInputAt(0, exception);
4453 }
4454
4455 bool IsControlFlow() const OVERRIDE { return true; }
4456
4457 bool NeedsEnvironment() const OVERRIDE { return true; }
4458
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004459 bool CanThrow() const OVERRIDE { return true; }
4460
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004461 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004462
4463 DECLARE_INSTRUCTION(Throw);
4464
4465 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004466 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004467
4468 DISALLOW_COPY_AND_ASSIGN(HThrow);
4469};
4470
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004471class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004472 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004473 HInstanceOf(HInstruction* object,
4474 HLoadClass* constant,
4475 bool class_is_final,
4476 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004477 : HExpression(Primitive::kPrimBoolean, SideEffectsForArchRuntimeCalls(class_is_final)),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004478 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004479 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004480 dex_pc_(dex_pc) {
4481 SetRawInputAt(0, object);
4482 SetRawInputAt(1, constant);
4483 }
4484
4485 bool CanBeMoved() const OVERRIDE { return true; }
4486
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004487 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004488 return true;
4489 }
4490
4491 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004492 return false;
4493 }
4494
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004495 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004496
4497 bool IsClassFinal() const { return class_is_final_; }
4498
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004499 // Used only in code generation.
4500 bool MustDoNullCheck() const { return must_do_null_check_; }
4501 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4502
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004503 static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) {
4504 return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC();
4505 }
4506
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004507 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004508
4509 private:
4510 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004511 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004512 const uint32_t dex_pc_;
4513
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004514 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4515};
4516
Calin Juravleb1498f62015-02-16 13:13:29 +00004517class HBoundType : public HExpression<1> {
4518 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004519 // Constructs an HBoundType with the given upper_bound.
4520 // Ensures that the upper_bound is valid.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004521 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004522 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004523 upper_bound_(upper_bound),
4524 upper_can_be_null_(upper_can_be_null),
4525 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004526 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004527 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004528 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004529 }
4530
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004531 // GetUpper* should only be used in reference type propagation.
4532 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4533 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004534
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004535 void SetCanBeNull(bool can_be_null) {
4536 DCHECK(upper_can_be_null_ || !can_be_null);
4537 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004538 }
4539
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004540 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4541
Calin Juravleb1498f62015-02-16 13:13:29 +00004542 DECLARE_INSTRUCTION(BoundType);
4543
4544 private:
4545 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004546 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4547 // It is used to bound the type in cases like:
4548 // if (x instanceof ClassX) {
4549 // // uper_bound_ will be ClassX
4550 // }
4551 const ReferenceTypeInfo upper_bound_;
4552 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4553 // is false then can_be_null_ cannot be true).
4554 const bool upper_can_be_null_;
4555 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004556
4557 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4558};
4559
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004560class HCheckCast : public HTemplateInstruction<2> {
4561 public:
4562 HCheckCast(HInstruction* object,
4563 HLoadClass* constant,
4564 bool class_is_final,
4565 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004566 : HTemplateInstruction(SideEffects::CanTriggerGC()),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004567 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004568 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004569 dex_pc_(dex_pc) {
4570 SetRawInputAt(0, object);
4571 SetRawInputAt(1, constant);
4572 }
4573
4574 bool CanBeMoved() const OVERRIDE { return true; }
4575
4576 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4577 return true;
4578 }
4579
4580 bool NeedsEnvironment() const OVERRIDE {
4581 // Instruction may throw a CheckCastError.
4582 return true;
4583 }
4584
4585 bool CanThrow() const OVERRIDE { return true; }
4586
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004587 bool MustDoNullCheck() const { return must_do_null_check_; }
4588 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4589
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004590 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004591
4592 bool IsClassFinal() const { return class_is_final_; }
4593
4594 DECLARE_INSTRUCTION(CheckCast);
4595
4596 private:
4597 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004598 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004599 const uint32_t dex_pc_;
4600
4601 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004602};
4603
Calin Juravle27df7582015-04-17 19:12:31 +01004604class HMemoryBarrier : public HTemplateInstruction<0> {
4605 public:
4606 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004607 : HTemplateInstruction(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004608 SideEffects::AllWritesAndReads()), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004609 barrier_kind_(barrier_kind) {}
4610
4611 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4612
4613 DECLARE_INSTRUCTION(MemoryBarrier);
4614
4615 private:
4616 const MemBarrierKind barrier_kind_;
4617
4618 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4619};
4620
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004621class HMonitorOperation : public HTemplateInstruction<1> {
4622 public:
4623 enum OperationKind {
4624 kEnter,
4625 kExit,
4626 };
4627
4628 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004629 : HTemplateInstruction(
4630 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Aart Bik854a02b2015-07-14 16:07:00 -07004631 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004632 SetRawInputAt(0, object);
4633 }
4634
4635 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004636 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4637
4638 bool CanThrow() const OVERRIDE {
4639 // Verifier guarantees that monitor-exit cannot throw.
4640 // This is important because it allows the HGraphBuilder to remove
4641 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4642 return IsEnter();
4643 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004644
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004645 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004646
4647 bool IsEnter() const { return kind_ == kEnter; }
4648
4649 DECLARE_INSTRUCTION(MonitorOperation);
4650
Calin Juravle52c48962014-12-16 17:02:57 +00004651 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004652 const OperationKind kind_;
4653 const uint32_t dex_pc_;
4654
4655 private:
4656 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4657};
4658
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004659/**
4660 * A HInstruction used as a marker for the replacement of new + <init>
4661 * of a String to a call to a StringFactory. Only baseline will see
4662 * the node at code generation, where it will be be treated as null.
4663 * When compiling non-baseline, `HFakeString` instructions are being removed
4664 * in the instruction simplifier.
4665 */
4666class HFakeString : public HTemplateInstruction<0> {
4667 public:
4668 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4669
4670 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4671
4672 DECLARE_INSTRUCTION(FakeString);
4673
4674 private:
4675 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4676};
4677
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004678class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004679 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004680 MoveOperands(Location source,
4681 Location destination,
4682 Primitive::Type type,
4683 HInstruction* instruction)
4684 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004685
4686 Location GetSource() const { return source_; }
4687 Location GetDestination() const { return destination_; }
4688
4689 void SetSource(Location value) { source_ = value; }
4690 void SetDestination(Location value) { destination_ = value; }
4691
4692 // The parallel move resolver marks moves as "in-progress" by clearing the
4693 // destination (but not the source).
4694 Location MarkPending() {
4695 DCHECK(!IsPending());
4696 Location dest = destination_;
4697 destination_ = Location::NoLocation();
4698 return dest;
4699 }
4700
4701 void ClearPending(Location dest) {
4702 DCHECK(IsPending());
4703 destination_ = dest;
4704 }
4705
4706 bool IsPending() const {
4707 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4708 return destination_.IsInvalid() && !source_.IsInvalid();
4709 }
4710
4711 // True if this blocks a move from the given location.
4712 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004713 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004714 }
4715
4716 // A move is redundant if it's been eliminated, if its source and
4717 // destination are the same, or if its destination is unneeded.
4718 bool IsRedundant() const {
4719 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4720 }
4721
4722 // We clear both operands to indicate move that's been eliminated.
4723 void Eliminate() {
4724 source_ = destination_ = Location::NoLocation();
4725 }
4726
4727 bool IsEliminated() const {
4728 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4729 return source_.IsInvalid();
4730 }
4731
Alexey Frunze4dda3372015-06-01 18:31:49 -07004732 Primitive::Type GetType() const { return type_; }
4733
Nicolas Geoffray90218252015-04-15 11:56:51 +01004734 bool Is64BitMove() const {
4735 return Primitive::Is64BitType(type_);
4736 }
4737
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004738 HInstruction* GetInstruction() const { return instruction_; }
4739
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004740 private:
4741 Location source_;
4742 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004743 // The type this move is for.
4744 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004745 // The instruction this move is assocatied with. Null when this move is
4746 // for moving an input in the expected locations of user (including a phi user).
4747 // This is only used in debug mode, to ensure we do not connect interval siblings
4748 // in the same parallel move.
4749 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004750};
4751
4752static constexpr size_t kDefaultNumberOfMoves = 4;
4753
4754class HParallelMove : public HTemplateInstruction<0> {
4755 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004756 explicit HParallelMove(ArenaAllocator* arena)
4757 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004758
Nicolas Geoffray90218252015-04-15 11:56:51 +01004759 void AddMove(Location source,
4760 Location destination,
4761 Primitive::Type type,
4762 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004763 DCHECK(source.IsValid());
4764 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004765 if (kIsDebugBuild) {
4766 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004767 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004768 if (moves_.Get(i).GetInstruction() == instruction) {
4769 // Special case the situation where the move is for the spill slot
4770 // of the instruction.
4771 if ((GetPrevious() == instruction)
4772 || ((GetPrevious() == nullptr)
4773 && instruction->IsPhi()
4774 && instruction->GetBlock() == GetBlock())) {
4775 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4776 << "Doing parallel moves for the same instruction.";
4777 } else {
4778 DCHECK(false) << "Doing parallel moves for the same instruction.";
4779 }
4780 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004781 }
4782 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004783 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004784 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004785 << "Overlapped destination for two moves in a parallel move: "
4786 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4787 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004788 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004789 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004790 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004791 }
4792
4793 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004794 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004795 }
4796
4797 size_t NumMoves() const { return moves_.Size(); }
4798
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004799 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004800
4801 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004802 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004803
4804 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4805};
4806
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004807class HGraphVisitor : public ValueObject {
4808 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004809 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4810 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004811
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004812 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004813 virtual void VisitBasicBlock(HBasicBlock* block);
4814
Roland Levillain633021e2014-10-01 14:12:25 +01004815 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004816 void VisitInsertionOrder();
4817
Roland Levillain633021e2014-10-01 14:12:25 +01004818 // Visit the graph following dominator tree reverse post-order.
4819 void VisitReversePostOrder();
4820
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004821 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004822
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004823 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004824#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004825 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4826
4827 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4828
4829#undef DECLARE_VISIT_INSTRUCTION
4830
4831 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004832 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004833
4834 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4835};
4836
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004837class HGraphDelegateVisitor : public HGraphVisitor {
4838 public:
4839 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4840 virtual ~HGraphDelegateVisitor() {}
4841
4842 // Visit functions that delegate to to super class.
4843#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004844 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004845
4846 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4847
4848#undef DECLARE_VISIT_INSTRUCTION
4849
4850 private:
4851 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4852};
4853
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004854class HInsertionOrderIterator : public ValueObject {
4855 public:
4856 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4857
4858 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4859 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4860 void Advance() { ++index_; }
4861
4862 private:
4863 const HGraph& graph_;
4864 size_t index_;
4865
4866 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4867};
4868
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004869class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004870 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004871 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4872 // Check that reverse post order of the graph has been built.
4873 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4874 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004875
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004876 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4877 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004878 void Advance() { ++index_; }
4879
4880 private:
4881 const HGraph& graph_;
4882 size_t index_;
4883
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004884 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004885};
4886
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004887class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004888 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004889 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004890 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4891 // Check that reverse post order of the graph has been built.
4892 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4893 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004894
4895 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004896 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004897 void Advance() { --index_; }
4898
4899 private:
4900 const HGraph& graph_;
4901 size_t index_;
4902
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004903 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004904};
4905
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004906class HLinearPostOrderIterator : public ValueObject {
4907 public:
4908 explicit HLinearPostOrderIterator(const HGraph& graph)
4909 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4910
4911 bool Done() const { return index_ == 0; }
4912
4913 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4914
4915 void Advance() {
4916 --index_;
4917 DCHECK_GE(index_, 0U);
4918 }
4919
4920 private:
4921 const GrowableArray<HBasicBlock*>& order_;
4922 size_t index_;
4923
4924 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4925};
4926
4927class HLinearOrderIterator : public ValueObject {
4928 public:
4929 explicit HLinearOrderIterator(const HGraph& graph)
4930 : order_(graph.GetLinearOrder()), index_(0) {}
4931
4932 bool Done() const { return index_ == order_.Size(); }
4933 HBasicBlock* Current() const { return order_.Get(index_); }
4934 void Advance() { ++index_; }
4935
4936 private:
4937 const GrowableArray<HBasicBlock*>& order_;
4938 size_t index_;
4939
4940 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4941};
4942
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004943// Iterator over the blocks that art part of the loop. Includes blocks part
4944// of an inner loop. The order in which the blocks are iterated is on their
4945// block id.
4946class HBlocksInLoopIterator : public ValueObject {
4947 public:
4948 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4949 : blocks_in_loop_(info.GetBlocks()),
4950 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4951 index_(0) {
4952 if (!blocks_in_loop_.IsBitSet(index_)) {
4953 Advance();
4954 }
4955 }
4956
4957 bool Done() const { return index_ == blocks_.Size(); }
4958 HBasicBlock* Current() const { return blocks_.Get(index_); }
4959 void Advance() {
4960 ++index_;
4961 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4962 if (blocks_in_loop_.IsBitSet(index_)) {
4963 break;
4964 }
4965 }
4966 }
4967
4968 private:
4969 const BitVector& blocks_in_loop_;
4970 const GrowableArray<HBasicBlock*>& blocks_;
4971 size_t index_;
4972
4973 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4974};
4975
Mingyao Yang3584bce2015-05-19 16:01:59 -07004976// Iterator over the blocks that art part of the loop. Includes blocks part
4977// of an inner loop. The order in which the blocks are iterated is reverse
4978// post order.
4979class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4980 public:
4981 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4982 : blocks_in_loop_(info.GetBlocks()),
4983 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4984 index_(0) {
4985 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4986 Advance();
4987 }
4988 }
4989
4990 bool Done() const { return index_ == blocks_.Size(); }
4991 HBasicBlock* Current() const { return blocks_.Get(index_); }
4992 void Advance() {
4993 ++index_;
4994 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4995 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4996 break;
4997 }
4998 }
4999 }
5000
5001 private:
5002 const BitVector& blocks_in_loop_;
5003 const GrowableArray<HBasicBlock*>& blocks_;
5004 size_t index_;
5005
5006 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5007};
5008
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005009inline int64_t Int64FromConstant(HConstant* constant) {
5010 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5011 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5012 : constant->AsLongConstant()->GetValue();
5013}
5014
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005015} // namespace art
5016
5017#endif // ART_COMPILER_OPTIMIZING_NODES_H_