blob: 5f128a707226d72f2487c84d13769b7bb2f1f686 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010038class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000039class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010040class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000041class HFloatConstant;
42class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000044class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000045class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000046class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000047class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010048class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010049class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010050class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000051class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010052class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000054
55static const int kDefaultNumberOfBlocks = 8;
56static const int kDefaultNumberOfSuccessors = 2;
57static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010058static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000059static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000060
Calin Juravle9aec02f2014-11-18 23:06:35 +000061static constexpr uint32_t kMaxIntShiftValue = 0x1f;
62static constexpr uint64_t kMaxLongShiftValue = 0x3f;
63
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010064static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
65
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010066static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
67
Dave Allison20dfc792014-06-16 20:44:29 -070068enum IfCondition {
69 kCondEQ,
70 kCondNE,
71 kCondLT,
72 kCondLE,
73 kCondGT,
74 kCondGE,
75};
76
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010077class HInstructionList {
78 public:
79 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
80
81 void AddInstruction(HInstruction* instruction);
82 void RemoveInstruction(HInstruction* instruction);
83
David Brazdilc3d743f2015-04-22 13:40:50 +010084 // Insert `instruction` before/after an existing instruction `cursor`.
85 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
86 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
87
Roland Levillain6b469232014-09-25 10:10:38 +010088 // Return true if this list contains `instruction`.
89 bool Contains(HInstruction* instruction) const;
90
Roland Levillainccc07a92014-09-16 14:48:16 +010091 // Return true if `instruction1` is found before `instruction2` in
92 // this instruction list and false otherwise. Abort if none
93 // of these instructions is found.
94 bool FoundBefore(const HInstruction* instruction1,
95 const HInstruction* instruction2) const;
96
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000097 bool IsEmpty() const { return first_instruction_ == nullptr; }
98 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
99
100 // Update the block of all instructions to be `block`.
101 void SetBlockOfInstructions(HBasicBlock* block) const;
102
103 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
104 void Add(const HInstructionList& instruction_list);
105
David Brazdil2d7352b2015-04-20 14:52:42 +0100106 // Return the number of instructions in the list. This is an expensive operation.
107 size_t CountSize() const;
108
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109 private:
110 HInstruction* first_instruction_;
111 HInstruction* last_instruction_;
112
113 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000114 friend class HGraph;
115 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100116 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100117 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100118
119 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
120};
121
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000122// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700123class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000124 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100125 HGraph(ArenaAllocator* arena,
126 const DexFile& dex_file,
127 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100128 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700129 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100130 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100131 bool debuggable = false,
132 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000133 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000134 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100135 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100136 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700137 entry_block_(nullptr),
138 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100139 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140 number_of_vregs_(0),
141 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000142 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400143 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000144 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000145 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100146 dex_file_(dex_file),
147 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100148 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100149 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100150 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700151 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000152 cached_null_constant_(nullptr),
153 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000154 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
155 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100156 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
157 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000158
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000159 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100160 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100161 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000162
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000163 HBasicBlock* GetEntryBlock() const { return entry_block_; }
164 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100165 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000166
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000167 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
168 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000169
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000170 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100171
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000172 // Try building the SSA form of this graph, with dominance computation and loop
173 // recognition. Returns whether it was successful in doing all these steps.
174 bool TryBuildingSsa() {
175 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000176 // The SSA builder requires loops to all be natural. Specifically, the dead phi
177 // elimination phase checks the consistency of the graph when doing a post-order
178 // visit for eliminating dead phis: a dead phi can only have loop header phi
179 // users remaining when being visited.
180 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100182 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000183 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000184 }
185
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000186 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000187 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100188 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000189
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000190 // Analyze all natural loops in this graph. Returns false if one
191 // loop is not natural, that is the header does not dominate the
192 // back edge.
193 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100194
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000195 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
196 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
197
David Brazdil2d7352b2015-04-20 14:52:42 +0100198 // Removes `block` from the graph.
199 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000200
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100201 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
202 void SimplifyLoop(HBasicBlock* header);
203
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000204 int32_t GetNextInstructionId() {
205 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000206 return current_instruction_id_++;
207 }
208
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000209 int32_t GetCurrentInstructionId() const {
210 return current_instruction_id_;
211 }
212
213 void SetCurrentInstructionId(int32_t id) {
214 current_instruction_id_ = id;
215 }
216
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100217 uint16_t GetMaximumNumberOfOutVRegs() const {
218 return maximum_number_of_out_vregs_;
219 }
220
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000221 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
222 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100223 }
224
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100225 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
226 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
227 }
228
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000229 void UpdateTemporariesVRegSlots(size_t slots) {
230 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100231 }
232
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000233 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100234 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000235 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100236 }
237
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100238 void SetNumberOfVRegs(uint16_t number_of_vregs) {
239 number_of_vregs_ = number_of_vregs;
240 }
241
242 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100243 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100244 return number_of_vregs_;
245 }
246
247 void SetNumberOfInVRegs(uint16_t value) {
248 number_of_in_vregs_ = value;
249 }
250
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100251 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100252 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100253 return number_of_vregs_ - number_of_in_vregs_;
254 }
255
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100256 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
257 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100258 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100259
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100260 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
261 return linear_order_;
262 }
263
Mark Mendell1152c922015-04-24 17:06:35 -0400264 bool HasBoundsChecks() const {
265 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800266 }
267
Mark Mendell1152c922015-04-24 17:06:35 -0400268 void SetHasBoundsChecks(bool value) {
269 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800270 }
271
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100272 bool ShouldGenerateConstructorBarrier() const {
273 return should_generate_constructor_barrier_;
274 }
275
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000276 bool IsDebuggable() const { return debuggable_; }
277
David Brazdil8d5b8b22015-03-24 10:51:52 +0000278 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000279 // already, it is created and inserted into the graph. This method is only for
280 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000281 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000282 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000283 HIntConstant* GetIntConstant(int32_t value) {
284 return CreateConstant(value, &cached_int_constants_);
285 }
286 HLongConstant* GetLongConstant(int64_t value) {
287 return CreateConstant(value, &cached_long_constants_);
288 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000289 HFloatConstant* GetFloatConstant(float value) {
290 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
291 }
292 HDoubleConstant* GetDoubleConstant(double value) {
293 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
294 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000295
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100296 HCurrentMethod* GetCurrentMethod();
297
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000298 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100299
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100300 const DexFile& GetDexFile() const {
301 return dex_file_;
302 }
303
304 uint32_t GetMethodIdx() const {
305 return method_idx_;
306 }
307
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100308 InvokeType GetInvokeType() const {
309 return invoke_type_;
310 }
311
David Brazdil2d7352b2015-04-20 14:52:42 +0100312 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000313 void VisitBlockForDominatorTree(HBasicBlock* block,
314 HBasicBlock* predecessor,
315 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100316 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000317 void VisitBlockForBackEdges(HBasicBlock* block,
318 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100319 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000320 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100321 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000322
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000323 template <class InstructionType, typename ValueType>
324 InstructionType* CreateConstant(ValueType value,
325 ArenaSafeMap<ValueType, InstructionType*>* cache) {
326 // Try to find an existing constant of the given value.
327 InstructionType* constant = nullptr;
328 auto cached_constant = cache->find(value);
329 if (cached_constant != cache->end()) {
330 constant = cached_constant->second;
331 }
332
333 // If not found or previously deleted, create and cache a new instruction.
334 if (constant == nullptr || constant->GetBlock() == nullptr) {
335 constant = new (arena_) InstructionType(value);
336 cache->Overwrite(value, constant);
337 InsertConstant(constant);
338 }
339 return constant;
340 }
341
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 void InsertConstant(HConstant* instruction);
343
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000344 // Cache a float constant into the graph. This method should only be
345 // called by the SsaBuilder when creating "equivalent" instructions.
346 void CacheFloatConstant(HFloatConstant* constant);
347
348 // See CacheFloatConstant comment.
349 void CacheDoubleConstant(HDoubleConstant* constant);
350
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000352
353 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000354 GrowableArray<HBasicBlock*> blocks_;
355
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100356 // List of blocks to perform a reverse post order tree traversal.
357 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000358
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100359 // List of blocks to perform a linear order tree traversal.
360 GrowableArray<HBasicBlock*> linear_order_;
361
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000362 HBasicBlock* entry_block_;
363 HBasicBlock* exit_block_;
364
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100365 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100366 uint16_t maximum_number_of_out_vregs_;
367
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100368 // The number of virtual registers in this method. Contains the parameters.
369 uint16_t number_of_vregs_;
370
371 // The number of virtual registers used by parameters of this method.
372 uint16_t number_of_in_vregs_;
373
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000374 // Number of vreg size slots that the temporaries use (used in baseline compiler).
375 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100376
Mark Mendell1152c922015-04-24 17:06:35 -0400377 // Has bounds checks. We can totally skip BCE if it's false.
378 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800379
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000380 // Indicates whether the graph should be compiled in a way that
381 // ensures full debuggability. If false, we can apply more
382 // aggressive optimizations that may limit the level of debugging.
383 const bool debuggable_;
384
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000385 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000386 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000387
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100388 // The dex file from which the method is from.
389 const DexFile& dex_file_;
390
391 // The method index in the dex file.
392 const uint32_t method_idx_;
393
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100394 // If inlined, this encodes how the callee is being invoked.
395 const InvokeType invoke_type_;
396
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100397 // Whether the graph has been transformed to SSA form. Only used
398 // in debug mode to ensure we are not using properties only valid
399 // for non-SSA form (like the number of temporaries).
400 bool in_ssa_form_;
401
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100402 const bool should_generate_constructor_barrier_;
403
Mathieu Chartiere401d142015-04-22 13:56:20 -0700404 const InstructionSet instruction_set_;
405
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000406 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000407 HNullConstant* cached_null_constant_;
408 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000409 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000410 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000411 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000412
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100413 HCurrentMethod* cached_current_method_;
414
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000415 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100416 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000417 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000418 DISALLOW_COPY_AND_ASSIGN(HGraph);
419};
420
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700421class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000422 public:
423 HLoopInformation(HBasicBlock* header, HGraph* graph)
424 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100425 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100426 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100427 // Make bit vector growable, as the number of blocks may change.
428 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100429
430 HBasicBlock* GetHeader() const {
431 return header_;
432 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000433
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000434 void SetHeader(HBasicBlock* block) {
435 header_ = block;
436 }
437
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100438 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
439 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
440 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
441
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000442 void AddBackEdge(HBasicBlock* back_edge) {
443 back_edges_.Add(back_edge);
444 }
445
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100446 void RemoveBackEdge(HBasicBlock* back_edge) {
447 back_edges_.Delete(back_edge);
448 }
449
David Brazdil46e2a392015-03-16 17:31:52 +0000450 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100451 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000452 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100453 }
454 return false;
455 }
456
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000457 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000458 return back_edges_.Size();
459 }
460
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100461 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100462
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100463 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
464 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100465 }
466
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100467 // Returns the lifetime position of the back edge that has the
468 // greatest lifetime position.
469 size_t GetLifetimeEnd() const;
470
471 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
472 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
473 if (back_edges_.Get(i) == existing) {
474 back_edges_.Put(i, new_back_edge);
475 return;
476 }
477 }
478 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100479 }
480
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100481 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100482 // that is the header dominates the back edge.
483 bool Populate();
484
David Brazdila4b8c212015-05-07 09:59:30 +0100485 // Reanalyzes the loop by removing loop info from its blocks and re-running
486 // Populate(). If there are no back edges left, the loop info is completely
487 // removed as well as its SuspendCheck instruction. It must be run on nested
488 // inner loops first.
489 void Update();
490
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100491 // Returns whether this loop information contains `block`.
492 // Note that this loop information *must* be populated before entering this function.
493 bool Contains(const HBasicBlock& block) const;
494
495 // Returns whether this loop information is an inner loop of `other`.
496 // Note that `other` *must* be populated before entering this function.
497 bool IsIn(const HLoopInformation& other) const;
498
499 const ArenaBitVector& GetBlocks() const { return blocks_; }
500
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000501 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000502 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000503
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000504 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100505 // Internal recursive implementation of `Populate`.
506 void PopulateRecursive(HBasicBlock* block);
507
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000508 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100509 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000510 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100511 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000512
513 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
514};
515
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100516static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100517static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100518
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000519// A block in a method. Contains the list of instructions represented
520// as a double linked list. Each block knows its predecessors and
521// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100522
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700523class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000524 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100525 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000526 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000527 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
528 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000529 loop_information_(nullptr),
530 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100531 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100532 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100533 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100534 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000535 lifetime_end_(kNoLifetime),
536 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000537
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100538 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
539 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000540 }
541
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100542 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
543 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000544 }
545
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100546 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
547 return dominated_blocks_;
548 }
549
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100550 bool IsEntryBlock() const {
551 return graph_->GetEntryBlock() == this;
552 }
553
554 bool IsExitBlock() const {
555 return graph_->GetExitBlock() == this;
556 }
557
David Brazdil46e2a392015-03-16 17:31:52 +0000558 bool IsSingleGoto() const;
559
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000560 void AddBackEdge(HBasicBlock* back_edge) {
561 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000562 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000563 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100564 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000565 loop_information_->AddBackEdge(back_edge);
566 }
567
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000568 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000569 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000570
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000571 int GetBlockId() const { return block_id_; }
572 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000573
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000574 HBasicBlock* GetDominator() const { return dominator_; }
575 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100576 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100577 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000578 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
579 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
580 if (dominated_blocks_.Get(i) == existing) {
581 dominated_blocks_.Put(i, new_block);
582 return;
583 }
584 }
585 LOG(FATAL) << "Unreachable";
586 UNREACHABLE();
587 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000588
589 int NumberOfBackEdges() const {
590 return loop_information_ == nullptr
591 ? 0
592 : loop_information_->NumberOfBackEdges();
593 }
594
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100595 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
596 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100597 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100598 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100599 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
600 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000601
602 void AddSuccessor(HBasicBlock* block) {
603 successors_.Add(block);
604 block->predecessors_.Add(this);
605 }
606
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100607 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
608 size_t successor_index = GetSuccessorIndexOf(existing);
609 DCHECK_NE(successor_index, static_cast<size_t>(-1));
610 existing->RemovePredecessor(this);
611 new_block->predecessors_.Add(this);
612 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000613 }
614
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000615 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
616 size_t predecessor_index = GetPredecessorIndexOf(existing);
617 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
618 existing->RemoveSuccessor(this);
619 new_block->successors_.Add(this);
620 predecessors_.Put(predecessor_index, new_block);
621 }
622
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100623 void RemovePredecessor(HBasicBlock* block) {
624 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100625 }
626
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000627 void RemoveSuccessor(HBasicBlock* block) {
628 successors_.Delete(block);
629 }
630
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100631 void ClearAllPredecessors() {
632 predecessors_.Reset();
633 }
634
635 void AddPredecessor(HBasicBlock* block) {
636 predecessors_.Add(block);
637 block->successors_.Add(this);
638 }
639
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100640 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100641 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100642 HBasicBlock* temp = predecessors_.Get(0);
643 predecessors_.Put(0, predecessors_.Get(1));
644 predecessors_.Put(1, temp);
645 }
646
David Brazdil769c9e52015-04-27 13:54:09 +0100647 void SwapSuccessors() {
648 DCHECK_EQ(successors_.Size(), 2u);
649 HBasicBlock* temp = successors_.Get(0);
650 successors_.Put(0, successors_.Get(1));
651 successors_.Put(1, temp);
652 }
653
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100654 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
655 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
656 if (predecessors_.Get(i) == predecessor) {
657 return i;
658 }
659 }
660 return -1;
661 }
662
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100663 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
664 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
665 if (successors_.Get(i) == successor) {
666 return i;
667 }
668 }
669 return -1;
670 }
671
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000672 // Split the block into two blocks just after `cursor`. Returns the newly
673 // created block. Note that this method just updates raw block information,
674 // like predecessors, successors, dominators, and instruction list. It does not
675 // update the graph, reverse post order, loop information, nor make sure the
676 // blocks are consistent (for example ending with a control flow instruction).
677 HBasicBlock* SplitAfter(HInstruction* cursor);
678
679 // Merge `other` at the end of `this`. Successors and dominated blocks of
680 // `other` are changed to be successors and dominated blocks of `this`. Note
681 // that this method does not update the graph, reverse post order, loop
682 // information, nor make sure the blocks are consistent (for example ending
683 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100684 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000685
686 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
687 // of `this` are moved to `other`.
688 // Note that this method does not update the graph, reverse post order, loop
689 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000690 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000691 void ReplaceWith(HBasicBlock* other);
692
David Brazdil2d7352b2015-04-20 14:52:42 +0100693 // Merge `other` at the end of `this`. This method updates loops, reverse post
694 // order, links to predecessors, successors, dominators and deletes the block
695 // from the graph. The two blocks must be successive, i.e. `this` the only
696 // predecessor of `other` and vice versa.
697 void MergeWith(HBasicBlock* other);
698
699 // Disconnects `this` from all its predecessors, successors and dominator,
700 // removes it from all loops it is included in and eventually from the graph.
701 // The block must not dominate any other block. Predecessors and successors
702 // are safely updated.
703 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000704
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000705 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100706 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100707 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100708 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100709 // Replace instruction `initial` with `replacement` within this block.
710 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
711 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100712 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100713 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000714 // RemoveInstruction and RemovePhi delete a given instruction from the respective
715 // instruction list. With 'ensure_safety' set to true, it verifies that the
716 // instruction is not in use and removes it from the use lists of its inputs.
717 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
718 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100719 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100720
721 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100722 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100723 }
724
Roland Levillain6b879dd2014-09-22 17:13:44 +0100725 bool IsLoopPreHeaderFirstPredecessor() const {
726 DCHECK(IsLoopHeader());
727 DCHECK(!GetPredecessors().IsEmpty());
728 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
729 }
730
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100731 HLoopInformation* GetLoopInformation() const {
732 return loop_information_;
733 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000734
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000735 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100736 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000737 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100738 void SetInLoop(HLoopInformation* info) {
739 if (IsLoopHeader()) {
740 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100741 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100742 loop_information_ = info;
743 } else if (loop_information_->Contains(*info->GetHeader())) {
744 // Block is currently part of an outer loop. Make it part of this inner loop.
745 // Note that a non loop header having a loop information means this loop information
746 // has already been populated
747 loop_information_ = info;
748 } else {
749 // Block is part of an inner loop. Do not update the loop information.
750 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
751 // at this point, because this method is being called while populating `info`.
752 }
753 }
754
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000755 // Raw update of the loop information.
756 void SetLoopInformation(HLoopInformation* info) {
757 loop_information_ = info;
758 }
759
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100760 bool IsInLoop() const { return loop_information_ != nullptr; }
761
David Brazdila4b8c212015-05-07 09:59:30 +0100762 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100763 bool Dominates(HBasicBlock* block) const;
764
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100765 size_t GetLifetimeStart() const { return lifetime_start_; }
766 size_t GetLifetimeEnd() const { return lifetime_end_; }
767
768 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
769 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
770
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100771 uint32_t GetDexPc() const { return dex_pc_; }
772
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000773 bool IsCatchBlock() const { return is_catch_block_; }
774 void SetIsCatchBlock() { is_catch_block_ = true; }
775
David Brazdil8d5b8b22015-03-24 10:51:52 +0000776 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000777 bool EndsWithIf() const;
778 bool HasSinglePhi() const;
779
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000780 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000781 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000782 GrowableArray<HBasicBlock*> predecessors_;
783 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100784 HInstructionList instructions_;
785 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000786 HLoopInformation* loop_information_;
787 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100788 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000789 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100790 // The dex program counter of the first instruction of this block.
791 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100792 size_t lifetime_start_;
793 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000794 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000795
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000796 friend class HGraph;
797 friend class HInstruction;
798
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000799 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
800};
801
David Brazdilb2bd1c52015-03-25 11:17:37 +0000802// Iterates over the LoopInformation of all loops which contain 'block'
803// from the innermost to the outermost.
804class HLoopInformationOutwardIterator : public ValueObject {
805 public:
806 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
807 : current_(block.GetLoopInformation()) {}
808
809 bool Done() const { return current_ == nullptr; }
810
811 void Advance() {
812 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100813 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000814 }
815
816 HLoopInformation* Current() const {
817 DCHECK(!Done());
818 return current_;
819 }
820
821 private:
822 HLoopInformation* current_;
823
824 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
825};
826
Alexandre Ramesef20f712015-06-09 10:29:30 +0100827#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100828 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000829 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000830 M(ArrayGet, Instruction) \
831 M(ArrayLength, Instruction) \
832 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100833 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000834 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000835 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000836 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100837 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000838 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100839 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100840 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700841 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000842 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000843 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000844 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100845 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000846 M(Exit, Instruction) \
847 M(FloatConstant, Constant) \
848 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100849 M(GreaterThan, Condition) \
850 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100851 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000852 M(InstanceFieldGet, Instruction) \
853 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000854 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100855 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000856 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000857 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100858 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000859 M(LessThan, Condition) \
860 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000861 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000862 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100863 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000864 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100865 M(Local, Instruction) \
866 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100867 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000868 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000869 M(Mul, BinaryOperation) \
870 M(Neg, UnaryOperation) \
871 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100872 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100873 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000874 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000875 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000876 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000877 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100878 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000879 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100880 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000881 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100882 M(Return, Instruction) \
883 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000884 M(Shl, BinaryOperation) \
885 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100886 M(StaticFieldGet, Instruction) \
887 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100888 M(StoreLocal, Instruction) \
889 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100890 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000891 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000892 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000893 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000894 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000895 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000896
Alexandre Ramesef20f712015-06-09 10:29:30 +0100897#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
898
899#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
900
901#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
902
903#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
904
905#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
906 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
907 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
908 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
909 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
910 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
911
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100912#define FOR_EACH_INSTRUCTION(M) \
913 FOR_EACH_CONCRETE_INSTRUCTION(M) \
914 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100915 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100916 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100917 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700918
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100919#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000920FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
921#undef FORWARD_DECLARATION
922
Roland Levillainccc07a92014-09-16 14:48:16 +0100923#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000924 InstructionKind GetKind() const OVERRIDE { return k##type; } \
925 const char* DebugName() const OVERRIDE { return #type; } \
926 const H##type* As##type() const OVERRIDE { return this; } \
927 H##type* As##type() OVERRIDE { return this; } \
928 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100929 return other->Is##type(); \
930 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000931 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000932
David Brazdiled596192015-01-23 10:39:45 +0000933template <typename T> class HUseList;
934
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100935template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700936class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000937 public:
David Brazdiled596192015-01-23 10:39:45 +0000938 HUseListNode* GetPrevious() const { return prev_; }
939 HUseListNode* GetNext() const { return next_; }
940 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100941 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100942 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100943
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000944 private:
David Brazdiled596192015-01-23 10:39:45 +0000945 HUseListNode(T user, size_t index)
946 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
947
948 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100949 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000950 HUseListNode<T>* prev_;
951 HUseListNode<T>* next_;
952
953 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000954
955 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
956};
957
David Brazdiled596192015-01-23 10:39:45 +0000958template <typename T>
959class HUseList : public ValueObject {
960 public:
961 HUseList() : first_(nullptr) {}
962
963 void Clear() {
964 first_ = nullptr;
965 }
966
967 // Adds a new entry at the beginning of the use list and returns
968 // the newly created node.
969 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000970 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000971 if (IsEmpty()) {
972 first_ = new_node;
973 } else {
974 first_->prev_ = new_node;
975 new_node->next_ = first_;
976 first_ = new_node;
977 }
978 return new_node;
979 }
980
981 HUseListNode<T>* GetFirst() const {
982 return first_;
983 }
984
985 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000986 DCHECK(node != nullptr);
987 DCHECK(Contains(node));
988
David Brazdiled596192015-01-23 10:39:45 +0000989 if (node->prev_ != nullptr) {
990 node->prev_->next_ = node->next_;
991 }
992 if (node->next_ != nullptr) {
993 node->next_->prev_ = node->prev_;
994 }
995 if (node == first_) {
996 first_ = node->next_;
997 }
998 }
999
David Brazdil1abb4192015-02-17 18:33:36 +00001000 bool Contains(const HUseListNode<T>* node) const {
1001 if (node == nullptr) {
1002 return false;
1003 }
1004 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1005 if (current == node) {
1006 return true;
1007 }
1008 }
1009 return false;
1010 }
1011
David Brazdiled596192015-01-23 10:39:45 +00001012 bool IsEmpty() const {
1013 return first_ == nullptr;
1014 }
1015
1016 bool HasOnlyOneUse() const {
1017 return first_ != nullptr && first_->next_ == nullptr;
1018 }
1019
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001020 size_t SizeSlow() const {
1021 size_t count = 0;
1022 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1023 ++count;
1024 }
1025 return count;
1026 }
1027
David Brazdiled596192015-01-23 10:39:45 +00001028 private:
1029 HUseListNode<T>* first_;
1030};
1031
1032template<typename T>
1033class HUseIterator : public ValueObject {
1034 public:
1035 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1036
1037 bool Done() const { return current_ == nullptr; }
1038
1039 void Advance() {
1040 DCHECK(!Done());
1041 current_ = current_->GetNext();
1042 }
1043
1044 HUseListNode<T>* Current() const {
1045 DCHECK(!Done());
1046 return current_;
1047 }
1048
1049 private:
1050 HUseListNode<T>* current_;
1051
1052 friend class HValue;
1053};
1054
David Brazdil1abb4192015-02-17 18:33:36 +00001055// This class is used by HEnvironment and HInstruction classes to record the
1056// instructions they use and pointers to the corresponding HUseListNodes kept
1057// by the used instructions.
1058template <typename T>
1059class HUserRecord : public ValueObject {
1060 public:
1061 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1062 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1063
1064 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1065 : instruction_(old_record.instruction_), use_node_(use_node) {
1066 DCHECK(instruction_ != nullptr);
1067 DCHECK(use_node_ != nullptr);
1068 DCHECK(old_record.use_node_ == nullptr);
1069 }
1070
1071 HInstruction* GetInstruction() const { return instruction_; }
1072 HUseListNode<T>* GetUseNode() const { return use_node_; }
1073
1074 private:
1075 // Instruction used by the user.
1076 HInstruction* instruction_;
1077
1078 // Corresponding entry in the use list kept by 'instruction_'.
1079 HUseListNode<T>* use_node_;
1080};
1081
Calin Juravle27df7582015-04-17 19:12:31 +01001082// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1083// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1084// flag is consider.
1085// - DependsOn suggests that there is a real dependency between side effects but it only
1086// checks DependendsOnSomething flag.
1087//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001088// Represents the side effects an instruction may have.
1089class SideEffects : public ValueObject {
1090 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001091 SideEffects() : flags_(0) {}
1092
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001093 static SideEffects None() {
1094 return SideEffects(0);
1095 }
1096
1097 static SideEffects All() {
1098 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1099 }
1100
1101 static SideEffects ChangesSomething() {
1102 return SideEffects((1 << kFlagChangesCount) - 1);
1103 }
1104
1105 static SideEffects DependsOnSomething() {
1106 int count = kFlagDependsOnCount - kFlagChangesCount;
1107 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1108 }
1109
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001110 SideEffects Union(SideEffects other) const {
1111 return SideEffects(flags_ | other.flags_);
1112 }
1113
Roland Levillain72bceff2014-09-15 18:29:00 +01001114 bool HasSideEffects() const {
1115 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1116 return (flags_ & all_bits_set) != 0;
1117 }
1118
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001119 bool HasAllSideEffects() const {
1120 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1121 return all_bits_set == (flags_ & all_bits_set);
1122 }
1123
1124 bool DependsOn(SideEffects other) const {
1125 size_t depends_flags = other.ComputeDependsFlags();
1126 return (flags_ & depends_flags) != 0;
1127 }
1128
1129 bool HasDependencies() const {
1130 int count = kFlagDependsOnCount - kFlagChangesCount;
1131 size_t all_bits_set = (1 << count) - 1;
1132 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1133 }
1134
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001135 private:
1136 static constexpr int kFlagChangesSomething = 0;
1137 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1138
1139 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1140 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1141
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001142 explicit SideEffects(size_t flags) : flags_(flags) {}
1143
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001144 size_t ComputeDependsFlags() const {
1145 return flags_ << kFlagChangesCount;
1146 }
1147
1148 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001149};
1150
David Brazdiled596192015-01-23 10:39:45 +00001151// A HEnvironment object contains the values of virtual registers at a given location.
1152class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1153 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001154 HEnvironment(ArenaAllocator* arena,
1155 size_t number_of_vregs,
1156 const DexFile& dex_file,
1157 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001158 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001159 InvokeType invoke_type,
1160 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001161 : vregs_(arena, number_of_vregs),
1162 locations_(arena, number_of_vregs),
1163 parent_(nullptr),
1164 dex_file_(dex_file),
1165 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001166 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001167 invoke_type_(invoke_type),
1168 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001169 vregs_.SetSize(number_of_vregs);
1170 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001171 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001172 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001173
1174 locations_.SetSize(number_of_vregs);
1175 for (size_t i = 0; i < number_of_vregs; ++i) {
1176 locations_.Put(i, Location());
1177 }
1178 }
1179
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001180 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001181 : HEnvironment(arena,
1182 to_copy.Size(),
1183 to_copy.GetDexFile(),
1184 to_copy.GetMethodIdx(),
1185 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001186 to_copy.GetInvokeType(),
1187 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001188
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001189 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001190 if (parent_ != nullptr) {
1191 parent_->SetAndCopyParentChain(allocator, parent);
1192 } else {
1193 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1194 parent_->CopyFrom(parent);
1195 if (parent->GetParent() != nullptr) {
1196 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1197 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001198 }
David Brazdiled596192015-01-23 10:39:45 +00001199 }
1200
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001201 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1202 void CopyFrom(HEnvironment* environment);
1203
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001204 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1205 // input to the loop phi instead. This is for inserting instructions that
1206 // require an environment (like HDeoptimization) in the loop pre-header.
1207 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001208
1209 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001210 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001211 }
1212
1213 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001214 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001215 }
1216
David Brazdil1abb4192015-02-17 18:33:36 +00001217 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001218
1219 size_t Size() const { return vregs_.Size(); }
1220
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001221 HEnvironment* GetParent() const { return parent_; }
1222
1223 void SetLocationAt(size_t index, Location location) {
1224 locations_.Put(index, location);
1225 }
1226
1227 Location GetLocationAt(size_t index) const {
1228 return locations_.Get(index);
1229 }
1230
1231 uint32_t GetDexPc() const {
1232 return dex_pc_;
1233 }
1234
1235 uint32_t GetMethodIdx() const {
1236 return method_idx_;
1237 }
1238
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001239 InvokeType GetInvokeType() const {
1240 return invoke_type_;
1241 }
1242
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001243 const DexFile& GetDexFile() const {
1244 return dex_file_;
1245 }
1246
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001247 HInstruction* GetHolder() const {
1248 return holder_;
1249 }
1250
David Brazdiled596192015-01-23 10:39:45 +00001251 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001252 // Record instructions' use entries of this environment for constant-time removal.
1253 // It should only be called by HInstruction when a new environment use is added.
1254 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1255 DCHECK(env_use->GetUser() == this);
1256 size_t index = env_use->GetIndex();
1257 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1258 }
David Brazdiled596192015-01-23 10:39:45 +00001259
David Brazdil1abb4192015-02-17 18:33:36 +00001260 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001261 GrowableArray<Location> locations_;
1262 HEnvironment* parent_;
1263 const DexFile& dex_file_;
1264 const uint32_t method_idx_;
1265 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001266 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001267
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001268 // The instruction that holds this environment. Only used in debug mode
1269 // to ensure the graph is consistent.
1270 HInstruction* const holder_;
1271
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001272 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001273
1274 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1275};
1276
Calin Juravleacf735c2015-02-12 15:25:22 +00001277class ReferenceTypeInfo : ValueObject {
1278 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001279 typedef Handle<mirror::Class> TypeHandle;
1280
1281 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1282 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1283 if (type_handle->IsObjectClass()) {
1284 // Override the type handle to be consistent with the case when we get to
1285 // Top but don't have the Object class available. It avoids having to guess
1286 // what value the type_handle has when it's Top.
1287 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1288 } else {
1289 return ReferenceTypeInfo(type_handle, is_exact, false);
1290 }
1291 }
1292
1293 static ReferenceTypeInfo CreateTop(bool is_exact) {
1294 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001295 }
1296
1297 bool IsExact() const { return is_exact_; }
1298 bool IsTop() const { return is_top_; }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001299 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1300 return !IsTop() && GetTypeHandle()->IsInterface();
1301 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001302
1303 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1304
Calin Juravleb1498f62015-02-16 13:13:29 +00001305 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001306 if (IsTop()) {
1307 // Top (equivalent for java.lang.Object) is supertype of anything.
1308 return true;
1309 }
1310 if (rti.IsTop()) {
1311 // If we get here `this` is not Top() so it can't be a supertype.
1312 return false;
1313 }
1314 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1315 }
1316
1317 // Returns true if the type information provide the same amount of details.
1318 // Note that it does not mean that the instructions have the same actual type
1319 // (e.g. tops are equal but they can be the result of a merge).
1320 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1321 if (IsExact() != rti.IsExact()) {
1322 return false;
1323 }
1324 if (IsTop() && rti.IsTop()) {
1325 // `Top` means java.lang.Object, so the types are equivalent.
1326 return true;
1327 }
1328 if (IsTop() || rti.IsTop()) {
1329 // If only one is top or object than they are not equivalent.
1330 // NB: We need this extra check because the type_handle of `Top` is invalid
1331 // and we cannot inspect its reference.
1332 return false;
1333 }
1334
1335 // Finally check the types.
1336 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1337 }
1338
1339 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001340 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1341 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1342 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1343
Calin Juravleacf735c2015-02-12 15:25:22 +00001344 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001345 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001346 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001347 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001348 bool is_exact_;
1349 // A true value here means that the object type should be java.lang.Object.
1350 // We don't have access to the corresponding mirror object every time so this
1351 // flag acts as a substitute. When true, the TypeHandle refers to a null
1352 // pointer and should not be used.
1353 bool is_top_;
1354};
1355
1356std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1357
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001358class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001359 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001360 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001361 : previous_(nullptr),
1362 next_(nullptr),
1363 block_(nullptr),
1364 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001365 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001366 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001367 locations_(nullptr),
1368 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001369 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001370 side_effects_(side_effects),
1371 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001372
Dave Allison20dfc792014-06-16 20:44:29 -07001373 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001374
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001375#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001376 enum InstructionKind {
1377 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1378 };
1379#undef DECLARE_KIND
1380
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001381 HInstruction* GetNext() const { return next_; }
1382 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001383
Calin Juravle77520bc2015-01-12 18:45:46 +00001384 HInstruction* GetNextDisregardingMoves() const;
1385 HInstruction* GetPreviousDisregardingMoves() const;
1386
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001387 HBasicBlock* GetBlock() const { return block_; }
1388 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001389 bool IsInBlock() const { return block_ != nullptr; }
1390 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001391 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001392
Roland Levillain6b879dd2014-09-22 17:13:44 +01001393 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001394 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001395
1396 virtual void Accept(HGraphVisitor* visitor) = 0;
1397 virtual const char* DebugName() const = 0;
1398
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001399 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001400 void SetRawInputAt(size_t index, HInstruction* input) {
1401 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1402 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001403
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001404 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001405 virtual uint32_t GetDexPc() const {
1406 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1407 " does not need an environment";
1408 UNREACHABLE();
1409 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001410 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001411 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001412 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001413
Calin Juravle10e244f2015-01-26 18:54:32 +00001414 // Does not apply for all instructions, but having this at top level greatly
1415 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001416 virtual bool CanBeNull() const {
1417 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1418 return true;
1419 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001420
Calin Juravle641547a2015-04-21 22:08:51 +01001421 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1422 UNUSED(obj);
1423 return false;
1424 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001425
Calin Juravleacf735c2015-02-12 15:25:22 +00001426 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001427 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001428 reference_type_info_ = reference_type_info;
1429 }
1430
Calin Juravle61d544b2015-02-23 16:46:57 +00001431 ReferenceTypeInfo GetReferenceTypeInfo() const {
1432 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1433 return reference_type_info_;
1434 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001435
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001436 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001437 DCHECK(user != nullptr);
1438 HUseListNode<HInstruction*>* use =
1439 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1440 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001441 }
1442
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001443 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001444 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001445 HUseListNode<HEnvironment*>* env_use =
1446 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1447 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001448 }
1449
David Brazdil1abb4192015-02-17 18:33:36 +00001450 void RemoveAsUserOfInput(size_t input) {
1451 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1452 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1453 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001454
David Brazdil1abb4192015-02-17 18:33:36 +00001455 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1456 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001457
David Brazdiled596192015-01-23 10:39:45 +00001458 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1459 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001460 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001461 bool HasOnlyOneNonEnvironmentUse() const {
1462 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1463 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001464
Roland Levillain6c82d402014-10-13 16:10:27 +01001465 // Does this instruction strictly dominate `other_instruction`?
1466 // Returns false if this instruction and `other_instruction` are the same.
1467 // Aborts if this instruction and `other_instruction` are both phis.
1468 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001469
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001470 int GetId() const { return id_; }
1471 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001472
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001473 int GetSsaIndex() const { return ssa_index_; }
1474 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1475 bool HasSsaIndex() const { return ssa_index_ != -1; }
1476
1477 bool HasEnvironment() const { return environment_ != nullptr; }
1478 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001479 // Set the `environment_` field. Raw because this method does not
1480 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001481 void SetRawEnvironment(HEnvironment* environment) {
1482 DCHECK(environment_ == nullptr);
1483 DCHECK_EQ(environment->GetHolder(), this);
1484 environment_ = environment;
1485 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001486
1487 // Set the environment of this instruction, copying it from `environment`. While
1488 // copying, the uses lists are being updated.
1489 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001490 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001491 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001492 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001493 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001494 if (environment->GetParent() != nullptr) {
1495 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1496 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001497 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001498
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001499 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1500 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001501 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001502 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001503 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001504 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001505 if (environment->GetParent() != nullptr) {
1506 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1507 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001508 }
1509
Nicolas Geoffray39468442014-09-02 15:17:15 +01001510 // Returns the number of entries in the environment. Typically, that is the
1511 // number of dex registers in a method. It could be more in case of inlining.
1512 size_t EnvironmentSize() const;
1513
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001514 LocationSummary* GetLocations() const { return locations_; }
1515 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001516
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001517 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001518 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001519
Alexandre Rames188d4312015-04-09 18:30:21 +01001520 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1521 // uses of this instruction by `other` are *not* updated.
1522 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1523 ReplaceWith(other);
1524 other->ReplaceInput(this, use_index);
1525 }
1526
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001527 // Move `this` instruction before `cursor`.
1528 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001529
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001530#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001531 bool Is##type() const { return (As##type() != nullptr); } \
1532 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001533 virtual H##type* As##type() { return nullptr; }
1534
1535 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1536#undef INSTRUCTION_TYPE_CHECK
1537
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001538 // Returns whether the instruction can be moved within the graph.
1539 virtual bool CanBeMoved() const { return false; }
1540
1541 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001542 virtual bool InstructionTypeEquals(HInstruction* other) const {
1543 UNUSED(other);
1544 return false;
1545 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001546
1547 // Returns whether any data encoded in the two instructions is equal.
1548 // This method does not look at the inputs. Both instructions must be
1549 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001550 virtual bool InstructionDataEquals(HInstruction* other) const {
1551 UNUSED(other);
1552 return false;
1553 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001554
1555 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001556 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001557 // 2) Their inputs are identical.
1558 bool Equals(HInstruction* other) const;
1559
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001560 virtual InstructionKind GetKind() const = 0;
1561
1562 virtual size_t ComputeHashCode() const {
1563 size_t result = GetKind();
1564 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1565 result = (result * 31) + InputAt(i)->GetId();
1566 }
1567 return result;
1568 }
1569
1570 SideEffects GetSideEffects() const { return side_effects_; }
1571
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001572 size_t GetLifetimePosition() const { return lifetime_position_; }
1573 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1574 LiveInterval* GetLiveInterval() const { return live_interval_; }
1575 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1576 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1577
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001578 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1579
1580 // Returns whether the code generation of the instruction will require to have access
1581 // to the current method. Such instructions are:
1582 // (1): Instructions that require an environment, as calling the runtime requires
1583 // to walk the stack and have the current method stored at a specific stack address.
1584 // (2): Object literals like classes and strings, that are loaded from the dex cache
1585 // fields of the current method.
1586 bool NeedsCurrentMethod() const {
1587 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1588 }
1589
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001590 virtual bool NeedsDexCache() const { return false; }
1591
David Brazdil1abb4192015-02-17 18:33:36 +00001592 protected:
1593 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1594 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1595
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001596 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001597 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1598
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001599 HInstruction* previous_;
1600 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001601 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001602
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001603 // An instruction gets an id when it is added to the graph.
1604 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001605 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001606 int id_;
1607
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001608 // When doing liveness analysis, instructions that have uses get an SSA index.
1609 int ssa_index_;
1610
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001611 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001612 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001613
1614 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001615 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001616
Nicolas Geoffray39468442014-09-02 15:17:15 +01001617 // The environment associated with this instruction. Not null if the instruction
1618 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001619 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001620
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001621 // Set by the code generator.
1622 LocationSummary* locations_;
1623
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001624 // Set by the liveness analysis.
1625 LiveInterval* live_interval_;
1626
1627 // Set by the liveness analysis, this is the position in a linear
1628 // order of blocks where this instruction's live interval start.
1629 size_t lifetime_position_;
1630
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001631 const SideEffects side_effects_;
1632
Calin Juravleacf735c2015-02-12 15:25:22 +00001633 // TODO: for primitive types this should be marked as invalid.
1634 ReferenceTypeInfo reference_type_info_;
1635
David Brazdil1abb4192015-02-17 18:33:36 +00001636 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001637 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001638 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001639 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001640 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001641
1642 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1643};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001644std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001645
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001646class HInputIterator : public ValueObject {
1647 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001648 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001649
1650 bool Done() const { return index_ == instruction_->InputCount(); }
1651 HInstruction* Current() const { return instruction_->InputAt(index_); }
1652 void Advance() { index_++; }
1653
1654 private:
1655 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001656 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001657
1658 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1659};
1660
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001661class HInstructionIterator : public ValueObject {
1662 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001663 explicit HInstructionIterator(const HInstructionList& instructions)
1664 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001665 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001666 }
1667
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001668 bool Done() const { return instruction_ == nullptr; }
1669 HInstruction* Current() const { return instruction_; }
1670 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001671 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001672 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001673 }
1674
1675 private:
1676 HInstruction* instruction_;
1677 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001678
1679 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001680};
1681
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001682class HBackwardInstructionIterator : public ValueObject {
1683 public:
1684 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1685 : instruction_(instructions.last_instruction_) {
1686 next_ = Done() ? nullptr : instruction_->GetPrevious();
1687 }
1688
1689 bool Done() const { return instruction_ == nullptr; }
1690 HInstruction* Current() const { return instruction_; }
1691 void Advance() {
1692 instruction_ = next_;
1693 next_ = Done() ? nullptr : instruction_->GetPrevious();
1694 }
1695
1696 private:
1697 HInstruction* instruction_;
1698 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001699
1700 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001701};
1702
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001703// An embedded container with N elements of type T. Used (with partial
1704// specialization for N=0) because embedded arrays cannot have size 0.
1705template<typename T, intptr_t N>
1706class EmbeddedArray {
1707 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001708 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001709
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001710 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001711
1712 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001713 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001714 return elements_[i];
1715 }
1716
1717 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001718 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001719 return elements_[i];
1720 }
1721
1722 const T& At(intptr_t i) const {
1723 return (*this)[i];
1724 }
1725
1726 void SetAt(intptr_t i, const T& val) {
1727 (*this)[i] = val;
1728 }
1729
1730 private:
1731 T elements_[N];
1732};
1733
1734template<typename T>
1735class EmbeddedArray<T, 0> {
1736 public:
1737 intptr_t length() const { return 0; }
1738 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001739 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001740 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001741 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001742 }
1743 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001744 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001745 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001746 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001747 }
1748};
1749
1750template<intptr_t N>
1751class HTemplateInstruction: public HInstruction {
1752 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001753 HTemplateInstruction<N>(SideEffects side_effects)
1754 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001755 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001756
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001757 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001758
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001759 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001760 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1761
1762 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1763 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001764 }
1765
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001766 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001767 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001768
1769 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001770};
1771
Dave Allison20dfc792014-06-16 20:44:29 -07001772template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001773class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001774 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001775 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1776 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001777 virtual ~HExpression() {}
1778
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001779 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001780
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001781 protected:
1782 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001783};
1784
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001785// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1786// instruction that branches to the exit block.
1787class HReturnVoid : public HTemplateInstruction<0> {
1788 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001789 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001790
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001791 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001792
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001793 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001794
1795 private:
1796 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1797};
1798
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001799// Represents dex's RETURN opcodes. A HReturn is a control flow
1800// instruction that branches to the exit block.
1801class HReturn : public HTemplateInstruction<1> {
1802 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001803 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001804 SetRawInputAt(0, value);
1805 }
1806
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001807 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001808
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001809 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001810
1811 private:
1812 DISALLOW_COPY_AND_ASSIGN(HReturn);
1813};
1814
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001815// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001816// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001817// exit block.
1818class HExit : public HTemplateInstruction<0> {
1819 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001820 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001821
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001822 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001823
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001824 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001825
1826 private:
1827 DISALLOW_COPY_AND_ASSIGN(HExit);
1828};
1829
1830// Jumps from one block to another.
1831class HGoto : public HTemplateInstruction<0> {
1832 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001833 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1834
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001835 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001836
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001837 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001838 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001839 }
1840
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001841 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001842
1843 private:
1844 DISALLOW_COPY_AND_ASSIGN(HGoto);
1845};
1846
Dave Allison20dfc792014-06-16 20:44:29 -07001847
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001848// Conditional branch. A block ending with an HIf instruction must have
1849// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001850class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001851 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001852 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001853 SetRawInputAt(0, input);
1854 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001855
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001856 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001857
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001858 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001859 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001860 }
1861
1862 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001863 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001864 }
1865
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001866 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001867
1868 private:
1869 DISALLOW_COPY_AND_ASSIGN(HIf);
1870};
1871
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001872// Deoptimize to interpreter, upon checking a condition.
1873class HDeoptimize : public HTemplateInstruction<1> {
1874 public:
1875 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1876 : HTemplateInstruction(SideEffects::None()),
1877 dex_pc_(dex_pc) {
1878 SetRawInputAt(0, cond);
1879 }
1880
1881 bool NeedsEnvironment() const OVERRIDE { return true; }
1882 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001883 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001884
1885 DECLARE_INSTRUCTION(Deoptimize);
1886
1887 private:
1888 uint32_t dex_pc_;
1889
1890 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1891};
1892
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001893// Represents the ArtMethod that was passed as a first argument to
1894// the method. It is used by instructions that depend on it, like
1895// instructions that work with the dex cache.
1896class HCurrentMethod : public HExpression<0> {
1897 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001898 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001899
1900 DECLARE_INSTRUCTION(CurrentMethod);
1901
1902 private:
1903 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
1904};
1905
Roland Levillain88cb1752014-10-20 16:36:47 +01001906class HUnaryOperation : public HExpression<1> {
1907 public:
1908 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1909 : HExpression(result_type, SideEffects::None()) {
1910 SetRawInputAt(0, input);
1911 }
1912
1913 HInstruction* GetInput() const { return InputAt(0); }
1914 Primitive::Type GetResultType() const { return GetType(); }
1915
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001916 bool CanBeMoved() const OVERRIDE { return true; }
1917 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001918 UNUSED(other);
1919 return true;
1920 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001921
Roland Levillain9240d6a2014-10-20 16:47:04 +01001922 // Try to statically evaluate `operation` and return a HConstant
1923 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001924 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001925 HConstant* TryStaticEvaluation() const;
1926
1927 // Apply this operation to `x`.
1928 virtual int32_t Evaluate(int32_t x) const = 0;
1929 virtual int64_t Evaluate(int64_t x) const = 0;
1930
Roland Levillain88cb1752014-10-20 16:36:47 +01001931 DECLARE_INSTRUCTION(UnaryOperation);
1932
1933 private:
1934 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1935};
1936
Dave Allison20dfc792014-06-16 20:44:29 -07001937class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001938 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001939 HBinaryOperation(Primitive::Type result_type,
1940 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001941 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001942 SetRawInputAt(0, left);
1943 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001944 }
1945
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001946 HInstruction* GetLeft() const { return InputAt(0); }
1947 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001948 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001949
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001950 virtual bool IsCommutative() const { return false; }
1951
1952 // Put constant on the right.
1953 // Returns whether order is changed.
1954 bool OrderInputsWithConstantOnTheRight() {
1955 HInstruction* left = InputAt(0);
1956 HInstruction* right = InputAt(1);
1957 if (left->IsConstant() && !right->IsConstant()) {
1958 ReplaceInput(right, 0);
1959 ReplaceInput(left, 1);
1960 return true;
1961 }
1962 return false;
1963 }
1964
1965 // Order inputs by instruction id, but favor constant on the right side.
1966 // This helps GVN for commutative ops.
1967 void OrderInputs() {
1968 DCHECK(IsCommutative());
1969 HInstruction* left = InputAt(0);
1970 HInstruction* right = InputAt(1);
1971 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1972 return;
1973 }
1974 if (OrderInputsWithConstantOnTheRight()) {
1975 return;
1976 }
1977 // Order according to instruction id.
1978 if (left->GetId() > right->GetId()) {
1979 ReplaceInput(right, 0);
1980 ReplaceInput(left, 1);
1981 }
1982 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001983
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001984 bool CanBeMoved() const OVERRIDE { return true; }
1985 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001986 UNUSED(other);
1987 return true;
1988 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001989
Roland Levillain9240d6a2014-10-20 16:47:04 +01001990 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001991 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001992 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001993 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001994
1995 // Apply this operation to `x` and `y`.
1996 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1997 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1998
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001999 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002000 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002001 HConstant* GetConstantRight() const;
2002
2003 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002004 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002005 HInstruction* GetLeastConstantLeft() const;
2006
Roland Levillainccc07a92014-09-16 14:48:16 +01002007 DECLARE_INSTRUCTION(BinaryOperation);
2008
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002009 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002010 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2011};
2012
Dave Allison20dfc792014-06-16 20:44:29 -07002013class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002014 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002015 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002016 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
2017 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002018
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002019 bool NeedsMaterialization() const { return needs_materialization_; }
2020 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002021
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002022 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002023 // `instruction`, and disregard moves in between.
2024 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002025
Dave Allison20dfc792014-06-16 20:44:29 -07002026 DECLARE_INSTRUCTION(Condition);
2027
2028 virtual IfCondition GetCondition() const = 0;
2029
2030 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002031 // For register allocation purposes, returns whether this instruction needs to be
2032 // materialized (that is, not just be in the processor flags).
2033 bool needs_materialization_;
2034
Dave Allison20dfc792014-06-16 20:44:29 -07002035 DISALLOW_COPY_AND_ASSIGN(HCondition);
2036};
2037
2038// Instruction to check if two inputs are equal to each other.
2039class HEqual : public HCondition {
2040 public:
2041 HEqual(HInstruction* first, HInstruction* second)
2042 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002043
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002044 bool IsCommutative() const OVERRIDE { return true; }
2045
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002046 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002047 return x == y ? 1 : 0;
2048 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002049 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002050 return x == y ? 1 : 0;
2051 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002052
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002053 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002054
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002055 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002056 return kCondEQ;
2057 }
2058
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002059 private:
2060 DISALLOW_COPY_AND_ASSIGN(HEqual);
2061};
2062
Dave Allison20dfc792014-06-16 20:44:29 -07002063class HNotEqual : public HCondition {
2064 public:
2065 HNotEqual(HInstruction* first, HInstruction* second)
2066 : HCondition(first, second) {}
2067
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002068 bool IsCommutative() const OVERRIDE { return true; }
2069
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002070 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002071 return x != y ? 1 : 0;
2072 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002073 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002074 return x != y ? 1 : 0;
2075 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002076
Dave Allison20dfc792014-06-16 20:44:29 -07002077 DECLARE_INSTRUCTION(NotEqual);
2078
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002079 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002080 return kCondNE;
2081 }
2082
2083 private:
2084 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2085};
2086
2087class HLessThan : public HCondition {
2088 public:
2089 HLessThan(HInstruction* first, HInstruction* second)
2090 : HCondition(first, second) {}
2091
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002092 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002093 return x < y ? 1 : 0;
2094 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002095 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002096 return x < y ? 1 : 0;
2097 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002098
Dave Allison20dfc792014-06-16 20:44:29 -07002099 DECLARE_INSTRUCTION(LessThan);
2100
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002101 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002102 return kCondLT;
2103 }
2104
2105 private:
2106 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2107};
2108
2109class HLessThanOrEqual : public HCondition {
2110 public:
2111 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2112 : HCondition(first, second) {}
2113
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002114 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002115 return x <= y ? 1 : 0;
2116 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002117 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002118 return x <= y ? 1 : 0;
2119 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002120
Dave Allison20dfc792014-06-16 20:44:29 -07002121 DECLARE_INSTRUCTION(LessThanOrEqual);
2122
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002123 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002124 return kCondLE;
2125 }
2126
2127 private:
2128 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2129};
2130
2131class HGreaterThan : public HCondition {
2132 public:
2133 HGreaterThan(HInstruction* first, HInstruction* second)
2134 : HCondition(first, second) {}
2135
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002136 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002137 return x > y ? 1 : 0;
2138 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002139 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002140 return x > y ? 1 : 0;
2141 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002142
Dave Allison20dfc792014-06-16 20:44:29 -07002143 DECLARE_INSTRUCTION(GreaterThan);
2144
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002145 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002146 return kCondGT;
2147 }
2148
2149 private:
2150 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2151};
2152
2153class HGreaterThanOrEqual : public HCondition {
2154 public:
2155 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2156 : HCondition(first, second) {}
2157
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002158 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002159 return x >= y ? 1 : 0;
2160 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002161 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002162 return x >= y ? 1 : 0;
2163 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002164
Dave Allison20dfc792014-06-16 20:44:29 -07002165 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2166
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002167 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002168 return kCondGE;
2169 }
2170
2171 private:
2172 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2173};
2174
2175
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002176// Instruction to check how two inputs compare to each other.
2177// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2178class HCompare : public HBinaryOperation {
2179 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002180 // The bias applies for floating point operations and indicates how NaN
2181 // comparisons are treated:
2182 enum Bias {
2183 kNoBias, // bias is not applicable (i.e. for long operation)
2184 kGtBias, // return 1 for NaN comparisons
2185 kLtBias, // return -1 for NaN comparisons
2186 };
2187
2188 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
2189 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002190 DCHECK_EQ(type, first->GetType());
2191 DCHECK_EQ(type, second->GetType());
2192 }
2193
Calin Juravleddb7df22014-11-25 20:56:51 +00002194 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002195 return
2196 x == y ? 0 :
2197 x > y ? 1 :
2198 -1;
2199 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002200
2201 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002202 return
2203 x == y ? 0 :
2204 x > y ? 1 :
2205 -1;
2206 }
2207
Calin Juravleddb7df22014-11-25 20:56:51 +00002208 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2209 return bias_ == other->AsCompare()->bias_;
2210 }
2211
2212 bool IsGtBias() { return bias_ == kGtBias; }
2213
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002214 DECLARE_INSTRUCTION(Compare);
2215
2216 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002217 const Bias bias_;
2218
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002219 DISALLOW_COPY_AND_ASSIGN(HCompare);
2220};
2221
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002222// A local in the graph. Corresponds to a Dex register.
2223class HLocal : public HTemplateInstruction<0> {
2224 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002225 explicit HLocal(uint16_t reg_number)
2226 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002227
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002228 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002229
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002230 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002231
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002232 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002233 // The Dex register number.
2234 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002235
2236 DISALLOW_COPY_AND_ASSIGN(HLocal);
2237};
2238
2239// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002240class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002241 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002242 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002243 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002244 SetRawInputAt(0, local);
2245 }
2246
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002247 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2248
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002249 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002250
2251 private:
2252 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2253};
2254
2255// Store a value in a given local. This instruction has two inputs: the value
2256// and the local.
2257class HStoreLocal : public HTemplateInstruction<2> {
2258 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002259 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002260 SetRawInputAt(0, local);
2261 SetRawInputAt(1, value);
2262 }
2263
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002264 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2265
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002266 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002267
2268 private:
2269 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2270};
2271
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002272class HConstant : public HExpression<0> {
2273 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002274 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2275
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002276 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002277
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002278 virtual bool IsMinusOne() const { return false; }
2279 virtual bool IsZero() const { return false; }
2280 virtual bool IsOne() const { return false; }
2281
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002282 DECLARE_INSTRUCTION(Constant);
2283
2284 private:
2285 DISALLOW_COPY_AND_ASSIGN(HConstant);
2286};
2287
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002288class HFloatConstant : public HConstant {
2289 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002290 float GetValue() const { return value_; }
2291
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002292 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002293 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2294 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002295 }
2296
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002297 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002298
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002299 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002300 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002301 }
2302 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002303 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002304 }
2305 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002306 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2307 }
2308 bool IsNaN() const {
2309 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002310 }
2311
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002312 DECLARE_INSTRUCTION(FloatConstant);
2313
2314 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002315 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002316 explicit HFloatConstant(int32_t value)
2317 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002318
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002319 const float value_;
2320
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002321 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002322 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002323 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002324 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2325};
2326
2327class HDoubleConstant : public HConstant {
2328 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002329 double GetValue() const { return value_; }
2330
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002331 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002332 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2333 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002334 }
2335
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002336 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002337
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002338 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002339 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002340 }
2341 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002342 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002343 }
2344 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002345 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2346 }
2347 bool IsNaN() const {
2348 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002349 }
2350
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002351 DECLARE_INSTRUCTION(DoubleConstant);
2352
2353 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002354 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002355 explicit HDoubleConstant(int64_t value)
2356 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002357
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002358 const double value_;
2359
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002360 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002361 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002362 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002363 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2364};
2365
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002366class HNullConstant : public HConstant {
2367 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002368 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2369 return true;
2370 }
2371
2372 size_t ComputeHashCode() const OVERRIDE { return 0; }
2373
2374 DECLARE_INSTRUCTION(NullConstant);
2375
2376 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002377 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2378
2379 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002380 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2381};
2382
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002383// Constants of the type int. Those can be from Dex instructions, or
2384// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002385class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002386 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002387 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002388
Calin Juravle61d544b2015-02-23 16:46:57 +00002389 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002390 return other->AsIntConstant()->value_ == value_;
2391 }
2392
Calin Juravle61d544b2015-02-23 16:46:57 +00002393 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2394
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002395 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2396 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2397 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2398
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002399 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002400
2401 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002402 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2403
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002404 const int32_t value_;
2405
David Brazdil8d5b8b22015-03-24 10:51:52 +00002406 friend class HGraph;
2407 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002408 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002409 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2410};
2411
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002412class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002413 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002414 int64_t GetValue() const { return value_; }
2415
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002416 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002417 return other->AsLongConstant()->value_ == value_;
2418 }
2419
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002420 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002421
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002422 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2423 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2424 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2425
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002426 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002427
2428 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002429 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2430
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002431 const int64_t value_;
2432
David Brazdil8d5b8b22015-03-24 10:51:52 +00002433 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002434 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2435};
2436
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002437enum class Intrinsics {
2438#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2439#include "intrinsics_list.h"
2440 kNone,
2441 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2442#undef INTRINSICS_LIST
2443#undef OPTIMIZING_INTRINSICS
2444};
2445std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2446
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002447class HInvoke : public HInstruction {
2448 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002449 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002450
2451 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2452 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002453 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002454
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002455 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002456 SetRawInputAt(index, argument);
2457 }
2458
Roland Levillain3e3d7332015-04-28 11:00:54 +01002459 // Return the number of arguments. This number can be lower than
2460 // the number of inputs returned by InputCount(), as some invoke
2461 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2462 // inputs at the end of their list of inputs.
2463 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2464
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002465 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002466
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002467 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002468
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002469 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002470 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002471
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002472 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2473
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002474 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002475 return intrinsic_;
2476 }
2477
2478 void SetIntrinsic(Intrinsics intrinsic) {
2479 intrinsic_ = intrinsic;
2480 }
2481
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002482 bool IsInlined() const {
2483 return GetEnvironment()->GetParent() != nullptr;
2484 }
2485
2486 bool CanThrow() const OVERRIDE { return true; }
2487
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002488 DECLARE_INSTRUCTION(Invoke);
2489
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002490 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002491 HInvoke(ArenaAllocator* arena,
2492 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002493 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002494 Primitive::Type return_type,
2495 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002496 uint32_t dex_method_index,
2497 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002498 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002499 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002500 inputs_(arena, number_of_arguments),
2501 return_type_(return_type),
2502 dex_pc_(dex_pc),
2503 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002504 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002505 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002506 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2507 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002508 }
2509
David Brazdil1abb4192015-02-17 18:33:36 +00002510 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2511 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2512 inputs_.Put(index, input);
2513 }
2514
Roland Levillain3e3d7332015-04-28 11:00:54 +01002515 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002516 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002517 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002518 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002519 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002520 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002521 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002522
2523 private:
2524 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2525};
2526
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002527class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002528 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002529 // Requirements of this method call regarding the class
2530 // initialization (clinit) check of its declaring class.
2531 enum class ClinitCheckRequirement {
2532 kNone, // Class already initialized.
2533 kExplicit, // Static call having explicit clinit check as last input.
2534 kImplicit, // Static call implicitly requiring a clinit check.
2535 };
2536
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002537 HInvokeStaticOrDirect(ArenaAllocator* arena,
2538 uint32_t number_of_arguments,
2539 Primitive::Type return_type,
2540 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002541 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002542 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002543 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002544 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002545 InvokeType invoke_type,
2546 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002547 : HInvoke(arena,
2548 number_of_arguments,
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002549 // There is one extra argument for the HCurrentMethod node, and
2550 // potentially one other if the clinit check is explicit.
2551 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 2u : 1u,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002552 return_type,
2553 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002554 dex_method_index,
2555 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002556 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002557 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002558 clinit_check_requirement_(clinit_check_requirement),
2559 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002560
Calin Juravle641547a2015-04-21 22:08:51 +01002561 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2562 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002563 // We access the method via the dex cache so we can't do an implicit null check.
2564 // TODO: for intrinsics we can generate implicit null checks.
2565 return false;
2566 }
2567
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002568 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002569 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002570 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002571 bool IsStringInit() const { return string_init_offset_ != 0; }
2572 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002573 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002574
Roland Levillain4c0eb422015-04-24 16:43:49 +01002575 // Is this instruction a call to a static method?
2576 bool IsStatic() const {
2577 return GetInvokeType() == kStatic;
2578 }
2579
Roland Levillain3e3d7332015-04-28 11:00:54 +01002580 // Remove the art::HLoadClass instruction set as last input by
2581 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2582 // the initial art::HClinitCheck instruction (only relevant for
2583 // static calls with explicit clinit check).
2584 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002585 DCHECK(IsStaticWithExplicitClinitCheck());
2586 size_t last_input_index = InputCount() - 1;
2587 HInstruction* last_input = InputAt(last_input_index);
2588 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002589 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002590 RemoveAsUserOfInput(last_input_index);
2591 inputs_.DeleteAt(last_input_index);
2592 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2593 DCHECK(IsStaticWithImplicitClinitCheck());
2594 }
2595
2596 // Is this a call to a static method whose declaring class has an
2597 // explicit intialization check in the graph?
2598 bool IsStaticWithExplicitClinitCheck() const {
2599 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2600 }
2601
2602 // Is this a call to a static method whose declaring class has an
2603 // implicit intialization check requirement?
2604 bool IsStaticWithImplicitClinitCheck() const {
2605 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2606 }
2607
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002608 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002609
Roland Levillain4c0eb422015-04-24 16:43:49 +01002610 protected:
2611 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2612 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2613 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2614 HInstruction* input = input_record.GetInstruction();
2615 // `input` is the last input of a static invoke marked as having
2616 // an explicit clinit check. It must either be:
2617 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2618 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2619 DCHECK(input != nullptr);
2620 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2621 }
2622 return input_record;
2623 }
2624
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002625 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002626 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002627 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002628 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002629 // Thread entrypoint offset for string init method if this is a string init invoke.
2630 // Note that there are multiple string init methods, each having its own offset.
2631 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002632
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002633 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002634};
2635
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002636class HInvokeVirtual : public HInvoke {
2637 public:
2638 HInvokeVirtual(ArenaAllocator* arena,
2639 uint32_t number_of_arguments,
2640 Primitive::Type return_type,
2641 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002642 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002643 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002644 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002645 vtable_index_(vtable_index) {}
2646
Calin Juravle641547a2015-04-21 22:08:51 +01002647 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002648 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002649 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002650 }
2651
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002652 uint32_t GetVTableIndex() const { return vtable_index_; }
2653
2654 DECLARE_INSTRUCTION(InvokeVirtual);
2655
2656 private:
2657 const uint32_t vtable_index_;
2658
2659 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2660};
2661
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002662class HInvokeInterface : public HInvoke {
2663 public:
2664 HInvokeInterface(ArenaAllocator* arena,
2665 uint32_t number_of_arguments,
2666 Primitive::Type return_type,
2667 uint32_t dex_pc,
2668 uint32_t dex_method_index,
2669 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002670 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002671 imt_index_(imt_index) {}
2672
Calin Juravle641547a2015-04-21 22:08:51 +01002673 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002674 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002675 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002676 }
2677
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002678 uint32_t GetImtIndex() const { return imt_index_; }
2679 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2680
2681 DECLARE_INSTRUCTION(InvokeInterface);
2682
2683 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002684 const uint32_t imt_index_;
2685
2686 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2687};
2688
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002689class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002690 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002691 HNewInstance(HCurrentMethod* current_method,
2692 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002693 uint16_t type_index,
2694 const DexFile& dex_file,
2695 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002696 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2697 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002698 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002699 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002700 entrypoint_(entrypoint) {
2701 SetRawInputAt(0, current_method);
2702 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002703
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002704 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002705 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002706 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002707
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002708 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002709 bool NeedsEnvironment() const OVERRIDE { return true; }
2710 // It may throw when called on:
2711 // - interfaces
2712 // - abstract/innaccessible/unknown classes
2713 // TODO: optimize when possible.
2714 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002715
Calin Juravle10e244f2015-01-26 18:54:32 +00002716 bool CanBeNull() const OVERRIDE { return false; }
2717
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002718 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2719
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002720 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002721
2722 private:
2723 const uint32_t dex_pc_;
2724 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002725 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002726 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002727
2728 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2729};
2730
Roland Levillain88cb1752014-10-20 16:36:47 +01002731class HNeg : public HUnaryOperation {
2732 public:
2733 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2734 : HUnaryOperation(result_type, input) {}
2735
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002736 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2737 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002738
Roland Levillain88cb1752014-10-20 16:36:47 +01002739 DECLARE_INSTRUCTION(Neg);
2740
2741 private:
2742 DISALLOW_COPY_AND_ASSIGN(HNeg);
2743};
2744
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002745class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002746 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002747 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002748 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002749 uint32_t dex_pc,
2750 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002751 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002752 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002753 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2754 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002755 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002756 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002757 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002758 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002759 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002760 }
2761
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002762 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002763 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002764 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002765
2766 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002767 bool NeedsEnvironment() const OVERRIDE { return true; }
2768
Mingyao Yang0c365e62015-03-31 15:09:29 -07002769 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2770 bool CanThrow() const OVERRIDE { return true; }
2771
Calin Juravle10e244f2015-01-26 18:54:32 +00002772 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002773
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002774 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2775
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002776 DECLARE_INSTRUCTION(NewArray);
2777
2778 private:
2779 const uint32_t dex_pc_;
2780 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002781 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002782 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002783
2784 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2785};
2786
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002787class HAdd : public HBinaryOperation {
2788 public:
2789 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2790 : HBinaryOperation(result_type, left, right) {}
2791
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002792 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002793
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002794 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002795 return x + y;
2796 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002797 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002798 return x + y;
2799 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002800
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002801 DECLARE_INSTRUCTION(Add);
2802
2803 private:
2804 DISALLOW_COPY_AND_ASSIGN(HAdd);
2805};
2806
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002807class HSub : public HBinaryOperation {
2808 public:
2809 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2810 : HBinaryOperation(result_type, left, right) {}
2811
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002812 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002813 return x - y;
2814 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002815 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002816 return x - y;
2817 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002818
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002819 DECLARE_INSTRUCTION(Sub);
2820
2821 private:
2822 DISALLOW_COPY_AND_ASSIGN(HSub);
2823};
2824
Calin Juravle34bacdf2014-10-07 20:23:36 +01002825class HMul : public HBinaryOperation {
2826 public:
2827 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2828 : HBinaryOperation(result_type, left, right) {}
2829
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002830 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002831
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002832 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2833 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002834
2835 DECLARE_INSTRUCTION(Mul);
2836
2837 private:
2838 DISALLOW_COPY_AND_ASSIGN(HMul);
2839};
2840
Calin Juravle7c4954d2014-10-28 16:57:40 +00002841class HDiv : public HBinaryOperation {
2842 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002843 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2844 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002845
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002846 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002847 // Our graph structure ensures we never have 0 for `y` during constant folding.
2848 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002849 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002850 return (y == -1) ? -x : x / y;
2851 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002852
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002853 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002854 DCHECK_NE(y, 0);
2855 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2856 return (y == -1) ? -x : x / y;
2857 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002858
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002859 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002860
Calin Juravle7c4954d2014-10-28 16:57:40 +00002861 DECLARE_INSTRUCTION(Div);
2862
2863 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002864 const uint32_t dex_pc_;
2865
Calin Juravle7c4954d2014-10-28 16:57:40 +00002866 DISALLOW_COPY_AND_ASSIGN(HDiv);
2867};
2868
Calin Juravlebacfec32014-11-14 15:54:36 +00002869class HRem : public HBinaryOperation {
2870 public:
2871 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2872 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2873
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002874 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002875 DCHECK_NE(y, 0);
2876 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2877 return (y == -1) ? 0 : x % y;
2878 }
2879
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002880 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002881 DCHECK_NE(y, 0);
2882 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2883 return (y == -1) ? 0 : x % y;
2884 }
2885
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002886 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002887
2888 DECLARE_INSTRUCTION(Rem);
2889
2890 private:
2891 const uint32_t dex_pc_;
2892
2893 DISALLOW_COPY_AND_ASSIGN(HRem);
2894};
2895
Calin Juravled0d48522014-11-04 16:40:20 +00002896class HDivZeroCheck : public HExpression<1> {
2897 public:
2898 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2899 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2900 SetRawInputAt(0, value);
2901 }
2902
2903 bool CanBeMoved() const OVERRIDE { return true; }
2904
2905 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2906 UNUSED(other);
2907 return true;
2908 }
2909
2910 bool NeedsEnvironment() const OVERRIDE { return true; }
2911 bool CanThrow() const OVERRIDE { return true; }
2912
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002913 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002914
2915 DECLARE_INSTRUCTION(DivZeroCheck);
2916
2917 private:
2918 const uint32_t dex_pc_;
2919
2920 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2921};
2922
Calin Juravle9aec02f2014-11-18 23:06:35 +00002923class HShl : public HBinaryOperation {
2924 public:
2925 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2926 : HBinaryOperation(result_type, left, right) {}
2927
2928 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2929 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2930
2931 DECLARE_INSTRUCTION(Shl);
2932
2933 private:
2934 DISALLOW_COPY_AND_ASSIGN(HShl);
2935};
2936
2937class HShr : public HBinaryOperation {
2938 public:
2939 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2940 : HBinaryOperation(result_type, left, right) {}
2941
2942 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2943 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2944
2945 DECLARE_INSTRUCTION(Shr);
2946
2947 private:
2948 DISALLOW_COPY_AND_ASSIGN(HShr);
2949};
2950
2951class HUShr : public HBinaryOperation {
2952 public:
2953 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2954 : HBinaryOperation(result_type, left, right) {}
2955
2956 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2957 uint32_t ux = static_cast<uint32_t>(x);
2958 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2959 return static_cast<int32_t>(ux >> uy);
2960 }
2961
2962 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2963 uint64_t ux = static_cast<uint64_t>(x);
2964 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2965 return static_cast<int64_t>(ux >> uy);
2966 }
2967
2968 DECLARE_INSTRUCTION(UShr);
2969
2970 private:
2971 DISALLOW_COPY_AND_ASSIGN(HUShr);
2972};
2973
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002974class HAnd : public HBinaryOperation {
2975 public:
2976 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2977 : HBinaryOperation(result_type, left, right) {}
2978
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002979 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002980
2981 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2982 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2983
2984 DECLARE_INSTRUCTION(And);
2985
2986 private:
2987 DISALLOW_COPY_AND_ASSIGN(HAnd);
2988};
2989
2990class HOr : public HBinaryOperation {
2991 public:
2992 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2993 : HBinaryOperation(result_type, left, right) {}
2994
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002995 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002996
2997 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2998 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2999
3000 DECLARE_INSTRUCTION(Or);
3001
3002 private:
3003 DISALLOW_COPY_AND_ASSIGN(HOr);
3004};
3005
3006class HXor : public HBinaryOperation {
3007 public:
3008 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3009 : HBinaryOperation(result_type, left, right) {}
3010
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003011 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003012
3013 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3014 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3015
3016 DECLARE_INSTRUCTION(Xor);
3017
3018 private:
3019 DISALLOW_COPY_AND_ASSIGN(HXor);
3020};
3021
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003022// The value of a parameter in this method. Its location depends on
3023// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003024class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003025 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003026 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3027 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003028
3029 uint8_t GetIndex() const { return index_; }
3030
Calin Juravle10e244f2015-01-26 18:54:32 +00003031 bool CanBeNull() const OVERRIDE { return !is_this_; }
3032
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003033 bool IsThis() const { return is_this_; }
3034
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003035 DECLARE_INSTRUCTION(ParameterValue);
3036
3037 private:
3038 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003039 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003040 const uint8_t index_;
3041
Calin Juravle10e244f2015-01-26 18:54:32 +00003042 // Whether or not the parameter value corresponds to 'this' argument.
3043 const bool is_this_;
3044
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003045 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3046};
3047
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003048class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003049 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003050 explicit HNot(Primitive::Type result_type, HInstruction* input)
3051 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003052
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003053 bool CanBeMoved() const OVERRIDE { return true; }
3054 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003055 UNUSED(other);
3056 return true;
3057 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003058
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003059 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3060 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003061
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003062 DECLARE_INSTRUCTION(Not);
3063
3064 private:
3065 DISALLOW_COPY_AND_ASSIGN(HNot);
3066};
3067
David Brazdil66d126e2015-04-03 16:02:44 +01003068class HBooleanNot : public HUnaryOperation {
3069 public:
3070 explicit HBooleanNot(HInstruction* input)
3071 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3072
3073 bool CanBeMoved() const OVERRIDE { return true; }
3074 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3075 UNUSED(other);
3076 return true;
3077 }
3078
3079 int32_t Evaluate(int32_t x) const OVERRIDE {
3080 DCHECK(IsUint<1>(x));
3081 return !x;
3082 }
3083
3084 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3085 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3086 UNREACHABLE();
3087 }
3088
3089 DECLARE_INSTRUCTION(BooleanNot);
3090
3091 private:
3092 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3093};
3094
Roland Levillaindff1f282014-11-05 14:15:05 +00003095class HTypeConversion : public HExpression<1> {
3096 public:
3097 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003098 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3099 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003100 SetRawInputAt(0, input);
3101 DCHECK_NE(input->GetType(), result_type);
3102 }
3103
3104 HInstruction* GetInput() const { return InputAt(0); }
3105 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3106 Primitive::Type GetResultType() const { return GetType(); }
3107
Roland Levillain624279f2014-12-04 11:54:28 +00003108 // Required by the x86 and ARM code generators when producing calls
3109 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003110 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003111
Roland Levillaindff1f282014-11-05 14:15:05 +00003112 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003113 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003114
Mark Mendelle82549b2015-05-06 10:55:34 -04003115 // Try to statically evaluate the conversion and return a HConstant
3116 // containing the result. If the input cannot be converted, return nullptr.
3117 HConstant* TryStaticEvaluation() const;
3118
Roland Levillaindff1f282014-11-05 14:15:05 +00003119 DECLARE_INSTRUCTION(TypeConversion);
3120
3121 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003122 const uint32_t dex_pc_;
3123
Roland Levillaindff1f282014-11-05 14:15:05 +00003124 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3125};
3126
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003127static constexpr uint32_t kNoRegNumber = -1;
3128
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003129class HPhi : public HInstruction {
3130 public:
3131 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003132 : HInstruction(SideEffects::None()),
3133 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003134 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003135 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003136 is_live_(false),
3137 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003138 inputs_.SetSize(number_of_inputs);
3139 }
3140
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003141 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3142 static Primitive::Type ToPhiType(Primitive::Type type) {
3143 switch (type) {
3144 case Primitive::kPrimBoolean:
3145 case Primitive::kPrimByte:
3146 case Primitive::kPrimShort:
3147 case Primitive::kPrimChar:
3148 return Primitive::kPrimInt;
3149 default:
3150 return type;
3151 }
3152 }
3153
Calin Juravle10e244f2015-01-26 18:54:32 +00003154 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003155
3156 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003157 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003158
Calin Juravle10e244f2015-01-26 18:54:32 +00003159 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003160 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003161
Calin Juravle10e244f2015-01-26 18:54:32 +00003162 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3163 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3164
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003165 uint32_t GetRegNumber() const { return reg_number_; }
3166
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003167 void SetDead() { is_live_ = false; }
3168 void SetLive() { is_live_ = true; }
3169 bool IsDead() const { return !is_live_; }
3170 bool IsLive() const { return is_live_; }
3171
Calin Juravlea4f88312015-04-16 12:57:19 +01003172 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3173 // An equivalent phi is a phi having the same dex register and type.
3174 // It assumes that phis with the same dex register are adjacent.
3175 HPhi* GetNextEquivalentPhiWithSameType() {
3176 HInstruction* next = GetNext();
3177 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3178 if (next->GetType() == GetType()) {
3179 return next->AsPhi();
3180 }
3181 next = next->GetNext();
3182 }
3183 return nullptr;
3184 }
3185
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003186 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003187
David Brazdil1abb4192015-02-17 18:33:36 +00003188 protected:
3189 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3190
3191 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3192 inputs_.Put(index, input);
3193 }
3194
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003195 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003196 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003197 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003198 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003199 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003200 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003201
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003202 DISALLOW_COPY_AND_ASSIGN(HPhi);
3203};
3204
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003205class HNullCheck : public HExpression<1> {
3206 public:
3207 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003208 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003209 SetRawInputAt(0, value);
3210 }
3211
Calin Juravle10e244f2015-01-26 18:54:32 +00003212 bool CanBeMoved() const OVERRIDE { return true; }
3213 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003214 UNUSED(other);
3215 return true;
3216 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003217
Calin Juravle10e244f2015-01-26 18:54:32 +00003218 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003219
Calin Juravle10e244f2015-01-26 18:54:32 +00003220 bool CanThrow() const OVERRIDE { return true; }
3221
3222 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003223
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003224 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003225
3226 DECLARE_INSTRUCTION(NullCheck);
3227
3228 private:
3229 const uint32_t dex_pc_;
3230
3231 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3232};
3233
3234class FieldInfo : public ValueObject {
3235 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003236 FieldInfo(MemberOffset field_offset,
3237 Primitive::Type field_type,
3238 bool is_volatile,
3239 uint32_t index,
3240 const DexFile& dex_file)
3241 : field_offset_(field_offset),
3242 field_type_(field_type),
3243 is_volatile_(is_volatile),
3244 index_(index),
3245 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003246
3247 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003248 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003249 uint32_t GetFieldIndex() const { return index_; }
3250 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003251 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003252
3253 private:
3254 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003255 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003256 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003257 uint32_t index_;
3258 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003259};
3260
3261class HInstanceFieldGet : public HExpression<1> {
3262 public:
3263 HInstanceFieldGet(HInstruction* value,
3264 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003265 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003266 bool is_volatile,
3267 uint32_t field_idx,
3268 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003269 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003270 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003271 SetRawInputAt(0, value);
3272 }
3273
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003274 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003275
3276 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3277 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3278 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003279 }
3280
Calin Juravle641547a2015-04-21 22:08:51 +01003281 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3282 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003283 }
3284
3285 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003286 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3287 }
3288
Calin Juravle52c48962014-12-16 17:02:57 +00003289 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003290 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003291 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003292 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003293
3294 DECLARE_INSTRUCTION(InstanceFieldGet);
3295
3296 private:
3297 const FieldInfo field_info_;
3298
3299 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3300};
3301
3302class HInstanceFieldSet : public HTemplateInstruction<2> {
3303 public:
3304 HInstanceFieldSet(HInstruction* object,
3305 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003306 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003307 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003308 bool is_volatile,
3309 uint32_t field_idx,
3310 const DexFile& dex_file)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003311 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003312 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003313 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003314 SetRawInputAt(0, object);
3315 SetRawInputAt(1, value);
3316 }
3317
Calin Juravle641547a2015-04-21 22:08:51 +01003318 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3319 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003320 }
3321
Calin Juravle52c48962014-12-16 17:02:57 +00003322 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003323 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003324 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003325 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003326 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003327 bool GetValueCanBeNull() const { return value_can_be_null_; }
3328 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003329
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003330 DECLARE_INSTRUCTION(InstanceFieldSet);
3331
3332 private:
3333 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003334 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003335
3336 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3337};
3338
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003339class HArrayGet : public HExpression<2> {
3340 public:
3341 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003342 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003343 SetRawInputAt(0, array);
3344 SetRawInputAt(1, index);
3345 }
3346
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003347 bool CanBeMoved() const OVERRIDE { return true; }
3348 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003349 UNUSED(other);
3350 return true;
3351 }
Calin Juravle641547a2015-04-21 22:08:51 +01003352 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3353 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003354 // TODO: We can be smarter here.
3355 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3356 // which generates the implicit null check. There are cases when these can be removed
3357 // to produce better code. If we ever add optimizations to do so we should allow an
3358 // implicit check here (as long as the address falls in the first page).
3359 return false;
3360 }
3361
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003362 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003363
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003364 HInstruction* GetArray() const { return InputAt(0); }
3365 HInstruction* GetIndex() const { return InputAt(1); }
3366
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003367 DECLARE_INSTRUCTION(ArrayGet);
3368
3369 private:
3370 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3371};
3372
3373class HArraySet : public HTemplateInstruction<3> {
3374 public:
3375 HArraySet(HInstruction* array,
3376 HInstruction* index,
3377 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003378 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003379 uint32_t dex_pc)
3380 : HTemplateInstruction(SideEffects::ChangesSomething()),
3381 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003382 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003383 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3384 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003385 SetRawInputAt(0, array);
3386 SetRawInputAt(1, index);
3387 SetRawInputAt(2, value);
3388 }
3389
Calin Juravle77520bc2015-01-12 18:45:46 +00003390 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003391 // We currently always call a runtime method to catch array store
3392 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003393 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003394 }
3395
Mingyao Yang81014cb2015-06-02 03:16:27 -07003396 // Can throw ArrayStoreException.
3397 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3398
Calin Juravle641547a2015-04-21 22:08:51 +01003399 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3400 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003401 // TODO: Same as for ArrayGet.
3402 return false;
3403 }
3404
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003405 void ClearNeedsTypeCheck() {
3406 needs_type_check_ = false;
3407 }
3408
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003409 void ClearValueCanBeNull() {
3410 value_can_be_null_ = false;
3411 }
3412
3413 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003414 bool NeedsTypeCheck() const { return needs_type_check_; }
3415
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003416 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003417
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003418 HInstruction* GetArray() const { return InputAt(0); }
3419 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003420 HInstruction* GetValue() const { return InputAt(2); }
3421
3422 Primitive::Type GetComponentType() const {
3423 // The Dex format does not type floating point index operations. Since the
3424 // `expected_component_type_` is set during building and can therefore not
3425 // be correct, we also check what is the value type. If it is a floating
3426 // point type, we must use that type.
3427 Primitive::Type value_type = GetValue()->GetType();
3428 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3429 ? value_type
3430 : expected_component_type_;
3431 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003432
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003433 DECLARE_INSTRUCTION(ArraySet);
3434
3435 private:
3436 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003437 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003438 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003439 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003440
3441 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3442};
3443
3444class HArrayLength : public HExpression<1> {
3445 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003446 explicit HArrayLength(HInstruction* array)
3447 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3448 // Note that arrays do not change length, so the instruction does not
3449 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003450 SetRawInputAt(0, array);
3451 }
3452
Calin Juravle77520bc2015-01-12 18:45:46 +00003453 bool CanBeMoved() const OVERRIDE { return true; }
3454 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003455 UNUSED(other);
3456 return true;
3457 }
Calin Juravle641547a2015-04-21 22:08:51 +01003458 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3459 return obj == InputAt(0);
3460 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003461
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003462 DECLARE_INSTRUCTION(ArrayLength);
3463
3464 private:
3465 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3466};
3467
3468class HBoundsCheck : public HExpression<2> {
3469 public:
3470 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003471 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003472 DCHECK(index->GetType() == Primitive::kPrimInt);
3473 SetRawInputAt(0, index);
3474 SetRawInputAt(1, length);
3475 }
3476
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003477 bool CanBeMoved() const OVERRIDE { return true; }
3478 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003479 UNUSED(other);
3480 return true;
3481 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003482
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003483 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003484
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003485 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003486
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003487 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003488
3489 DECLARE_INSTRUCTION(BoundsCheck);
3490
3491 private:
3492 const uint32_t dex_pc_;
3493
3494 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3495};
3496
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003497/**
3498 * Some DEX instructions are folded into multiple HInstructions that need
3499 * to stay live until the last HInstruction. This class
3500 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003501 * HInstruction stays live. `index` represents the stack location index of the
3502 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003503 */
3504class HTemporary : public HTemplateInstruction<0> {
3505 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003506 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003507
3508 size_t GetIndex() const { return index_; }
3509
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003510 Primitive::Type GetType() const OVERRIDE {
3511 // The previous instruction is the one that will be stored in the temporary location.
3512 DCHECK(GetPrevious() != nullptr);
3513 return GetPrevious()->GetType();
3514 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003515
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003516 DECLARE_INSTRUCTION(Temporary);
3517
3518 private:
3519 const size_t index_;
3520
3521 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3522};
3523
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003524class HSuspendCheck : public HTemplateInstruction<0> {
3525 public:
3526 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003527 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003528
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003529 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003530 return true;
3531 }
3532
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003533 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003534 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3535 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003536
3537 DECLARE_INSTRUCTION(SuspendCheck);
3538
3539 private:
3540 const uint32_t dex_pc_;
3541
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003542 // Only used for code generation, in order to share the same slow path between back edges
3543 // of a same loop.
3544 SlowPathCode* slow_path_;
3545
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003546 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3547};
3548
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003549/**
3550 * Instruction to load a Class object.
3551 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003552class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003553 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003554 HLoadClass(HCurrentMethod* current_method,
3555 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003556 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003557 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003558 uint32_t dex_pc)
3559 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3560 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003561 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003562 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003563 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003564 generate_clinit_check_(false),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003565 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
3566 SetRawInputAt(0, current_method);
3567 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003568
3569 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003570
3571 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3572 return other->AsLoadClass()->type_index_ == type_index_;
3573 }
3574
3575 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3576
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003577 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003578 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003579 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003580
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003581 bool NeedsEnvironment() const OVERRIDE {
3582 // Will call runtime and load the class if the class is not loaded yet.
3583 // TODO: finer grain decision.
3584 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003585 }
3586
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003587 bool MustGenerateClinitCheck() const {
3588 return generate_clinit_check_;
3589 }
3590
Calin Juravle0ba218d2015-05-19 18:46:01 +01003591 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3592 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003593 }
3594
3595 bool CanCallRuntime() const {
3596 return MustGenerateClinitCheck() || !is_referrers_class_;
3597 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003598
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003599 bool CanThrow() const OVERRIDE {
3600 // May call runtime and and therefore can throw.
3601 // TODO: finer grain decision.
3602 return !is_referrers_class_;
3603 }
3604
Calin Juravleacf735c2015-02-12 15:25:22 +00003605 ReferenceTypeInfo GetLoadedClassRTI() {
3606 return loaded_class_rti_;
3607 }
3608
3609 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3610 // Make sure we only set exact types (the loaded class should never be merged).
3611 DCHECK(rti.IsExact());
3612 loaded_class_rti_ = rti;
3613 }
3614
3615 bool IsResolved() {
3616 return loaded_class_rti_.IsExact();
3617 }
3618
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003619 const DexFile& GetDexFile() { return dex_file_; }
3620
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003621 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3622
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003623 DECLARE_INSTRUCTION(LoadClass);
3624
3625 private:
3626 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003627 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003628 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003629 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003630 // Whether this instruction must generate the initialization check.
3631 // Used for code generation.
3632 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003633
Calin Juravleacf735c2015-02-12 15:25:22 +00003634 ReferenceTypeInfo loaded_class_rti_;
3635
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003636 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3637};
3638
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003639class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003640 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003641 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003642 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3643 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003644 dex_pc_(dex_pc) {
3645 SetRawInputAt(0, current_method);
3646 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003647
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003648 bool CanBeMoved() const OVERRIDE { return true; }
3649
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003650 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3651 return other->AsLoadString()->string_index_ == string_index_;
3652 }
3653
3654 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3655
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003656 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003657 uint32_t GetStringIndex() const { return string_index_; }
3658
3659 // TODO: Can we deopt or debug when we resolve a string?
3660 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003661 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003662
3663 DECLARE_INSTRUCTION(LoadString);
3664
3665 private:
3666 const uint32_t string_index_;
3667 const uint32_t dex_pc_;
3668
3669 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3670};
3671
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003672/**
3673 * Performs an initialization check on its Class object input.
3674 */
3675class HClinitCheck : public HExpression<1> {
3676 public:
3677 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003678 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003679 dex_pc_(dex_pc) {
3680 SetRawInputAt(0, constant);
3681 }
3682
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003683 bool CanBeMoved() const OVERRIDE { return true; }
3684 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3685 UNUSED(other);
3686 return true;
3687 }
3688
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003689 bool NeedsEnvironment() const OVERRIDE {
3690 // May call runtime to initialize the class.
3691 return true;
3692 }
3693
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003694 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003695
3696 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3697
3698 DECLARE_INSTRUCTION(ClinitCheck);
3699
3700 private:
3701 const uint32_t dex_pc_;
3702
3703 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3704};
3705
3706class HStaticFieldGet : public HExpression<1> {
3707 public:
3708 HStaticFieldGet(HInstruction* cls,
3709 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003710 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003711 bool is_volatile,
3712 uint32_t field_idx,
3713 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003714 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003715 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003716 SetRawInputAt(0, cls);
3717 }
3718
Calin Juravle52c48962014-12-16 17:02:57 +00003719
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003720 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003721
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003722 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003723 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3724 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003725 }
3726
3727 size_t ComputeHashCode() const OVERRIDE {
3728 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3729 }
3730
Calin Juravle52c48962014-12-16 17:02:57 +00003731 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003732 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3733 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003734 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003735
3736 DECLARE_INSTRUCTION(StaticFieldGet);
3737
3738 private:
3739 const FieldInfo field_info_;
3740
3741 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3742};
3743
3744class HStaticFieldSet : public HTemplateInstruction<2> {
3745 public:
3746 HStaticFieldSet(HInstruction* cls,
3747 HInstruction* value,
3748 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003749 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003750 bool is_volatile,
3751 uint32_t field_idx,
3752 const DexFile& dex_file)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003753 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003754 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003755 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003756 SetRawInputAt(0, cls);
3757 SetRawInputAt(1, value);
3758 }
3759
Calin Juravle52c48962014-12-16 17:02:57 +00003760 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003761 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3762 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003763 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003764
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003765 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003766 bool GetValueCanBeNull() const { return value_can_be_null_; }
3767 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003768
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003769 DECLARE_INSTRUCTION(StaticFieldSet);
3770
3771 private:
3772 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003773 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003774
3775 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3776};
3777
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003778// Implement the move-exception DEX instruction.
3779class HLoadException : public HExpression<0> {
3780 public:
3781 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3782
3783 DECLARE_INSTRUCTION(LoadException);
3784
3785 private:
3786 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3787};
3788
3789class HThrow : public HTemplateInstruction<1> {
3790 public:
3791 HThrow(HInstruction* exception, uint32_t dex_pc)
3792 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3793 SetRawInputAt(0, exception);
3794 }
3795
3796 bool IsControlFlow() const OVERRIDE { return true; }
3797
3798 bool NeedsEnvironment() const OVERRIDE { return true; }
3799
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003800 bool CanThrow() const OVERRIDE { return true; }
3801
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003802 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003803
3804 DECLARE_INSTRUCTION(Throw);
3805
3806 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003807 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003808
3809 DISALLOW_COPY_AND_ASSIGN(HThrow);
3810};
3811
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003812class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003813 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003814 HInstanceOf(HInstruction* object,
3815 HLoadClass* constant,
3816 bool class_is_final,
3817 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003818 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3819 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003820 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003821 dex_pc_(dex_pc) {
3822 SetRawInputAt(0, object);
3823 SetRawInputAt(1, constant);
3824 }
3825
3826 bool CanBeMoved() const OVERRIDE { return true; }
3827
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003828 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003829 return true;
3830 }
3831
3832 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003833 return false;
3834 }
3835
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003836 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003837
3838 bool IsClassFinal() const { return class_is_final_; }
3839
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003840 // Used only in code generation.
3841 bool MustDoNullCheck() const { return must_do_null_check_; }
3842 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3843
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003844 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003845
3846 private:
3847 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003848 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003849 const uint32_t dex_pc_;
3850
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003851 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3852};
3853
Calin Juravleb1498f62015-02-16 13:13:29 +00003854class HBoundType : public HExpression<1> {
3855 public:
3856 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3857 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3858 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003859 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003860 SetRawInputAt(0, input);
3861 }
3862
3863 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3864
3865 bool CanBeNull() const OVERRIDE {
3866 // `null instanceof ClassX` always return false so we can't be null.
3867 return false;
3868 }
3869
3870 DECLARE_INSTRUCTION(BoundType);
3871
3872 private:
3873 // Encodes the most upper class that this instruction can have. In other words
3874 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3875 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3876 const ReferenceTypeInfo bound_type_;
3877
3878 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3879};
3880
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003881class HCheckCast : public HTemplateInstruction<2> {
3882 public:
3883 HCheckCast(HInstruction* object,
3884 HLoadClass* constant,
3885 bool class_is_final,
3886 uint32_t dex_pc)
3887 : HTemplateInstruction(SideEffects::None()),
3888 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003889 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003890 dex_pc_(dex_pc) {
3891 SetRawInputAt(0, object);
3892 SetRawInputAt(1, constant);
3893 }
3894
3895 bool CanBeMoved() const OVERRIDE { return true; }
3896
3897 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3898 return true;
3899 }
3900
3901 bool NeedsEnvironment() const OVERRIDE {
3902 // Instruction may throw a CheckCastError.
3903 return true;
3904 }
3905
3906 bool CanThrow() const OVERRIDE { return true; }
3907
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003908 bool MustDoNullCheck() const { return must_do_null_check_; }
3909 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3910
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003911 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003912
3913 bool IsClassFinal() const { return class_is_final_; }
3914
3915 DECLARE_INSTRUCTION(CheckCast);
3916
3917 private:
3918 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003919 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003920 const uint32_t dex_pc_;
3921
3922 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003923};
3924
Calin Juravle27df7582015-04-17 19:12:31 +01003925class HMemoryBarrier : public HTemplateInstruction<0> {
3926 public:
3927 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3928 : HTemplateInstruction(SideEffects::None()),
3929 barrier_kind_(barrier_kind) {}
3930
3931 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3932
3933 DECLARE_INSTRUCTION(MemoryBarrier);
3934
3935 private:
3936 const MemBarrierKind barrier_kind_;
3937
3938 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3939};
3940
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003941class HMonitorOperation : public HTemplateInstruction<1> {
3942 public:
3943 enum OperationKind {
3944 kEnter,
3945 kExit,
3946 };
3947
3948 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3949 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3950 SetRawInputAt(0, object);
3951 }
3952
3953 // Instruction may throw a Java exception, so we need an environment.
3954 bool NeedsEnvironment() const OVERRIDE { return true; }
3955 bool CanThrow() const OVERRIDE { return true; }
3956
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003957 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003958
3959 bool IsEnter() const { return kind_ == kEnter; }
3960
3961 DECLARE_INSTRUCTION(MonitorOperation);
3962
Calin Juravle52c48962014-12-16 17:02:57 +00003963 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003964 const OperationKind kind_;
3965 const uint32_t dex_pc_;
3966
3967 private:
3968 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3969};
3970
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003971class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003972 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003973 MoveOperands(Location source,
3974 Location destination,
3975 Primitive::Type type,
3976 HInstruction* instruction)
3977 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003978
3979 Location GetSource() const { return source_; }
3980 Location GetDestination() const { return destination_; }
3981
3982 void SetSource(Location value) { source_ = value; }
3983 void SetDestination(Location value) { destination_ = value; }
3984
3985 // The parallel move resolver marks moves as "in-progress" by clearing the
3986 // destination (but not the source).
3987 Location MarkPending() {
3988 DCHECK(!IsPending());
3989 Location dest = destination_;
3990 destination_ = Location::NoLocation();
3991 return dest;
3992 }
3993
3994 void ClearPending(Location dest) {
3995 DCHECK(IsPending());
3996 destination_ = dest;
3997 }
3998
3999 bool IsPending() const {
4000 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4001 return destination_.IsInvalid() && !source_.IsInvalid();
4002 }
4003
4004 // True if this blocks a move from the given location.
4005 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004006 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004007 }
4008
4009 // A move is redundant if it's been eliminated, if its source and
4010 // destination are the same, or if its destination is unneeded.
4011 bool IsRedundant() const {
4012 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4013 }
4014
4015 // We clear both operands to indicate move that's been eliminated.
4016 void Eliminate() {
4017 source_ = destination_ = Location::NoLocation();
4018 }
4019
4020 bool IsEliminated() const {
4021 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4022 return source_.IsInvalid();
4023 }
4024
Nicolas Geoffray90218252015-04-15 11:56:51 +01004025 bool Is64BitMove() const {
4026 return Primitive::Is64BitType(type_);
4027 }
4028
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004029 HInstruction* GetInstruction() const { return instruction_; }
4030
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004031 private:
4032 Location source_;
4033 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004034 // The type this move is for.
4035 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004036 // The instruction this move is assocatied with. Null when this move is
4037 // for moving an input in the expected locations of user (including a phi user).
4038 // This is only used in debug mode, to ensure we do not connect interval siblings
4039 // in the same parallel move.
4040 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004041};
4042
4043static constexpr size_t kDefaultNumberOfMoves = 4;
4044
4045class HParallelMove : public HTemplateInstruction<0> {
4046 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004047 explicit HParallelMove(ArenaAllocator* arena)
4048 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004049
Nicolas Geoffray90218252015-04-15 11:56:51 +01004050 void AddMove(Location source,
4051 Location destination,
4052 Primitive::Type type,
4053 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004054 DCHECK(source.IsValid());
4055 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004056 if (kIsDebugBuild) {
4057 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004058 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004059 if (moves_.Get(i).GetInstruction() == instruction) {
4060 // Special case the situation where the move is for the spill slot
4061 // of the instruction.
4062 if ((GetPrevious() == instruction)
4063 || ((GetPrevious() == nullptr)
4064 && instruction->IsPhi()
4065 && instruction->GetBlock() == GetBlock())) {
4066 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4067 << "Doing parallel moves for the same instruction.";
4068 } else {
4069 DCHECK(false) << "Doing parallel moves for the same instruction.";
4070 }
4071 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004072 }
4073 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004074 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004075 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004076 << "Overlapped destination for two moves in a parallel move: "
4077 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4078 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004079 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004080 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004081 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004082 }
4083
4084 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004085 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004086 }
4087
4088 size_t NumMoves() const { return moves_.Size(); }
4089
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004090 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004091
4092 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004093 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004094
4095 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4096};
4097
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004098class HGraphVisitor : public ValueObject {
4099 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004100 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4101 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004102
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004103 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004104 virtual void VisitBasicBlock(HBasicBlock* block);
4105
Roland Levillain633021e2014-10-01 14:12:25 +01004106 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004107 void VisitInsertionOrder();
4108
Roland Levillain633021e2014-10-01 14:12:25 +01004109 // Visit the graph following dominator tree reverse post-order.
4110 void VisitReversePostOrder();
4111
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004112 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004113
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004114 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004115#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004116 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4117
4118 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4119
4120#undef DECLARE_VISIT_INSTRUCTION
4121
4122 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004123 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004124
4125 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4126};
4127
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004128class HGraphDelegateVisitor : public HGraphVisitor {
4129 public:
4130 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4131 virtual ~HGraphDelegateVisitor() {}
4132
4133 // Visit functions that delegate to to super class.
4134#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004135 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004136
4137 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4138
4139#undef DECLARE_VISIT_INSTRUCTION
4140
4141 private:
4142 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4143};
4144
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004145class HInsertionOrderIterator : public ValueObject {
4146 public:
4147 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4148
4149 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4150 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4151 void Advance() { ++index_; }
4152
4153 private:
4154 const HGraph& graph_;
4155 size_t index_;
4156
4157 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4158};
4159
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004160class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004161 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004162 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4163 // Check that reverse post order of the graph has been built.
4164 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4165 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004166
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004167 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4168 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004169 void Advance() { ++index_; }
4170
4171 private:
4172 const HGraph& graph_;
4173 size_t index_;
4174
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004175 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004176};
4177
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004178class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004179 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004180 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004181 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4182 // Check that reverse post order of the graph has been built.
4183 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4184 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004185
4186 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004187 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004188 void Advance() { --index_; }
4189
4190 private:
4191 const HGraph& graph_;
4192 size_t index_;
4193
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004194 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004195};
4196
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004197class HLinearPostOrderIterator : public ValueObject {
4198 public:
4199 explicit HLinearPostOrderIterator(const HGraph& graph)
4200 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4201
4202 bool Done() const { return index_ == 0; }
4203
4204 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4205
4206 void Advance() {
4207 --index_;
4208 DCHECK_GE(index_, 0U);
4209 }
4210
4211 private:
4212 const GrowableArray<HBasicBlock*>& order_;
4213 size_t index_;
4214
4215 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4216};
4217
4218class HLinearOrderIterator : public ValueObject {
4219 public:
4220 explicit HLinearOrderIterator(const HGraph& graph)
4221 : order_(graph.GetLinearOrder()), index_(0) {}
4222
4223 bool Done() const { return index_ == order_.Size(); }
4224 HBasicBlock* Current() const { return order_.Get(index_); }
4225 void Advance() { ++index_; }
4226
4227 private:
4228 const GrowableArray<HBasicBlock*>& order_;
4229 size_t index_;
4230
4231 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4232};
4233
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004234// Iterator over the blocks that art part of the loop. Includes blocks part
4235// of an inner loop. The order in which the blocks are iterated is on their
4236// block id.
4237class HBlocksInLoopIterator : public ValueObject {
4238 public:
4239 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4240 : blocks_in_loop_(info.GetBlocks()),
4241 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4242 index_(0) {
4243 if (!blocks_in_loop_.IsBitSet(index_)) {
4244 Advance();
4245 }
4246 }
4247
4248 bool Done() const { return index_ == blocks_.Size(); }
4249 HBasicBlock* Current() const { return blocks_.Get(index_); }
4250 void Advance() {
4251 ++index_;
4252 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4253 if (blocks_in_loop_.IsBitSet(index_)) {
4254 break;
4255 }
4256 }
4257 }
4258
4259 private:
4260 const BitVector& blocks_in_loop_;
4261 const GrowableArray<HBasicBlock*>& blocks_;
4262 size_t index_;
4263
4264 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4265};
4266
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004267inline int64_t Int64FromConstant(HConstant* constant) {
4268 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4269 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4270 : constant->AsLongConstant()->GetValue();
4271}
4272
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004273} // namespace art
4274
4275#endif // ART_COMPILER_OPTIMIZING_NODES_H_