blob: f09e958d2915f55275ddf398ca7eb75964403c72 [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),
570 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000571 loop_information_(nullptr),
572 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100573 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100574 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100575 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100576 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000577 lifetime_end_(kNoLifetime),
578 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000579
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100580 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
581 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000582 }
583
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100584 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
585 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000586 }
587
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100588 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
589 return dominated_blocks_;
590 }
591
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100592 bool IsEntryBlock() const {
593 return graph_->GetEntryBlock() == this;
594 }
595
596 bool IsExitBlock() const {
597 return graph_->GetExitBlock() == this;
598 }
599
David Brazdil46e2a392015-03-16 17:31:52 +0000600 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000601 bool IsSingleTryBoundary() const;
602
603 // Returns true if this block emits nothing but a jump.
604 bool IsSingleJump() const {
605 HLoopInformation* loop_info = GetLoopInformation();
606 return (IsSingleGoto() || IsSingleTryBoundary())
607 // Back edges generate a suspend check.
608 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
609 }
David Brazdil46e2a392015-03-16 17:31:52 +0000610
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000611 void AddBackEdge(HBasicBlock* back_edge) {
612 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000613 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000614 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100615 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000616 loop_information_->AddBackEdge(back_edge);
617 }
618
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000619 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000620 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000621
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000622 int GetBlockId() const { return block_id_; }
623 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000624
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000625 HBasicBlock* GetDominator() const { return dominator_; }
626 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100627 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100628 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000629 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
630 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
631 if (dominated_blocks_.Get(i) == existing) {
632 dominated_blocks_.Put(i, new_block);
633 return;
634 }
635 }
636 LOG(FATAL) << "Unreachable";
637 UNREACHABLE();
638 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100639 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000640
641 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100642 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000643 }
644
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100645 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
646 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100647 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100648 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100649 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
650 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000651
652 void AddSuccessor(HBasicBlock* block) {
653 successors_.Add(block);
654 block->predecessors_.Add(this);
655 }
656
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100657 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
658 size_t successor_index = GetSuccessorIndexOf(existing);
659 DCHECK_NE(successor_index, static_cast<size_t>(-1));
660 existing->RemovePredecessor(this);
661 new_block->predecessors_.Add(this);
662 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000663 }
664
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000665 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
666 size_t predecessor_index = GetPredecessorIndexOf(existing);
667 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
668 existing->RemoveSuccessor(this);
669 new_block->successors_.Add(this);
670 predecessors_.Put(predecessor_index, new_block);
671 }
672
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100673 // Insert `this` between `predecessor` and `successor. This method
674 // preserves the indicies, and will update the first edge found between
675 // `predecessor` and `successor`.
676 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
677 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
678 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
679 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
680 DCHECK_NE(successor_index, static_cast<size_t>(-1));
681 successor->predecessors_.Put(predecessor_index, this);
682 predecessor->successors_.Put(successor_index, this);
683 successors_.Add(successor);
684 predecessors_.Add(predecessor);
685 }
686
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100687 void RemovePredecessor(HBasicBlock* block) {
688 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100689 }
690
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000691 void RemoveSuccessor(HBasicBlock* block) {
692 successors_.Delete(block);
693 }
694
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100695 void ClearAllPredecessors() {
696 predecessors_.Reset();
697 }
698
699 void AddPredecessor(HBasicBlock* block) {
700 predecessors_.Add(block);
701 block->successors_.Add(this);
702 }
703
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100704 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100705 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100706 HBasicBlock* temp = predecessors_.Get(0);
707 predecessors_.Put(0, predecessors_.Get(1));
708 predecessors_.Put(1, temp);
709 }
710
David Brazdil769c9e52015-04-27 13:54:09 +0100711 void SwapSuccessors() {
712 DCHECK_EQ(successors_.Size(), 2u);
713 HBasicBlock* temp = successors_.Get(0);
714 successors_.Put(0, successors_.Get(1));
715 successors_.Put(1, temp);
716 }
717
David Brazdilfc6a86a2015-06-26 10:33:45 +0000718 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100719 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
720 if (predecessors_.Get(i) == predecessor) {
721 return i;
722 }
723 }
724 return -1;
725 }
726
David Brazdilfc6a86a2015-06-26 10:33:45 +0000727 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100728 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
729 if (successors_.Get(i) == successor) {
730 return i;
731 }
732 }
733 return -1;
734 }
735
David Brazdilfc6a86a2015-06-26 10:33:45 +0000736 HBasicBlock* GetSinglePredecessor() const {
737 DCHECK_EQ(GetPredecessors().Size(), 1u);
738 return GetPredecessors().Get(0);
739 }
740
741 HBasicBlock* GetSingleSuccessor() const {
742 DCHECK_EQ(GetSuccessors().Size(), 1u);
743 return GetSuccessors().Get(0);
744 }
745
746 // Returns whether the first occurrence of `predecessor` in the list of
747 // predecessors is at index `idx`.
748 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
749 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
750 return GetPredecessorIndexOf(predecessor) == idx;
751 }
752
David Brazdilffee3d32015-07-06 11:48:53 +0100753 // Returns the number of non-exceptional successors. SsaChecker ensures that
754 // these are stored at the beginning of the successor list.
755 size_t NumberOfNormalSuccessors() const {
756 return EndsWithTryBoundary() ? 1 : GetSuccessors().Size();
757 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000758
759 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100760 // created, latter block. Note that this method will add the block to the
761 // graph, create a Goto at the end of the former block and will create an edge
762 // between the blocks. It will not, however, update the reverse post order or
763 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000764 HBasicBlock* SplitBefore(HInstruction* cursor);
765
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000766 // Split the block into two blocks just after `cursor`. Returns the newly
767 // created block. Note that this method just updates raw block information,
768 // like predecessors, successors, dominators, and instruction list. It does not
769 // update the graph, reverse post order, loop information, nor make sure the
770 // blocks are consistent (for example ending with a control flow instruction).
771 HBasicBlock* SplitAfter(HInstruction* cursor);
772
773 // Merge `other` at the end of `this`. Successors and dominated blocks of
774 // `other` are changed to be successors and dominated blocks of `this`. Note
775 // that this method does not update the graph, reverse post order, loop
776 // information, nor make sure the blocks are consistent (for example ending
777 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100778 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000779
780 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
781 // of `this` are moved to `other`.
782 // Note that this method does not update the graph, reverse post order, loop
783 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000784 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000785 void ReplaceWith(HBasicBlock* other);
786
David Brazdil2d7352b2015-04-20 14:52:42 +0100787 // Merge `other` at the end of `this`. This method updates loops, reverse post
788 // order, links to predecessors, successors, dominators and deletes the block
789 // from the graph. The two blocks must be successive, i.e. `this` the only
790 // predecessor of `other` and vice versa.
791 void MergeWith(HBasicBlock* other);
792
793 // Disconnects `this` from all its predecessors, successors and dominator,
794 // removes it from all loops it is included in and eventually from the graph.
795 // The block must not dominate any other block. Predecessors and successors
796 // are safely updated.
797 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000798
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000799 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100800 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100801 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100802 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100803 // Replace instruction `initial` with `replacement` within this block.
804 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
805 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100806 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100807 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000808 // RemoveInstruction and RemovePhi delete a given instruction from the respective
809 // instruction list. With 'ensure_safety' set to true, it verifies that the
810 // instruction is not in use and removes it from the use lists of its inputs.
811 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
812 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100813 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100814
815 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100816 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100817 }
818
Roland Levillain6b879dd2014-09-22 17:13:44 +0100819 bool IsLoopPreHeaderFirstPredecessor() const {
820 DCHECK(IsLoopHeader());
821 DCHECK(!GetPredecessors().IsEmpty());
822 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
823 }
824
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100825 HLoopInformation* GetLoopInformation() const {
826 return loop_information_;
827 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000828
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000829 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100830 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000831 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100832 void SetInLoop(HLoopInformation* info) {
833 if (IsLoopHeader()) {
834 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100835 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100836 loop_information_ = info;
837 } else if (loop_information_->Contains(*info->GetHeader())) {
838 // Block is currently part of an outer loop. Make it part of this inner loop.
839 // Note that a non loop header having a loop information means this loop information
840 // has already been populated
841 loop_information_ = info;
842 } else {
843 // Block is part of an inner loop. Do not update the loop information.
844 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
845 // at this point, because this method is being called while populating `info`.
846 }
847 }
848
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000849 // Raw update of the loop information.
850 void SetLoopInformation(HLoopInformation* info) {
851 loop_information_ = info;
852 }
853
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100854 bool IsInLoop() const { return loop_information_ != nullptr; }
855
David Brazdilffee3d32015-07-06 11:48:53 +0100856 HTryBoundary* GetTryEntry() const { return try_entry_; }
857 void SetTryEntry(HTryBoundary* try_entry) { try_entry_ = try_entry; }
858 bool IsInTry() const { return try_entry_ != nullptr; }
859
860 // Returns the try entry that this block's successors should have. They will
861 // be in the same try, unless the block ends in a try boundary. In that case,
862 // the appropriate try entry will be returned.
863 HTryBoundary* ComputeTryEntryOfSuccessors() const;
864
David Brazdila4b8c212015-05-07 09:59:30 +0100865 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100866 bool Dominates(HBasicBlock* block) const;
867
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100868 size_t GetLifetimeStart() const { return lifetime_start_; }
869 size_t GetLifetimeEnd() const { return lifetime_end_; }
870
871 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
872 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
873
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100874 uint32_t GetDexPc() const { return dex_pc_; }
875
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000876 bool IsCatchBlock() const { return is_catch_block_; }
877 void SetIsCatchBlock() { is_catch_block_ = true; }
878
David Brazdil8d5b8b22015-03-24 10:51:52 +0000879 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000880 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100881 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000882 bool HasSinglePhi() const;
883
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000884 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000885 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000886 GrowableArray<HBasicBlock*> predecessors_;
887 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100888 HInstructionList instructions_;
889 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000890 HLoopInformation* loop_information_;
891 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100892 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000893 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100894 // The dex program counter of the first instruction of this block.
895 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100896 size_t lifetime_start_;
897 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000898 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000899
David Brazdilffee3d32015-07-06 11:48:53 +0100900 // If this block is in a try block, `try_entry_` stores one of, possibly
901 // several, TryBoundary instructions entering it.
902 HTryBoundary* try_entry_;
903
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000904 friend class HGraph;
905 friend class HInstruction;
906
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000907 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
908};
909
David Brazdilb2bd1c52015-03-25 11:17:37 +0000910// Iterates over the LoopInformation of all loops which contain 'block'
911// from the innermost to the outermost.
912class HLoopInformationOutwardIterator : public ValueObject {
913 public:
914 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
915 : current_(block.GetLoopInformation()) {}
916
917 bool Done() const { return current_ == nullptr; }
918
919 void Advance() {
920 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100921 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000922 }
923
924 HLoopInformation* Current() const {
925 DCHECK(!Done());
926 return current_;
927 }
928
929 private:
930 HLoopInformation* current_;
931
932 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
933};
934
Alexandre Ramesef20f712015-06-09 10:29:30 +0100935#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100936 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000937 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000938 M(ArrayGet, Instruction) \
939 M(ArrayLength, Instruction) \
940 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100941 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000942 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000943 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000944 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +0100945 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100946 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000947 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100948 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100949 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700950 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000951 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000952 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000953 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100954 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000955 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100956 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000957 M(FloatConstant, Constant) \
958 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100959 M(GreaterThan, Condition) \
960 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100961 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000962 M(InstanceFieldGet, Instruction) \
963 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000964 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100965 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000966 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000967 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100968 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000969 M(LessThan, Condition) \
970 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000971 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000972 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100973 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000974 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100975 M(Local, Instruction) \
976 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100977 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000978 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000979 M(Mul, BinaryOperation) \
980 M(Neg, UnaryOperation) \
981 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100982 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100983 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000984 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000985 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000986 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000987 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100988 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000989 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100990 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000991 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100992 M(Return, Instruction) \
993 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000994 M(Shl, BinaryOperation) \
995 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100996 M(StaticFieldGet, Instruction) \
997 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100998 M(StoreLocal, Instruction) \
999 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001000 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001001 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001002 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001003 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001004 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001005 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001006 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007
Alexandre Ramesef20f712015-06-09 10:29:30 +01001008#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1009
1010#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1011
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001012#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1013
Alexandre Ramesef20f712015-06-09 10:29:30 +01001014#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1015
1016#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1017
1018#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1019 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1020 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1021 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001022 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001023 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1024 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1025
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001026#define FOR_EACH_INSTRUCTION(M) \
1027 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1028 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001029 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001030 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001031 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001032
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001033#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001034FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1035#undef FORWARD_DECLARATION
1036
Roland Levillainccc07a92014-09-16 14:48:16 +01001037#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001038 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1039 const char* DebugName() const OVERRIDE { return #type; } \
1040 const H##type* As##type() const OVERRIDE { return this; } \
1041 H##type* As##type() OVERRIDE { return this; } \
1042 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001043 return other->Is##type(); \
1044 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001045 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001046
David Brazdiled596192015-01-23 10:39:45 +00001047template <typename T> class HUseList;
1048
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001049template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001050class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001051 public:
David Brazdiled596192015-01-23 10:39:45 +00001052 HUseListNode* GetPrevious() const { return prev_; }
1053 HUseListNode* GetNext() const { return next_; }
1054 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001055 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001056 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001057
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001058 private:
David Brazdiled596192015-01-23 10:39:45 +00001059 HUseListNode(T user, size_t index)
1060 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1061
1062 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001063 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001064 HUseListNode<T>* prev_;
1065 HUseListNode<T>* next_;
1066
1067 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001068
1069 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1070};
1071
David Brazdiled596192015-01-23 10:39:45 +00001072template <typename T>
1073class HUseList : public ValueObject {
1074 public:
1075 HUseList() : first_(nullptr) {}
1076
1077 void Clear() {
1078 first_ = nullptr;
1079 }
1080
1081 // Adds a new entry at the beginning of the use list and returns
1082 // the newly created node.
1083 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001084 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001085 if (IsEmpty()) {
1086 first_ = new_node;
1087 } else {
1088 first_->prev_ = new_node;
1089 new_node->next_ = first_;
1090 first_ = new_node;
1091 }
1092 return new_node;
1093 }
1094
1095 HUseListNode<T>* GetFirst() const {
1096 return first_;
1097 }
1098
1099 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001100 DCHECK(node != nullptr);
1101 DCHECK(Contains(node));
1102
David Brazdiled596192015-01-23 10:39:45 +00001103 if (node->prev_ != nullptr) {
1104 node->prev_->next_ = node->next_;
1105 }
1106 if (node->next_ != nullptr) {
1107 node->next_->prev_ = node->prev_;
1108 }
1109 if (node == first_) {
1110 first_ = node->next_;
1111 }
1112 }
1113
David Brazdil1abb4192015-02-17 18:33:36 +00001114 bool Contains(const HUseListNode<T>* node) const {
1115 if (node == nullptr) {
1116 return false;
1117 }
1118 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1119 if (current == node) {
1120 return true;
1121 }
1122 }
1123 return false;
1124 }
1125
David Brazdiled596192015-01-23 10:39:45 +00001126 bool IsEmpty() const {
1127 return first_ == nullptr;
1128 }
1129
1130 bool HasOnlyOneUse() const {
1131 return first_ != nullptr && first_->next_ == nullptr;
1132 }
1133
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001134 size_t SizeSlow() const {
1135 size_t count = 0;
1136 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1137 ++count;
1138 }
1139 return count;
1140 }
1141
David Brazdiled596192015-01-23 10:39:45 +00001142 private:
1143 HUseListNode<T>* first_;
1144};
1145
1146template<typename T>
1147class HUseIterator : public ValueObject {
1148 public:
1149 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1150
1151 bool Done() const { return current_ == nullptr; }
1152
1153 void Advance() {
1154 DCHECK(!Done());
1155 current_ = current_->GetNext();
1156 }
1157
1158 HUseListNode<T>* Current() const {
1159 DCHECK(!Done());
1160 return current_;
1161 }
1162
1163 private:
1164 HUseListNode<T>* current_;
1165
1166 friend class HValue;
1167};
1168
David Brazdil1abb4192015-02-17 18:33:36 +00001169// This class is used by HEnvironment and HInstruction classes to record the
1170// instructions they use and pointers to the corresponding HUseListNodes kept
1171// by the used instructions.
1172template <typename T>
1173class HUserRecord : public ValueObject {
1174 public:
1175 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1176 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1177
1178 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1179 : instruction_(old_record.instruction_), use_node_(use_node) {
1180 DCHECK(instruction_ != nullptr);
1181 DCHECK(use_node_ != nullptr);
1182 DCHECK(old_record.use_node_ == nullptr);
1183 }
1184
1185 HInstruction* GetInstruction() const { return instruction_; }
1186 HUseListNode<T>* GetUseNode() const { return use_node_; }
1187
1188 private:
1189 // Instruction used by the user.
1190 HInstruction* instruction_;
1191
1192 // Corresponding entry in the use list kept by 'instruction_'.
1193 HUseListNode<T>* use_node_;
1194};
1195
Aart Bik854a02b2015-07-14 16:07:00 -07001196/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001197 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001198 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001199 * For write/read dependences on fields/arrays, the dependence analysis uses
1200 * type disambiguation (e.g. a float field write cannot modify the value of an
1201 * integer field read) and the access type (e.g. a reference array write cannot
1202 * modify the value of a reference field read [although it may modify the
1203 * reference fetch prior to reading the field, which is represented by its own
1204 * write/read dependence]). The analysis makes conservative points-to
1205 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1206 * the same, and any reference read depends on any reference read without
1207 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001208 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001209 * The internal representation uses 38-bit and is described in the table below.
1210 * The first line indicates the side effect, and for field/array accesses the
1211 * second line indicates the type of the access (in the order of the
1212 * Primitive::Type enum).
1213 * The two numbered lines below indicate the bit position in the bitfield (read
1214 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001215 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001216 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1217 * +-------------+---------+---------+--------------+---------+---------+
1218 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1219 * | 3 |333333322|222222221| 1 |111111110|000000000|
1220 * | 7 |654321098|765432109| 8 |765432109|876543210|
1221 *
1222 * Note that, to ease the implementation, 'changes' bits are least significant
1223 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001224 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001225class SideEffects : public ValueObject {
1226 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001227 SideEffects() : flags_(0) {}
1228
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001229 static SideEffects None() {
1230 return SideEffects(0);
1231 }
1232
1233 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001234 return SideEffects(kAllChangeBits | kAllDependOnBits);
1235 }
1236
1237 static SideEffects AllChanges() {
1238 return SideEffects(kAllChangeBits);
1239 }
1240
1241 static SideEffects AllDependencies() {
1242 return SideEffects(kAllDependOnBits);
1243 }
1244
1245 static SideEffects AllExceptGCDependency() {
1246 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1247 }
1248
1249 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001250 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001251 }
1252
Aart Bik34c3ba92015-07-20 14:08:59 -07001253 static SideEffects AllWrites() {
1254 return SideEffects(kAllWrites);
1255 }
1256
1257 static SideEffects AllReads() {
1258 return SideEffects(kAllReads);
1259 }
1260
1261 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1262 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001263 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001264 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001265 }
1266
Aart Bik854a02b2015-07-14 16:07:00 -07001267 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1268 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001269 }
1270
Aart Bik34c3ba92015-07-20 14:08:59 -07001271 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1272 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001273 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001274 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001275 }
1276
1277 static SideEffects ArrayReadOfType(Primitive::Type type) {
1278 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1279 }
1280
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001281 static SideEffects CanTriggerGC() {
1282 return SideEffects(1ULL << kCanTriggerGCBit);
1283 }
1284
1285 static SideEffects DependsOnGC() {
1286 return SideEffects(1ULL << kDependsOnGCBit);
1287 }
1288
Aart Bik854a02b2015-07-14 16:07:00 -07001289 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001290 SideEffects Union(SideEffects other) const {
1291 return SideEffects(flags_ | other.flags_);
1292 }
1293
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001294 SideEffects Exclusion(SideEffects other) const {
1295 return SideEffects(flags_ & ~other.flags_);
1296 }
1297
1298 bool Includes(SideEffects other) const {
1299 return (other.flags_ & flags_) == other.flags_;
1300 }
1301
1302 bool HasSideEffects() const {
1303 return (flags_ & kAllChangeBits);
1304 }
1305
1306 bool HasDependencies() const {
1307 return (flags_ & kAllDependOnBits);
1308 }
1309
1310 // Returns true if there are no side effects or dependencies.
1311 bool DoesNothing() const {
1312 return flags_ == 0;
1313 }
1314
Aart Bik854a02b2015-07-14 16:07:00 -07001315 // Returns true if something is written.
1316 bool DoesAnyWrite() const {
1317 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001318 }
1319
Aart Bik854a02b2015-07-14 16:07:00 -07001320 // Returns true if something is read.
1321 bool DoesAnyRead() const {
1322 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001323 }
1324
Aart Bik854a02b2015-07-14 16:07:00 -07001325 // Returns true if potentially everything is written and read
1326 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001327 bool DoesAllReadWrite() const {
1328 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1329 }
1330
Aart Bik854a02b2015-07-14 16:07:00 -07001331 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001332 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001333 }
1334
1335 // Returns true if this may read something written by other.
1336 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001337 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1338 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001339 }
1340
1341 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001342 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001343 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001344 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001345 for (int s = kLastBit; s >= 0; s--) {
1346 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1347 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1348 // This is a bit for the GC side effect.
1349 if (current_bit_is_set) {
1350 flags += "GC";
1351 }
Aart Bik854a02b2015-07-14 16:07:00 -07001352 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001353 } else {
1354 // This is a bit for the array/field analysis.
1355 // The underscore character stands for the 'can trigger GC' bit.
1356 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1357 if (current_bit_is_set) {
1358 flags += kDebug[s];
1359 }
1360 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1361 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1362 flags += "|";
1363 }
1364 }
Aart Bik854a02b2015-07-14 16:07:00 -07001365 }
1366 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001367 }
1368
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001369 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001370
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001371 private:
1372 static constexpr int kFieldArrayAnalysisBits = 9;
1373
1374 static constexpr int kFieldWriteOffset = 0;
1375 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1376 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1377 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1378
1379 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1380
1381 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1382 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1383 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1384 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1385
1386 static constexpr int kLastBit = kDependsOnGCBit;
1387 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1388
1389 // Aliases.
1390
1391 static_assert(kChangeBits == kDependOnBits,
1392 "the 'change' bits should match the 'depend on' bits.");
1393
1394 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1395 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1396 static constexpr uint64_t kAllWrites =
1397 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1398 static constexpr uint64_t kAllReads =
1399 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001400
Aart Bik854a02b2015-07-14 16:07:00 -07001401 // Work around the fact that HIR aliases I/F and J/D.
1402 // TODO: remove this interceptor once HIR types are clean
1403 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1404 switch (type) {
1405 case Primitive::kPrimInt:
1406 case Primitive::kPrimFloat:
1407 return TypeFlag(Primitive::kPrimInt, offset) |
1408 TypeFlag(Primitive::kPrimFloat, offset);
1409 case Primitive::kPrimLong:
1410 case Primitive::kPrimDouble:
1411 return TypeFlag(Primitive::kPrimLong, offset) |
1412 TypeFlag(Primitive::kPrimDouble, offset);
1413 default:
1414 return TypeFlag(type, offset);
1415 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001416 }
1417
Aart Bik854a02b2015-07-14 16:07:00 -07001418 // Translates type to bit flag.
1419 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1420 CHECK_NE(type, Primitive::kPrimVoid);
1421 const uint64_t one = 1;
1422 const int shift = type; // 0-based consecutive enum
1423 DCHECK_LE(kFieldWriteOffset, shift);
1424 DCHECK_LT(shift, kArrayWriteOffset);
1425 return one << (type + offset);
1426 }
1427
1428 // Private constructor on direct flags value.
1429 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1430
1431 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001432};
1433
David Brazdiled596192015-01-23 10:39:45 +00001434// A HEnvironment object contains the values of virtual registers at a given location.
1435class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1436 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001437 HEnvironment(ArenaAllocator* arena,
1438 size_t number_of_vregs,
1439 const DexFile& dex_file,
1440 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001441 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001442 InvokeType invoke_type,
1443 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001444 : vregs_(arena, number_of_vregs),
1445 locations_(arena, number_of_vregs),
1446 parent_(nullptr),
1447 dex_file_(dex_file),
1448 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001449 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001450 invoke_type_(invoke_type),
1451 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001452 vregs_.SetSize(number_of_vregs);
1453 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001454 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001455 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001456
1457 locations_.SetSize(number_of_vregs);
1458 for (size_t i = 0; i < number_of_vregs; ++i) {
1459 locations_.Put(i, Location());
1460 }
1461 }
1462
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001463 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001464 : HEnvironment(arena,
1465 to_copy.Size(),
1466 to_copy.GetDexFile(),
1467 to_copy.GetMethodIdx(),
1468 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001469 to_copy.GetInvokeType(),
1470 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001471
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001472 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001473 if (parent_ != nullptr) {
1474 parent_->SetAndCopyParentChain(allocator, parent);
1475 } else {
1476 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1477 parent_->CopyFrom(parent);
1478 if (parent->GetParent() != nullptr) {
1479 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1480 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001481 }
David Brazdiled596192015-01-23 10:39:45 +00001482 }
1483
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001484 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1485 void CopyFrom(HEnvironment* environment);
1486
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001487 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1488 // input to the loop phi instead. This is for inserting instructions that
1489 // require an environment (like HDeoptimization) in the loop pre-header.
1490 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001491
1492 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001493 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001494 }
1495
1496 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001497 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001498 }
1499
David Brazdil1abb4192015-02-17 18:33:36 +00001500 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001501
1502 size_t Size() const { return vregs_.Size(); }
1503
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001504 HEnvironment* GetParent() const { return parent_; }
1505
1506 void SetLocationAt(size_t index, Location location) {
1507 locations_.Put(index, location);
1508 }
1509
1510 Location GetLocationAt(size_t index) const {
1511 return locations_.Get(index);
1512 }
1513
1514 uint32_t GetDexPc() const {
1515 return dex_pc_;
1516 }
1517
1518 uint32_t GetMethodIdx() const {
1519 return method_idx_;
1520 }
1521
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001522 InvokeType GetInvokeType() const {
1523 return invoke_type_;
1524 }
1525
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001526 const DexFile& GetDexFile() const {
1527 return dex_file_;
1528 }
1529
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001530 HInstruction* GetHolder() const {
1531 return holder_;
1532 }
1533
David Brazdiled596192015-01-23 10:39:45 +00001534 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001535 // Record instructions' use entries of this environment for constant-time removal.
1536 // It should only be called by HInstruction when a new environment use is added.
1537 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1538 DCHECK(env_use->GetUser() == this);
1539 size_t index = env_use->GetIndex();
1540 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1541 }
David Brazdiled596192015-01-23 10:39:45 +00001542
David Brazdil1abb4192015-02-17 18:33:36 +00001543 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001544 GrowableArray<Location> locations_;
1545 HEnvironment* parent_;
1546 const DexFile& dex_file_;
1547 const uint32_t method_idx_;
1548 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001549 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001550
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001551 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001552 HInstruction* const holder_;
1553
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001554 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001555
1556 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1557};
1558
Calin Juravleacf735c2015-02-12 15:25:22 +00001559class ReferenceTypeInfo : ValueObject {
1560 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001561 typedef Handle<mirror::Class> TypeHandle;
1562
Calin Juravle2e768302015-07-28 14:41:11 +00001563 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1564 // The constructor will check that the type_handle is valid.
1565 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001566 }
1567
Calin Juravle2e768302015-07-28 14:41:11 +00001568 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1569
1570 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1571 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001572 }
1573
Calin Juravle2e768302015-07-28 14:41:11 +00001574 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1575 return IsValidHandle(type_handle_);
1576 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001577 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001578
1579 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1580 DCHECK(IsValid());
1581 return GetTypeHandle()->IsObjectClass();
1582 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001583 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001584 DCHECK(IsValid());
1585 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001586 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001587
1588 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1589
Mathieu Chartier90443472015-07-16 20:32:27 -07001590 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001591 DCHECK(IsValid());
1592 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001593 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1594 }
1595
1596 // Returns true if the type information provide the same amount of details.
1597 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001598 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001599 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001600 if (!IsValid() && !rti.IsValid()) {
1601 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001602 return true;
1603 }
Calin Juravle2e768302015-07-28 14:41:11 +00001604 if (!IsValid() || !rti.IsValid()) {
1605 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001606 return false;
1607 }
Calin Juravle2e768302015-07-28 14:41:11 +00001608 return IsExact() == rti.IsExact()
1609 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001610 }
1611
1612 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001613 ReferenceTypeInfo();
1614 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001615
Calin Juravleacf735c2015-02-12 15:25:22 +00001616 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001617 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001618 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001619 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001620 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001621};
1622
1623std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1624
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001625class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001626 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001627 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001628 : previous_(nullptr),
1629 next_(nullptr),
1630 block_(nullptr),
1631 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001632 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001633 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001634 locations_(nullptr),
1635 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001636 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001637 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001638 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001639
Dave Allison20dfc792014-06-16 20:44:29 -07001640 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001641
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001642#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001643 enum InstructionKind {
1644 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1645 };
1646#undef DECLARE_KIND
1647
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001648 HInstruction* GetNext() const { return next_; }
1649 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001650
Calin Juravle77520bc2015-01-12 18:45:46 +00001651 HInstruction* GetNextDisregardingMoves() const;
1652 HInstruction* GetPreviousDisregardingMoves() const;
1653
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001654 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001655 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001656 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001657 bool IsInBlock() const { return block_ != nullptr; }
1658 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001659 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001660
Roland Levillain6b879dd2014-09-22 17:13:44 +01001661 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001662 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001663
1664 virtual void Accept(HGraphVisitor* visitor) = 0;
1665 virtual const char* DebugName() const = 0;
1666
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001667 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001668 void SetRawInputAt(size_t index, HInstruction* input) {
1669 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1670 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001671
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001672 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001673 virtual uint32_t GetDexPc() const {
1674 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1675 " does not need an environment";
1676 UNREACHABLE();
1677 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001678 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001679 virtual bool CanThrow() const { return false; }
Aart Bik854a02b2015-07-14 16:07:00 -07001680
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001681 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001682 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001683
Calin Juravle10e244f2015-01-26 18:54:32 +00001684 // Does not apply for all instructions, but having this at top level greatly
1685 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001686 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001687 virtual bool CanBeNull() const {
1688 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1689 return true;
1690 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001691
Calin Juravle641547a2015-04-21 22:08:51 +01001692 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1693 UNUSED(obj);
1694 return false;
1695 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001696
Calin Juravle2e768302015-07-28 14:41:11 +00001697 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001698
Calin Juravle61d544b2015-02-23 16:46:57 +00001699 ReferenceTypeInfo GetReferenceTypeInfo() const {
1700 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1701 return reference_type_info_;
1702 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001703
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001704 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001705 DCHECK(user != nullptr);
1706 HUseListNode<HInstruction*>* use =
1707 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1708 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001709 }
1710
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001711 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001712 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001713 HUseListNode<HEnvironment*>* env_use =
1714 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1715 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001716 }
1717
David Brazdil1abb4192015-02-17 18:33:36 +00001718 void RemoveAsUserOfInput(size_t input) {
1719 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1720 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1721 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001722
David Brazdil1abb4192015-02-17 18:33:36 +00001723 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1724 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001725
David Brazdiled596192015-01-23 10:39:45 +00001726 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1727 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001728 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001729 bool HasOnlyOneNonEnvironmentUse() const {
1730 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1731 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001732
Roland Levillain6c82d402014-10-13 16:10:27 +01001733 // Does this instruction strictly dominate `other_instruction`?
1734 // Returns false if this instruction and `other_instruction` are the same.
1735 // Aborts if this instruction and `other_instruction` are both phis.
1736 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001737
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001738 int GetId() const { return id_; }
1739 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001740
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001741 int GetSsaIndex() const { return ssa_index_; }
1742 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1743 bool HasSsaIndex() const { return ssa_index_ != -1; }
1744
1745 bool HasEnvironment() const { return environment_ != nullptr; }
1746 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001747 // Set the `environment_` field. Raw because this method does not
1748 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001749 void SetRawEnvironment(HEnvironment* environment) {
1750 DCHECK(environment_ == nullptr);
1751 DCHECK_EQ(environment->GetHolder(), this);
1752 environment_ = environment;
1753 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001754
1755 // Set the environment of this instruction, copying it from `environment`. While
1756 // copying, the uses lists are being updated.
1757 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001758 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001759 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001760 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001761 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001762 if (environment->GetParent() != nullptr) {
1763 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1764 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001765 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001766
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001767 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1768 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001769 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001770 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001771 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001772 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001773 if (environment->GetParent() != nullptr) {
1774 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1775 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001776 }
1777
Nicolas Geoffray39468442014-09-02 15:17:15 +01001778 // Returns the number of entries in the environment. Typically, that is the
1779 // number of dex registers in a method. It could be more in case of inlining.
1780 size_t EnvironmentSize() const;
1781
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001782 LocationSummary* GetLocations() const { return locations_; }
1783 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001784
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001785 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001786 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001787
Alexandre Rames188d4312015-04-09 18:30:21 +01001788 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1789 // uses of this instruction by `other` are *not* updated.
1790 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1791 ReplaceWith(other);
1792 other->ReplaceInput(this, use_index);
1793 }
1794
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001795 // Move `this` instruction before `cursor`.
1796 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001797
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001798#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001799 bool Is##type() const { return (As##type() != nullptr); } \
1800 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001801 virtual H##type* As##type() { return nullptr; }
1802
1803 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1804#undef INSTRUCTION_TYPE_CHECK
1805
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001806 // Returns whether the instruction can be moved within the graph.
1807 virtual bool CanBeMoved() const { return false; }
1808
1809 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001810 virtual bool InstructionTypeEquals(HInstruction* other) const {
1811 UNUSED(other);
1812 return false;
1813 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001814
1815 // Returns whether any data encoded in the two instructions is equal.
1816 // This method does not look at the inputs. Both instructions must be
1817 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001818 virtual bool InstructionDataEquals(HInstruction* other) const {
1819 UNUSED(other);
1820 return false;
1821 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001822
1823 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001824 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001825 // 2) Their inputs are identical.
1826 bool Equals(HInstruction* other) const;
1827
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001828 virtual InstructionKind GetKind() const = 0;
1829
1830 virtual size_t ComputeHashCode() const {
1831 size_t result = GetKind();
1832 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1833 result = (result * 31) + InputAt(i)->GetId();
1834 }
1835 return result;
1836 }
1837
1838 SideEffects GetSideEffects() const { return side_effects_; }
1839
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001840 size_t GetLifetimePosition() const { return lifetime_position_; }
1841 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1842 LiveInterval* GetLiveInterval() const { return live_interval_; }
1843 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1844 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1845
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001846 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1847
1848 // Returns whether the code generation of the instruction will require to have access
1849 // to the current method. Such instructions are:
1850 // (1): Instructions that require an environment, as calling the runtime requires
1851 // to walk the stack and have the current method stored at a specific stack address.
1852 // (2): Object literals like classes and strings, that are loaded from the dex cache
1853 // fields of the current method.
1854 bool NeedsCurrentMethod() const {
1855 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1856 }
1857
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001858 virtual bool NeedsDexCache() const { return false; }
1859
Mark Mendellc4701932015-04-10 13:18:51 -04001860 // Does this instruction have any use in an environment before
1861 // control flow hits 'other'?
1862 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1863
1864 // Remove all references to environment uses of this instruction.
1865 // The caller must ensure that this is safe to do.
1866 void RemoveEnvironmentUsers();
1867
David Brazdil1abb4192015-02-17 18:33:36 +00001868 protected:
1869 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1870 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1871
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001872 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001873 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1874
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001875 HInstruction* previous_;
1876 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001877 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001878
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001879 // An instruction gets an id when it is added to the graph.
1880 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001881 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001882 int id_;
1883
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001884 // When doing liveness analysis, instructions that have uses get an SSA index.
1885 int ssa_index_;
1886
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001887 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001888 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001889
1890 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001891 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001892
Nicolas Geoffray39468442014-09-02 15:17:15 +01001893 // The environment associated with this instruction. Not null if the instruction
1894 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001895 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001896
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001897 // Set by the code generator.
1898 LocationSummary* locations_;
1899
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001900 // Set by the liveness analysis.
1901 LiveInterval* live_interval_;
1902
1903 // Set by the liveness analysis, this is the position in a linear
1904 // order of blocks where this instruction's live interval start.
1905 size_t lifetime_position_;
1906
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001907 const SideEffects side_effects_;
1908
Calin Juravleacf735c2015-02-12 15:25:22 +00001909 // TODO: for primitive types this should be marked as invalid.
1910 ReferenceTypeInfo reference_type_info_;
1911
David Brazdil1abb4192015-02-17 18:33:36 +00001912 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001913 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001914 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001915 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001916 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001917
1918 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1919};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001920std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001921
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001922class HInputIterator : public ValueObject {
1923 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001924 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001925
1926 bool Done() const { return index_ == instruction_->InputCount(); }
1927 HInstruction* Current() const { return instruction_->InputAt(index_); }
1928 void Advance() { index_++; }
1929
1930 private:
1931 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001932 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001933
1934 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1935};
1936
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001937class HInstructionIterator : public ValueObject {
1938 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001939 explicit HInstructionIterator(const HInstructionList& instructions)
1940 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001941 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001942 }
1943
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001944 bool Done() const { return instruction_ == nullptr; }
1945 HInstruction* Current() const { return instruction_; }
1946 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001947 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001948 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001949 }
1950
1951 private:
1952 HInstruction* instruction_;
1953 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001954
1955 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001956};
1957
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001958class HBackwardInstructionIterator : public ValueObject {
1959 public:
1960 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1961 : instruction_(instructions.last_instruction_) {
1962 next_ = Done() ? nullptr : instruction_->GetPrevious();
1963 }
1964
1965 bool Done() const { return instruction_ == nullptr; }
1966 HInstruction* Current() const { return instruction_; }
1967 void Advance() {
1968 instruction_ = next_;
1969 next_ = Done() ? nullptr : instruction_->GetPrevious();
1970 }
1971
1972 private:
1973 HInstruction* instruction_;
1974 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001975
1976 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001977};
1978
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001979// An embedded container with N elements of type T. Used (with partial
1980// specialization for N=0) because embedded arrays cannot have size 0.
1981template<typename T, intptr_t N>
1982class EmbeddedArray {
1983 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001984 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001985
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001986 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001987
1988 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001989 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001990 return elements_[i];
1991 }
1992
1993 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001994 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001995 return elements_[i];
1996 }
1997
1998 const T& At(intptr_t i) const {
1999 return (*this)[i];
2000 }
2001
2002 void SetAt(intptr_t i, const T& val) {
2003 (*this)[i] = val;
2004 }
2005
2006 private:
2007 T elements_[N];
2008};
2009
2010template<typename T>
2011class EmbeddedArray<T, 0> {
2012 public:
2013 intptr_t length() const { return 0; }
2014 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002015 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002016 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002017 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002018 }
2019 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002020 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002021 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002022 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002023 }
2024};
2025
2026template<intptr_t N>
2027class HTemplateInstruction: public HInstruction {
2028 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002029 HTemplateInstruction<N>(SideEffects side_effects)
2030 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002031 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002032
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002033 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002034
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002035 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00002036 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
2037
2038 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
2039 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002040 }
2041
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002042 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002043 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002044
2045 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002046};
2047
Dave Allison20dfc792014-06-16 20:44:29 -07002048template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002049class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002050 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002051 HExpression<N>(Primitive::Type type, SideEffects side_effects)
2052 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002053 virtual ~HExpression() {}
2054
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002055 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002056
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002057 protected:
2058 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002059};
2060
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002061// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2062// instruction that branches to the exit block.
2063class HReturnVoid : public HTemplateInstruction<0> {
2064 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002065 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002066
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002067 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002068
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002069 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002070
2071 private:
2072 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2073};
2074
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002075// Represents dex's RETURN opcodes. A HReturn is a control flow
2076// instruction that branches to the exit block.
2077class HReturn : public HTemplateInstruction<1> {
2078 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002079 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002080 SetRawInputAt(0, value);
2081 }
2082
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002083 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002084
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002085 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002086
2087 private:
2088 DISALLOW_COPY_AND_ASSIGN(HReturn);
2089};
2090
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002091// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002092// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002093// exit block.
2094class HExit : public HTemplateInstruction<0> {
2095 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002096 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002097
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002098 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002099
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002100 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002101
2102 private:
2103 DISALLOW_COPY_AND_ASSIGN(HExit);
2104};
2105
2106// Jumps from one block to another.
2107class HGoto : public HTemplateInstruction<0> {
2108 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002109 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2110
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002111 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002112
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002113 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002114 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002115 }
2116
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002117 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002118
2119 private:
2120 DISALLOW_COPY_AND_ASSIGN(HGoto);
2121};
2122
Roland Levillain9867bc72015-08-05 10:21:34 +01002123class HConstant : public HExpression<0> {
2124 public:
2125 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2126
2127 bool CanBeMoved() const OVERRIDE { return true; }
2128
2129 virtual bool IsMinusOne() const { return false; }
2130 virtual bool IsZero() const { return false; }
2131 virtual bool IsOne() const { return false; }
2132
2133 DECLARE_INSTRUCTION(Constant);
2134
2135 private:
2136 DISALLOW_COPY_AND_ASSIGN(HConstant);
2137};
2138
2139class HNullConstant : public HConstant {
2140 public:
2141 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2142 return true;
2143 }
2144
2145 size_t ComputeHashCode() const OVERRIDE { return 0; }
2146
2147 DECLARE_INSTRUCTION(NullConstant);
2148
2149 private:
2150 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2151
2152 friend class HGraph;
2153 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2154};
2155
2156// Constants of the type int. Those can be from Dex instructions, or
2157// synthesized (for example with the if-eqz instruction).
2158class HIntConstant : public HConstant {
2159 public:
2160 int32_t GetValue() const { return value_; }
2161
2162 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2163 DCHECK(other->IsIntConstant());
2164 return other->AsIntConstant()->value_ == value_;
2165 }
2166
2167 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2168
2169 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2170 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2171 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2172
2173 DECLARE_INSTRUCTION(IntConstant);
2174
2175 private:
2176 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2177 explicit HIntConstant(bool value) : HConstant(Primitive::kPrimInt), value_(value ? 1 : 0) {}
2178
2179 const int32_t value_;
2180
2181 friend class HGraph;
2182 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2183 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2184 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2185};
2186
2187class HLongConstant : public HConstant {
2188 public:
2189 int64_t GetValue() const { return value_; }
2190
2191 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2192 DCHECK(other->IsLongConstant());
2193 return other->AsLongConstant()->value_ == value_;
2194 }
2195
2196 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2197
2198 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2199 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2200 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2201
2202 DECLARE_INSTRUCTION(LongConstant);
2203
2204 private:
2205 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2206
2207 const int64_t value_;
2208
2209 friend class HGraph;
2210 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2211};
Dave Allison20dfc792014-06-16 20:44:29 -07002212
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002213// Conditional branch. A block ending with an HIf instruction must have
2214// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002215class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002216 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002217 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002218 SetRawInputAt(0, input);
2219 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002220
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002221 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002222
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002223 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002224 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002225 }
2226
2227 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002228 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002229 }
2230
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002231 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002232
2233 private:
2234 DISALLOW_COPY_AND_ASSIGN(HIf);
2235};
2236
David Brazdilfc6a86a2015-06-26 10:33:45 +00002237
2238// Abstract instruction which marks the beginning and/or end of a try block and
2239// links it to the respective exception handlers. Behaves the same as a Goto in
2240// non-exceptional control flow.
2241// Normal-flow successor is stored at index zero, exception handlers under
2242// higher indices in no particular order.
2243class HTryBoundary : public HTemplateInstruction<0> {
2244 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002245 enum BoundaryKind {
2246 kEntry,
2247 kExit,
2248 };
2249
2250 explicit HTryBoundary(BoundaryKind kind)
2251 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002252
2253 bool IsControlFlow() const OVERRIDE { return true; }
2254
2255 // Returns the block's non-exceptional successor (index zero).
2256 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2257
2258 // Returns whether `handler` is among its exception handlers (non-zero index
2259 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002260 bool HasExceptionHandler(const HBasicBlock& handler) const {
2261 DCHECK(handler.IsCatchBlock());
2262 return GetBlock()->GetSuccessors().Contains(
2263 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002264 }
2265
2266 // If not present already, adds `handler` to its block's list of exception
2267 // handlers.
2268 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002269 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002270 GetBlock()->AddSuccessor(handler);
2271 }
2272 }
2273
David Brazdil56e1acc2015-06-30 15:41:36 +01002274 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002275
David Brazdilffee3d32015-07-06 11:48:53 +01002276 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2277
David Brazdilfc6a86a2015-06-26 10:33:45 +00002278 DECLARE_INSTRUCTION(TryBoundary);
2279
2280 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002281 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002282
2283 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2284};
2285
David Brazdilffee3d32015-07-06 11:48:53 +01002286// Iterator over exception handlers of a given HTryBoundary, i.e. over
2287// exceptional successors of its basic block.
2288class HExceptionHandlerIterator : public ValueObject {
2289 public:
2290 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2291 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2292
2293 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2294 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
2295 size_t CurrentSuccessorIndex() const { return index_; }
2296 void Advance() { ++index_; }
2297
2298 private:
2299 const HBasicBlock& block_;
2300 size_t index_;
2301
2302 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2303};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002304
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002305// Deoptimize to interpreter, upon checking a condition.
2306class HDeoptimize : public HTemplateInstruction<1> {
2307 public:
2308 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2309 : HTemplateInstruction(SideEffects::None()),
2310 dex_pc_(dex_pc) {
2311 SetRawInputAt(0, cond);
2312 }
2313
2314 bool NeedsEnvironment() const OVERRIDE { return true; }
2315 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002316 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002317
2318 DECLARE_INSTRUCTION(Deoptimize);
2319
2320 private:
2321 uint32_t dex_pc_;
2322
2323 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2324};
2325
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002326// Represents the ArtMethod that was passed as a first argument to
2327// the method. It is used by instructions that depend on it, like
2328// instructions that work with the dex cache.
2329class HCurrentMethod : public HExpression<0> {
2330 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002331 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002332
2333 DECLARE_INSTRUCTION(CurrentMethod);
2334
2335 private:
2336 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2337};
2338
Roland Levillain88cb1752014-10-20 16:36:47 +01002339class HUnaryOperation : public HExpression<1> {
2340 public:
2341 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2342 : HExpression(result_type, SideEffects::None()) {
2343 SetRawInputAt(0, input);
2344 }
2345
2346 HInstruction* GetInput() const { return InputAt(0); }
2347 Primitive::Type GetResultType() const { return GetType(); }
2348
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002349 bool CanBeMoved() const OVERRIDE { return true; }
2350 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002351 UNUSED(other);
2352 return true;
2353 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002354
Roland Levillain9240d6a2014-10-20 16:47:04 +01002355 // Try to statically evaluate `operation` and return a HConstant
2356 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002357 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002358 HConstant* TryStaticEvaluation() const;
2359
2360 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002361 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2362 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002363
Roland Levillain88cb1752014-10-20 16:36:47 +01002364 DECLARE_INSTRUCTION(UnaryOperation);
2365
2366 private:
2367 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2368};
2369
Dave Allison20dfc792014-06-16 20:44:29 -07002370class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002371 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002372 HBinaryOperation(Primitive::Type result_type,
2373 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002374 HInstruction* right,
2375 SideEffects side_effects = SideEffects::None())
2376 : HExpression(result_type, side_effects) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002377 SetRawInputAt(0, left);
2378 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002379 }
2380
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002381 HInstruction* GetLeft() const { return InputAt(0); }
2382 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002383 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002384
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002385 virtual bool IsCommutative() const { return false; }
2386
2387 // Put constant on the right.
2388 // Returns whether order is changed.
2389 bool OrderInputsWithConstantOnTheRight() {
2390 HInstruction* left = InputAt(0);
2391 HInstruction* right = InputAt(1);
2392 if (left->IsConstant() && !right->IsConstant()) {
2393 ReplaceInput(right, 0);
2394 ReplaceInput(left, 1);
2395 return true;
2396 }
2397 return false;
2398 }
2399
2400 // Order inputs by instruction id, but favor constant on the right side.
2401 // This helps GVN for commutative ops.
2402 void OrderInputs() {
2403 DCHECK(IsCommutative());
2404 HInstruction* left = InputAt(0);
2405 HInstruction* right = InputAt(1);
2406 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2407 return;
2408 }
2409 if (OrderInputsWithConstantOnTheRight()) {
2410 return;
2411 }
2412 // Order according to instruction id.
2413 if (left->GetId() > right->GetId()) {
2414 ReplaceInput(right, 0);
2415 ReplaceInput(left, 1);
2416 }
2417 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002418
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002419 bool CanBeMoved() const OVERRIDE { return true; }
2420 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002421 UNUSED(other);
2422 return true;
2423 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002424
Roland Levillain9240d6a2014-10-20 16:47:04 +01002425 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002426 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002427 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002428 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002429
2430 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002431 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2432 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2433 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2434 HLongConstant* y ATTRIBUTE_UNUSED) const {
2435 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2436 return nullptr;
2437 }
2438 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2439 HIntConstant* y ATTRIBUTE_UNUSED) const {
2440 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2441 return nullptr;
2442 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002443
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002444 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002445 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002446 HConstant* GetConstantRight() const;
2447
2448 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002449 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002450 HInstruction* GetLeastConstantLeft() const;
2451
Roland Levillainccc07a92014-09-16 14:48:16 +01002452 DECLARE_INSTRUCTION(BinaryOperation);
2453
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002454 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002455 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2456};
2457
Mark Mendellc4701932015-04-10 13:18:51 -04002458// The comparison bias applies for floating point operations and indicates how NaN
2459// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002460enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002461 kNoBias, // bias is not applicable (i.e. for long operation)
2462 kGtBias, // return 1 for NaN comparisons
2463 kLtBias, // return -1 for NaN comparisons
2464};
2465
Dave Allison20dfc792014-06-16 20:44:29 -07002466class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002467 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002468 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002469 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002470 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002471 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002472
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002473 bool NeedsMaterialization() const { return needs_materialization_; }
2474 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002475
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002476 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002477 // `instruction`, and disregard moves in between.
2478 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002479
Dave Allison20dfc792014-06-16 20:44:29 -07002480 DECLARE_INSTRUCTION(Condition);
2481
2482 virtual IfCondition GetCondition() const = 0;
2483
Mark Mendellc4701932015-04-10 13:18:51 -04002484 virtual IfCondition GetOppositeCondition() const = 0;
2485
Roland Levillain4fa13f62015-07-06 18:11:54 +01002486 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002487
2488 void SetBias(ComparisonBias bias) { bias_ = bias; }
2489
2490 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2491 return bias_ == other->AsCondition()->bias_;
2492 }
2493
Roland Levillain4fa13f62015-07-06 18:11:54 +01002494 bool IsFPConditionTrueIfNaN() const {
2495 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2496 IfCondition if_cond = GetCondition();
2497 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2498 }
2499
2500 bool IsFPConditionFalseIfNaN() const {
2501 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2502 IfCondition if_cond = GetCondition();
2503 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2504 }
2505
Dave Allison20dfc792014-06-16 20:44:29 -07002506 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002507 // For register allocation purposes, returns whether this instruction needs to be
2508 // materialized (that is, not just be in the processor flags).
2509 bool needs_materialization_;
2510
Mark Mendellc4701932015-04-10 13:18:51 -04002511 // Needed if we merge a HCompare into a HCondition.
2512 ComparisonBias bias_;
2513
Dave Allison20dfc792014-06-16 20:44:29 -07002514 DISALLOW_COPY_AND_ASSIGN(HCondition);
2515};
2516
2517// Instruction to check if two inputs are equal to each other.
2518class HEqual : public HCondition {
2519 public:
2520 HEqual(HInstruction* first, HInstruction* second)
2521 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002522
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002523 bool IsCommutative() const OVERRIDE { return true; }
2524
Roland Levillain9867bc72015-08-05 10:21:34 +01002525 template <typename T> bool Compute(T x, T y) const { return x == y; }
2526
2527 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2528 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002529 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002530 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2531 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002532 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002533
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002534 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002535
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002536 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002537 return kCondEQ;
2538 }
2539
Mark Mendellc4701932015-04-10 13:18:51 -04002540 IfCondition GetOppositeCondition() const OVERRIDE {
2541 return kCondNE;
2542 }
2543
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002544 private:
2545 DISALLOW_COPY_AND_ASSIGN(HEqual);
2546};
2547
Dave Allison20dfc792014-06-16 20:44:29 -07002548class HNotEqual : public HCondition {
2549 public:
2550 HNotEqual(HInstruction* first, HInstruction* second)
2551 : HCondition(first, second) {}
2552
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002553 bool IsCommutative() const OVERRIDE { return true; }
2554
Roland Levillain9867bc72015-08-05 10:21:34 +01002555 template <typename T> bool Compute(T x, T y) const { return x != y; }
2556
2557 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2558 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002559 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002560 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2561 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002562 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002563
Dave Allison20dfc792014-06-16 20:44:29 -07002564 DECLARE_INSTRUCTION(NotEqual);
2565
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002566 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002567 return kCondNE;
2568 }
2569
Mark Mendellc4701932015-04-10 13:18:51 -04002570 IfCondition GetOppositeCondition() const OVERRIDE {
2571 return kCondEQ;
2572 }
2573
Dave Allison20dfc792014-06-16 20:44:29 -07002574 private:
2575 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2576};
2577
2578class HLessThan : public HCondition {
2579 public:
2580 HLessThan(HInstruction* first, HInstruction* second)
2581 : HCondition(first, second) {}
2582
Roland Levillain9867bc72015-08-05 10:21:34 +01002583 template <typename T> bool Compute(T x, T y) const { return x < y; }
2584
2585 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2586 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002587 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002588 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2589 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002590 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002591
Dave Allison20dfc792014-06-16 20:44:29 -07002592 DECLARE_INSTRUCTION(LessThan);
2593
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002594 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002595 return kCondLT;
2596 }
2597
Mark Mendellc4701932015-04-10 13:18:51 -04002598 IfCondition GetOppositeCondition() const OVERRIDE {
2599 return kCondGE;
2600 }
2601
Dave Allison20dfc792014-06-16 20:44:29 -07002602 private:
2603 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2604};
2605
2606class HLessThanOrEqual : public HCondition {
2607 public:
2608 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2609 : HCondition(first, second) {}
2610
Roland Levillain9867bc72015-08-05 10:21:34 +01002611 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2612
2613 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2614 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002615 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002616 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2617 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002618 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002619
Dave Allison20dfc792014-06-16 20:44:29 -07002620 DECLARE_INSTRUCTION(LessThanOrEqual);
2621
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002622 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002623 return kCondLE;
2624 }
2625
Mark Mendellc4701932015-04-10 13:18:51 -04002626 IfCondition GetOppositeCondition() const OVERRIDE {
2627 return kCondGT;
2628 }
2629
Dave Allison20dfc792014-06-16 20:44:29 -07002630 private:
2631 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2632};
2633
2634class HGreaterThan : public HCondition {
2635 public:
2636 HGreaterThan(HInstruction* first, HInstruction* second)
2637 : HCondition(first, second) {}
2638
Roland Levillain9867bc72015-08-05 10:21:34 +01002639 template <typename T> bool Compute(T x, T y) const { return x > y; }
2640
2641 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2642 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002643 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002644 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2645 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002646 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002647
Dave Allison20dfc792014-06-16 20:44:29 -07002648 DECLARE_INSTRUCTION(GreaterThan);
2649
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002650 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002651 return kCondGT;
2652 }
2653
Mark Mendellc4701932015-04-10 13:18:51 -04002654 IfCondition GetOppositeCondition() const OVERRIDE {
2655 return kCondLE;
2656 }
2657
Dave Allison20dfc792014-06-16 20:44:29 -07002658 private:
2659 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2660};
2661
2662class HGreaterThanOrEqual : public HCondition {
2663 public:
2664 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2665 : HCondition(first, second) {}
2666
Roland Levillain9867bc72015-08-05 10:21:34 +01002667 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2668
2669 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2670 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002671 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002672 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2673 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01002674 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002675
Dave Allison20dfc792014-06-16 20:44:29 -07002676 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2677
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002678 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002679 return kCondGE;
2680 }
2681
Mark Mendellc4701932015-04-10 13:18:51 -04002682 IfCondition GetOppositeCondition() const OVERRIDE {
2683 return kCondLT;
2684 }
2685
Dave Allison20dfc792014-06-16 20:44:29 -07002686 private:
2687 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2688};
2689
2690
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002691// Instruction to check how two inputs compare to each other.
2692// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2693class HCompare : public HBinaryOperation {
2694 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002695 HCompare(Primitive::Type type,
2696 HInstruction* first,
2697 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002698 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002699 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002700 : HBinaryOperation(Primitive::kPrimInt, first, second, SideEffectsForArchRuntimeCalls(type)),
2701 bias_(bias),
2702 dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002703 DCHECK_EQ(type, first->GetType());
2704 DCHECK_EQ(type, second->GetType());
2705 }
2706
Roland Levillain9867bc72015-08-05 10:21:34 +01002707 template <typename T>
2708 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002709
Roland Levillain9867bc72015-08-05 10:21:34 +01002710 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2711 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
2712 }
2713 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2714 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain556c3d12014-09-18 15:25:07 +01002715 }
2716
Calin Juravleddb7df22014-11-25 20:56:51 +00002717 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2718 return bias_ == other->AsCompare()->bias_;
2719 }
2720
Mark Mendellc4701932015-04-10 13:18:51 -04002721 ComparisonBias GetBias() const { return bias_; }
2722
Roland Levillain4fa13f62015-07-06 18:11:54 +01002723 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002724
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002725 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
2726
2727 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2728 // MIPS64 uses a runtime call for FP comparisons.
2729 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2730 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002731
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002732 DECLARE_INSTRUCTION(Compare);
2733
2734 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002735 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002736 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002737
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002738 DISALLOW_COPY_AND_ASSIGN(HCompare);
2739};
2740
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002741// A local in the graph. Corresponds to a Dex register.
2742class HLocal : public HTemplateInstruction<0> {
2743 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002744 explicit HLocal(uint16_t reg_number)
2745 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002746
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002747 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002748
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002749 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002750
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002751 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002752 // The Dex register number.
2753 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002754
2755 DISALLOW_COPY_AND_ASSIGN(HLocal);
2756};
2757
2758// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002759class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002760 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002761 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002762 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002763 SetRawInputAt(0, local);
2764 }
2765
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002766 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2767
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002768 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002769
2770 private:
2771 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2772};
2773
2774// Store a value in a given local. This instruction has two inputs: the value
2775// and the local.
2776class HStoreLocal : public HTemplateInstruction<2> {
2777 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002778 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002779 SetRawInputAt(0, local);
2780 SetRawInputAt(1, value);
2781 }
2782
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002783 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2784
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002785 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002786
2787 private:
2788 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2789};
2790
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002791class HFloatConstant : public HConstant {
2792 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002793 float GetValue() const { return value_; }
2794
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002795 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002796 DCHECK(other->IsFloatConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002797 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2798 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002799 }
2800
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002801 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002802
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002803 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002804 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002805 }
2806 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002807 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002808 }
2809 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002810 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2811 }
2812 bool IsNaN() const {
2813 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002814 }
2815
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002816 DECLARE_INSTRUCTION(FloatConstant);
2817
2818 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002819 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002820 explicit HFloatConstant(int32_t value)
2821 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002822
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002823 const float value_;
2824
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002825 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002826 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002827 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002828 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2829};
2830
2831class HDoubleConstant : public HConstant {
2832 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002833 double GetValue() const { return value_; }
2834
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002835 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002836 DCHECK(other->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00002837 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2838 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002839 }
2840
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002841 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002842
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002843 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002844 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002845 }
2846 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002847 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002848 }
2849 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002850 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2851 }
2852 bool IsNaN() const {
2853 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002854 }
2855
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002856 DECLARE_INSTRUCTION(DoubleConstant);
2857
2858 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002859 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002860 explicit HDoubleConstant(int64_t value)
2861 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002862
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002863 const double value_;
2864
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002865 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002866 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002867 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002868 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2869};
2870
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002871enum class Intrinsics {
agicsaki57b81ec2015-08-11 17:39:37 -07002872#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironment) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002873#include "intrinsics_list.h"
2874 kNone,
2875 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2876#undef INTRINSICS_LIST
2877#undef OPTIMIZING_INTRINSICS
2878};
2879std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2880
agicsaki57b81ec2015-08-11 17:39:37 -07002881enum IntrinsicNeedsEnvironment {
2882 kNoEnvironment, // Intrinsic does not require an environment.
2883 kNeedsEnvironment // Intrinsic requires an environment.
2884};
2885
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002886class HInvoke : public HInstruction {
2887 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002888 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002889
2890 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2891 // know their environment.
agicsaki57b81ec2015-08-11 17:39:37 -07002892 bool NeedsEnvironment() const OVERRIDE { return needs_environment_ == kNeedsEnvironment; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002893
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002894 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002895 SetRawInputAt(index, argument);
2896 }
2897
Roland Levillain3e3d7332015-04-28 11:00:54 +01002898 // Return the number of arguments. This number can be lower than
2899 // the number of inputs returned by InputCount(), as some invoke
2900 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2901 // inputs at the end of their list of inputs.
2902 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2903
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002904 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002905
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002906 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002907
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002908 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002909 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002910
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002911 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2912
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002913 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002914 return intrinsic_;
2915 }
2916
agicsaki57b81ec2015-08-11 17:39:37 -07002917 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironment needs_environment) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002918 intrinsic_ = intrinsic;
agicsaki57b81ec2015-08-11 17:39:37 -07002919 needs_environment_ = needs_environment;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002920 }
2921
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002922 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002923 return GetEnvironment()->GetParent() != nullptr;
2924 }
2925
2926 bool CanThrow() const OVERRIDE { return true; }
2927
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002928 DECLARE_INSTRUCTION(Invoke);
2929
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002930 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002931 HInvoke(ArenaAllocator* arena,
2932 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002933 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002934 Primitive::Type return_type,
2935 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002936 uint32_t dex_method_index,
2937 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002938 : HInstruction(
2939 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01002940 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002941 inputs_(arena, number_of_arguments),
2942 return_type_(return_type),
2943 dex_pc_(dex_pc),
2944 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002945 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07002946 intrinsic_(Intrinsics::kNone),
2947 needs_environment_(kNeedsEnvironment) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002948 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2949 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002950 }
2951
David Brazdil1abb4192015-02-17 18:33:36 +00002952 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2953 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2954 inputs_.Put(index, input);
2955 }
2956
Roland Levillain3e3d7332015-04-28 11:00:54 +01002957 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002958 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002959 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002960 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002961 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002962 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002963 Intrinsics intrinsic_;
agicsaki57b81ec2015-08-11 17:39:37 -07002964 IntrinsicNeedsEnvironment needs_environment_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002965
2966 private:
2967 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2968};
2969
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002970class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002971 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002972 // Requirements of this method call regarding the class
2973 // initialization (clinit) check of its declaring class.
2974 enum class ClinitCheckRequirement {
2975 kNone, // Class already initialized.
2976 kExplicit, // Static call having explicit clinit check as last input.
2977 kImplicit, // Static call implicitly requiring a clinit check.
2978 };
2979
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002980 HInvokeStaticOrDirect(ArenaAllocator* arena,
2981 uint32_t number_of_arguments,
2982 Primitive::Type return_type,
2983 uint32_t dex_pc,
Vladimir Markob2c431e2015-08-19 12:45:42 +00002984 uint32_t dex_method_index,
2985 bool is_recursive,
2986 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002987 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002988 InvokeType invoke_type,
2989 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002990 : HInvoke(arena,
2991 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002992 // There is one extra argument for the HCurrentMethod node, and
2993 // potentially one other if the clinit check is explicit, and one other
2994 // if the method is a string factory.
2995 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Markob2c431e2015-08-19 12:45:42 +00002996 + (string_init_offset ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002997 return_type,
2998 dex_pc,
Vladimir Markob2c431e2015-08-19 12:45:42 +00002999 dex_method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003000 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003001 invoke_type_(invoke_type),
Vladimir Markob2c431e2015-08-19 12:45:42 +00003002 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08003003 clinit_check_requirement_(clinit_check_requirement),
Vladimir Markob2c431e2015-08-19 12:45:42 +00003004 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003005
Calin Juravle641547a2015-04-21 22:08:51 +01003006 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3007 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003008 // We access the method via the dex cache so we can't do an implicit null check.
3009 // TODO: for intrinsics we can generate implicit null checks.
3010 return false;
3011 }
3012
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003013 bool CanBeNull() const OVERRIDE {
3014 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3015 }
3016
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003017 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003018 bool IsRecursive() const { return is_recursive_; }
3019 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
3020 bool IsStringInit() const { return string_init_offset_ != 0; }
3021 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003022 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003023
Roland Levillain4c0eb422015-04-24 16:43:49 +01003024 // Is this instruction a call to a static method?
3025 bool IsStatic() const {
3026 return GetInvokeType() == kStatic;
3027 }
3028
Roland Levillain3e3d7332015-04-28 11:00:54 +01003029 // Remove the art::HLoadClass instruction set as last input by
3030 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3031 // the initial art::HClinitCheck instruction (only relevant for
3032 // static calls with explicit clinit check).
3033 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003034 DCHECK(IsStaticWithExplicitClinitCheck());
3035 size_t last_input_index = InputCount() - 1;
3036 HInstruction* last_input = InputAt(last_input_index);
3037 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003038 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003039 RemoveAsUserOfInput(last_input_index);
3040 inputs_.DeleteAt(last_input_index);
3041 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3042 DCHECK(IsStaticWithImplicitClinitCheck());
3043 }
3044
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003045 bool IsStringFactoryFor(HFakeString* str) const {
3046 if (!IsStringInit()) return false;
3047 // +1 for the current method.
3048 if (InputCount() == (number_of_arguments_ + 1)) return false;
3049 return InputAt(InputCount() - 1)->AsFakeString() == str;
3050 }
3051
3052 void RemoveFakeStringArgumentAsLastInput() {
3053 DCHECK(IsStringInit());
3054 size_t last_input_index = InputCount() - 1;
3055 HInstruction* last_input = InputAt(last_input_index);
3056 DCHECK(last_input != nullptr);
3057 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3058 RemoveAsUserOfInput(last_input_index);
3059 inputs_.DeleteAt(last_input_index);
3060 }
3061
Roland Levillain4c0eb422015-04-24 16:43:49 +01003062 // Is this a call to a static method whose declaring class has an
3063 // explicit intialization check in the graph?
3064 bool IsStaticWithExplicitClinitCheck() const {
3065 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3066 }
3067
3068 // Is this a call to a static method whose declaring class has an
3069 // implicit intialization check requirement?
3070 bool IsStaticWithImplicitClinitCheck() const {
3071 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3072 }
3073
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003074 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003075
Roland Levillain4c0eb422015-04-24 16:43:49 +01003076 protected:
3077 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3078 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3079 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3080 HInstruction* input = input_record.GetInstruction();
3081 // `input` is the last input of a static invoke marked as having
3082 // an explicit clinit check. It must either be:
3083 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3084 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3085 DCHECK(input != nullptr);
3086 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3087 }
3088 return input_record;
3089 }
3090
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003091 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003092 const InvokeType invoke_type_;
Vladimir Markob2c431e2015-08-19 12:45:42 +00003093 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003094 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Markob2c431e2015-08-19 12:45:42 +00003095 // Thread entrypoint offset for string init method if this is a string init invoke.
3096 // Note that there are multiple string init methods, each having its own offset.
3097 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003098
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003099 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003100};
3101
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003102class HInvokeVirtual : public HInvoke {
3103 public:
3104 HInvokeVirtual(ArenaAllocator* arena,
3105 uint32_t number_of_arguments,
3106 Primitive::Type return_type,
3107 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003108 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003109 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003110 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003111 vtable_index_(vtable_index) {}
3112
Calin Juravle641547a2015-04-21 22:08:51 +01003113 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003114 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003115 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003116 }
3117
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003118 uint32_t GetVTableIndex() const { return vtable_index_; }
3119
3120 DECLARE_INSTRUCTION(InvokeVirtual);
3121
3122 private:
3123 const uint32_t vtable_index_;
3124
3125 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3126};
3127
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003128class HInvokeInterface : public HInvoke {
3129 public:
3130 HInvokeInterface(ArenaAllocator* arena,
3131 uint32_t number_of_arguments,
3132 Primitive::Type return_type,
3133 uint32_t dex_pc,
3134 uint32_t dex_method_index,
3135 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003136 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003137 imt_index_(imt_index) {}
3138
Calin Juravle641547a2015-04-21 22:08:51 +01003139 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003140 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003141 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003142 }
3143
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003144 uint32_t GetImtIndex() const { return imt_index_; }
3145 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3146
3147 DECLARE_INSTRUCTION(InvokeInterface);
3148
3149 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003150 const uint32_t imt_index_;
3151
3152 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3153};
3154
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003155class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003156 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003157 HNewInstance(HCurrentMethod* current_method,
3158 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003159 uint16_t type_index,
3160 const DexFile& dex_file,
3161 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003162 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003163 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003164 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003165 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003166 entrypoint_(entrypoint) {
3167 SetRawInputAt(0, current_method);
3168 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003169
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003170 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003171 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003172 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003173
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003174 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003175 bool NeedsEnvironment() const OVERRIDE { return true; }
3176 // It may throw when called on:
3177 // - interfaces
3178 // - abstract/innaccessible/unknown classes
3179 // TODO: optimize when possible.
3180 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003181
Calin Juravle10e244f2015-01-26 18:54:32 +00003182 bool CanBeNull() const OVERRIDE { return false; }
3183
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003184 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3185
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003186 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003187
3188 private:
3189 const uint32_t dex_pc_;
3190 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003191 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003192 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003193
3194 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3195};
3196
Roland Levillain88cb1752014-10-20 16:36:47 +01003197class HNeg : public HUnaryOperation {
3198 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003199 HNeg(Primitive::Type result_type, HInstruction* input)
Roland Levillain88cb1752014-10-20 16:36:47 +01003200 : HUnaryOperation(result_type, input) {}
3201
Roland Levillain9867bc72015-08-05 10:21:34 +01003202 template <typename T> T Compute(T x) const { return -x; }
3203
3204 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3205 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3206 }
3207 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3208 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3209 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003210
Roland Levillain88cb1752014-10-20 16:36:47 +01003211 DECLARE_INSTRUCTION(Neg);
3212
3213 private:
3214 DISALLOW_COPY_AND_ASSIGN(HNeg);
3215};
3216
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003217class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003218 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003219 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003220 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003221 uint32_t dex_pc,
3222 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003223 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003224 QuickEntrypointEnum entrypoint)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003225 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC()),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003226 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003227 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003228 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003229 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003230 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003231 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003232 }
3233
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003234 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003235 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003236 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003237
3238 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003239 bool NeedsEnvironment() const OVERRIDE { return true; }
3240
Mingyao Yang0c365e62015-03-31 15:09:29 -07003241 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3242 bool CanThrow() const OVERRIDE { return true; }
3243
Calin Juravle10e244f2015-01-26 18:54:32 +00003244 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003245
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003246 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3247
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003248 DECLARE_INSTRUCTION(NewArray);
3249
3250 private:
3251 const uint32_t dex_pc_;
3252 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003253 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003254 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003255
3256 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3257};
3258
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003259class HAdd : public HBinaryOperation {
3260 public:
3261 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3262 : HBinaryOperation(result_type, left, right) {}
3263
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003264 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003265
Roland Levillain9867bc72015-08-05 10:21:34 +01003266 template <typename T> T Compute(T x, T y) const { return x + y; }
3267
3268 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3269 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003270 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003271 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3272 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003273 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003274
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003275 DECLARE_INSTRUCTION(Add);
3276
3277 private:
3278 DISALLOW_COPY_AND_ASSIGN(HAdd);
3279};
3280
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003281class HSub : public HBinaryOperation {
3282 public:
3283 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3284 : HBinaryOperation(result_type, left, right) {}
3285
Roland Levillain9867bc72015-08-05 10:21:34 +01003286 template <typename T> T Compute(T x, T y) const { return x - y; }
3287
3288 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3289 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003290 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003291 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3292 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Roland Levillain93445682014-10-06 19:24:02 +01003293 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003294
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003295 DECLARE_INSTRUCTION(Sub);
3296
3297 private:
3298 DISALLOW_COPY_AND_ASSIGN(HSub);
3299};
3300
Calin Juravle34bacdf2014-10-07 20:23:36 +01003301class HMul : public HBinaryOperation {
3302 public:
3303 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3304 : HBinaryOperation(result_type, left, right) {}
3305
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003306 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003307
Roland Levillain9867bc72015-08-05 10:21:34 +01003308 template <typename T> T Compute(T x, T y) const { return x * y; }
3309
3310 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3311 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3312 }
3313 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3314 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3315 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003316
3317 DECLARE_INSTRUCTION(Mul);
3318
3319 private:
3320 DISALLOW_COPY_AND_ASSIGN(HMul);
3321};
3322
Calin Juravle7c4954d2014-10-28 16:57:40 +00003323class HDiv : public HBinaryOperation {
3324 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003325 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003326 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3327 dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003328
Roland Levillain9867bc72015-08-05 10:21:34 +01003329 template <typename T>
3330 T Compute(T x, T y) const {
3331 // Our graph structure ensures we never have 0 for `y` during
3332 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003333 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003334 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003335 return (y == -1) ? -x : x / y;
3336 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003337
Roland Levillain9867bc72015-08-05 10:21:34 +01003338 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3339 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3340 }
3341 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3342 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003343 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003344
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003345 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003346
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003347 static SideEffects SideEffectsForArchRuntimeCalls() {
3348 // The generated code can use a runtime call.
3349 return SideEffects::CanTriggerGC();
3350 }
3351
Calin Juravle7c4954d2014-10-28 16:57:40 +00003352 DECLARE_INSTRUCTION(Div);
3353
3354 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003355 const uint32_t dex_pc_;
3356
Calin Juravle7c4954d2014-10-28 16:57:40 +00003357 DISALLOW_COPY_AND_ASSIGN(HDiv);
3358};
3359
Calin Juravlebacfec32014-11-14 15:54:36 +00003360class HRem : public HBinaryOperation {
3361 public:
3362 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003363 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls()),
3364 dex_pc_(dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003365
Roland Levillain9867bc72015-08-05 10:21:34 +01003366 template <typename T>
3367 T Compute(T x, T y) const {
3368 // Our graph structure ensures we never have 0 for `y` during
3369 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003370 DCHECK_NE(y, 0);
3371 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3372 return (y == -1) ? 0 : x % y;
3373 }
3374
Roland Levillain9867bc72015-08-05 10:21:34 +01003375 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3376 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3377 }
3378 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3379 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
Calin Juravlebacfec32014-11-14 15:54:36 +00003380 }
3381
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003382 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003383
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003384 static SideEffects SideEffectsForArchRuntimeCalls() {
3385 return SideEffects::CanTriggerGC();
3386 }
3387
Calin Juravlebacfec32014-11-14 15:54:36 +00003388 DECLARE_INSTRUCTION(Rem);
3389
3390 private:
3391 const uint32_t dex_pc_;
3392
3393 DISALLOW_COPY_AND_ASSIGN(HRem);
3394};
3395
Calin Juravled0d48522014-11-04 16:40:20 +00003396class HDivZeroCheck : public HExpression<1> {
3397 public:
3398 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3399 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3400 SetRawInputAt(0, value);
3401 }
3402
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003403 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3404
Calin Juravled0d48522014-11-04 16:40:20 +00003405 bool CanBeMoved() const OVERRIDE { return true; }
3406
3407 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3408 UNUSED(other);
3409 return true;
3410 }
3411
3412 bool NeedsEnvironment() const OVERRIDE { return true; }
3413 bool CanThrow() const OVERRIDE { return true; }
3414
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003415 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003416
3417 DECLARE_INSTRUCTION(DivZeroCheck);
3418
3419 private:
3420 const uint32_t dex_pc_;
3421
3422 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3423};
3424
Calin Juravle9aec02f2014-11-18 23:06:35 +00003425class HShl : public HBinaryOperation {
3426 public:
3427 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3428 : HBinaryOperation(result_type, left, right) {}
3429
Roland Levillain9867bc72015-08-05 10:21:34 +01003430 template <typename T, typename U, typename V>
3431 T Compute(T x, U y, V max_shift_value) const {
3432 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3433 "V is not the unsigned integer type corresponding to T");
3434 return x << (y & max_shift_value);
3435 }
3436
3437 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3438 return GetBlock()->GetGraph()->GetIntConstant(
3439 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3440 }
3441 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3442 // case is handled as `x << static_cast<int>(y)`.
3443 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3444 return GetBlock()->GetGraph()->GetLongConstant(
3445 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3446 }
3447 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3448 return GetBlock()->GetGraph()->GetLongConstant(
3449 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3450 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003451
3452 DECLARE_INSTRUCTION(Shl);
3453
3454 private:
3455 DISALLOW_COPY_AND_ASSIGN(HShl);
3456};
3457
3458class HShr : public HBinaryOperation {
3459 public:
3460 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3461 : HBinaryOperation(result_type, left, right) {}
3462
Roland Levillain9867bc72015-08-05 10:21:34 +01003463 template <typename T, typename U, typename V>
3464 T Compute(T x, U y, V max_shift_value) const {
3465 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3466 "V is not the unsigned integer type corresponding to T");
3467 return x >> (y & max_shift_value);
3468 }
3469
3470 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3471 return GetBlock()->GetGraph()->GetIntConstant(
3472 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3473 }
3474 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3475 // case is handled as `x >> static_cast<int>(y)`.
3476 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3477 return GetBlock()->GetGraph()->GetLongConstant(
3478 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3479 }
3480 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3481 return GetBlock()->GetGraph()->GetLongConstant(
3482 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3483 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003484
3485 DECLARE_INSTRUCTION(Shr);
3486
3487 private:
3488 DISALLOW_COPY_AND_ASSIGN(HShr);
3489};
3490
3491class HUShr : public HBinaryOperation {
3492 public:
3493 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3494 : HBinaryOperation(result_type, left, right) {}
3495
Roland Levillain9867bc72015-08-05 10:21:34 +01003496 template <typename T, typename U, typename V>
3497 T Compute(T x, U y, V max_shift_value) const {
3498 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3499 "V is not the unsigned integer type corresponding to T");
3500 V ux = static_cast<V>(x);
3501 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003502 }
3503
Roland Levillain9867bc72015-08-05 10:21:34 +01003504 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3505 return GetBlock()->GetGraph()->GetIntConstant(
3506 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue));
3507 }
3508 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3509 // case is handled as `x >>> static_cast<int>(y)`.
3510 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3511 return GetBlock()->GetGraph()->GetLongConstant(
3512 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
3513 }
3514 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3515 return GetBlock()->GetGraph()->GetLongConstant(
3516 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003517 }
3518
3519 DECLARE_INSTRUCTION(UShr);
3520
3521 private:
3522 DISALLOW_COPY_AND_ASSIGN(HUShr);
3523};
3524
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003525class HAnd : public HBinaryOperation {
3526 public:
3527 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3528 : HBinaryOperation(result_type, left, right) {}
3529
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003530 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003531
Roland Levillain9867bc72015-08-05 10:21:34 +01003532 template <typename T, typename U>
3533 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3534
3535 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3536 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3537 }
3538 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3539 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3540 }
3541 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3542 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3543 }
3544 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3545 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3546 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003547
3548 DECLARE_INSTRUCTION(And);
3549
3550 private:
3551 DISALLOW_COPY_AND_ASSIGN(HAnd);
3552};
3553
3554class HOr : public HBinaryOperation {
3555 public:
3556 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3557 : HBinaryOperation(result_type, left, right) {}
3558
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003559 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003560
Roland Levillain9867bc72015-08-05 10:21:34 +01003561 template <typename T, typename U>
3562 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3563
3564 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3565 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3566 }
3567 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3568 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3569 }
3570 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3571 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3572 }
3573 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3574 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3575 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003576
3577 DECLARE_INSTRUCTION(Or);
3578
3579 private:
3580 DISALLOW_COPY_AND_ASSIGN(HOr);
3581};
3582
3583class HXor : public HBinaryOperation {
3584 public:
3585 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3586 : HBinaryOperation(result_type, left, right) {}
3587
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003588 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003589
Roland Levillain9867bc72015-08-05 10:21:34 +01003590 template <typename T, typename U>
3591 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3592
3593 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3594 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue(), y->GetValue()));
3595 }
3596 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
3597 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3598 }
3599 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3600 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3601 }
3602 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3603 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue(), y->GetValue()));
3604 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003605
3606 DECLARE_INSTRUCTION(Xor);
3607
3608 private:
3609 DISALLOW_COPY_AND_ASSIGN(HXor);
3610};
3611
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003612// The value of a parameter in this method. Its location depends on
3613// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003614class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003615 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003616 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3617 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003618
3619 uint8_t GetIndex() const { return index_; }
3620
Calin Juravle10e244f2015-01-26 18:54:32 +00003621 bool CanBeNull() const OVERRIDE { return !is_this_; }
3622
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003623 bool IsThis() const { return is_this_; }
3624
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003625 DECLARE_INSTRUCTION(ParameterValue);
3626
3627 private:
3628 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003629 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003630 const uint8_t index_;
3631
Calin Juravle10e244f2015-01-26 18:54:32 +00003632 // Whether or not the parameter value corresponds to 'this' argument.
3633 const bool is_this_;
3634
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003635 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3636};
3637
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003638class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003639 public:
Roland Levillain3887c462015-08-12 18:15:42 +01003640 HNot(Primitive::Type result_type, HInstruction* input)
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003641 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003642
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003643 bool CanBeMoved() const OVERRIDE { return true; }
3644 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003645 UNUSED(other);
3646 return true;
3647 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003648
Roland Levillain9867bc72015-08-05 10:21:34 +01003649 template <typename T> T Compute(T x) const { return ~x; }
3650
3651 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3652 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3653 }
3654 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
3655 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()));
3656 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003657
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003658 DECLARE_INSTRUCTION(Not);
3659
3660 private:
3661 DISALLOW_COPY_AND_ASSIGN(HNot);
3662};
3663
David Brazdil66d126e2015-04-03 16:02:44 +01003664class HBooleanNot : public HUnaryOperation {
3665 public:
3666 explicit HBooleanNot(HInstruction* input)
3667 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3668
3669 bool CanBeMoved() const OVERRIDE { return true; }
3670 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3671 UNUSED(other);
3672 return true;
3673 }
3674
Roland Levillain9867bc72015-08-05 10:21:34 +01003675 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003676 DCHECK(IsUint<1>(x));
3677 return !x;
3678 }
3679
Roland Levillain9867bc72015-08-05 10:21:34 +01003680 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
3681 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()));
3682 }
3683 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3684 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003685 UNREACHABLE();
3686 }
3687
3688 DECLARE_INSTRUCTION(BooleanNot);
3689
3690 private:
3691 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3692};
3693
Roland Levillaindff1f282014-11-05 14:15:05 +00003694class HTypeConversion : public HExpression<1> {
3695 public:
3696 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003697 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003698 : HExpression(result_type, SideEffectsForArchRuntimeCalls(input->GetType(), result_type)),
3699 dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003700 SetRawInputAt(0, input);
3701 DCHECK_NE(input->GetType(), result_type);
3702 }
3703
3704 HInstruction* GetInput() const { return InputAt(0); }
3705 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3706 Primitive::Type GetResultType() const { return GetType(); }
3707
Roland Levillain624279f2014-12-04 11:54:28 +00003708 // Required by the x86 and ARM code generators when producing calls
3709 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003710 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003711
Roland Levillaindff1f282014-11-05 14:15:05 +00003712 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003713 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003714
Mark Mendelle82549b2015-05-06 10:55:34 -04003715 // Try to statically evaluate the conversion and return a HConstant
3716 // containing the result. If the input cannot be converted, return nullptr.
3717 HConstant* TryStaticEvaluation() const;
3718
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003719 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
3720 Primitive::Type result_type) {
3721 // Some architectures may not require the 'GC' side effects, but at this point
3722 // in the compilation process we do not know what architecture we will
3723 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01003724 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
3725 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003726 return SideEffects::CanTriggerGC();
3727 }
3728 return SideEffects::None();
3729 }
3730
Roland Levillaindff1f282014-11-05 14:15:05 +00003731 DECLARE_INSTRUCTION(TypeConversion);
3732
3733 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003734 const uint32_t dex_pc_;
3735
Roland Levillaindff1f282014-11-05 14:15:05 +00003736 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3737};
3738
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003739static constexpr uint32_t kNoRegNumber = -1;
3740
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003741class HPhi : public HInstruction {
3742 public:
3743 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003744 : HInstruction(SideEffects::None()),
3745 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003746 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003747 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003748 is_live_(false),
3749 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003750 inputs_.SetSize(number_of_inputs);
3751 }
3752
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003753 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3754 static Primitive::Type ToPhiType(Primitive::Type type) {
3755 switch (type) {
3756 case Primitive::kPrimBoolean:
3757 case Primitive::kPrimByte:
3758 case Primitive::kPrimShort:
3759 case Primitive::kPrimChar:
3760 return Primitive::kPrimInt;
3761 default:
3762 return type;
3763 }
3764 }
3765
David Brazdilffee3d32015-07-06 11:48:53 +01003766 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3767
Calin Juravle10e244f2015-01-26 18:54:32 +00003768 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003769
3770 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003771 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003772
Calin Juravle10e244f2015-01-26 18:54:32 +00003773 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003774 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003775
Calin Juravle10e244f2015-01-26 18:54:32 +00003776 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3777 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3778
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003779 uint32_t GetRegNumber() const { return reg_number_; }
3780
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003781 void SetDead() { is_live_ = false; }
3782 void SetLive() { is_live_ = true; }
3783 bool IsDead() const { return !is_live_; }
3784 bool IsLive() const { return is_live_; }
3785
Calin Juravlea4f88312015-04-16 12:57:19 +01003786 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3787 // An equivalent phi is a phi having the same dex register and type.
3788 // It assumes that phis with the same dex register are adjacent.
3789 HPhi* GetNextEquivalentPhiWithSameType() {
3790 HInstruction* next = GetNext();
3791 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3792 if (next->GetType() == GetType()) {
3793 return next->AsPhi();
3794 }
3795 next = next->GetNext();
3796 }
3797 return nullptr;
3798 }
3799
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003800 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003801
David Brazdil1abb4192015-02-17 18:33:36 +00003802 protected:
3803 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3804
3805 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3806 inputs_.Put(index, input);
3807 }
3808
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003809 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003810 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003811 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003812 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003813 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003814 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003815
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003816 DISALLOW_COPY_AND_ASSIGN(HPhi);
3817};
3818
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003819class HNullCheck : public HExpression<1> {
3820 public:
3821 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003822 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003823 SetRawInputAt(0, value);
3824 }
3825
Calin Juravle10e244f2015-01-26 18:54:32 +00003826 bool CanBeMoved() const OVERRIDE { return true; }
3827 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003828 UNUSED(other);
3829 return true;
3830 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003831
Calin Juravle10e244f2015-01-26 18:54:32 +00003832 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003833
Calin Juravle10e244f2015-01-26 18:54:32 +00003834 bool CanThrow() const OVERRIDE { return true; }
3835
3836 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003837
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003838 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003839
3840 DECLARE_INSTRUCTION(NullCheck);
3841
3842 private:
3843 const uint32_t dex_pc_;
3844
3845 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3846};
3847
3848class FieldInfo : public ValueObject {
3849 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003850 FieldInfo(MemberOffset field_offset,
3851 Primitive::Type field_type,
3852 bool is_volatile,
3853 uint32_t index,
3854 const DexFile& dex_file)
3855 : field_offset_(field_offset),
3856 field_type_(field_type),
3857 is_volatile_(is_volatile),
3858 index_(index),
3859 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003860
3861 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003862 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003863 uint32_t GetFieldIndex() const { return index_; }
3864 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003865 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003866
3867 private:
3868 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003869 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003870 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003871 uint32_t index_;
3872 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003873};
3874
3875class HInstanceFieldGet : public HExpression<1> {
3876 public:
3877 HInstanceFieldGet(HInstruction* value,
3878 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003879 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003880 bool is_volatile,
3881 uint32_t field_idx,
3882 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003883 : HExpression(
3884 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01003885 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003886 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003887 SetRawInputAt(0, value);
3888 }
3889
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003890 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003891
3892 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3893 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3894 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003895 }
3896
Calin Juravle641547a2015-04-21 22:08:51 +01003897 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3898 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003899 }
3900
3901 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003902 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3903 }
3904
Calin Juravle52c48962014-12-16 17:02:57 +00003905 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003906 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003907 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003908 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003909
3910 DECLARE_INSTRUCTION(InstanceFieldGet);
3911
3912 private:
3913 const FieldInfo field_info_;
3914
3915 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3916};
3917
3918class HInstanceFieldSet : public HTemplateInstruction<2> {
3919 public:
3920 HInstanceFieldSet(HInstruction* object,
3921 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003922 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003923 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003924 bool is_volatile,
3925 uint32_t field_idx,
3926 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003927 : HTemplateInstruction(
3928 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003929 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003930 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003931 SetRawInputAt(0, object);
3932 SetRawInputAt(1, value);
3933 }
3934
Calin Juravle641547a2015-04-21 22:08:51 +01003935 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3936 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003937 }
3938
Calin Juravle52c48962014-12-16 17:02:57 +00003939 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003940 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003941 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003942 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003943 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003944 bool GetValueCanBeNull() const { return value_can_be_null_; }
3945 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003946
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003947 DECLARE_INSTRUCTION(InstanceFieldSet);
3948
3949 private:
3950 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003951 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003952
3953 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3954};
3955
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003956class HArrayGet : public HExpression<2> {
3957 public:
3958 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07003959 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003960 SetRawInputAt(0, array);
3961 SetRawInputAt(1, index);
3962 }
3963
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003964 bool CanBeMoved() const OVERRIDE { return true; }
3965 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003966 UNUSED(other);
3967 return true;
3968 }
Calin Juravle641547a2015-04-21 22:08:51 +01003969 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3970 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003971 // TODO: We can be smarter here.
3972 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3973 // which generates the implicit null check. There are cases when these can be removed
3974 // to produce better code. If we ever add optimizations to do so we should allow an
3975 // implicit check here (as long as the address falls in the first page).
3976 return false;
3977 }
3978
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003979 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003980
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003981 HInstruction* GetArray() const { return InputAt(0); }
3982 HInstruction* GetIndex() const { return InputAt(1); }
3983
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003984 DECLARE_INSTRUCTION(ArrayGet);
3985
3986 private:
3987 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3988};
3989
3990class HArraySet : public HTemplateInstruction<3> {
3991 public:
3992 HArraySet(HInstruction* array,
3993 HInstruction* index,
3994 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003995 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003996 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003997 : HTemplateInstruction(
3998 SideEffects::ArrayWriteOfType(expected_component_type).Union(
3999 SideEffectsForArchRuntimeCalls(value->GetType()))),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004000 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004001 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004002 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4003 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004004 SetRawInputAt(0, array);
4005 SetRawInputAt(1, index);
4006 SetRawInputAt(2, value);
4007 }
4008
Calin Juravle77520bc2015-01-12 18:45:46 +00004009 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004010 // We currently always call a runtime method to catch array store
4011 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004012 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004013 }
4014
Mingyao Yang81014cb2015-06-02 03:16:27 -07004015 // Can throw ArrayStoreException.
4016 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4017
Calin Juravle641547a2015-04-21 22:08:51 +01004018 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4019 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004020 // TODO: Same as for ArrayGet.
4021 return false;
4022 }
4023
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004024 void ClearNeedsTypeCheck() {
4025 needs_type_check_ = false;
4026 }
4027
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004028 void ClearValueCanBeNull() {
4029 value_can_be_null_ = false;
4030 }
4031
4032 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004033 bool NeedsTypeCheck() const { return needs_type_check_; }
4034
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004035 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004036
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004037 HInstruction* GetArray() const { return InputAt(0); }
4038 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004039 HInstruction* GetValue() const { return InputAt(2); }
4040
4041 Primitive::Type GetComponentType() const {
4042 // The Dex format does not type floating point index operations. Since the
4043 // `expected_component_type_` is set during building and can therefore not
4044 // be correct, we also check what is the value type. If it is a floating
4045 // point type, we must use that type.
4046 Primitive::Type value_type = GetValue()->GetType();
4047 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4048 ? value_type
4049 : expected_component_type_;
4050 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004051
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004052 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4053 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4054 }
4055
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004056 DECLARE_INSTRUCTION(ArraySet);
4057
4058 private:
4059 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004060 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004061 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004062 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004063
4064 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4065};
4066
4067class HArrayLength : public HExpression<1> {
4068 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004069 explicit HArrayLength(HInstruction* array)
4070 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
4071 // Note that arrays do not change length, so the instruction does not
4072 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004073 SetRawInputAt(0, array);
4074 }
4075
Calin Juravle77520bc2015-01-12 18:45:46 +00004076 bool CanBeMoved() const OVERRIDE { return true; }
4077 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004078 UNUSED(other);
4079 return true;
4080 }
Calin Juravle641547a2015-04-21 22:08:51 +01004081 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4082 return obj == InputAt(0);
4083 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004084
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004085 DECLARE_INSTRUCTION(ArrayLength);
4086
4087 private:
4088 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4089};
4090
4091class HBoundsCheck : public HExpression<2> {
4092 public:
4093 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004094 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004095 DCHECK(index->GetType() == Primitive::kPrimInt);
4096 SetRawInputAt(0, index);
4097 SetRawInputAt(1, length);
4098 }
4099
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004100 bool CanBeMoved() const OVERRIDE { return true; }
4101 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004102 UNUSED(other);
4103 return true;
4104 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004105
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004106 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004107
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004108 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004109
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004110 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004111
4112 DECLARE_INSTRUCTION(BoundsCheck);
4113
4114 private:
4115 const uint32_t dex_pc_;
4116
4117 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4118};
4119
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004120/**
4121 * Some DEX instructions are folded into multiple HInstructions that need
4122 * to stay live until the last HInstruction. This class
4123 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004124 * HInstruction stays live. `index` represents the stack location index of the
4125 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004126 */
4127class HTemporary : public HTemplateInstruction<0> {
4128 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004129 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004130
4131 size_t GetIndex() const { return index_; }
4132
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004133 Primitive::Type GetType() const OVERRIDE {
4134 // The previous instruction is the one that will be stored in the temporary location.
4135 DCHECK(GetPrevious() != nullptr);
4136 return GetPrevious()->GetType();
4137 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004138
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004139 DECLARE_INSTRUCTION(Temporary);
4140
4141 private:
4142 const size_t index_;
4143
4144 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4145};
4146
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004147class HSuspendCheck : public HTemplateInstruction<0> {
4148 public:
4149 explicit HSuspendCheck(uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004150 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004151
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004152 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004153 return true;
4154 }
4155
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004156 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004157 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4158 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004159
4160 DECLARE_INSTRUCTION(SuspendCheck);
4161
4162 private:
4163 const uint32_t dex_pc_;
4164
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004165 // Only used for code generation, in order to share the same slow path between back edges
4166 // of a same loop.
4167 SlowPathCode* slow_path_;
4168
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004169 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4170};
4171
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004172/**
4173 * Instruction to load a Class object.
4174 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004175class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004176 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004177 HLoadClass(HCurrentMethod* current_method,
4178 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004179 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004180 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004181 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004182 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004183 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004184 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004185 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004186 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00004187 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004188 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004189 SetRawInputAt(0, current_method);
4190 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004191
4192 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004193
4194 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4195 return other->AsLoadClass()->type_index_ == type_index_;
4196 }
4197
4198 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4199
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004200 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004201 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004202 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004203 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004204
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004205 bool NeedsEnvironment() const OVERRIDE {
4206 // Will call runtime and load the class if the class is not loaded yet.
4207 // TODO: finer grain decision.
4208 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004209 }
4210
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004211 bool MustGenerateClinitCheck() const {
4212 return generate_clinit_check_;
4213 }
4214
Calin Juravle0ba218d2015-05-19 18:46:01 +01004215 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4216 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004217 }
4218
4219 bool CanCallRuntime() const {
4220 return MustGenerateClinitCheck() || !is_referrers_class_;
4221 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004222
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004223 bool CanThrow() const OVERRIDE {
4224 // May call runtime and and therefore can throw.
4225 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004226 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004227 }
4228
Calin Juravleacf735c2015-02-12 15:25:22 +00004229 ReferenceTypeInfo GetLoadedClassRTI() {
4230 return loaded_class_rti_;
4231 }
4232
4233 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4234 // Make sure we only set exact types (the loaded class should never be merged).
4235 DCHECK(rti.IsExact());
4236 loaded_class_rti_ = rti;
4237 }
4238
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004239 const DexFile& GetDexFile() { return dex_file_; }
4240
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004241 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4242
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004243 static SideEffects SideEffectsForArchRuntimeCalls() {
4244 return SideEffects::CanTriggerGC();
4245 }
4246
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004247 DECLARE_INSTRUCTION(LoadClass);
4248
4249 private:
4250 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004251 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004252 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004253 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004254 // Whether this instruction must generate the initialization check.
4255 // Used for code generation.
4256 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004257
Calin Juravleacf735c2015-02-12 15:25:22 +00004258 ReferenceTypeInfo loaded_class_rti_;
4259
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004260 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4261};
4262
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004263class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004264 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004265 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004266 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls()),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004267 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004268 dex_pc_(dex_pc) {
4269 SetRawInputAt(0, current_method);
4270 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004271
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004272 bool CanBeMoved() const OVERRIDE { return true; }
4273
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004274 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4275 return other->AsLoadString()->string_index_ == string_index_;
4276 }
4277
4278 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4279
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004280 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004281 uint32_t GetStringIndex() const { return string_index_; }
4282
4283 // TODO: Can we deopt or debug when we resolve a string?
4284 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004285 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004286
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004287 static SideEffects SideEffectsForArchRuntimeCalls() {
4288 return SideEffects::CanTriggerGC();
4289 }
4290
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004291 DECLARE_INSTRUCTION(LoadString);
4292
4293 private:
4294 const uint32_t string_index_;
4295 const uint32_t dex_pc_;
4296
4297 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4298};
4299
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004300/**
4301 * Performs an initialization check on its Class object input.
4302 */
4303class HClinitCheck : public HExpression<1> {
4304 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004305 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004306 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004307 Primitive::kPrimNot,
4308 SideEffects::AllChanges()), // Assume write/read on all fields/arrays.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004309 dex_pc_(dex_pc) {
4310 SetRawInputAt(0, constant);
4311 }
4312
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004313 bool CanBeMoved() const OVERRIDE { return true; }
4314 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4315 UNUSED(other);
4316 return true;
4317 }
4318
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004319 bool NeedsEnvironment() const OVERRIDE {
4320 // May call runtime to initialize the class.
4321 return true;
4322 }
4323
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004324 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004325
4326 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4327
4328 DECLARE_INSTRUCTION(ClinitCheck);
4329
4330 private:
4331 const uint32_t dex_pc_;
4332
4333 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4334};
4335
4336class HStaticFieldGet : public HExpression<1> {
4337 public:
4338 HStaticFieldGet(HInstruction* cls,
4339 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004340 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004341 bool is_volatile,
4342 uint32_t field_idx,
4343 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004344 : HExpression(
4345 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004346 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004347 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004348 SetRawInputAt(0, cls);
4349 }
4350
Calin Juravle52c48962014-12-16 17:02:57 +00004351
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004352 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004353
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004354 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004355 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4356 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004357 }
4358
4359 size_t ComputeHashCode() const OVERRIDE {
4360 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4361 }
4362
Calin Juravle52c48962014-12-16 17:02:57 +00004363 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004364 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4365 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004366 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004367
4368 DECLARE_INSTRUCTION(StaticFieldGet);
4369
4370 private:
4371 const FieldInfo field_info_;
4372
4373 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4374};
4375
4376class HStaticFieldSet : public HTemplateInstruction<2> {
4377 public:
4378 HStaticFieldSet(HInstruction* cls,
4379 HInstruction* value,
4380 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004381 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004382 bool is_volatile,
4383 uint32_t field_idx,
4384 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004385 : HTemplateInstruction(
4386 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004387 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004388 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004389 SetRawInputAt(0, cls);
4390 SetRawInputAt(1, value);
4391 }
4392
Calin Juravle52c48962014-12-16 17:02:57 +00004393 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004394 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4395 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004396 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004397
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004398 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004399 bool GetValueCanBeNull() const { return value_can_be_null_; }
4400 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004401
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004402 DECLARE_INSTRUCTION(StaticFieldSet);
4403
4404 private:
4405 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004406 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004407
4408 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4409};
4410
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004411// Implement the move-exception DEX instruction.
4412class HLoadException : public HExpression<0> {
4413 public:
4414 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4415
4416 DECLARE_INSTRUCTION(LoadException);
4417
4418 private:
4419 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4420};
4421
David Brazdilcb1c0552015-08-04 16:22:25 +01004422// Implicit part of move-exception which clears thread-local exception storage.
4423// Must not be removed because the runtime expects the TLS to get cleared.
4424class HClearException : public HTemplateInstruction<0> {
4425 public:
4426 HClearException() : HTemplateInstruction(SideEffects::AllWrites()) {}
4427
4428 DECLARE_INSTRUCTION(ClearException);
4429
4430 private:
4431 DISALLOW_COPY_AND_ASSIGN(HClearException);
4432};
4433
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004434class HThrow : public HTemplateInstruction<1> {
4435 public:
4436 HThrow(HInstruction* exception, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004437 : HTemplateInstruction(SideEffects::CanTriggerGC()), dex_pc_(dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004438 SetRawInputAt(0, exception);
4439 }
4440
4441 bool IsControlFlow() const OVERRIDE { return true; }
4442
4443 bool NeedsEnvironment() const OVERRIDE { return true; }
4444
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004445 bool CanThrow() const OVERRIDE { return true; }
4446
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004447 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004448
4449 DECLARE_INSTRUCTION(Throw);
4450
4451 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004452 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004453
4454 DISALLOW_COPY_AND_ASSIGN(HThrow);
4455};
4456
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004457class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004458 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004459 HInstanceOf(HInstruction* object,
4460 HLoadClass* constant,
4461 bool class_is_final,
4462 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004463 : HExpression(Primitive::kPrimBoolean, SideEffectsForArchRuntimeCalls(class_is_final)),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004464 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004465 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004466 dex_pc_(dex_pc) {
4467 SetRawInputAt(0, object);
4468 SetRawInputAt(1, constant);
4469 }
4470
4471 bool CanBeMoved() const OVERRIDE { return true; }
4472
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004473 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004474 return true;
4475 }
4476
4477 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004478 return false;
4479 }
4480
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004481 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004482
4483 bool IsClassFinal() const { return class_is_final_; }
4484
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004485 // Used only in code generation.
4486 bool MustDoNullCheck() const { return must_do_null_check_; }
4487 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4488
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004489 static SideEffects SideEffectsForArchRuntimeCalls(bool class_is_final) {
4490 return class_is_final ? SideEffects::None() : SideEffects::CanTriggerGC();
4491 }
4492
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004493 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004494
4495 private:
4496 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004497 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004498 const uint32_t dex_pc_;
4499
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004500 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4501};
4502
Calin Juravleb1498f62015-02-16 13:13:29 +00004503class HBoundType : public HExpression<1> {
4504 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004505 // Constructs an HBoundType with the given upper_bound.
4506 // Ensures that the upper_bound is valid.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004507 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004508 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004509 upper_bound_(upper_bound),
4510 upper_can_be_null_(upper_can_be_null),
4511 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004512 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004513 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004514 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004515 }
4516
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004517 // GetUpper* should only be used in reference type propagation.
4518 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4519 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004520
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004521 void SetCanBeNull(bool can_be_null) {
4522 DCHECK(upper_can_be_null_ || !can_be_null);
4523 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004524 }
4525
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004526 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4527
Calin Juravleb1498f62015-02-16 13:13:29 +00004528 DECLARE_INSTRUCTION(BoundType);
4529
4530 private:
4531 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004532 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4533 // It is used to bound the type in cases like:
4534 // if (x instanceof ClassX) {
4535 // // uper_bound_ will be ClassX
4536 // }
4537 const ReferenceTypeInfo upper_bound_;
4538 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4539 // is false then can_be_null_ cannot be true).
4540 const bool upper_can_be_null_;
4541 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004542
4543 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4544};
4545
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004546class HCheckCast : public HTemplateInstruction<2> {
4547 public:
4548 HCheckCast(HInstruction* object,
4549 HLoadClass* constant,
4550 bool class_is_final,
4551 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004552 : HTemplateInstruction(SideEffects::CanTriggerGC()),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004553 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004554 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004555 dex_pc_(dex_pc) {
4556 SetRawInputAt(0, object);
4557 SetRawInputAt(1, constant);
4558 }
4559
4560 bool CanBeMoved() const OVERRIDE { return true; }
4561
4562 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4563 return true;
4564 }
4565
4566 bool NeedsEnvironment() const OVERRIDE {
4567 // Instruction may throw a CheckCastError.
4568 return true;
4569 }
4570
4571 bool CanThrow() const OVERRIDE { return true; }
4572
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004573 bool MustDoNullCheck() const { return must_do_null_check_; }
4574 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4575
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004576 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004577
4578 bool IsClassFinal() const { return class_is_final_; }
4579
4580 DECLARE_INSTRUCTION(CheckCast);
4581
4582 private:
4583 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004584 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004585 const uint32_t dex_pc_;
4586
4587 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004588};
4589
Calin Juravle27df7582015-04-17 19:12:31 +01004590class HMemoryBarrier : public HTemplateInstruction<0> {
4591 public:
4592 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004593 : HTemplateInstruction(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004594 SideEffects::AllWritesAndReads()), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004595 barrier_kind_(barrier_kind) {}
4596
4597 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4598
4599 DECLARE_INSTRUCTION(MemoryBarrier);
4600
4601 private:
4602 const MemBarrierKind barrier_kind_;
4603
4604 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4605};
4606
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004607class HMonitorOperation : public HTemplateInstruction<1> {
4608 public:
4609 enum OperationKind {
4610 kEnter,
4611 kExit,
4612 };
4613
4614 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004615 : HTemplateInstruction(
4616 SideEffects::AllExceptGCDependency()), // Assume write/read on all fields/arrays.
Aart Bik854a02b2015-07-14 16:07:00 -07004617 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004618 SetRawInputAt(0, object);
4619 }
4620
4621 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004622 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4623
4624 bool CanThrow() const OVERRIDE {
4625 // Verifier guarantees that monitor-exit cannot throw.
4626 // This is important because it allows the HGraphBuilder to remove
4627 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4628 return IsEnter();
4629 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004630
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004631 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004632
4633 bool IsEnter() const { return kind_ == kEnter; }
4634
4635 DECLARE_INSTRUCTION(MonitorOperation);
4636
Calin Juravle52c48962014-12-16 17:02:57 +00004637 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004638 const OperationKind kind_;
4639 const uint32_t dex_pc_;
4640
4641 private:
4642 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4643};
4644
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004645/**
4646 * A HInstruction used as a marker for the replacement of new + <init>
4647 * of a String to a call to a StringFactory. Only baseline will see
4648 * the node at code generation, where it will be be treated as null.
4649 * When compiling non-baseline, `HFakeString` instructions are being removed
4650 * in the instruction simplifier.
4651 */
4652class HFakeString : public HTemplateInstruction<0> {
4653 public:
4654 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4655
4656 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4657
4658 DECLARE_INSTRUCTION(FakeString);
4659
4660 private:
4661 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4662};
4663
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004664class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004665 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004666 MoveOperands(Location source,
4667 Location destination,
4668 Primitive::Type type,
4669 HInstruction* instruction)
4670 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004671
4672 Location GetSource() const { return source_; }
4673 Location GetDestination() const { return destination_; }
4674
4675 void SetSource(Location value) { source_ = value; }
4676 void SetDestination(Location value) { destination_ = value; }
4677
4678 // The parallel move resolver marks moves as "in-progress" by clearing the
4679 // destination (but not the source).
4680 Location MarkPending() {
4681 DCHECK(!IsPending());
4682 Location dest = destination_;
4683 destination_ = Location::NoLocation();
4684 return dest;
4685 }
4686
4687 void ClearPending(Location dest) {
4688 DCHECK(IsPending());
4689 destination_ = dest;
4690 }
4691
4692 bool IsPending() const {
4693 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4694 return destination_.IsInvalid() && !source_.IsInvalid();
4695 }
4696
4697 // True if this blocks a move from the given location.
4698 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004699 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004700 }
4701
4702 // A move is redundant if it's been eliminated, if its source and
4703 // destination are the same, or if its destination is unneeded.
4704 bool IsRedundant() const {
4705 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4706 }
4707
4708 // We clear both operands to indicate move that's been eliminated.
4709 void Eliminate() {
4710 source_ = destination_ = Location::NoLocation();
4711 }
4712
4713 bool IsEliminated() const {
4714 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4715 return source_.IsInvalid();
4716 }
4717
Alexey Frunze4dda3372015-06-01 18:31:49 -07004718 Primitive::Type GetType() const { return type_; }
4719
Nicolas Geoffray90218252015-04-15 11:56:51 +01004720 bool Is64BitMove() const {
4721 return Primitive::Is64BitType(type_);
4722 }
4723
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004724 HInstruction* GetInstruction() const { return instruction_; }
4725
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004726 private:
4727 Location source_;
4728 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004729 // The type this move is for.
4730 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004731 // The instruction this move is assocatied with. Null when this move is
4732 // for moving an input in the expected locations of user (including a phi user).
4733 // This is only used in debug mode, to ensure we do not connect interval siblings
4734 // in the same parallel move.
4735 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004736};
4737
4738static constexpr size_t kDefaultNumberOfMoves = 4;
4739
4740class HParallelMove : public HTemplateInstruction<0> {
4741 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004742 explicit HParallelMove(ArenaAllocator* arena)
4743 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004744
Nicolas Geoffray90218252015-04-15 11:56:51 +01004745 void AddMove(Location source,
4746 Location destination,
4747 Primitive::Type type,
4748 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004749 DCHECK(source.IsValid());
4750 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004751 if (kIsDebugBuild) {
4752 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004753 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004754 if (moves_.Get(i).GetInstruction() == instruction) {
4755 // Special case the situation where the move is for the spill slot
4756 // of the instruction.
4757 if ((GetPrevious() == instruction)
4758 || ((GetPrevious() == nullptr)
4759 && instruction->IsPhi()
4760 && instruction->GetBlock() == GetBlock())) {
4761 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4762 << "Doing parallel moves for the same instruction.";
4763 } else {
4764 DCHECK(false) << "Doing parallel moves for the same instruction.";
4765 }
4766 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004767 }
4768 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004769 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004770 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004771 << "Overlapped destination for two moves in a parallel move: "
4772 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4773 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004774 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004775 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004776 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004777 }
4778
4779 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004780 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004781 }
4782
4783 size_t NumMoves() const { return moves_.Size(); }
4784
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004785 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004786
4787 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004788 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004789
4790 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4791};
4792
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004793class HGraphVisitor : public ValueObject {
4794 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004795 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4796 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004797
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004798 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004799 virtual void VisitBasicBlock(HBasicBlock* block);
4800
Roland Levillain633021e2014-10-01 14:12:25 +01004801 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004802 void VisitInsertionOrder();
4803
Roland Levillain633021e2014-10-01 14:12:25 +01004804 // Visit the graph following dominator tree reverse post-order.
4805 void VisitReversePostOrder();
4806
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004807 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004808
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004809 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004810#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004811 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4812
4813 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4814
4815#undef DECLARE_VISIT_INSTRUCTION
4816
4817 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004818 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004819
4820 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4821};
4822
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004823class HGraphDelegateVisitor : public HGraphVisitor {
4824 public:
4825 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4826 virtual ~HGraphDelegateVisitor() {}
4827
4828 // Visit functions that delegate to to super class.
4829#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004830 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004831
4832 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4833
4834#undef DECLARE_VISIT_INSTRUCTION
4835
4836 private:
4837 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4838};
4839
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004840class HInsertionOrderIterator : public ValueObject {
4841 public:
4842 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4843
4844 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4845 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4846 void Advance() { ++index_; }
4847
4848 private:
4849 const HGraph& graph_;
4850 size_t index_;
4851
4852 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4853};
4854
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004855class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004856 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004857 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4858 // Check that reverse post order of the graph has been built.
4859 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4860 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004861
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004862 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4863 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004864 void Advance() { ++index_; }
4865
4866 private:
4867 const HGraph& graph_;
4868 size_t index_;
4869
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004870 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004871};
4872
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004873class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004874 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004875 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004876 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4877 // Check that reverse post order of the graph has been built.
4878 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4879 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004880
4881 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004882 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004883 void Advance() { --index_; }
4884
4885 private:
4886 const HGraph& graph_;
4887 size_t index_;
4888
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004889 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004890};
4891
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004892class HLinearPostOrderIterator : public ValueObject {
4893 public:
4894 explicit HLinearPostOrderIterator(const HGraph& graph)
4895 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4896
4897 bool Done() const { return index_ == 0; }
4898
4899 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4900
4901 void Advance() {
4902 --index_;
4903 DCHECK_GE(index_, 0U);
4904 }
4905
4906 private:
4907 const GrowableArray<HBasicBlock*>& order_;
4908 size_t index_;
4909
4910 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4911};
4912
4913class HLinearOrderIterator : public ValueObject {
4914 public:
4915 explicit HLinearOrderIterator(const HGraph& graph)
4916 : order_(graph.GetLinearOrder()), index_(0) {}
4917
4918 bool Done() const { return index_ == order_.Size(); }
4919 HBasicBlock* Current() const { return order_.Get(index_); }
4920 void Advance() { ++index_; }
4921
4922 private:
4923 const GrowableArray<HBasicBlock*>& order_;
4924 size_t index_;
4925
4926 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4927};
4928
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004929// Iterator over the blocks that art part of the loop. Includes blocks part
4930// of an inner loop. The order in which the blocks are iterated is on their
4931// block id.
4932class HBlocksInLoopIterator : public ValueObject {
4933 public:
4934 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4935 : blocks_in_loop_(info.GetBlocks()),
4936 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4937 index_(0) {
4938 if (!blocks_in_loop_.IsBitSet(index_)) {
4939 Advance();
4940 }
4941 }
4942
4943 bool Done() const { return index_ == blocks_.Size(); }
4944 HBasicBlock* Current() const { return blocks_.Get(index_); }
4945 void Advance() {
4946 ++index_;
4947 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4948 if (blocks_in_loop_.IsBitSet(index_)) {
4949 break;
4950 }
4951 }
4952 }
4953
4954 private:
4955 const BitVector& blocks_in_loop_;
4956 const GrowableArray<HBasicBlock*>& blocks_;
4957 size_t index_;
4958
4959 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4960};
4961
Mingyao Yang3584bce2015-05-19 16:01:59 -07004962// Iterator over the blocks that art part of the loop. Includes blocks part
4963// of an inner loop. The order in which the blocks are iterated is reverse
4964// post order.
4965class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4966 public:
4967 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4968 : blocks_in_loop_(info.GetBlocks()),
4969 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4970 index_(0) {
4971 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4972 Advance();
4973 }
4974 }
4975
4976 bool Done() const { return index_ == blocks_.Size(); }
4977 HBasicBlock* Current() const { return blocks_.Get(index_); }
4978 void Advance() {
4979 ++index_;
4980 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4981 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4982 break;
4983 }
4984 }
4985 }
4986
4987 private:
4988 const BitVector& blocks_in_loop_;
4989 const GrowableArray<HBasicBlock*>& blocks_;
4990 size_t index_;
4991
4992 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4993};
4994
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004995inline int64_t Int64FromConstant(HConstant* constant) {
4996 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4997 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4998 : constant->AsLongConstant()->GetValue();
4999}
5000
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005001} // namespace art
5002
5003#endif // ART_COMPILER_OPTIMIZING_NODES_H_