blob: c5fc5637a0ee079227bf84c74fb5d4335d0f6cb7 [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
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100827#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
828 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
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100897#define FOR_EACH_INSTRUCTION(M) \
898 FOR_EACH_CONCRETE_INSTRUCTION(M) \
899 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100900 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100901 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100902 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700903
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100904#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000905FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
906#undef FORWARD_DECLARATION
907
Roland Levillainccc07a92014-09-16 14:48:16 +0100908#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000909 InstructionKind GetKind() const OVERRIDE { return k##type; } \
910 const char* DebugName() const OVERRIDE { return #type; } \
911 const H##type* As##type() const OVERRIDE { return this; } \
912 H##type* As##type() OVERRIDE { return this; } \
913 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100914 return other->Is##type(); \
915 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000916 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000917
David Brazdiled596192015-01-23 10:39:45 +0000918template <typename T> class HUseList;
919
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100920template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700921class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000922 public:
David Brazdiled596192015-01-23 10:39:45 +0000923 HUseListNode* GetPrevious() const { return prev_; }
924 HUseListNode* GetNext() const { return next_; }
925 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100926 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100927 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100928
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000929 private:
David Brazdiled596192015-01-23 10:39:45 +0000930 HUseListNode(T user, size_t index)
931 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
932
933 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100934 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000935 HUseListNode<T>* prev_;
936 HUseListNode<T>* next_;
937
938 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000939
940 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
941};
942
David Brazdiled596192015-01-23 10:39:45 +0000943template <typename T>
944class HUseList : public ValueObject {
945 public:
946 HUseList() : first_(nullptr) {}
947
948 void Clear() {
949 first_ = nullptr;
950 }
951
952 // Adds a new entry at the beginning of the use list and returns
953 // the newly created node.
954 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000955 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000956 if (IsEmpty()) {
957 first_ = new_node;
958 } else {
959 first_->prev_ = new_node;
960 new_node->next_ = first_;
961 first_ = new_node;
962 }
963 return new_node;
964 }
965
966 HUseListNode<T>* GetFirst() const {
967 return first_;
968 }
969
970 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000971 DCHECK(node != nullptr);
972 DCHECK(Contains(node));
973
David Brazdiled596192015-01-23 10:39:45 +0000974 if (node->prev_ != nullptr) {
975 node->prev_->next_ = node->next_;
976 }
977 if (node->next_ != nullptr) {
978 node->next_->prev_ = node->prev_;
979 }
980 if (node == first_) {
981 first_ = node->next_;
982 }
983 }
984
David Brazdil1abb4192015-02-17 18:33:36 +0000985 bool Contains(const HUseListNode<T>* node) const {
986 if (node == nullptr) {
987 return false;
988 }
989 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
990 if (current == node) {
991 return true;
992 }
993 }
994 return false;
995 }
996
David Brazdiled596192015-01-23 10:39:45 +0000997 bool IsEmpty() const {
998 return first_ == nullptr;
999 }
1000
1001 bool HasOnlyOneUse() const {
1002 return first_ != nullptr && first_->next_ == nullptr;
1003 }
1004
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001005 size_t SizeSlow() const {
1006 size_t count = 0;
1007 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1008 ++count;
1009 }
1010 return count;
1011 }
1012
David Brazdiled596192015-01-23 10:39:45 +00001013 private:
1014 HUseListNode<T>* first_;
1015};
1016
1017template<typename T>
1018class HUseIterator : public ValueObject {
1019 public:
1020 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1021
1022 bool Done() const { return current_ == nullptr; }
1023
1024 void Advance() {
1025 DCHECK(!Done());
1026 current_ = current_->GetNext();
1027 }
1028
1029 HUseListNode<T>* Current() const {
1030 DCHECK(!Done());
1031 return current_;
1032 }
1033
1034 private:
1035 HUseListNode<T>* current_;
1036
1037 friend class HValue;
1038};
1039
David Brazdil1abb4192015-02-17 18:33:36 +00001040// This class is used by HEnvironment and HInstruction classes to record the
1041// instructions they use and pointers to the corresponding HUseListNodes kept
1042// by the used instructions.
1043template <typename T>
1044class HUserRecord : public ValueObject {
1045 public:
1046 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1047 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1048
1049 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1050 : instruction_(old_record.instruction_), use_node_(use_node) {
1051 DCHECK(instruction_ != nullptr);
1052 DCHECK(use_node_ != nullptr);
1053 DCHECK(old_record.use_node_ == nullptr);
1054 }
1055
1056 HInstruction* GetInstruction() const { return instruction_; }
1057 HUseListNode<T>* GetUseNode() const { return use_node_; }
1058
1059 private:
1060 // Instruction used by the user.
1061 HInstruction* instruction_;
1062
1063 // Corresponding entry in the use list kept by 'instruction_'.
1064 HUseListNode<T>* use_node_;
1065};
1066
Calin Juravle27df7582015-04-17 19:12:31 +01001067// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1068// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1069// flag is consider.
1070// - DependsOn suggests that there is a real dependency between side effects but it only
1071// checks DependendsOnSomething flag.
1072//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001073// Represents the side effects an instruction may have.
1074class SideEffects : public ValueObject {
1075 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001076 SideEffects() : flags_(0) {}
1077
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001078 static SideEffects None() {
1079 return SideEffects(0);
1080 }
1081
1082 static SideEffects All() {
1083 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1084 }
1085
1086 static SideEffects ChangesSomething() {
1087 return SideEffects((1 << kFlagChangesCount) - 1);
1088 }
1089
1090 static SideEffects DependsOnSomething() {
1091 int count = kFlagDependsOnCount - kFlagChangesCount;
1092 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1093 }
1094
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001095 SideEffects Union(SideEffects other) const {
1096 return SideEffects(flags_ | other.flags_);
1097 }
1098
Roland Levillain72bceff2014-09-15 18:29:00 +01001099 bool HasSideEffects() const {
1100 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1101 return (flags_ & all_bits_set) != 0;
1102 }
1103
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001104 bool HasAllSideEffects() const {
1105 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1106 return all_bits_set == (flags_ & all_bits_set);
1107 }
1108
1109 bool DependsOn(SideEffects other) const {
1110 size_t depends_flags = other.ComputeDependsFlags();
1111 return (flags_ & depends_flags) != 0;
1112 }
1113
1114 bool HasDependencies() const {
1115 int count = kFlagDependsOnCount - kFlagChangesCount;
1116 size_t all_bits_set = (1 << count) - 1;
1117 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1118 }
1119
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001120 private:
1121 static constexpr int kFlagChangesSomething = 0;
1122 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1123
1124 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1125 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1126
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001127 explicit SideEffects(size_t flags) : flags_(flags) {}
1128
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001129 size_t ComputeDependsFlags() const {
1130 return flags_ << kFlagChangesCount;
1131 }
1132
1133 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001134};
1135
David Brazdiled596192015-01-23 10:39:45 +00001136// A HEnvironment object contains the values of virtual registers at a given location.
1137class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1138 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001139 HEnvironment(ArenaAllocator* arena,
1140 size_t number_of_vregs,
1141 const DexFile& dex_file,
1142 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001143 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001144 InvokeType invoke_type,
1145 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001146 : vregs_(arena, number_of_vregs),
1147 locations_(arena, number_of_vregs),
1148 parent_(nullptr),
1149 dex_file_(dex_file),
1150 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001151 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001152 invoke_type_(invoke_type),
1153 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001154 vregs_.SetSize(number_of_vregs);
1155 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001156 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001157 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001158
1159 locations_.SetSize(number_of_vregs);
1160 for (size_t i = 0; i < number_of_vregs; ++i) {
1161 locations_.Put(i, Location());
1162 }
1163 }
1164
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001165 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001166 : HEnvironment(arena,
1167 to_copy.Size(),
1168 to_copy.GetDexFile(),
1169 to_copy.GetMethodIdx(),
1170 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001171 to_copy.GetInvokeType(),
1172 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001173
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001174 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001175 if (parent_ != nullptr) {
1176 parent_->SetAndCopyParentChain(allocator, parent);
1177 } else {
1178 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1179 parent_->CopyFrom(parent);
1180 if (parent->GetParent() != nullptr) {
1181 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1182 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001183 }
David Brazdiled596192015-01-23 10:39:45 +00001184 }
1185
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001186 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1187 void CopyFrom(HEnvironment* environment);
1188
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001189 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1190 // input to the loop phi instead. This is for inserting instructions that
1191 // require an environment (like HDeoptimization) in the loop pre-header.
1192 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001193
1194 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001195 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001196 }
1197
1198 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001199 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001200 }
1201
David Brazdil1abb4192015-02-17 18:33:36 +00001202 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001203
1204 size_t Size() const { return vregs_.Size(); }
1205
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001206 HEnvironment* GetParent() const { return parent_; }
1207
1208 void SetLocationAt(size_t index, Location location) {
1209 locations_.Put(index, location);
1210 }
1211
1212 Location GetLocationAt(size_t index) const {
1213 return locations_.Get(index);
1214 }
1215
1216 uint32_t GetDexPc() const {
1217 return dex_pc_;
1218 }
1219
1220 uint32_t GetMethodIdx() const {
1221 return method_idx_;
1222 }
1223
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001224 InvokeType GetInvokeType() const {
1225 return invoke_type_;
1226 }
1227
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001228 const DexFile& GetDexFile() const {
1229 return dex_file_;
1230 }
1231
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001232 HInstruction* GetHolder() const {
1233 return holder_;
1234 }
1235
David Brazdiled596192015-01-23 10:39:45 +00001236 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001237 // Record instructions' use entries of this environment for constant-time removal.
1238 // It should only be called by HInstruction when a new environment use is added.
1239 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1240 DCHECK(env_use->GetUser() == this);
1241 size_t index = env_use->GetIndex();
1242 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1243 }
David Brazdiled596192015-01-23 10:39:45 +00001244
David Brazdil1abb4192015-02-17 18:33:36 +00001245 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001246 GrowableArray<Location> locations_;
1247 HEnvironment* parent_;
1248 const DexFile& dex_file_;
1249 const uint32_t method_idx_;
1250 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001251 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001252
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001253 // The instruction that holds this environment. Only used in debug mode
1254 // to ensure the graph is consistent.
1255 HInstruction* const holder_;
1256
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001257 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001258
1259 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1260};
1261
Calin Juravleacf735c2015-02-12 15:25:22 +00001262class ReferenceTypeInfo : ValueObject {
1263 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001264 typedef Handle<mirror::Class> TypeHandle;
1265
1266 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1267 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1268 if (type_handle->IsObjectClass()) {
1269 // Override the type handle to be consistent with the case when we get to
1270 // Top but don't have the Object class available. It avoids having to guess
1271 // what value the type_handle has when it's Top.
1272 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1273 } else {
1274 return ReferenceTypeInfo(type_handle, is_exact, false);
1275 }
1276 }
1277
1278 static ReferenceTypeInfo CreateTop(bool is_exact) {
1279 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001280 }
1281
1282 bool IsExact() const { return is_exact_; }
1283 bool IsTop() const { return is_top_; }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001284 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1285 return !IsTop() && GetTypeHandle()->IsInterface();
1286 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001287
1288 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1289
Calin Juravleb1498f62015-02-16 13:13:29 +00001290 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001291 if (IsTop()) {
1292 // Top (equivalent for java.lang.Object) is supertype of anything.
1293 return true;
1294 }
1295 if (rti.IsTop()) {
1296 // If we get here `this` is not Top() so it can't be a supertype.
1297 return false;
1298 }
1299 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1300 }
1301
1302 // Returns true if the type information provide the same amount of details.
1303 // Note that it does not mean that the instructions have the same actual type
1304 // (e.g. tops are equal but they can be the result of a merge).
1305 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1306 if (IsExact() != rti.IsExact()) {
1307 return false;
1308 }
1309 if (IsTop() && rti.IsTop()) {
1310 // `Top` means java.lang.Object, so the types are equivalent.
1311 return true;
1312 }
1313 if (IsTop() || rti.IsTop()) {
1314 // If only one is top or object than they are not equivalent.
1315 // NB: We need this extra check because the type_handle of `Top` is invalid
1316 // and we cannot inspect its reference.
1317 return false;
1318 }
1319
1320 // Finally check the types.
1321 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1322 }
1323
1324 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001325 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1326 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1327 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1328
Calin Juravleacf735c2015-02-12 15:25:22 +00001329 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001330 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001331 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001332 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001333 bool is_exact_;
1334 // A true value here means that the object type should be java.lang.Object.
1335 // We don't have access to the corresponding mirror object every time so this
1336 // flag acts as a substitute. When true, the TypeHandle refers to a null
1337 // pointer and should not be used.
1338 bool is_top_;
1339};
1340
1341std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1342
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001343class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001344 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001345 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001346 : previous_(nullptr),
1347 next_(nullptr),
1348 block_(nullptr),
1349 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001350 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001351 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001352 locations_(nullptr),
1353 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001354 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001355 side_effects_(side_effects),
1356 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001357
Dave Allison20dfc792014-06-16 20:44:29 -07001358 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001359
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001360#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001361 enum InstructionKind {
1362 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1363 };
1364#undef DECLARE_KIND
1365
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001366 HInstruction* GetNext() const { return next_; }
1367 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001368
Calin Juravle77520bc2015-01-12 18:45:46 +00001369 HInstruction* GetNextDisregardingMoves() const;
1370 HInstruction* GetPreviousDisregardingMoves() const;
1371
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001372 HBasicBlock* GetBlock() const { return block_; }
1373 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001374 bool IsInBlock() const { return block_ != nullptr; }
1375 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001376 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001377
Roland Levillain6b879dd2014-09-22 17:13:44 +01001378 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001379 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001380
1381 virtual void Accept(HGraphVisitor* visitor) = 0;
1382 virtual const char* DebugName() const = 0;
1383
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001384 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001385 void SetRawInputAt(size_t index, HInstruction* input) {
1386 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1387 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001388
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001389 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001390 virtual uint32_t GetDexPc() const {
1391 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1392 " does not need an environment";
1393 UNREACHABLE();
1394 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001395 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001396 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001397 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001398
Calin Juravle10e244f2015-01-26 18:54:32 +00001399 // Does not apply for all instructions, but having this at top level greatly
1400 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001401 virtual bool CanBeNull() const {
1402 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1403 return true;
1404 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001405
Calin Juravle641547a2015-04-21 22:08:51 +01001406 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1407 UNUSED(obj);
1408 return false;
1409 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001410
Calin Juravleacf735c2015-02-12 15:25:22 +00001411 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001412 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001413 reference_type_info_ = reference_type_info;
1414 }
1415
Calin Juravle61d544b2015-02-23 16:46:57 +00001416 ReferenceTypeInfo GetReferenceTypeInfo() const {
1417 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1418 return reference_type_info_;
1419 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001420
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001421 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001422 DCHECK(user != nullptr);
1423 HUseListNode<HInstruction*>* use =
1424 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1425 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001426 }
1427
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001428 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001429 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001430 HUseListNode<HEnvironment*>* env_use =
1431 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1432 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001433 }
1434
David Brazdil1abb4192015-02-17 18:33:36 +00001435 void RemoveAsUserOfInput(size_t input) {
1436 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1437 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1438 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001439
David Brazdil1abb4192015-02-17 18:33:36 +00001440 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1441 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001442
David Brazdiled596192015-01-23 10:39:45 +00001443 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1444 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001445 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001446 bool HasOnlyOneNonEnvironmentUse() const {
1447 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1448 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001449
Roland Levillain6c82d402014-10-13 16:10:27 +01001450 // Does this instruction strictly dominate `other_instruction`?
1451 // Returns false if this instruction and `other_instruction` are the same.
1452 // Aborts if this instruction and `other_instruction` are both phis.
1453 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001454
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001455 int GetId() const { return id_; }
1456 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001457
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001458 int GetSsaIndex() const { return ssa_index_; }
1459 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1460 bool HasSsaIndex() const { return ssa_index_ != -1; }
1461
1462 bool HasEnvironment() const { return environment_ != nullptr; }
1463 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001464 // Set the `environment_` field. Raw because this method does not
1465 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001466 void SetRawEnvironment(HEnvironment* environment) {
1467 DCHECK(environment_ == nullptr);
1468 DCHECK_EQ(environment->GetHolder(), this);
1469 environment_ = environment;
1470 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001471
1472 // Set the environment of this instruction, copying it from `environment`. While
1473 // copying, the uses lists are being updated.
1474 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001475 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001476 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001477 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001478 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001479 if (environment->GetParent() != nullptr) {
1480 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1481 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001482 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001483
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001484 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1485 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001486 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001487 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001488 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001489 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001490 if (environment->GetParent() != nullptr) {
1491 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1492 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001493 }
1494
Nicolas Geoffray39468442014-09-02 15:17:15 +01001495 // Returns the number of entries in the environment. Typically, that is the
1496 // number of dex registers in a method. It could be more in case of inlining.
1497 size_t EnvironmentSize() const;
1498
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001499 LocationSummary* GetLocations() const { return locations_; }
1500 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001501
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001502 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001503 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001504
Alexandre Rames188d4312015-04-09 18:30:21 +01001505 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1506 // uses of this instruction by `other` are *not* updated.
1507 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1508 ReplaceWith(other);
1509 other->ReplaceInput(this, use_index);
1510 }
1511
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001512 // Move `this` instruction before `cursor`.
1513 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001514
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001515#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001516 bool Is##type() const { return (As##type() != nullptr); } \
1517 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001518 virtual H##type* As##type() { return nullptr; }
1519
1520 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1521#undef INSTRUCTION_TYPE_CHECK
1522
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001523 // Returns whether the instruction can be moved within the graph.
1524 virtual bool CanBeMoved() const { return false; }
1525
1526 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001527 virtual bool InstructionTypeEquals(HInstruction* other) const {
1528 UNUSED(other);
1529 return false;
1530 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001531
1532 // Returns whether any data encoded in the two instructions is equal.
1533 // This method does not look at the inputs. Both instructions must be
1534 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001535 virtual bool InstructionDataEquals(HInstruction* other) const {
1536 UNUSED(other);
1537 return false;
1538 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001539
1540 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001541 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001542 // 2) Their inputs are identical.
1543 bool Equals(HInstruction* other) const;
1544
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001545 virtual InstructionKind GetKind() const = 0;
1546
1547 virtual size_t ComputeHashCode() const {
1548 size_t result = GetKind();
1549 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1550 result = (result * 31) + InputAt(i)->GetId();
1551 }
1552 return result;
1553 }
1554
1555 SideEffects GetSideEffects() const { return side_effects_; }
1556
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001557 size_t GetLifetimePosition() const { return lifetime_position_; }
1558 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1559 LiveInterval* GetLiveInterval() const { return live_interval_; }
1560 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1561 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1562
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001563 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1564
1565 // Returns whether the code generation of the instruction will require to have access
1566 // to the current method. Such instructions are:
1567 // (1): Instructions that require an environment, as calling the runtime requires
1568 // to walk the stack and have the current method stored at a specific stack address.
1569 // (2): Object literals like classes and strings, that are loaded from the dex cache
1570 // fields of the current method.
1571 bool NeedsCurrentMethod() const {
1572 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1573 }
1574
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001575 virtual bool NeedsDexCache() const { return false; }
1576
David Brazdil1abb4192015-02-17 18:33:36 +00001577 protected:
1578 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1579 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1580
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001581 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001582 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1583
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001584 HInstruction* previous_;
1585 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001586 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001587
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001588 // An instruction gets an id when it is added to the graph.
1589 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001590 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001591 int id_;
1592
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001593 // When doing liveness analysis, instructions that have uses get an SSA index.
1594 int ssa_index_;
1595
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001596 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001597 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001598
1599 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001600 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001601
Nicolas Geoffray39468442014-09-02 15:17:15 +01001602 // The environment associated with this instruction. Not null if the instruction
1603 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001604 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001605
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001606 // Set by the code generator.
1607 LocationSummary* locations_;
1608
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001609 // Set by the liveness analysis.
1610 LiveInterval* live_interval_;
1611
1612 // Set by the liveness analysis, this is the position in a linear
1613 // order of blocks where this instruction's live interval start.
1614 size_t lifetime_position_;
1615
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001616 const SideEffects side_effects_;
1617
Calin Juravleacf735c2015-02-12 15:25:22 +00001618 // TODO: for primitive types this should be marked as invalid.
1619 ReferenceTypeInfo reference_type_info_;
1620
David Brazdil1abb4192015-02-17 18:33:36 +00001621 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001622 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001623 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001624 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001625 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001626
1627 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1628};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001629std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001630
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001631class HInputIterator : public ValueObject {
1632 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001633 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001634
1635 bool Done() const { return index_ == instruction_->InputCount(); }
1636 HInstruction* Current() const { return instruction_->InputAt(index_); }
1637 void Advance() { index_++; }
1638
1639 private:
1640 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001641 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001642
1643 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1644};
1645
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001646class HInstructionIterator : public ValueObject {
1647 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001648 explicit HInstructionIterator(const HInstructionList& instructions)
1649 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001650 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001651 }
1652
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001653 bool Done() const { return instruction_ == nullptr; }
1654 HInstruction* Current() const { return instruction_; }
1655 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001656 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001657 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001658 }
1659
1660 private:
1661 HInstruction* instruction_;
1662 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001663
1664 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001665};
1666
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001667class HBackwardInstructionIterator : public ValueObject {
1668 public:
1669 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1670 : instruction_(instructions.last_instruction_) {
1671 next_ = Done() ? nullptr : instruction_->GetPrevious();
1672 }
1673
1674 bool Done() const { return instruction_ == nullptr; }
1675 HInstruction* Current() const { return instruction_; }
1676 void Advance() {
1677 instruction_ = next_;
1678 next_ = Done() ? nullptr : instruction_->GetPrevious();
1679 }
1680
1681 private:
1682 HInstruction* instruction_;
1683 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001684
1685 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001686};
1687
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001688// An embedded container with N elements of type T. Used (with partial
1689// specialization for N=0) because embedded arrays cannot have size 0.
1690template<typename T, intptr_t N>
1691class EmbeddedArray {
1692 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001693 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001694
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001695 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001696
1697 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001698 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001699 return elements_[i];
1700 }
1701
1702 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001703 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001704 return elements_[i];
1705 }
1706
1707 const T& At(intptr_t i) const {
1708 return (*this)[i];
1709 }
1710
1711 void SetAt(intptr_t i, const T& val) {
1712 (*this)[i] = val;
1713 }
1714
1715 private:
1716 T elements_[N];
1717};
1718
1719template<typename T>
1720class EmbeddedArray<T, 0> {
1721 public:
1722 intptr_t length() const { return 0; }
1723 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001724 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001725 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001726 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001727 }
1728 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001729 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001730 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001731 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001732 }
1733};
1734
1735template<intptr_t N>
1736class HTemplateInstruction: public HInstruction {
1737 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001738 HTemplateInstruction<N>(SideEffects side_effects)
1739 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001740 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001741
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001742 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001743
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001744 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001745 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1746
1747 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1748 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001749 }
1750
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001751 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001752 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001753
1754 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001755};
1756
Dave Allison20dfc792014-06-16 20:44:29 -07001757template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001758class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001759 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001760 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1761 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001762 virtual ~HExpression() {}
1763
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001764 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001765
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001766 protected:
1767 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001768};
1769
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001770// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1771// instruction that branches to the exit block.
1772class HReturnVoid : public HTemplateInstruction<0> {
1773 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001774 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001775
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001776 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001777
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001778 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001779
1780 private:
1781 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1782};
1783
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001784// Represents dex's RETURN opcodes. A HReturn is a control flow
1785// instruction that branches to the exit block.
1786class HReturn : public HTemplateInstruction<1> {
1787 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001788 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001789 SetRawInputAt(0, value);
1790 }
1791
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001792 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001793
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001794 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001795
1796 private:
1797 DISALLOW_COPY_AND_ASSIGN(HReturn);
1798};
1799
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001800// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001801// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001802// exit block.
1803class HExit : public HTemplateInstruction<0> {
1804 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001805 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001806
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001807 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001808
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001809 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001810
1811 private:
1812 DISALLOW_COPY_AND_ASSIGN(HExit);
1813};
1814
1815// Jumps from one block to another.
1816class HGoto : public HTemplateInstruction<0> {
1817 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001818 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1819
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001820 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001821
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001822 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001823 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001824 }
1825
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001826 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001827
1828 private:
1829 DISALLOW_COPY_AND_ASSIGN(HGoto);
1830};
1831
Dave Allison20dfc792014-06-16 20:44:29 -07001832
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001833// Conditional branch. A block ending with an HIf instruction must have
1834// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001835class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001836 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001837 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001838 SetRawInputAt(0, input);
1839 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001840
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001841 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001842
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001843 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001844 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001845 }
1846
1847 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001848 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001849 }
1850
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001851 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001852
1853 private:
1854 DISALLOW_COPY_AND_ASSIGN(HIf);
1855};
1856
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001857// Deoptimize to interpreter, upon checking a condition.
1858class HDeoptimize : public HTemplateInstruction<1> {
1859 public:
1860 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1861 : HTemplateInstruction(SideEffects::None()),
1862 dex_pc_(dex_pc) {
1863 SetRawInputAt(0, cond);
1864 }
1865
1866 bool NeedsEnvironment() const OVERRIDE { return true; }
1867 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001868 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001869
1870 DECLARE_INSTRUCTION(Deoptimize);
1871
1872 private:
1873 uint32_t dex_pc_;
1874
1875 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1876};
1877
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001878// Represents the ArtMethod that was passed as a first argument to
1879// the method. It is used by instructions that depend on it, like
1880// instructions that work with the dex cache.
1881class HCurrentMethod : public HExpression<0> {
1882 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001883 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001884
1885 DECLARE_INSTRUCTION(CurrentMethod);
1886
1887 private:
1888 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
1889};
1890
Roland Levillain88cb1752014-10-20 16:36:47 +01001891class HUnaryOperation : public HExpression<1> {
1892 public:
1893 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1894 : HExpression(result_type, SideEffects::None()) {
1895 SetRawInputAt(0, input);
1896 }
1897
1898 HInstruction* GetInput() const { return InputAt(0); }
1899 Primitive::Type GetResultType() const { return GetType(); }
1900
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001901 bool CanBeMoved() const OVERRIDE { return true; }
1902 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001903 UNUSED(other);
1904 return true;
1905 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001906
Roland Levillain9240d6a2014-10-20 16:47:04 +01001907 // Try to statically evaluate `operation` and return a HConstant
1908 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001909 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001910 HConstant* TryStaticEvaluation() const;
1911
1912 // Apply this operation to `x`.
1913 virtual int32_t Evaluate(int32_t x) const = 0;
1914 virtual int64_t Evaluate(int64_t x) const = 0;
1915
Roland Levillain88cb1752014-10-20 16:36:47 +01001916 DECLARE_INSTRUCTION(UnaryOperation);
1917
1918 private:
1919 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1920};
1921
Dave Allison20dfc792014-06-16 20:44:29 -07001922class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001923 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001924 HBinaryOperation(Primitive::Type result_type,
1925 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001926 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001927 SetRawInputAt(0, left);
1928 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001929 }
1930
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001931 HInstruction* GetLeft() const { return InputAt(0); }
1932 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001933 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001934
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001935 virtual bool IsCommutative() const { return false; }
1936
1937 // Put constant on the right.
1938 // Returns whether order is changed.
1939 bool OrderInputsWithConstantOnTheRight() {
1940 HInstruction* left = InputAt(0);
1941 HInstruction* right = InputAt(1);
1942 if (left->IsConstant() && !right->IsConstant()) {
1943 ReplaceInput(right, 0);
1944 ReplaceInput(left, 1);
1945 return true;
1946 }
1947 return false;
1948 }
1949
1950 // Order inputs by instruction id, but favor constant on the right side.
1951 // This helps GVN for commutative ops.
1952 void OrderInputs() {
1953 DCHECK(IsCommutative());
1954 HInstruction* left = InputAt(0);
1955 HInstruction* right = InputAt(1);
1956 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1957 return;
1958 }
1959 if (OrderInputsWithConstantOnTheRight()) {
1960 return;
1961 }
1962 // Order according to instruction id.
1963 if (left->GetId() > right->GetId()) {
1964 ReplaceInput(right, 0);
1965 ReplaceInput(left, 1);
1966 }
1967 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001968
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001969 bool CanBeMoved() const OVERRIDE { return true; }
1970 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001971 UNUSED(other);
1972 return true;
1973 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001974
Roland Levillain9240d6a2014-10-20 16:47:04 +01001975 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001976 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001977 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001978 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001979
1980 // Apply this operation to `x` and `y`.
1981 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1982 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1983
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001984 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001985 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001986 HConstant* GetConstantRight() const;
1987
1988 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001989 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001990 HInstruction* GetLeastConstantLeft() const;
1991
Roland Levillainccc07a92014-09-16 14:48:16 +01001992 DECLARE_INSTRUCTION(BinaryOperation);
1993
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001994 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001995 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1996};
1997
Dave Allison20dfc792014-06-16 20:44:29 -07001998class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001999 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002000 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002001 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
2002 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002003
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002004 bool NeedsMaterialization() const { return needs_materialization_; }
2005 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002006
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002007 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002008 // `instruction`, and disregard moves in between.
2009 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002010
Dave Allison20dfc792014-06-16 20:44:29 -07002011 DECLARE_INSTRUCTION(Condition);
2012
2013 virtual IfCondition GetCondition() const = 0;
2014
2015 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002016 // For register allocation purposes, returns whether this instruction needs to be
2017 // materialized (that is, not just be in the processor flags).
2018 bool needs_materialization_;
2019
Dave Allison20dfc792014-06-16 20:44:29 -07002020 DISALLOW_COPY_AND_ASSIGN(HCondition);
2021};
2022
2023// Instruction to check if two inputs are equal to each other.
2024class HEqual : public HCondition {
2025 public:
2026 HEqual(HInstruction* first, HInstruction* second)
2027 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002028
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002029 bool IsCommutative() const OVERRIDE { return true; }
2030
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002031 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002032 return x == y ? 1 : 0;
2033 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002034 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002035 return x == y ? 1 : 0;
2036 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002037
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002038 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002039
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002040 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002041 return kCondEQ;
2042 }
2043
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002044 private:
2045 DISALLOW_COPY_AND_ASSIGN(HEqual);
2046};
2047
Dave Allison20dfc792014-06-16 20:44:29 -07002048class HNotEqual : public HCondition {
2049 public:
2050 HNotEqual(HInstruction* first, HInstruction* second)
2051 : HCondition(first, second) {}
2052
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002053 bool IsCommutative() const OVERRIDE { return true; }
2054
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002055 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002056 return x != y ? 1 : 0;
2057 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002058 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002059 return x != y ? 1 : 0;
2060 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002061
Dave Allison20dfc792014-06-16 20:44:29 -07002062 DECLARE_INSTRUCTION(NotEqual);
2063
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002064 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002065 return kCondNE;
2066 }
2067
2068 private:
2069 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2070};
2071
2072class HLessThan : public HCondition {
2073 public:
2074 HLessThan(HInstruction* first, HInstruction* second)
2075 : HCondition(first, second) {}
2076
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002077 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002078 return x < y ? 1 : 0;
2079 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002080 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002081 return x < y ? 1 : 0;
2082 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002083
Dave Allison20dfc792014-06-16 20:44:29 -07002084 DECLARE_INSTRUCTION(LessThan);
2085
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002086 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002087 return kCondLT;
2088 }
2089
2090 private:
2091 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2092};
2093
2094class HLessThanOrEqual : public HCondition {
2095 public:
2096 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2097 : HCondition(first, second) {}
2098
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002099 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002100 return x <= y ? 1 : 0;
2101 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002102 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002103 return x <= y ? 1 : 0;
2104 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002105
Dave Allison20dfc792014-06-16 20:44:29 -07002106 DECLARE_INSTRUCTION(LessThanOrEqual);
2107
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002108 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002109 return kCondLE;
2110 }
2111
2112 private:
2113 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2114};
2115
2116class HGreaterThan : public HCondition {
2117 public:
2118 HGreaterThan(HInstruction* first, HInstruction* second)
2119 : HCondition(first, second) {}
2120
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002121 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002122 return x > y ? 1 : 0;
2123 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002124 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002125 return x > y ? 1 : 0;
2126 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002127
Dave Allison20dfc792014-06-16 20:44:29 -07002128 DECLARE_INSTRUCTION(GreaterThan);
2129
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002130 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002131 return kCondGT;
2132 }
2133
2134 private:
2135 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2136};
2137
2138class HGreaterThanOrEqual : public HCondition {
2139 public:
2140 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2141 : HCondition(first, second) {}
2142
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002143 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002144 return x >= y ? 1 : 0;
2145 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002146 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002147 return x >= y ? 1 : 0;
2148 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002149
Dave Allison20dfc792014-06-16 20:44:29 -07002150 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2151
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002152 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002153 return kCondGE;
2154 }
2155
2156 private:
2157 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2158};
2159
2160
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002161// Instruction to check how two inputs compare to each other.
2162// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2163class HCompare : public HBinaryOperation {
2164 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002165 // The bias applies for floating point operations and indicates how NaN
2166 // comparisons are treated:
2167 enum Bias {
2168 kNoBias, // bias is not applicable (i.e. for long operation)
2169 kGtBias, // return 1 for NaN comparisons
2170 kLtBias, // return -1 for NaN comparisons
2171 };
2172
2173 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
2174 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002175 DCHECK_EQ(type, first->GetType());
2176 DCHECK_EQ(type, second->GetType());
2177 }
2178
Calin Juravleddb7df22014-11-25 20:56:51 +00002179 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002180 return
2181 x == y ? 0 :
2182 x > y ? 1 :
2183 -1;
2184 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002185
2186 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002187 return
2188 x == y ? 0 :
2189 x > y ? 1 :
2190 -1;
2191 }
2192
Calin Juravleddb7df22014-11-25 20:56:51 +00002193 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2194 return bias_ == other->AsCompare()->bias_;
2195 }
2196
2197 bool IsGtBias() { return bias_ == kGtBias; }
2198
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002199 DECLARE_INSTRUCTION(Compare);
2200
2201 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002202 const Bias bias_;
2203
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002204 DISALLOW_COPY_AND_ASSIGN(HCompare);
2205};
2206
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002207// A local in the graph. Corresponds to a Dex register.
2208class HLocal : public HTemplateInstruction<0> {
2209 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002210 explicit HLocal(uint16_t reg_number)
2211 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002212
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002213 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002214
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002215 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002216
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002217 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002218 // The Dex register number.
2219 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002220
2221 DISALLOW_COPY_AND_ASSIGN(HLocal);
2222};
2223
2224// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002225class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002226 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002227 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002228 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002229 SetRawInputAt(0, local);
2230 }
2231
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002232 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2233
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002234 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002235
2236 private:
2237 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2238};
2239
2240// Store a value in a given local. This instruction has two inputs: the value
2241// and the local.
2242class HStoreLocal : public HTemplateInstruction<2> {
2243 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002244 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002245 SetRawInputAt(0, local);
2246 SetRawInputAt(1, value);
2247 }
2248
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002249 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2250
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002251 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002252
2253 private:
2254 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2255};
2256
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002257class HConstant : public HExpression<0> {
2258 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002259 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2260
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002261 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002262
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002263 virtual bool IsMinusOne() const { return false; }
2264 virtual bool IsZero() const { return false; }
2265 virtual bool IsOne() const { return false; }
2266
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002267 DECLARE_INSTRUCTION(Constant);
2268
2269 private:
2270 DISALLOW_COPY_AND_ASSIGN(HConstant);
2271};
2272
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002273class HFloatConstant : public HConstant {
2274 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002275 float GetValue() const { return value_; }
2276
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002277 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002278 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2279 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002280 }
2281
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002282 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002283
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002284 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002285 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002286 }
2287 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002288 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002289 }
2290 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002291 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2292 }
2293 bool IsNaN() const {
2294 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002295 }
2296
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002297 DECLARE_INSTRUCTION(FloatConstant);
2298
2299 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002300 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002301 explicit HFloatConstant(int32_t value)
2302 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002303
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002304 const float value_;
2305
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002306 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002307 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002308 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002309 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2310};
2311
2312class HDoubleConstant : public HConstant {
2313 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002314 double GetValue() const { return value_; }
2315
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002316 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002317 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2318 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002319 }
2320
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002321 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002322
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002323 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002324 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002325 }
2326 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002327 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002328 }
2329 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002330 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2331 }
2332 bool IsNaN() const {
2333 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002334 }
2335
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002336 DECLARE_INSTRUCTION(DoubleConstant);
2337
2338 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002339 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002340 explicit HDoubleConstant(int64_t value)
2341 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002342
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002343 const double value_;
2344
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002345 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002346 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002347 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002348 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2349};
2350
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002351class HNullConstant : public HConstant {
2352 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002353 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2354 return true;
2355 }
2356
2357 size_t ComputeHashCode() const OVERRIDE { return 0; }
2358
2359 DECLARE_INSTRUCTION(NullConstant);
2360
2361 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002362 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2363
2364 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002365 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2366};
2367
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002368// Constants of the type int. Those can be from Dex instructions, or
2369// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002370class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002371 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002372 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002373
Calin Juravle61d544b2015-02-23 16:46:57 +00002374 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002375 return other->AsIntConstant()->value_ == value_;
2376 }
2377
Calin Juravle61d544b2015-02-23 16:46:57 +00002378 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2379
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002380 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2381 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2382 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2383
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002384 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002385
2386 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002387 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2388
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002389 const int32_t value_;
2390
David Brazdil8d5b8b22015-03-24 10:51:52 +00002391 friend class HGraph;
2392 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002393 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002394 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2395};
2396
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002397class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002398 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002399 int64_t GetValue() const { return value_; }
2400
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002401 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002402 return other->AsLongConstant()->value_ == value_;
2403 }
2404
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002405 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002406
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002407 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2408 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2409 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2410
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002411 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002412
2413 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002414 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2415
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002416 const int64_t value_;
2417
David Brazdil8d5b8b22015-03-24 10:51:52 +00002418 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002419 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2420};
2421
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002422enum class Intrinsics {
2423#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2424#include "intrinsics_list.h"
2425 kNone,
2426 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2427#undef INTRINSICS_LIST
2428#undef OPTIMIZING_INTRINSICS
2429};
2430std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2431
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002432class HInvoke : public HInstruction {
2433 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002434 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002435
2436 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2437 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002438 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002439
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002440 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002441 SetRawInputAt(index, argument);
2442 }
2443
Roland Levillain3e3d7332015-04-28 11:00:54 +01002444 // Return the number of arguments. This number can be lower than
2445 // the number of inputs returned by InputCount(), as some invoke
2446 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2447 // inputs at the end of their list of inputs.
2448 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2449
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002450 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002451
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002452 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002453
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002454 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002455 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002456
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002457 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2458
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002459 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002460 return intrinsic_;
2461 }
2462
2463 void SetIntrinsic(Intrinsics intrinsic) {
2464 intrinsic_ = intrinsic;
2465 }
2466
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002467 bool IsInlined() const {
2468 return GetEnvironment()->GetParent() != nullptr;
2469 }
2470
2471 bool CanThrow() const OVERRIDE { return true; }
2472
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002473 DECLARE_INSTRUCTION(Invoke);
2474
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002475 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002476 HInvoke(ArenaAllocator* arena,
2477 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002478 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002479 Primitive::Type return_type,
2480 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002481 uint32_t dex_method_index,
2482 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002483 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002484 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002485 inputs_(arena, number_of_arguments),
2486 return_type_(return_type),
2487 dex_pc_(dex_pc),
2488 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002489 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002490 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002491 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2492 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002493 }
2494
David Brazdil1abb4192015-02-17 18:33:36 +00002495 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2496 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2497 inputs_.Put(index, input);
2498 }
2499
Roland Levillain3e3d7332015-04-28 11:00:54 +01002500 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002501 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002502 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002503 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002504 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002505 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002506 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002507
2508 private:
2509 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2510};
2511
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002512class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002513 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002514 // Requirements of this method call regarding the class
2515 // initialization (clinit) check of its declaring class.
2516 enum class ClinitCheckRequirement {
2517 kNone, // Class already initialized.
2518 kExplicit, // Static call having explicit clinit check as last input.
2519 kImplicit, // Static call implicitly requiring a clinit check.
2520 };
2521
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002522 HInvokeStaticOrDirect(ArenaAllocator* arena,
2523 uint32_t number_of_arguments,
2524 Primitive::Type return_type,
2525 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002526 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002527 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002528 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002529 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002530 InvokeType invoke_type,
2531 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002532 : HInvoke(arena,
2533 number_of_arguments,
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002534 // There is one extra argument for the HCurrentMethod node, and
2535 // potentially one other if the clinit check is explicit.
2536 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 2u : 1u,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002537 return_type,
2538 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002539 dex_method_index,
2540 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002541 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002542 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002543 clinit_check_requirement_(clinit_check_requirement),
2544 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002545
Calin Juravle641547a2015-04-21 22:08:51 +01002546 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2547 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002548 // We access the method via the dex cache so we can't do an implicit null check.
2549 // TODO: for intrinsics we can generate implicit null checks.
2550 return false;
2551 }
2552
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002553 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002554 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002555 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002556 bool IsStringInit() const { return string_init_offset_ != 0; }
2557 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002558 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002559
Roland Levillain4c0eb422015-04-24 16:43:49 +01002560 // Is this instruction a call to a static method?
2561 bool IsStatic() const {
2562 return GetInvokeType() == kStatic;
2563 }
2564
Roland Levillain3e3d7332015-04-28 11:00:54 +01002565 // Remove the art::HLoadClass instruction set as last input by
2566 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2567 // the initial art::HClinitCheck instruction (only relevant for
2568 // static calls with explicit clinit check).
2569 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002570 DCHECK(IsStaticWithExplicitClinitCheck());
2571 size_t last_input_index = InputCount() - 1;
2572 HInstruction* last_input = InputAt(last_input_index);
2573 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002574 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002575 RemoveAsUserOfInput(last_input_index);
2576 inputs_.DeleteAt(last_input_index);
2577 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2578 DCHECK(IsStaticWithImplicitClinitCheck());
2579 }
2580
2581 // Is this a call to a static method whose declaring class has an
2582 // explicit intialization check in the graph?
2583 bool IsStaticWithExplicitClinitCheck() const {
2584 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2585 }
2586
2587 // Is this a call to a static method whose declaring class has an
2588 // implicit intialization check requirement?
2589 bool IsStaticWithImplicitClinitCheck() const {
2590 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2591 }
2592
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002593 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002594
Roland Levillain4c0eb422015-04-24 16:43:49 +01002595 protected:
2596 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2597 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2598 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2599 HInstruction* input = input_record.GetInstruction();
2600 // `input` is the last input of a static invoke marked as having
2601 // an explicit clinit check. It must either be:
2602 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2603 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2604 DCHECK(input != nullptr);
2605 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2606 }
2607 return input_record;
2608 }
2609
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002610 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002611 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002612 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002613 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002614 // Thread entrypoint offset for string init method if this is a string init invoke.
2615 // Note that there are multiple string init methods, each having its own offset.
2616 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002617
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002618 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002619};
2620
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002621class HInvokeVirtual : public HInvoke {
2622 public:
2623 HInvokeVirtual(ArenaAllocator* arena,
2624 uint32_t number_of_arguments,
2625 Primitive::Type return_type,
2626 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002627 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002628 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002629 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002630 vtable_index_(vtable_index) {}
2631
Calin Juravle641547a2015-04-21 22:08:51 +01002632 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002633 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002634 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002635 }
2636
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002637 uint32_t GetVTableIndex() const { return vtable_index_; }
2638
2639 DECLARE_INSTRUCTION(InvokeVirtual);
2640
2641 private:
2642 const uint32_t vtable_index_;
2643
2644 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2645};
2646
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002647class HInvokeInterface : public HInvoke {
2648 public:
2649 HInvokeInterface(ArenaAllocator* arena,
2650 uint32_t number_of_arguments,
2651 Primitive::Type return_type,
2652 uint32_t dex_pc,
2653 uint32_t dex_method_index,
2654 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002655 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002656 imt_index_(imt_index) {}
2657
Calin Juravle641547a2015-04-21 22:08:51 +01002658 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002659 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002660 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002661 }
2662
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002663 uint32_t GetImtIndex() const { return imt_index_; }
2664 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2665
2666 DECLARE_INSTRUCTION(InvokeInterface);
2667
2668 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002669 const uint32_t imt_index_;
2670
2671 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2672};
2673
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002674class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002675 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002676 HNewInstance(HCurrentMethod* current_method,
2677 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002678 uint16_t type_index,
2679 const DexFile& dex_file,
2680 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002681 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2682 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002683 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002684 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002685 entrypoint_(entrypoint) {
2686 SetRawInputAt(0, current_method);
2687 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002688
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002689 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002690 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002691 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002692
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002693 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002694 bool NeedsEnvironment() const OVERRIDE { return true; }
2695 // It may throw when called on:
2696 // - interfaces
2697 // - abstract/innaccessible/unknown classes
2698 // TODO: optimize when possible.
2699 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002700
Calin Juravle10e244f2015-01-26 18:54:32 +00002701 bool CanBeNull() const OVERRIDE { return false; }
2702
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002703 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2704
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002705 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002706
2707 private:
2708 const uint32_t dex_pc_;
2709 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002710 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002711 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002712
2713 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2714};
2715
Roland Levillain88cb1752014-10-20 16:36:47 +01002716class HNeg : public HUnaryOperation {
2717 public:
2718 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2719 : HUnaryOperation(result_type, input) {}
2720
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002721 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2722 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002723
Roland Levillain88cb1752014-10-20 16:36:47 +01002724 DECLARE_INSTRUCTION(Neg);
2725
2726 private:
2727 DISALLOW_COPY_AND_ASSIGN(HNeg);
2728};
2729
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002730class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002731 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002732 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002733 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002734 uint32_t dex_pc,
2735 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002736 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002737 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002738 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2739 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002740 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002741 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002742 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002743 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002744 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002745 }
2746
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002747 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002748 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002749 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002750
2751 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002752 bool NeedsEnvironment() const OVERRIDE { return true; }
2753
Mingyao Yang0c365e62015-03-31 15:09:29 -07002754 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2755 bool CanThrow() const OVERRIDE { return true; }
2756
Calin Juravle10e244f2015-01-26 18:54:32 +00002757 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002758
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002759 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2760
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002761 DECLARE_INSTRUCTION(NewArray);
2762
2763 private:
2764 const uint32_t dex_pc_;
2765 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002766 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002767 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002768
2769 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2770};
2771
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002772class HAdd : public HBinaryOperation {
2773 public:
2774 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2775 : HBinaryOperation(result_type, left, right) {}
2776
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002777 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002778
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002779 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002780 return x + y;
2781 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002782 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002783 return x + y;
2784 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002785
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002786 DECLARE_INSTRUCTION(Add);
2787
2788 private:
2789 DISALLOW_COPY_AND_ASSIGN(HAdd);
2790};
2791
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002792class HSub : public HBinaryOperation {
2793 public:
2794 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2795 : HBinaryOperation(result_type, left, right) {}
2796
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002797 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002798 return x - y;
2799 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002800 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002801 return x - y;
2802 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002803
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002804 DECLARE_INSTRUCTION(Sub);
2805
2806 private:
2807 DISALLOW_COPY_AND_ASSIGN(HSub);
2808};
2809
Calin Juravle34bacdf2014-10-07 20:23:36 +01002810class HMul : public HBinaryOperation {
2811 public:
2812 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2813 : HBinaryOperation(result_type, left, right) {}
2814
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002815 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002816
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002817 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2818 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002819
2820 DECLARE_INSTRUCTION(Mul);
2821
2822 private:
2823 DISALLOW_COPY_AND_ASSIGN(HMul);
2824};
2825
Calin Juravle7c4954d2014-10-28 16:57:40 +00002826class HDiv : public HBinaryOperation {
2827 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002828 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2829 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002830
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002831 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002832 // Our graph structure ensures we never have 0 for `y` during constant folding.
2833 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002834 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002835 return (y == -1) ? -x : x / y;
2836 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002837
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002838 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002839 DCHECK_NE(y, 0);
2840 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2841 return (y == -1) ? -x : x / y;
2842 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002843
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002844 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002845
Calin Juravle7c4954d2014-10-28 16:57:40 +00002846 DECLARE_INSTRUCTION(Div);
2847
2848 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002849 const uint32_t dex_pc_;
2850
Calin Juravle7c4954d2014-10-28 16:57:40 +00002851 DISALLOW_COPY_AND_ASSIGN(HDiv);
2852};
2853
Calin Juravlebacfec32014-11-14 15:54:36 +00002854class HRem : public HBinaryOperation {
2855 public:
2856 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2857 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2858
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002859 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002860 DCHECK_NE(y, 0);
2861 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2862 return (y == -1) ? 0 : x % y;
2863 }
2864
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002865 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002866 DCHECK_NE(y, 0);
2867 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2868 return (y == -1) ? 0 : x % y;
2869 }
2870
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002871 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002872
2873 DECLARE_INSTRUCTION(Rem);
2874
2875 private:
2876 const uint32_t dex_pc_;
2877
2878 DISALLOW_COPY_AND_ASSIGN(HRem);
2879};
2880
Calin Juravled0d48522014-11-04 16:40:20 +00002881class HDivZeroCheck : public HExpression<1> {
2882 public:
2883 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2884 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2885 SetRawInputAt(0, value);
2886 }
2887
2888 bool CanBeMoved() const OVERRIDE { return true; }
2889
2890 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2891 UNUSED(other);
2892 return true;
2893 }
2894
2895 bool NeedsEnvironment() const OVERRIDE { return true; }
2896 bool CanThrow() const OVERRIDE { return true; }
2897
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002898 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002899
2900 DECLARE_INSTRUCTION(DivZeroCheck);
2901
2902 private:
2903 const uint32_t dex_pc_;
2904
2905 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2906};
2907
Calin Juravle9aec02f2014-11-18 23:06:35 +00002908class HShl : public HBinaryOperation {
2909 public:
2910 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2911 : HBinaryOperation(result_type, left, right) {}
2912
2913 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2914 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2915
2916 DECLARE_INSTRUCTION(Shl);
2917
2918 private:
2919 DISALLOW_COPY_AND_ASSIGN(HShl);
2920};
2921
2922class HShr : public HBinaryOperation {
2923 public:
2924 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2925 : HBinaryOperation(result_type, left, right) {}
2926
2927 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2928 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2929
2930 DECLARE_INSTRUCTION(Shr);
2931
2932 private:
2933 DISALLOW_COPY_AND_ASSIGN(HShr);
2934};
2935
2936class HUShr : public HBinaryOperation {
2937 public:
2938 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2939 : HBinaryOperation(result_type, left, right) {}
2940
2941 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2942 uint32_t ux = static_cast<uint32_t>(x);
2943 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2944 return static_cast<int32_t>(ux >> uy);
2945 }
2946
2947 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2948 uint64_t ux = static_cast<uint64_t>(x);
2949 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2950 return static_cast<int64_t>(ux >> uy);
2951 }
2952
2953 DECLARE_INSTRUCTION(UShr);
2954
2955 private:
2956 DISALLOW_COPY_AND_ASSIGN(HUShr);
2957};
2958
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002959class HAnd : public HBinaryOperation {
2960 public:
2961 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2962 : HBinaryOperation(result_type, left, right) {}
2963
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002964 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002965
2966 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2967 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2968
2969 DECLARE_INSTRUCTION(And);
2970
2971 private:
2972 DISALLOW_COPY_AND_ASSIGN(HAnd);
2973};
2974
2975class HOr : public HBinaryOperation {
2976 public:
2977 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2978 : HBinaryOperation(result_type, left, right) {}
2979
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002980 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002981
2982 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2983 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2984
2985 DECLARE_INSTRUCTION(Or);
2986
2987 private:
2988 DISALLOW_COPY_AND_ASSIGN(HOr);
2989};
2990
2991class HXor : public HBinaryOperation {
2992 public:
2993 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2994 : HBinaryOperation(result_type, left, right) {}
2995
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002996 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002997
2998 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2999 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3000
3001 DECLARE_INSTRUCTION(Xor);
3002
3003 private:
3004 DISALLOW_COPY_AND_ASSIGN(HXor);
3005};
3006
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003007// The value of a parameter in this method. Its location depends on
3008// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003009class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003010 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003011 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3012 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003013
3014 uint8_t GetIndex() const { return index_; }
3015
Calin Juravle10e244f2015-01-26 18:54:32 +00003016 bool CanBeNull() const OVERRIDE { return !is_this_; }
3017
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003018 bool IsThis() const { return is_this_; }
3019
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003020 DECLARE_INSTRUCTION(ParameterValue);
3021
3022 private:
3023 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003024 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003025 const uint8_t index_;
3026
Calin Juravle10e244f2015-01-26 18:54:32 +00003027 // Whether or not the parameter value corresponds to 'this' argument.
3028 const bool is_this_;
3029
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003030 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3031};
3032
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003033class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003034 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003035 explicit HNot(Primitive::Type result_type, HInstruction* input)
3036 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003037
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003038 bool CanBeMoved() const OVERRIDE { return true; }
3039 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003040 UNUSED(other);
3041 return true;
3042 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003043
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003044 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3045 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003046
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003047 DECLARE_INSTRUCTION(Not);
3048
3049 private:
3050 DISALLOW_COPY_AND_ASSIGN(HNot);
3051};
3052
David Brazdil66d126e2015-04-03 16:02:44 +01003053class HBooleanNot : public HUnaryOperation {
3054 public:
3055 explicit HBooleanNot(HInstruction* input)
3056 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3057
3058 bool CanBeMoved() const OVERRIDE { return true; }
3059 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3060 UNUSED(other);
3061 return true;
3062 }
3063
3064 int32_t Evaluate(int32_t x) const OVERRIDE {
3065 DCHECK(IsUint<1>(x));
3066 return !x;
3067 }
3068
3069 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3070 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3071 UNREACHABLE();
3072 }
3073
3074 DECLARE_INSTRUCTION(BooleanNot);
3075
3076 private:
3077 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3078};
3079
Roland Levillaindff1f282014-11-05 14:15:05 +00003080class HTypeConversion : public HExpression<1> {
3081 public:
3082 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003083 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3084 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003085 SetRawInputAt(0, input);
3086 DCHECK_NE(input->GetType(), result_type);
3087 }
3088
3089 HInstruction* GetInput() const { return InputAt(0); }
3090 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3091 Primitive::Type GetResultType() const { return GetType(); }
3092
Roland Levillain624279f2014-12-04 11:54:28 +00003093 // Required by the x86 and ARM code generators when producing calls
3094 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003095 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003096
Roland Levillaindff1f282014-11-05 14:15:05 +00003097 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003098 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003099
Mark Mendelle82549b2015-05-06 10:55:34 -04003100 // Try to statically evaluate the conversion and return a HConstant
3101 // containing the result. If the input cannot be converted, return nullptr.
3102 HConstant* TryStaticEvaluation() const;
3103
Roland Levillaindff1f282014-11-05 14:15:05 +00003104 DECLARE_INSTRUCTION(TypeConversion);
3105
3106 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003107 const uint32_t dex_pc_;
3108
Roland Levillaindff1f282014-11-05 14:15:05 +00003109 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3110};
3111
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003112static constexpr uint32_t kNoRegNumber = -1;
3113
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003114class HPhi : public HInstruction {
3115 public:
3116 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003117 : HInstruction(SideEffects::None()),
3118 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003119 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003120 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003121 is_live_(false),
3122 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003123 inputs_.SetSize(number_of_inputs);
3124 }
3125
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003126 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3127 static Primitive::Type ToPhiType(Primitive::Type type) {
3128 switch (type) {
3129 case Primitive::kPrimBoolean:
3130 case Primitive::kPrimByte:
3131 case Primitive::kPrimShort:
3132 case Primitive::kPrimChar:
3133 return Primitive::kPrimInt;
3134 default:
3135 return type;
3136 }
3137 }
3138
Calin Juravle10e244f2015-01-26 18:54:32 +00003139 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003140
3141 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003142 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003143
Calin Juravle10e244f2015-01-26 18:54:32 +00003144 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003145 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003146
Calin Juravle10e244f2015-01-26 18:54:32 +00003147 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3148 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3149
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003150 uint32_t GetRegNumber() const { return reg_number_; }
3151
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003152 void SetDead() { is_live_ = false; }
3153 void SetLive() { is_live_ = true; }
3154 bool IsDead() const { return !is_live_; }
3155 bool IsLive() const { return is_live_; }
3156
Calin Juravlea4f88312015-04-16 12:57:19 +01003157 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3158 // An equivalent phi is a phi having the same dex register and type.
3159 // It assumes that phis with the same dex register are adjacent.
3160 HPhi* GetNextEquivalentPhiWithSameType() {
3161 HInstruction* next = GetNext();
3162 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3163 if (next->GetType() == GetType()) {
3164 return next->AsPhi();
3165 }
3166 next = next->GetNext();
3167 }
3168 return nullptr;
3169 }
3170
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003171 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003172
David Brazdil1abb4192015-02-17 18:33:36 +00003173 protected:
3174 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3175
3176 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3177 inputs_.Put(index, input);
3178 }
3179
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003180 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003181 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003182 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003183 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003184 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003185 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003186
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003187 DISALLOW_COPY_AND_ASSIGN(HPhi);
3188};
3189
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003190class HNullCheck : public HExpression<1> {
3191 public:
3192 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003193 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003194 SetRawInputAt(0, value);
3195 }
3196
Calin Juravle10e244f2015-01-26 18:54:32 +00003197 bool CanBeMoved() const OVERRIDE { return true; }
3198 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003199 UNUSED(other);
3200 return true;
3201 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003202
Calin Juravle10e244f2015-01-26 18:54:32 +00003203 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003204
Calin Juravle10e244f2015-01-26 18:54:32 +00003205 bool CanThrow() const OVERRIDE { return true; }
3206
3207 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003208
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003209 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003210
3211 DECLARE_INSTRUCTION(NullCheck);
3212
3213 private:
3214 const uint32_t dex_pc_;
3215
3216 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3217};
3218
3219class FieldInfo : public ValueObject {
3220 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003221 FieldInfo(MemberOffset field_offset,
3222 Primitive::Type field_type,
3223 bool is_volatile,
3224 uint32_t index,
3225 const DexFile& dex_file)
3226 : field_offset_(field_offset),
3227 field_type_(field_type),
3228 is_volatile_(is_volatile),
3229 index_(index),
3230 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003231
3232 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003233 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003234 uint32_t GetFieldIndex() const { return index_; }
3235 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003236 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003237
3238 private:
3239 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003240 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003241 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003242 uint32_t index_;
3243 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003244};
3245
3246class HInstanceFieldGet : public HExpression<1> {
3247 public:
3248 HInstanceFieldGet(HInstruction* value,
3249 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003250 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003251 bool is_volatile,
3252 uint32_t field_idx,
3253 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003254 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003255 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003256 SetRawInputAt(0, value);
3257 }
3258
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003259 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003260
3261 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3262 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3263 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003264 }
3265
Calin Juravle641547a2015-04-21 22:08:51 +01003266 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3267 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003268 }
3269
3270 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003271 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3272 }
3273
Calin Juravle52c48962014-12-16 17:02:57 +00003274 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003275 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003276 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003277 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003278
3279 DECLARE_INSTRUCTION(InstanceFieldGet);
3280
3281 private:
3282 const FieldInfo field_info_;
3283
3284 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3285};
3286
3287class HInstanceFieldSet : public HTemplateInstruction<2> {
3288 public:
3289 HInstanceFieldSet(HInstruction* object,
3290 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003291 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003292 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003293 bool is_volatile,
3294 uint32_t field_idx,
3295 const DexFile& dex_file)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003296 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003297 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003298 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003299 SetRawInputAt(0, object);
3300 SetRawInputAt(1, value);
3301 }
3302
Calin Juravle641547a2015-04-21 22:08:51 +01003303 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3304 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003305 }
3306
Calin Juravle52c48962014-12-16 17:02:57 +00003307 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003308 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003309 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003310 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003311 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003312 bool GetValueCanBeNull() const { return value_can_be_null_; }
3313 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003314
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003315 DECLARE_INSTRUCTION(InstanceFieldSet);
3316
3317 private:
3318 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003319 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003320
3321 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3322};
3323
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003324class HArrayGet : public HExpression<2> {
3325 public:
3326 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003327 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003328 SetRawInputAt(0, array);
3329 SetRawInputAt(1, index);
3330 }
3331
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003332 bool CanBeMoved() const OVERRIDE { return true; }
3333 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003334 UNUSED(other);
3335 return true;
3336 }
Calin Juravle641547a2015-04-21 22:08:51 +01003337 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3338 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003339 // TODO: We can be smarter here.
3340 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3341 // which generates the implicit null check. There are cases when these can be removed
3342 // to produce better code. If we ever add optimizations to do so we should allow an
3343 // implicit check here (as long as the address falls in the first page).
3344 return false;
3345 }
3346
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003347 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003348
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003349 HInstruction* GetArray() const { return InputAt(0); }
3350 HInstruction* GetIndex() const { return InputAt(1); }
3351
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003352 DECLARE_INSTRUCTION(ArrayGet);
3353
3354 private:
3355 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3356};
3357
3358class HArraySet : public HTemplateInstruction<3> {
3359 public:
3360 HArraySet(HInstruction* array,
3361 HInstruction* index,
3362 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003363 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003364 uint32_t dex_pc)
3365 : HTemplateInstruction(SideEffects::ChangesSomething()),
3366 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003367 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003368 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3369 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003370 SetRawInputAt(0, array);
3371 SetRawInputAt(1, index);
3372 SetRawInputAt(2, value);
3373 }
3374
Calin Juravle77520bc2015-01-12 18:45:46 +00003375 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003376 // We currently always call a runtime method to catch array store
3377 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003378 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003379 }
3380
Mingyao Yang81014cb2015-06-02 03:16:27 -07003381 // Can throw ArrayStoreException.
3382 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3383
Calin Juravle641547a2015-04-21 22:08:51 +01003384 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3385 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003386 // TODO: Same as for ArrayGet.
3387 return false;
3388 }
3389
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003390 void ClearNeedsTypeCheck() {
3391 needs_type_check_ = false;
3392 }
3393
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003394 void ClearValueCanBeNull() {
3395 value_can_be_null_ = false;
3396 }
3397
3398 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003399 bool NeedsTypeCheck() const { return needs_type_check_; }
3400
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003401 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003402
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003403 HInstruction* GetArray() const { return InputAt(0); }
3404 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003405 HInstruction* GetValue() const { return InputAt(2); }
3406
3407 Primitive::Type GetComponentType() const {
3408 // The Dex format does not type floating point index operations. Since the
3409 // `expected_component_type_` is set during building and can therefore not
3410 // be correct, we also check what is the value type. If it is a floating
3411 // point type, we must use that type.
3412 Primitive::Type value_type = GetValue()->GetType();
3413 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3414 ? value_type
3415 : expected_component_type_;
3416 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003417
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003418 DECLARE_INSTRUCTION(ArraySet);
3419
3420 private:
3421 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003422 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003423 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003424 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003425
3426 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3427};
3428
3429class HArrayLength : public HExpression<1> {
3430 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003431 explicit HArrayLength(HInstruction* array)
3432 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3433 // Note that arrays do not change length, so the instruction does not
3434 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003435 SetRawInputAt(0, array);
3436 }
3437
Calin Juravle77520bc2015-01-12 18:45:46 +00003438 bool CanBeMoved() const OVERRIDE { return true; }
3439 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003440 UNUSED(other);
3441 return true;
3442 }
Calin Juravle641547a2015-04-21 22:08:51 +01003443 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3444 return obj == InputAt(0);
3445 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003446
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003447 DECLARE_INSTRUCTION(ArrayLength);
3448
3449 private:
3450 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3451};
3452
3453class HBoundsCheck : public HExpression<2> {
3454 public:
3455 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003456 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003457 DCHECK(index->GetType() == Primitive::kPrimInt);
3458 SetRawInputAt(0, index);
3459 SetRawInputAt(1, length);
3460 }
3461
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003462 bool CanBeMoved() const OVERRIDE { return true; }
3463 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003464 UNUSED(other);
3465 return true;
3466 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003467
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003468 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003469
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003470 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003471
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003472 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003473
3474 DECLARE_INSTRUCTION(BoundsCheck);
3475
3476 private:
3477 const uint32_t dex_pc_;
3478
3479 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3480};
3481
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003482/**
3483 * Some DEX instructions are folded into multiple HInstructions that need
3484 * to stay live until the last HInstruction. This class
3485 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003486 * HInstruction stays live. `index` represents the stack location index of the
3487 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003488 */
3489class HTemporary : public HTemplateInstruction<0> {
3490 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003491 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003492
3493 size_t GetIndex() const { return index_; }
3494
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003495 Primitive::Type GetType() const OVERRIDE {
3496 // The previous instruction is the one that will be stored in the temporary location.
3497 DCHECK(GetPrevious() != nullptr);
3498 return GetPrevious()->GetType();
3499 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003500
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003501 DECLARE_INSTRUCTION(Temporary);
3502
3503 private:
3504 const size_t index_;
3505
3506 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3507};
3508
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003509class HSuspendCheck : public HTemplateInstruction<0> {
3510 public:
3511 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003512 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003513
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003514 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003515 return true;
3516 }
3517
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003518 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003519 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3520 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003521
3522 DECLARE_INSTRUCTION(SuspendCheck);
3523
3524 private:
3525 const uint32_t dex_pc_;
3526
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003527 // Only used for code generation, in order to share the same slow path between back edges
3528 // of a same loop.
3529 SlowPathCode* slow_path_;
3530
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003531 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3532};
3533
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003534/**
3535 * Instruction to load a Class object.
3536 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003537class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003538 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003539 HLoadClass(HCurrentMethod* current_method,
3540 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003541 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003542 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003543 uint32_t dex_pc)
3544 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3545 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003546 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003547 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003548 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003549 generate_clinit_check_(false),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003550 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
3551 SetRawInputAt(0, current_method);
3552 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003553
3554 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003555
3556 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3557 return other->AsLoadClass()->type_index_ == type_index_;
3558 }
3559
3560 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3561
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003562 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003563 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003564 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003565
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003566 bool NeedsEnvironment() const OVERRIDE {
3567 // Will call runtime and load the class if the class is not loaded yet.
3568 // TODO: finer grain decision.
3569 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003570 }
3571
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003572 bool MustGenerateClinitCheck() const {
3573 return generate_clinit_check_;
3574 }
3575
Calin Juravle0ba218d2015-05-19 18:46:01 +01003576 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3577 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003578 }
3579
3580 bool CanCallRuntime() const {
3581 return MustGenerateClinitCheck() || !is_referrers_class_;
3582 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003583
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003584 bool CanThrow() const OVERRIDE {
3585 // May call runtime and and therefore can throw.
3586 // TODO: finer grain decision.
3587 return !is_referrers_class_;
3588 }
3589
Calin Juravleacf735c2015-02-12 15:25:22 +00003590 ReferenceTypeInfo GetLoadedClassRTI() {
3591 return loaded_class_rti_;
3592 }
3593
3594 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3595 // Make sure we only set exact types (the loaded class should never be merged).
3596 DCHECK(rti.IsExact());
3597 loaded_class_rti_ = rti;
3598 }
3599
3600 bool IsResolved() {
3601 return loaded_class_rti_.IsExact();
3602 }
3603
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003604 const DexFile& GetDexFile() { return dex_file_; }
3605
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003606 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3607
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003608 DECLARE_INSTRUCTION(LoadClass);
3609
3610 private:
3611 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003612 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003613 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003614 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003615 // Whether this instruction must generate the initialization check.
3616 // Used for code generation.
3617 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003618
Calin Juravleacf735c2015-02-12 15:25:22 +00003619 ReferenceTypeInfo loaded_class_rti_;
3620
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003621 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3622};
3623
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003624class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003625 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003626 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003627 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3628 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003629 dex_pc_(dex_pc) {
3630 SetRawInputAt(0, current_method);
3631 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003632
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003633 bool CanBeMoved() const OVERRIDE { return true; }
3634
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003635 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3636 return other->AsLoadString()->string_index_ == string_index_;
3637 }
3638
3639 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3640
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003641 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003642 uint32_t GetStringIndex() const { return string_index_; }
3643
3644 // TODO: Can we deopt or debug when we resolve a string?
3645 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003646 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003647
3648 DECLARE_INSTRUCTION(LoadString);
3649
3650 private:
3651 const uint32_t string_index_;
3652 const uint32_t dex_pc_;
3653
3654 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3655};
3656
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003657/**
3658 * Performs an initialization check on its Class object input.
3659 */
3660class HClinitCheck : public HExpression<1> {
3661 public:
3662 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003663 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003664 dex_pc_(dex_pc) {
3665 SetRawInputAt(0, constant);
3666 }
3667
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003668 bool CanBeMoved() const OVERRIDE { return true; }
3669 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3670 UNUSED(other);
3671 return true;
3672 }
3673
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003674 bool NeedsEnvironment() const OVERRIDE {
3675 // May call runtime to initialize the class.
3676 return true;
3677 }
3678
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003679 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003680
3681 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3682
3683 DECLARE_INSTRUCTION(ClinitCheck);
3684
3685 private:
3686 const uint32_t dex_pc_;
3687
3688 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3689};
3690
3691class HStaticFieldGet : public HExpression<1> {
3692 public:
3693 HStaticFieldGet(HInstruction* cls,
3694 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003695 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003696 bool is_volatile,
3697 uint32_t field_idx,
3698 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003699 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003700 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003701 SetRawInputAt(0, cls);
3702 }
3703
Calin Juravle52c48962014-12-16 17:02:57 +00003704
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003705 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003706
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003707 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003708 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3709 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003710 }
3711
3712 size_t ComputeHashCode() const OVERRIDE {
3713 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3714 }
3715
Calin Juravle52c48962014-12-16 17:02:57 +00003716 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003717 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3718 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003719 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003720
3721 DECLARE_INSTRUCTION(StaticFieldGet);
3722
3723 private:
3724 const FieldInfo field_info_;
3725
3726 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3727};
3728
3729class HStaticFieldSet : public HTemplateInstruction<2> {
3730 public:
3731 HStaticFieldSet(HInstruction* cls,
3732 HInstruction* value,
3733 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003734 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003735 bool is_volatile,
3736 uint32_t field_idx,
3737 const DexFile& dex_file)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003738 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003739 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003740 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003741 SetRawInputAt(0, cls);
3742 SetRawInputAt(1, value);
3743 }
3744
Calin Juravle52c48962014-12-16 17:02:57 +00003745 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003746 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3747 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003748 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003749
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003750 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003751 bool GetValueCanBeNull() const { return value_can_be_null_; }
3752 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003753
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003754 DECLARE_INSTRUCTION(StaticFieldSet);
3755
3756 private:
3757 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003758 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003759
3760 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3761};
3762
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003763// Implement the move-exception DEX instruction.
3764class HLoadException : public HExpression<0> {
3765 public:
3766 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3767
3768 DECLARE_INSTRUCTION(LoadException);
3769
3770 private:
3771 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3772};
3773
3774class HThrow : public HTemplateInstruction<1> {
3775 public:
3776 HThrow(HInstruction* exception, uint32_t dex_pc)
3777 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3778 SetRawInputAt(0, exception);
3779 }
3780
3781 bool IsControlFlow() const OVERRIDE { return true; }
3782
3783 bool NeedsEnvironment() const OVERRIDE { return true; }
3784
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003785 bool CanThrow() const OVERRIDE { return true; }
3786
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003787 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003788
3789 DECLARE_INSTRUCTION(Throw);
3790
3791 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003792 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003793
3794 DISALLOW_COPY_AND_ASSIGN(HThrow);
3795};
3796
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003797class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003798 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003799 HInstanceOf(HInstruction* object,
3800 HLoadClass* constant,
3801 bool class_is_final,
3802 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003803 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3804 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003805 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003806 dex_pc_(dex_pc) {
3807 SetRawInputAt(0, object);
3808 SetRawInputAt(1, constant);
3809 }
3810
3811 bool CanBeMoved() const OVERRIDE { return true; }
3812
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003813 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003814 return true;
3815 }
3816
3817 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003818 return false;
3819 }
3820
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003821 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003822
3823 bool IsClassFinal() const { return class_is_final_; }
3824
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003825 // Used only in code generation.
3826 bool MustDoNullCheck() const { return must_do_null_check_; }
3827 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3828
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003829 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003830
3831 private:
3832 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003833 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003834 const uint32_t dex_pc_;
3835
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003836 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3837};
3838
Calin Juravleb1498f62015-02-16 13:13:29 +00003839class HBoundType : public HExpression<1> {
3840 public:
3841 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3842 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3843 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003844 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003845 SetRawInputAt(0, input);
3846 }
3847
3848 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3849
3850 bool CanBeNull() const OVERRIDE {
3851 // `null instanceof ClassX` always return false so we can't be null.
3852 return false;
3853 }
3854
3855 DECLARE_INSTRUCTION(BoundType);
3856
3857 private:
3858 // Encodes the most upper class that this instruction can have. In other words
3859 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3860 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3861 const ReferenceTypeInfo bound_type_;
3862
3863 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3864};
3865
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003866class HCheckCast : public HTemplateInstruction<2> {
3867 public:
3868 HCheckCast(HInstruction* object,
3869 HLoadClass* constant,
3870 bool class_is_final,
3871 uint32_t dex_pc)
3872 : HTemplateInstruction(SideEffects::None()),
3873 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003874 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003875 dex_pc_(dex_pc) {
3876 SetRawInputAt(0, object);
3877 SetRawInputAt(1, constant);
3878 }
3879
3880 bool CanBeMoved() const OVERRIDE { return true; }
3881
3882 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3883 return true;
3884 }
3885
3886 bool NeedsEnvironment() const OVERRIDE {
3887 // Instruction may throw a CheckCastError.
3888 return true;
3889 }
3890
3891 bool CanThrow() const OVERRIDE { return true; }
3892
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003893 bool MustDoNullCheck() const { return must_do_null_check_; }
3894 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3895
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003896 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003897
3898 bool IsClassFinal() const { return class_is_final_; }
3899
3900 DECLARE_INSTRUCTION(CheckCast);
3901
3902 private:
3903 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003904 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003905 const uint32_t dex_pc_;
3906
3907 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003908};
3909
Calin Juravle27df7582015-04-17 19:12:31 +01003910class HMemoryBarrier : public HTemplateInstruction<0> {
3911 public:
3912 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3913 : HTemplateInstruction(SideEffects::None()),
3914 barrier_kind_(barrier_kind) {}
3915
3916 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3917
3918 DECLARE_INSTRUCTION(MemoryBarrier);
3919
3920 private:
3921 const MemBarrierKind barrier_kind_;
3922
3923 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3924};
3925
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003926class HMonitorOperation : public HTemplateInstruction<1> {
3927 public:
3928 enum OperationKind {
3929 kEnter,
3930 kExit,
3931 };
3932
3933 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3934 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3935 SetRawInputAt(0, object);
3936 }
3937
3938 // Instruction may throw a Java exception, so we need an environment.
3939 bool NeedsEnvironment() const OVERRIDE { return true; }
3940 bool CanThrow() const OVERRIDE { return true; }
3941
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003942 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003943
3944 bool IsEnter() const { return kind_ == kEnter; }
3945
3946 DECLARE_INSTRUCTION(MonitorOperation);
3947
Calin Juravle52c48962014-12-16 17:02:57 +00003948 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003949 const OperationKind kind_;
3950 const uint32_t dex_pc_;
3951
3952 private:
3953 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3954};
3955
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003956class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003957 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003958 MoveOperands(Location source,
3959 Location destination,
3960 Primitive::Type type,
3961 HInstruction* instruction)
3962 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003963
3964 Location GetSource() const { return source_; }
3965 Location GetDestination() const { return destination_; }
3966
3967 void SetSource(Location value) { source_ = value; }
3968 void SetDestination(Location value) { destination_ = value; }
3969
3970 // The parallel move resolver marks moves as "in-progress" by clearing the
3971 // destination (but not the source).
3972 Location MarkPending() {
3973 DCHECK(!IsPending());
3974 Location dest = destination_;
3975 destination_ = Location::NoLocation();
3976 return dest;
3977 }
3978
3979 void ClearPending(Location dest) {
3980 DCHECK(IsPending());
3981 destination_ = dest;
3982 }
3983
3984 bool IsPending() const {
3985 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3986 return destination_.IsInvalid() && !source_.IsInvalid();
3987 }
3988
3989 // True if this blocks a move from the given location.
3990 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003991 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003992 }
3993
3994 // A move is redundant if it's been eliminated, if its source and
3995 // destination are the same, or if its destination is unneeded.
3996 bool IsRedundant() const {
3997 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3998 }
3999
4000 // We clear both operands to indicate move that's been eliminated.
4001 void Eliminate() {
4002 source_ = destination_ = Location::NoLocation();
4003 }
4004
4005 bool IsEliminated() const {
4006 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4007 return source_.IsInvalid();
4008 }
4009
Nicolas Geoffray90218252015-04-15 11:56:51 +01004010 bool Is64BitMove() const {
4011 return Primitive::Is64BitType(type_);
4012 }
4013
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004014 HInstruction* GetInstruction() const { return instruction_; }
4015
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004016 private:
4017 Location source_;
4018 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004019 // The type this move is for.
4020 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004021 // The instruction this move is assocatied with. Null when this move is
4022 // for moving an input in the expected locations of user (including a phi user).
4023 // This is only used in debug mode, to ensure we do not connect interval siblings
4024 // in the same parallel move.
4025 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004026};
4027
4028static constexpr size_t kDefaultNumberOfMoves = 4;
4029
4030class HParallelMove : public HTemplateInstruction<0> {
4031 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004032 explicit HParallelMove(ArenaAllocator* arena)
4033 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004034
Nicolas Geoffray90218252015-04-15 11:56:51 +01004035 void AddMove(Location source,
4036 Location destination,
4037 Primitive::Type type,
4038 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004039 DCHECK(source.IsValid());
4040 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004041 if (kIsDebugBuild) {
4042 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004043 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004044 if (moves_.Get(i).GetInstruction() == instruction) {
4045 // Special case the situation where the move is for the spill slot
4046 // of the instruction.
4047 if ((GetPrevious() == instruction)
4048 || ((GetPrevious() == nullptr)
4049 && instruction->IsPhi()
4050 && instruction->GetBlock() == GetBlock())) {
4051 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4052 << "Doing parallel moves for the same instruction.";
4053 } else {
4054 DCHECK(false) << "Doing parallel moves for the same instruction.";
4055 }
4056 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004057 }
4058 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004059 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004060 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004061 << "Overlapped destination for two moves in a parallel move: "
4062 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4063 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004064 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004065 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004066 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004067 }
4068
4069 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004070 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004071 }
4072
4073 size_t NumMoves() const { return moves_.Size(); }
4074
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004075 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004076
4077 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004078 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004079
4080 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4081};
4082
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004083class HGraphVisitor : public ValueObject {
4084 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004085 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4086 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004087
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004088 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004089 virtual void VisitBasicBlock(HBasicBlock* block);
4090
Roland Levillain633021e2014-10-01 14:12:25 +01004091 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004092 void VisitInsertionOrder();
4093
Roland Levillain633021e2014-10-01 14:12:25 +01004094 // Visit the graph following dominator tree reverse post-order.
4095 void VisitReversePostOrder();
4096
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004097 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004098
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004099 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004100#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004101 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4102
4103 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4104
4105#undef DECLARE_VISIT_INSTRUCTION
4106
4107 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004108 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004109
4110 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4111};
4112
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004113class HGraphDelegateVisitor : public HGraphVisitor {
4114 public:
4115 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4116 virtual ~HGraphDelegateVisitor() {}
4117
4118 // Visit functions that delegate to to super class.
4119#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004120 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004121
4122 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4123
4124#undef DECLARE_VISIT_INSTRUCTION
4125
4126 private:
4127 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4128};
4129
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004130class HInsertionOrderIterator : public ValueObject {
4131 public:
4132 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4133
4134 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4135 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4136 void Advance() { ++index_; }
4137
4138 private:
4139 const HGraph& graph_;
4140 size_t index_;
4141
4142 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4143};
4144
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004145class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004146 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004147 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4148 // Check that reverse post order of the graph has been built.
4149 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4150 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004151
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004152 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4153 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004154 void Advance() { ++index_; }
4155
4156 private:
4157 const HGraph& graph_;
4158 size_t index_;
4159
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004160 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004161};
4162
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004163class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004164 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004165 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004166 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4167 // Check that reverse post order of the graph has been built.
4168 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4169 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004170
4171 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004172 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004173 void Advance() { --index_; }
4174
4175 private:
4176 const HGraph& graph_;
4177 size_t index_;
4178
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004179 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004180};
4181
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004182class HLinearPostOrderIterator : public ValueObject {
4183 public:
4184 explicit HLinearPostOrderIterator(const HGraph& graph)
4185 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4186
4187 bool Done() const { return index_ == 0; }
4188
4189 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4190
4191 void Advance() {
4192 --index_;
4193 DCHECK_GE(index_, 0U);
4194 }
4195
4196 private:
4197 const GrowableArray<HBasicBlock*>& order_;
4198 size_t index_;
4199
4200 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4201};
4202
4203class HLinearOrderIterator : public ValueObject {
4204 public:
4205 explicit HLinearOrderIterator(const HGraph& graph)
4206 : order_(graph.GetLinearOrder()), index_(0) {}
4207
4208 bool Done() const { return index_ == order_.Size(); }
4209 HBasicBlock* Current() const { return order_.Get(index_); }
4210 void Advance() { ++index_; }
4211
4212 private:
4213 const GrowableArray<HBasicBlock*>& order_;
4214 size_t index_;
4215
4216 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4217};
4218
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004219// Iterator over the blocks that art part of the loop. Includes blocks part
4220// of an inner loop. The order in which the blocks are iterated is on their
4221// block id.
4222class HBlocksInLoopIterator : public ValueObject {
4223 public:
4224 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4225 : blocks_in_loop_(info.GetBlocks()),
4226 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4227 index_(0) {
4228 if (!blocks_in_loop_.IsBitSet(index_)) {
4229 Advance();
4230 }
4231 }
4232
4233 bool Done() const { return index_ == blocks_.Size(); }
4234 HBasicBlock* Current() const { return blocks_.Get(index_); }
4235 void Advance() {
4236 ++index_;
4237 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4238 if (blocks_in_loop_.IsBitSet(index_)) {
4239 break;
4240 }
4241 }
4242 }
4243
4244 private:
4245 const BitVector& blocks_in_loop_;
4246 const GrowableArray<HBasicBlock*>& blocks_;
4247 size_t index_;
4248
4249 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4250};
4251
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004252inline int64_t Int64FromConstant(HConstant* constant) {
4253 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4254 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4255 : constant->AsLongConstant()->GetValue();
4256}
4257
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004258} // namespace art
4259
4260#endif // ART_COMPILER_OPTIMIZING_NODES_H_