blob: 486968cf9ebd586e2ab99a45682c4d6ddfdb7a87 [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
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
David Brazdil8d5b8b22015-03-24 10:51:52 +000024#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080025#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000026#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010027#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000028#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000029#include "handle.h"
30#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000031#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010032#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000033#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000034#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010035#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070036#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000037#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39namespace art {
40
David Brazdil1abb4192015-02-17 18:33:36 +000041class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000044class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010045class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010046class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Calin Juravle9aec02f2014-11-18 23:06:35 +000074static constexpr uint32_t kMaxIntShiftValue = 0x1f;
75static constexpr uint64_t kMaxLongShiftValue = 0x3f;
76
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010077static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
78
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010079static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
80
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060081static constexpr uint32_t kNoDexPc = -1;
82
Dave Allison20dfc792014-06-16 20:44:29 -070083enum IfCondition {
84 kCondEQ,
85 kCondNE,
86 kCondLT,
87 kCondLE,
88 kCondGT,
89 kCondGE,
90};
91
Vladimir Markof9f64412015-09-02 14:05:49 +010092class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010093 public:
94 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
95
96 void AddInstruction(HInstruction* instruction);
97 void RemoveInstruction(HInstruction* instruction);
98
David Brazdilc3d743f2015-04-22 13:40:50 +010099 // Insert `instruction` before/after an existing instruction `cursor`.
100 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
101 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
102
Roland Levillain6b469232014-09-25 10:10:38 +0100103 // Return true if this list contains `instruction`.
104 bool Contains(HInstruction* instruction) const;
105
Roland Levillainccc07a92014-09-16 14:48:16 +0100106 // Return true if `instruction1` is found before `instruction2` in
107 // this instruction list and false otherwise. Abort if none
108 // of these instructions is found.
109 bool FoundBefore(const HInstruction* instruction1,
110 const HInstruction* instruction2) const;
111
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000112 bool IsEmpty() const { return first_instruction_ == nullptr; }
113 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
114
115 // Update the block of all instructions to be `block`.
116 void SetBlockOfInstructions(HBasicBlock* block) const;
117
118 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
119 void Add(const HInstructionList& instruction_list);
120
David Brazdil2d7352b2015-04-20 14:52:42 +0100121 // Return the number of instructions in the list. This is an expensive operation.
122 size_t CountSize() const;
123
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100124 private:
125 HInstruction* first_instruction_;
126 HInstruction* last_instruction_;
127
128 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000129 friend class HGraph;
130 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100131 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100132 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100133
134 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
135};
136
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000137// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100138class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000139 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100140 HGraph(ArenaAllocator* arena,
141 const DexFile& dex_file,
142 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100143 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700144 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100145 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100146 bool debuggable = false,
147 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000148 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100149 blocks_(arena->Adapter(kArenaAllocBlockList)),
150 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
151 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700152 entry_block_(nullptr),
153 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100154 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100155 number_of_vregs_(0),
156 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000157 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400158 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000159 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000160 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000161 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100162 dex_file_(dex_file),
163 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100164 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100165 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100166 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700167 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000168 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100169 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
170 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
171 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
172 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
173 cached_current_method_(nullptr) {
174 blocks_.reserve(kDefaultNumberOfBlocks);
175 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000176
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000177 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100178 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
179
180 HBasicBlock* GetBlock(size_t id) const {
181 DCHECK_LT(id, blocks_.size());
182 return blocks_[id];
183 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000184
David Brazdil69ba7b72015-06-23 18:27:30 +0100185 bool IsInSsaForm() const { return in_ssa_form_; }
186
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000187 HBasicBlock* GetEntryBlock() const { return entry_block_; }
188 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100189 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000190
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000191 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
192 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000193
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000194 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100195
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000196 // Try building the SSA form of this graph, with dominance computation and loop
197 // recognition. Returns whether it was successful in doing all these steps.
198 bool TryBuildingSsa() {
199 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000200 // The SSA builder requires loops to all be natural. Specifically, the dead phi
201 // elimination phase checks the consistency of the graph when doing a post-order
202 // visit for eliminating dead phis: a dead phi can only have loop header phi
203 // users remaining when being visited.
204 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100205 // Precompute per-block try membership before entering the SSA builder,
206 // which needs the information to build catch block phis from values of
207 // locals at throwing instructions inside try blocks.
208 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000209 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100210 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000211 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212 }
213
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100214 void ComputeDominanceInformation();
215 void ClearDominanceInformation();
216
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000217 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000218 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100219 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100220 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000221
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000222 // Analyze all natural loops in this graph. Returns false if one
223 // loop is not natural, that is the header does not dominate the
224 // back edge.
225 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100226
David Brazdilffee3d32015-07-06 11:48:53 +0100227 // Iterate over blocks to compute try block membership. Needs reverse post
228 // order and loop information.
229 void ComputeTryBlockInformation();
230
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000231 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000232 // Returns the instruction used to replace the invoke expression or null if the
233 // invoke is for a void method.
234 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000235
Mingyao Yang3584bce2015-05-19 16:01:59 -0700236 // Need to add a couple of blocks to test if the loop body is entered and
237 // put deoptimization instructions, etc.
238 void TransformLoopHeaderForBCE(HBasicBlock* header);
239
David Brazdil2d7352b2015-04-20 14:52:42 +0100240 // Removes `block` from the graph.
241 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000242
David Brazdilfc6a86a2015-06-26 10:33:45 +0000243 // Splits the edge between `block` and `successor` while preserving the
244 // indices in the predecessor/successor lists. If there are multiple edges
245 // between the blocks, the lowest indices are used.
246 // Returns the new block which is empty and has the same dex pc as `successor`.
247 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
248
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100249 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
250 void SimplifyLoop(HBasicBlock* header);
251
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000252 int32_t GetNextInstructionId() {
253 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000254 return current_instruction_id_++;
255 }
256
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000257 int32_t GetCurrentInstructionId() const {
258 return current_instruction_id_;
259 }
260
261 void SetCurrentInstructionId(int32_t id) {
262 current_instruction_id_ = id;
263 }
264
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100265 uint16_t GetMaximumNumberOfOutVRegs() const {
266 return maximum_number_of_out_vregs_;
267 }
268
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000269 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
270 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100271 }
272
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100273 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
274 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
275 }
276
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000277 void UpdateTemporariesVRegSlots(size_t slots) {
278 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100279 }
280
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000281 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100282 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000283 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100284 }
285
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100286 void SetNumberOfVRegs(uint16_t number_of_vregs) {
287 number_of_vregs_ = number_of_vregs;
288 }
289
290 uint16_t GetNumberOfVRegs() const {
291 return number_of_vregs_;
292 }
293
294 void SetNumberOfInVRegs(uint16_t value) {
295 number_of_in_vregs_ = value;
296 }
297
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100298 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100299 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100300 return number_of_vregs_ - number_of_in_vregs_;
301 }
302
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100303 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100304 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100305 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100306
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100307 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100308 return linear_order_;
309 }
310
Mark Mendell1152c922015-04-24 17:06:35 -0400311 bool HasBoundsChecks() const {
312 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800313 }
314
Mark Mendell1152c922015-04-24 17:06:35 -0400315 void SetHasBoundsChecks(bool value) {
316 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800317 }
318
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100319 bool ShouldGenerateConstructorBarrier() const {
320 return should_generate_constructor_barrier_;
321 }
322
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000323 bool IsDebuggable() const { return debuggable_; }
324
David Brazdil8d5b8b22015-03-24 10:51:52 +0000325 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000326 // already, it is created and inserted into the graph. This method is only for
327 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600328 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000329
330 // TODO: This is problematic for the consistency of reference type propagation
331 // because it can be created anytime after the pass and thus it will be left
332 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600333 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000334
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600335 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
336 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000337 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600338 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
339 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000340 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600341 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
342 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000343 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600344 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
345 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000346 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000347
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100348 HCurrentMethod* GetCurrentMethod();
349
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000350 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100351
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100352 const DexFile& GetDexFile() const {
353 return dex_file_;
354 }
355
356 uint32_t GetMethodIdx() const {
357 return method_idx_;
358 }
359
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100360 InvokeType GetInvokeType() const {
361 return invoke_type_;
362 }
363
Mark Mendellc4701932015-04-10 13:18:51 -0400364 InstructionSet GetInstructionSet() const {
365 return instruction_set_;
366 }
367
David Brazdil77a48ae2015-09-15 12:34:04 +0000368 bool HasTryCatch() const { return has_try_catch_; }
369 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100370
David Brazdil2d7352b2015-04-20 14:52:42 +0100371 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100372 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000373 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100374 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000375
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000376 template <class InstructionType, typename ValueType>
377 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600378 ArenaSafeMap<ValueType, InstructionType*>* cache,
379 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000380 // Try to find an existing constant of the given value.
381 InstructionType* constant = nullptr;
382 auto cached_constant = cache->find(value);
383 if (cached_constant != cache->end()) {
384 constant = cached_constant->second;
385 }
386
387 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100388 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000389 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600390 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000391 cache->Overwrite(value, constant);
392 InsertConstant(constant);
393 }
394 return constant;
395 }
396
David Brazdil8d5b8b22015-03-24 10:51:52 +0000397 void InsertConstant(HConstant* instruction);
398
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000399 // Cache a float constant into the graph. This method should only be
400 // called by the SsaBuilder when creating "equivalent" instructions.
401 void CacheFloatConstant(HFloatConstant* constant);
402
403 // See CacheFloatConstant comment.
404 void CacheDoubleConstant(HDoubleConstant* constant);
405
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000406 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000407
408 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100409 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000410
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100411 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100412 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000413
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100414 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100415 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100416
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000417 HBasicBlock* entry_block_;
418 HBasicBlock* exit_block_;
419
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100420 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100421 uint16_t maximum_number_of_out_vregs_;
422
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100423 // The number of virtual registers in this method. Contains the parameters.
424 uint16_t number_of_vregs_;
425
426 // The number of virtual registers used by parameters of this method.
427 uint16_t number_of_in_vregs_;
428
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000429 // Number of vreg size slots that the temporaries use (used in baseline compiler).
430 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100431
Mark Mendell1152c922015-04-24 17:06:35 -0400432 // Has bounds checks. We can totally skip BCE if it's false.
433 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800434
David Brazdil77a48ae2015-09-15 12:34:04 +0000435 // Flag whether there are any try/catch blocks in the graph. We will skip
436 // try/catch-related passes if false.
437 bool has_try_catch_;
438
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000439 // Indicates whether the graph should be compiled in a way that
440 // ensures full debuggability. If false, we can apply more
441 // aggressive optimizations that may limit the level of debugging.
442 const bool debuggable_;
443
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000444 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000445 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000446
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100447 // The dex file from which the method is from.
448 const DexFile& dex_file_;
449
450 // The method index in the dex file.
451 const uint32_t method_idx_;
452
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100453 // If inlined, this encodes how the callee is being invoked.
454 const InvokeType invoke_type_;
455
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100456 // Whether the graph has been transformed to SSA form. Only used
457 // in debug mode to ensure we are not using properties only valid
458 // for non-SSA form (like the number of temporaries).
459 bool in_ssa_form_;
460
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100461 const bool should_generate_constructor_barrier_;
462
Mathieu Chartiere401d142015-04-22 13:56:20 -0700463 const InstructionSet instruction_set_;
464
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000465 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000466 HNullConstant* cached_null_constant_;
467 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000468 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000469 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000470 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000471
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100472 HCurrentMethod* cached_current_method_;
473
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000474 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100475 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000476 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000477 DISALLOW_COPY_AND_ASSIGN(HGraph);
478};
479
Vladimir Markof9f64412015-09-02 14:05:49 +0100480class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000481 public:
482 HLoopInformation(HBasicBlock* header, HGraph* graph)
483 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100484 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100485 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100486 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100487 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
488 back_edges_.reserve(kDefaultNumberOfBackEdges);
489 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100490
491 HBasicBlock* GetHeader() const {
492 return header_;
493 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000494
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000495 void SetHeader(HBasicBlock* block) {
496 header_ = block;
497 }
498
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100499 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
500 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
501 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
502
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000503 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100504 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000505 }
506
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100507 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100508 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100509 }
510
David Brazdil46e2a392015-03-16 17:31:52 +0000511 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100512 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100513 }
514
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000515 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100516 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000517 }
518
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100519 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100520
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100521 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100522 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100523 }
524
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100525 // Returns the lifetime position of the back edge that has the
526 // greatest lifetime position.
527 size_t GetLifetimeEnd() const;
528
529 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100530 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100531 }
532
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100533 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100534 // that is the header dominates the back edge.
535 bool Populate();
536
David Brazdila4b8c212015-05-07 09:59:30 +0100537 // Reanalyzes the loop by removing loop info from its blocks and re-running
538 // Populate(). If there are no back edges left, the loop info is completely
539 // removed as well as its SuspendCheck instruction. It must be run on nested
540 // inner loops first.
541 void Update();
542
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100543 // Returns whether this loop information contains `block`.
544 // Note that this loop information *must* be populated before entering this function.
545 bool Contains(const HBasicBlock& block) const;
546
547 // Returns whether this loop information is an inner loop of `other`.
548 // Note that `other` *must* be populated before entering this function.
549 bool IsIn(const HLoopInformation& other) const;
550
551 const ArenaBitVector& GetBlocks() const { return blocks_; }
552
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000553 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000554 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000555
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000556 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100557 // Internal recursive implementation of `Populate`.
558 void PopulateRecursive(HBasicBlock* block);
559
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000560 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100561 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100562 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100563 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000564
565 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
566};
567
David Brazdilec16f792015-08-19 15:04:01 +0100568// Stores try/catch information for basic blocks.
569// Note that HGraph is constructed so that catch blocks cannot simultaneously
570// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100571class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100572 public:
573 // Try block information constructor.
574 explicit TryCatchInformation(const HTryBoundary& try_entry)
575 : try_entry_(&try_entry),
576 catch_dex_file_(nullptr),
577 catch_type_index_(DexFile::kDexNoIndex16) {
578 DCHECK(try_entry_ != nullptr);
579 }
580
581 // Catch block information constructor.
582 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
583 : try_entry_(nullptr),
584 catch_dex_file_(&dex_file),
585 catch_type_index_(catch_type_index) {}
586
587 bool IsTryBlock() const { return try_entry_ != nullptr; }
588
589 const HTryBoundary& GetTryEntry() const {
590 DCHECK(IsTryBlock());
591 return *try_entry_;
592 }
593
594 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
595
596 bool IsCatchAllTypeIndex() const {
597 DCHECK(IsCatchBlock());
598 return catch_type_index_ == DexFile::kDexNoIndex16;
599 }
600
601 uint16_t GetCatchTypeIndex() const {
602 DCHECK(IsCatchBlock());
603 return catch_type_index_;
604 }
605
606 const DexFile& GetCatchDexFile() const {
607 DCHECK(IsCatchBlock());
608 return *catch_dex_file_;
609 }
610
611 private:
612 // One of possibly several TryBoundary instructions entering the block's try.
613 // Only set for try blocks.
614 const HTryBoundary* try_entry_;
615
616 // Exception type information. Only set for catch blocks.
617 const DexFile* catch_dex_file_;
618 const uint16_t catch_type_index_;
619};
620
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100621static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100622static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100623
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000624// A block in a method. Contains the list of instructions represented
625// as a double linked list. Each block knows its predecessors and
626// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100627
Vladimir Markof9f64412015-09-02 14:05:49 +0100628class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000629 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600630 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000631 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000632 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
633 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000634 loop_information_(nullptr),
635 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000636 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100637 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100638 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100639 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000640 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000641 try_catch_information_(nullptr) {
642 predecessors_.reserve(kDefaultNumberOfPredecessors);
643 successors_.reserve(kDefaultNumberOfSuccessors);
644 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
645 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000646
Vladimir Marko60584552015-09-03 13:35:12 +0000647 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100648 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000649 }
650
Vladimir Marko60584552015-09-03 13:35:12 +0000651 HBasicBlock* GetPredecessor(size_t pred_idx) const {
652 DCHECK_LT(pred_idx, predecessors_.size());
653 return predecessors_[pred_idx];
654 }
655
656 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100657 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000658 }
659
Vladimir Marko60584552015-09-03 13:35:12 +0000660 HBasicBlock* GetSuccessor(size_t succ_idx) const {
661 DCHECK_LT(succ_idx, successors_.size());
662 return successors_[succ_idx];
663 }
664
665 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
666 return ContainsElement(successors_, block, start_from);
667 }
668
669 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100670 return dominated_blocks_;
671 }
672
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100673 bool IsEntryBlock() const {
674 return graph_->GetEntryBlock() == this;
675 }
676
677 bool IsExitBlock() const {
678 return graph_->GetExitBlock() == this;
679 }
680
David Brazdil46e2a392015-03-16 17:31:52 +0000681 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000682 bool IsSingleTryBoundary() const;
683
684 // Returns true if this block emits nothing but a jump.
685 bool IsSingleJump() const {
686 HLoopInformation* loop_info = GetLoopInformation();
687 return (IsSingleGoto() || IsSingleTryBoundary())
688 // Back edges generate a suspend check.
689 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
690 }
David Brazdil46e2a392015-03-16 17:31:52 +0000691
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000692 void AddBackEdge(HBasicBlock* back_edge) {
693 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000694 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000695 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100696 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000697 loop_information_->AddBackEdge(back_edge);
698 }
699
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000700 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000701 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000702
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100703 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000704 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600705 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000706
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000707 HBasicBlock* GetDominator() const { return dominator_; }
708 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000709 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
710
711 void RemoveDominatedBlock(HBasicBlock* block) {
712 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100713 }
Vladimir Marko60584552015-09-03 13:35:12 +0000714
715 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
716 ReplaceElement(dominated_blocks_, existing, new_block);
717 }
718
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100719 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000720
721 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100722 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000723 }
724
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100725 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
726 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100727 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100728 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100729 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
730 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000731
732 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000733 successors_.push_back(block);
734 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000735 }
736
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100737 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
738 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100739 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000740 new_block->predecessors_.push_back(this);
741 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000742 }
743
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000744 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
745 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000746 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000747 new_block->successors_.push_back(this);
748 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000749 }
750
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100751 // Insert `this` between `predecessor` and `successor. This method
752 // preserves the indicies, and will update the first edge found between
753 // `predecessor` and `successor`.
754 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
755 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100756 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000757 successor->predecessors_[predecessor_index] = this;
758 predecessor->successors_[successor_index] = this;
759 successors_.push_back(successor);
760 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100761 }
762
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100763 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000764 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100765 }
766
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000767 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000768 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000769 }
770
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100771 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000772 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100773 }
774
775 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000776 predecessors_.push_back(block);
777 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100778 }
779
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100780 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000781 DCHECK_EQ(predecessors_.size(), 2u);
782 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100783 }
784
David Brazdil769c9e52015-04-27 13:54:09 +0100785 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000786 DCHECK_EQ(successors_.size(), 2u);
787 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100788 }
789
David Brazdilfc6a86a2015-06-26 10:33:45 +0000790 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000791 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100792 }
793
David Brazdilfc6a86a2015-06-26 10:33:45 +0000794 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000795 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100796 }
797
David Brazdilfc6a86a2015-06-26 10:33:45 +0000798 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000799 DCHECK_EQ(GetPredecessors().size(), 1u);
800 return GetPredecessor(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000801 }
802
803 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000804 DCHECK_EQ(GetSuccessors().size(), 1u);
805 return GetSuccessor(0);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000806 }
807
808 // Returns whether the first occurrence of `predecessor` in the list of
809 // predecessors is at index `idx`.
810 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000811 DCHECK_EQ(GetPredecessor(idx), predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000812 return GetPredecessorIndexOf(predecessor) == idx;
813 }
814
David Brazdilffee3d32015-07-06 11:48:53 +0100815 // Returns the number of non-exceptional successors. SsaChecker ensures that
816 // these are stored at the beginning of the successor list.
817 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000818 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100819 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000820
David Brazdild7558da2015-09-22 13:04:14 +0100821 // Create a new block between this block and its predecessors. The new block
822 // is added to the graph, all predecessor edges are relinked to it and an edge
823 // is created to `this`. Returns the new empty block. Reverse post order or
824 // loop and try/catch information are not updated.
825 HBasicBlock* CreateImmediateDominator();
826
David Brazdilfc6a86a2015-06-26 10:33:45 +0000827 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100828 // created, latter block. Note that this method will add the block to the
829 // graph, create a Goto at the end of the former block and will create an edge
830 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100831 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000832 HBasicBlock* SplitBefore(HInstruction* cursor);
833
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000834 // Split the block into two blocks just after `cursor`. Returns the newly
835 // created block. Note that this method just updates raw block information,
836 // like predecessors, successors, dominators, and instruction list. It does not
837 // update the graph, reverse post order, loop information, nor make sure the
838 // blocks are consistent (for example ending with a control flow instruction).
839 HBasicBlock* SplitAfter(HInstruction* cursor);
840
841 // Merge `other` at the end of `this`. Successors and dominated blocks of
842 // `other` are changed to be successors and dominated blocks of `this`. Note
843 // that this method does not update the graph, reverse post order, loop
844 // information, nor make sure the blocks are consistent (for example ending
845 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100846 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000847
848 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
849 // of `this` are moved to `other`.
850 // Note that this method does not update the graph, reverse post order, loop
851 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000852 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000853 void ReplaceWith(HBasicBlock* other);
854
David Brazdil2d7352b2015-04-20 14:52:42 +0100855 // Merge `other` at the end of `this`. This method updates loops, reverse post
856 // order, links to predecessors, successors, dominators and deletes the block
857 // from the graph. The two blocks must be successive, i.e. `this` the only
858 // predecessor of `other` and vice versa.
859 void MergeWith(HBasicBlock* other);
860
861 // Disconnects `this` from all its predecessors, successors and dominator,
862 // removes it from all loops it is included in and eventually from the graph.
863 // The block must not dominate any other block. Predecessors and successors
864 // are safely updated.
865 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000866
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000867 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100868 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100869 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100870 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100871 // Replace instruction `initial` with `replacement` within this block.
872 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
873 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100874 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100875 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000876 // RemoveInstruction and RemovePhi delete a given instruction from the respective
877 // instruction list. With 'ensure_safety' set to true, it verifies that the
878 // instruction is not in use and removes it from the use lists of its inputs.
879 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
880 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100881 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100882
883 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100884 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100885 }
886
Roland Levillain6b879dd2014-09-22 17:13:44 +0100887 bool IsLoopPreHeaderFirstPredecessor() const {
888 DCHECK(IsLoopHeader());
Vladimir Marko60584552015-09-03 13:35:12 +0000889 return GetPredecessor(0) == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100890 }
891
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100892 HLoopInformation* GetLoopInformation() const {
893 return loop_information_;
894 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000895
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000896 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100897 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000898 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100899 void SetInLoop(HLoopInformation* info) {
900 if (IsLoopHeader()) {
901 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100902 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100903 loop_information_ = info;
904 } else if (loop_information_->Contains(*info->GetHeader())) {
905 // Block is currently part of an outer loop. Make it part of this inner loop.
906 // Note that a non loop header having a loop information means this loop information
907 // has already been populated
908 loop_information_ = info;
909 } else {
910 // Block is part of an inner loop. Do not update the loop information.
911 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
912 // at this point, because this method is being called while populating `info`.
913 }
914 }
915
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000916 // Raw update of the loop information.
917 void SetLoopInformation(HLoopInformation* info) {
918 loop_information_ = info;
919 }
920
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100921 bool IsInLoop() const { return loop_information_ != nullptr; }
922
David Brazdilec16f792015-08-19 15:04:01 +0100923 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
924
925 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
926 try_catch_information_ = try_catch_information;
927 }
928
929 bool IsTryBlock() const {
930 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
931 }
932
933 bool IsCatchBlock() const {
934 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
935 }
David Brazdilffee3d32015-07-06 11:48:53 +0100936
937 // Returns the try entry that this block's successors should have. They will
938 // be in the same try, unless the block ends in a try boundary. In that case,
939 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100940 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100941
David Brazdild7558da2015-09-22 13:04:14 +0100942 bool HasThrowingInstructions() const;
943
David Brazdila4b8c212015-05-07 09:59:30 +0100944 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100945 bool Dominates(HBasicBlock* block) const;
946
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100947 size_t GetLifetimeStart() const { return lifetime_start_; }
948 size_t GetLifetimeEnd() const { return lifetime_end_; }
949
950 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
951 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
952
David Brazdil8d5b8b22015-03-24 10:51:52 +0000953 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000954 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100955 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000956 bool HasSinglePhi() const;
957
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000958 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000959 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000960 ArenaVector<HBasicBlock*> predecessors_;
961 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100962 HInstructionList instructions_;
963 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000964 HLoopInformation* loop_information_;
965 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000966 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100967 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100968 // The dex program counter of the first instruction of this block.
969 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100970 size_t lifetime_start_;
971 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100972 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100973
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000974 friend class HGraph;
975 friend class HInstruction;
976
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000977 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
978};
979
David Brazdilb2bd1c52015-03-25 11:17:37 +0000980// Iterates over the LoopInformation of all loops which contain 'block'
981// from the innermost to the outermost.
982class HLoopInformationOutwardIterator : public ValueObject {
983 public:
984 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
985 : current_(block.GetLoopInformation()) {}
986
987 bool Done() const { return current_ == nullptr; }
988
989 void Advance() {
990 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100991 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000992 }
993
994 HLoopInformation* Current() const {
995 DCHECK(!Done());
996 return current_;
997 }
998
999 private:
1000 HLoopInformation* current_;
1001
1002 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1003};
1004
Alexandre Ramesef20f712015-06-09 10:29:30 +01001005#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001006 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001007 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001008 M(ArrayGet, Instruction) \
1009 M(ArrayLength, Instruction) \
1010 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +01001011 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001012 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001013 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001014 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001015 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001016 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001017 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001018 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001019 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001020 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001021 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001022 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001023 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001024 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001025 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001026 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001027 M(FloatConstant, Constant) \
1028 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001029 M(GreaterThan, Condition) \
1030 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001031 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001032 M(InstanceFieldGet, Instruction) \
1033 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001034 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001035 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001036 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001037 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001038 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001039 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001040 M(LessThan, Condition) \
1041 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001042 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001043 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001044 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001045 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001046 M(Local, Instruction) \
1047 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001048 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001049 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001050 M(Mul, BinaryOperation) \
1051 M(Neg, UnaryOperation) \
1052 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001053 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001054 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001055 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001056 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001057 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001058 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001059 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001060 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001061 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001062 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001063 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001064 M(Return, Instruction) \
1065 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001066 M(Shl, BinaryOperation) \
1067 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001068 M(StaticFieldGet, Instruction) \
1069 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001070 M(StoreLocal, Instruction) \
1071 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001072 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001073 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001074 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001075 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001076 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001077 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001078 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001079
Alexandre Ramesef20f712015-06-09 10:29:30 +01001080#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1081
1082#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1083
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001084#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1085
Mark Mendell0616ae02015-04-17 12:49:27 -04001086#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1087 M(X86ComputeBaseMethodAddress, Instruction) \
1088 M(X86LoadFromConstantTable, Instruction)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001089
1090#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1091
1092#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1093 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1094 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1095 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001096 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001097 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1098 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1099
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001100#define FOR_EACH_INSTRUCTION(M) \
1101 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1102 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001103 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001104 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001105 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001106
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001107#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001108FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1109#undef FORWARD_DECLARATION
1110
Roland Levillainccc07a92014-09-16 14:48:16 +01001111#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001112 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1113 const char* DebugName() const OVERRIDE { return #type; } \
1114 const H##type* As##type() const OVERRIDE { return this; } \
1115 H##type* As##type() OVERRIDE { return this; } \
1116 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001117 return other->Is##type(); \
1118 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001119 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001120
David Brazdiled596192015-01-23 10:39:45 +00001121template <typename T> class HUseList;
1122
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001123template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001124class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001125 public:
David Brazdiled596192015-01-23 10:39:45 +00001126 HUseListNode* GetPrevious() const { return prev_; }
1127 HUseListNode* GetNext() const { return next_; }
1128 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001129 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001130 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001131
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001132 private:
David Brazdiled596192015-01-23 10:39:45 +00001133 HUseListNode(T user, size_t index)
1134 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1135
1136 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001137 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001138 HUseListNode<T>* prev_;
1139 HUseListNode<T>* next_;
1140
1141 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001142
1143 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1144};
1145
David Brazdiled596192015-01-23 10:39:45 +00001146template <typename T>
1147class HUseList : public ValueObject {
1148 public:
1149 HUseList() : first_(nullptr) {}
1150
1151 void Clear() {
1152 first_ = nullptr;
1153 }
1154
1155 // Adds a new entry at the beginning of the use list and returns
1156 // the newly created node.
1157 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001158 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001159 if (IsEmpty()) {
1160 first_ = new_node;
1161 } else {
1162 first_->prev_ = new_node;
1163 new_node->next_ = first_;
1164 first_ = new_node;
1165 }
1166 return new_node;
1167 }
1168
1169 HUseListNode<T>* GetFirst() const {
1170 return first_;
1171 }
1172
1173 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001174 DCHECK(node != nullptr);
1175 DCHECK(Contains(node));
1176
David Brazdiled596192015-01-23 10:39:45 +00001177 if (node->prev_ != nullptr) {
1178 node->prev_->next_ = node->next_;
1179 }
1180 if (node->next_ != nullptr) {
1181 node->next_->prev_ = node->prev_;
1182 }
1183 if (node == first_) {
1184 first_ = node->next_;
1185 }
1186 }
1187
David Brazdil1abb4192015-02-17 18:33:36 +00001188 bool Contains(const HUseListNode<T>* node) const {
1189 if (node == nullptr) {
1190 return false;
1191 }
1192 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1193 if (current == node) {
1194 return true;
1195 }
1196 }
1197 return false;
1198 }
1199
David Brazdiled596192015-01-23 10:39:45 +00001200 bool IsEmpty() const {
1201 return first_ == nullptr;
1202 }
1203
1204 bool HasOnlyOneUse() const {
1205 return first_ != nullptr && first_->next_ == nullptr;
1206 }
1207
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001208 size_t SizeSlow() const {
1209 size_t count = 0;
1210 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1211 ++count;
1212 }
1213 return count;
1214 }
1215
David Brazdiled596192015-01-23 10:39:45 +00001216 private:
1217 HUseListNode<T>* first_;
1218};
1219
1220template<typename T>
1221class HUseIterator : public ValueObject {
1222 public:
1223 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1224
1225 bool Done() const { return current_ == nullptr; }
1226
1227 void Advance() {
1228 DCHECK(!Done());
1229 current_ = current_->GetNext();
1230 }
1231
1232 HUseListNode<T>* Current() const {
1233 DCHECK(!Done());
1234 return current_;
1235 }
1236
1237 private:
1238 HUseListNode<T>* current_;
1239
1240 friend class HValue;
1241};
1242
David Brazdil1abb4192015-02-17 18:33:36 +00001243// This class is used by HEnvironment and HInstruction classes to record the
1244// instructions they use and pointers to the corresponding HUseListNodes kept
1245// by the used instructions.
1246template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001247class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001248 public:
1249 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1250 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1251
1252 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1253 : instruction_(old_record.instruction_), use_node_(use_node) {
1254 DCHECK(instruction_ != nullptr);
1255 DCHECK(use_node_ != nullptr);
1256 DCHECK(old_record.use_node_ == nullptr);
1257 }
1258
1259 HInstruction* GetInstruction() const { return instruction_; }
1260 HUseListNode<T>* GetUseNode() const { return use_node_; }
1261
1262 private:
1263 // Instruction used by the user.
1264 HInstruction* instruction_;
1265
1266 // Corresponding entry in the use list kept by 'instruction_'.
1267 HUseListNode<T>* use_node_;
1268};
1269
Aart Bik854a02b2015-07-14 16:07:00 -07001270/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001271 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001272 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001273 * For write/read dependences on fields/arrays, the dependence analysis uses
1274 * type disambiguation (e.g. a float field write cannot modify the value of an
1275 * integer field read) and the access type (e.g. a reference array write cannot
1276 * modify the value of a reference field read [although it may modify the
1277 * reference fetch prior to reading the field, which is represented by its own
1278 * write/read dependence]). The analysis makes conservative points-to
1279 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1280 * the same, and any reference read depends on any reference read without
1281 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001282 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001283 * The internal representation uses 38-bit and is described in the table below.
1284 * The first line indicates the side effect, and for field/array accesses the
1285 * second line indicates the type of the access (in the order of the
1286 * Primitive::Type enum).
1287 * The two numbered lines below indicate the bit position in the bitfield (read
1288 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001289 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001290 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1291 * +-------------+---------+---------+--------------+---------+---------+
1292 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1293 * | 3 |333333322|222222221| 1 |111111110|000000000|
1294 * | 7 |654321098|765432109| 8 |765432109|876543210|
1295 *
1296 * Note that, to ease the implementation, 'changes' bits are least significant
1297 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001298 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001299class SideEffects : public ValueObject {
1300 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001301 SideEffects() : flags_(0) {}
1302
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001303 static SideEffects None() {
1304 return SideEffects(0);
1305 }
1306
1307 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001308 return SideEffects(kAllChangeBits | kAllDependOnBits);
1309 }
1310
1311 static SideEffects AllChanges() {
1312 return SideEffects(kAllChangeBits);
1313 }
1314
1315 static SideEffects AllDependencies() {
1316 return SideEffects(kAllDependOnBits);
1317 }
1318
1319 static SideEffects AllExceptGCDependency() {
1320 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1321 }
1322
1323 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001324 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001325 }
1326
Aart Bik34c3ba92015-07-20 14:08:59 -07001327 static SideEffects AllWrites() {
1328 return SideEffects(kAllWrites);
1329 }
1330
1331 static SideEffects AllReads() {
1332 return SideEffects(kAllReads);
1333 }
1334
1335 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1336 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001337 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001338 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001339 }
1340
Aart Bik854a02b2015-07-14 16:07:00 -07001341 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1342 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001343 }
1344
Aart Bik34c3ba92015-07-20 14:08:59 -07001345 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1346 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001347 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001348 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001349 }
1350
1351 static SideEffects ArrayReadOfType(Primitive::Type type) {
1352 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1353 }
1354
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001355 static SideEffects CanTriggerGC() {
1356 return SideEffects(1ULL << kCanTriggerGCBit);
1357 }
1358
1359 static SideEffects DependsOnGC() {
1360 return SideEffects(1ULL << kDependsOnGCBit);
1361 }
1362
Aart Bik854a02b2015-07-14 16:07:00 -07001363 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001364 SideEffects Union(SideEffects other) const {
1365 return SideEffects(flags_ | other.flags_);
1366 }
1367
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001368 SideEffects Exclusion(SideEffects other) const {
1369 return SideEffects(flags_ & ~other.flags_);
1370 }
1371
1372 bool Includes(SideEffects other) const {
1373 return (other.flags_ & flags_) == other.flags_;
1374 }
1375
1376 bool HasSideEffects() const {
1377 return (flags_ & kAllChangeBits);
1378 }
1379
1380 bool HasDependencies() const {
1381 return (flags_ & kAllDependOnBits);
1382 }
1383
1384 // Returns true if there are no side effects or dependencies.
1385 bool DoesNothing() const {
1386 return flags_ == 0;
1387 }
1388
Aart Bik854a02b2015-07-14 16:07:00 -07001389 // Returns true if something is written.
1390 bool DoesAnyWrite() const {
1391 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001392 }
1393
Aart Bik854a02b2015-07-14 16:07:00 -07001394 // Returns true if something is read.
1395 bool DoesAnyRead() const {
1396 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001397 }
1398
Aart Bik854a02b2015-07-14 16:07:00 -07001399 // Returns true if potentially everything is written and read
1400 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001401 bool DoesAllReadWrite() const {
1402 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1403 }
1404
Aart Bik854a02b2015-07-14 16:07:00 -07001405 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001406 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001407 }
1408
1409 // Returns true if this may read something written by other.
1410 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001411 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1412 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001413 }
1414
1415 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001416 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001417 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001418 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001419 for (int s = kLastBit; s >= 0; s--) {
1420 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1421 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1422 // This is a bit for the GC side effect.
1423 if (current_bit_is_set) {
1424 flags += "GC";
1425 }
Aart Bik854a02b2015-07-14 16:07:00 -07001426 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001427 } else {
1428 // This is a bit for the array/field analysis.
1429 // The underscore character stands for the 'can trigger GC' bit.
1430 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1431 if (current_bit_is_set) {
1432 flags += kDebug[s];
1433 }
1434 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1435 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1436 flags += "|";
1437 }
1438 }
Aart Bik854a02b2015-07-14 16:07:00 -07001439 }
1440 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001441 }
1442
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001443 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001444
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001445 private:
1446 static constexpr int kFieldArrayAnalysisBits = 9;
1447
1448 static constexpr int kFieldWriteOffset = 0;
1449 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1450 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1451 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1452
1453 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1454
1455 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1456 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1457 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1458 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1459
1460 static constexpr int kLastBit = kDependsOnGCBit;
1461 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1462
1463 // Aliases.
1464
1465 static_assert(kChangeBits == kDependOnBits,
1466 "the 'change' bits should match the 'depend on' bits.");
1467
1468 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1469 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1470 static constexpr uint64_t kAllWrites =
1471 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1472 static constexpr uint64_t kAllReads =
1473 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001474
Aart Bik854a02b2015-07-14 16:07:00 -07001475 // Work around the fact that HIR aliases I/F and J/D.
1476 // TODO: remove this interceptor once HIR types are clean
1477 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1478 switch (type) {
1479 case Primitive::kPrimInt:
1480 case Primitive::kPrimFloat:
1481 return TypeFlag(Primitive::kPrimInt, offset) |
1482 TypeFlag(Primitive::kPrimFloat, offset);
1483 case Primitive::kPrimLong:
1484 case Primitive::kPrimDouble:
1485 return TypeFlag(Primitive::kPrimLong, offset) |
1486 TypeFlag(Primitive::kPrimDouble, offset);
1487 default:
1488 return TypeFlag(type, offset);
1489 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001490 }
1491
Aart Bik854a02b2015-07-14 16:07:00 -07001492 // Translates type to bit flag.
1493 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1494 CHECK_NE(type, Primitive::kPrimVoid);
1495 const uint64_t one = 1;
1496 const int shift = type; // 0-based consecutive enum
1497 DCHECK_LE(kFieldWriteOffset, shift);
1498 DCHECK_LT(shift, kArrayWriteOffset);
1499 return one << (type + offset);
1500 }
1501
1502 // Private constructor on direct flags value.
1503 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1504
1505 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001506};
1507
David Brazdiled596192015-01-23 10:39:45 +00001508// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001509class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001510 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001511 HEnvironment(ArenaAllocator* arena,
1512 size_t number_of_vregs,
1513 const DexFile& dex_file,
1514 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001515 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001516 InvokeType invoke_type,
1517 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001518 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1519 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001520 parent_(nullptr),
1521 dex_file_(dex_file),
1522 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001523 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001524 invoke_type_(invoke_type),
1525 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001526 }
1527
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001528 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001529 : HEnvironment(arena,
1530 to_copy.Size(),
1531 to_copy.GetDexFile(),
1532 to_copy.GetMethodIdx(),
1533 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001534 to_copy.GetInvokeType(),
1535 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001536
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001537 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001538 if (parent_ != nullptr) {
1539 parent_->SetAndCopyParentChain(allocator, parent);
1540 } else {
1541 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1542 parent_->CopyFrom(parent);
1543 if (parent->GetParent() != nullptr) {
1544 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1545 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001546 }
David Brazdiled596192015-01-23 10:39:45 +00001547 }
1548
Vladimir Marko71bf8092015-09-15 15:33:14 +01001549 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001550 void CopyFrom(HEnvironment* environment);
1551
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001552 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1553 // input to the loop phi instead. This is for inserting instructions that
1554 // require an environment (like HDeoptimization) in the loop pre-header.
1555 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001556
1557 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001558 DCHECK_LT(index, Size());
1559 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001560 }
1561
1562 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001563 DCHECK_LT(index, Size());
1564 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001565 }
1566
David Brazdil1abb4192015-02-17 18:33:36 +00001567 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001568
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001569 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001570
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001571 HEnvironment* GetParent() const { return parent_; }
1572
1573 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001574 DCHECK_LT(index, Size());
1575 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001576 }
1577
1578 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001579 DCHECK_LT(index, Size());
1580 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001581 }
1582
1583 uint32_t GetDexPc() const {
1584 return dex_pc_;
1585 }
1586
1587 uint32_t GetMethodIdx() const {
1588 return method_idx_;
1589 }
1590
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001591 InvokeType GetInvokeType() const {
1592 return invoke_type_;
1593 }
1594
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001595 const DexFile& GetDexFile() const {
1596 return dex_file_;
1597 }
1598
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001599 HInstruction* GetHolder() const {
1600 return holder_;
1601 }
1602
David Brazdiled596192015-01-23 10:39:45 +00001603 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001604 // Record instructions' use entries of this environment for constant-time removal.
1605 // It should only be called by HInstruction when a new environment use is added.
1606 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1607 DCHECK(env_use->GetUser() == this);
1608 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001609 DCHECK_LT(index, Size());
1610 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001611 }
David Brazdiled596192015-01-23 10:39:45 +00001612
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001613 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1614 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001615 HEnvironment* parent_;
1616 const DexFile& dex_file_;
1617 const uint32_t method_idx_;
1618 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001619 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001620
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001621 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001622 HInstruction* const holder_;
1623
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001624 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001625
1626 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1627};
1628
Calin Juravleacf735c2015-02-12 15:25:22 +00001629class ReferenceTypeInfo : ValueObject {
1630 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001631 typedef Handle<mirror::Class> TypeHandle;
1632
Calin Juravle2e768302015-07-28 14:41:11 +00001633 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1634 // The constructor will check that the type_handle is valid.
1635 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001636 }
1637
Calin Juravle2e768302015-07-28 14:41:11 +00001638 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1639
1640 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1641 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001642 }
1643
Calin Juravle2e768302015-07-28 14:41:11 +00001644 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1645 return IsValidHandle(type_handle_);
1646 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001647 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001648
1649 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1650 DCHECK(IsValid());
1651 return GetTypeHandle()->IsObjectClass();
1652 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001653 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001654 DCHECK(IsValid());
1655 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001656 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001657
1658 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1659
Mathieu Chartier90443472015-07-16 20:32:27 -07001660 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001661 DCHECK(IsValid());
1662 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001663 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1664 }
1665
1666 // Returns true if the type information provide the same amount of details.
1667 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001668 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001669 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001670 if (!IsValid() && !rti.IsValid()) {
1671 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001672 return true;
1673 }
Calin Juravle2e768302015-07-28 14:41:11 +00001674 if (!IsValid() || !rti.IsValid()) {
1675 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001676 return false;
1677 }
Calin Juravle2e768302015-07-28 14:41:11 +00001678 return IsExact() == rti.IsExact()
1679 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001680 }
1681
1682 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001683 ReferenceTypeInfo();
1684 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001685
Calin Juravleacf735c2015-02-12 15:25:22 +00001686 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001687 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001688 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001689 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001690 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001691};
1692
1693std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1694
Vladimir Markof9f64412015-09-02 14:05:49 +01001695class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001696 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001697 HInstruction(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001698 : previous_(nullptr),
1699 next_(nullptr),
1700 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001701 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001702 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001703 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001704 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001705 locations_(nullptr),
1706 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001707 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001708 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001709 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001710
Dave Allison20dfc792014-06-16 20:44:29 -07001711 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001712
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001713#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001714 enum InstructionKind {
1715 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1716 };
1717#undef DECLARE_KIND
1718
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001719 HInstruction* GetNext() const { return next_; }
1720 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001721
Calin Juravle77520bc2015-01-12 18:45:46 +00001722 HInstruction* GetNextDisregardingMoves() const;
1723 HInstruction* GetPreviousDisregardingMoves() const;
1724
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001725 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001726 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001727 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001728 bool IsInBlock() const { return block_ != nullptr; }
1729 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001730 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001731
Roland Levillain6b879dd2014-09-22 17:13:44 +01001732 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001733 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001734
1735 virtual void Accept(HGraphVisitor* visitor) = 0;
1736 virtual const char* DebugName() const = 0;
1737
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001738 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001739 void SetRawInputAt(size_t index, HInstruction* input) {
1740 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1741 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001742
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001743 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001744
1745 uint32_t GetDexPc() const { return dex_pc_; }
1746
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001747 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001748
Roland Levillaine161a2a2014-10-03 12:45:18 +01001749 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001750 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001751
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001752 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001753 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001754
Calin Juravle10e244f2015-01-26 18:54:32 +00001755 // Does not apply for all instructions, but having this at top level greatly
1756 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001757 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001758 virtual bool CanBeNull() const {
1759 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1760 return true;
1761 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001762
Calin Juravle641547a2015-04-21 22:08:51 +01001763 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1764 UNUSED(obj);
1765 return false;
1766 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001767
Calin Juravle2e768302015-07-28 14:41:11 +00001768 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001769
Calin Juravle61d544b2015-02-23 16:46:57 +00001770 ReferenceTypeInfo GetReferenceTypeInfo() const {
1771 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1772 return reference_type_info_;
1773 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001774
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001775 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001776 DCHECK(user != nullptr);
1777 HUseListNode<HInstruction*>* use =
1778 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1779 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001780 }
1781
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001782 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001783 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001784 HUseListNode<HEnvironment*>* env_use =
1785 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1786 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001787 }
1788
David Brazdil1abb4192015-02-17 18:33:36 +00001789 void RemoveAsUserOfInput(size_t input) {
1790 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1791 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1792 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001793
David Brazdil1abb4192015-02-17 18:33:36 +00001794 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1795 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001796
David Brazdiled596192015-01-23 10:39:45 +00001797 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1798 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001799 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001800 bool HasOnlyOneNonEnvironmentUse() const {
1801 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1802 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001803
Roland Levillain6c82d402014-10-13 16:10:27 +01001804 // Does this instruction strictly dominate `other_instruction`?
1805 // Returns false if this instruction and `other_instruction` are the same.
1806 // Aborts if this instruction and `other_instruction` are both phis.
1807 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001808
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001809 int GetId() const { return id_; }
1810 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001811
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001812 int GetSsaIndex() const { return ssa_index_; }
1813 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1814 bool HasSsaIndex() const { return ssa_index_ != -1; }
1815
1816 bool HasEnvironment() const { return environment_ != nullptr; }
1817 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001818 // Set the `environment_` field. Raw because this method does not
1819 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001820 void SetRawEnvironment(HEnvironment* environment) {
1821 DCHECK(environment_ == nullptr);
1822 DCHECK_EQ(environment->GetHolder(), this);
1823 environment_ = environment;
1824 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001825
1826 // Set the environment of this instruction, copying it from `environment`. While
1827 // copying, the uses lists are being updated.
1828 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001829 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001830 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001831 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001832 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001833 if (environment->GetParent() != nullptr) {
1834 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1835 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001836 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001837
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001838 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1839 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001840 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001841 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001842 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001843 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001844 if (environment->GetParent() != nullptr) {
1845 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1846 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001847 }
1848
Nicolas Geoffray39468442014-09-02 15:17:15 +01001849 // Returns the number of entries in the environment. Typically, that is the
1850 // number of dex registers in a method. It could be more in case of inlining.
1851 size_t EnvironmentSize() const;
1852
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001853 LocationSummary* GetLocations() const { return locations_; }
1854 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001856 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001857 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001858
Alexandre Rames188d4312015-04-09 18:30:21 +01001859 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1860 // uses of this instruction by `other` are *not* updated.
1861 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1862 ReplaceWith(other);
1863 other->ReplaceInput(this, use_index);
1864 }
1865
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001866 // Move `this` instruction before `cursor`.
1867 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001868
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001869#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001870 bool Is##type() const { return (As##type() != nullptr); } \
1871 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001872 virtual H##type* As##type() { return nullptr; }
1873
1874 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1875#undef INSTRUCTION_TYPE_CHECK
1876
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001877 // Returns whether the instruction can be moved within the graph.
1878 virtual bool CanBeMoved() const { return false; }
1879
1880 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001881 virtual bool InstructionTypeEquals(HInstruction* other) const {
1882 UNUSED(other);
1883 return false;
1884 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001885
1886 // Returns whether any data encoded in the two instructions is equal.
1887 // This method does not look at the inputs. Both instructions must be
1888 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001889 virtual bool InstructionDataEquals(HInstruction* other) const {
1890 UNUSED(other);
1891 return false;
1892 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001893
1894 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001895 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001896 // 2) Their inputs are identical.
1897 bool Equals(HInstruction* other) const;
1898
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001899 virtual InstructionKind GetKind() const = 0;
1900
1901 virtual size_t ComputeHashCode() const {
1902 size_t result = GetKind();
1903 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1904 result = (result * 31) + InputAt(i)->GetId();
1905 }
1906 return result;
1907 }
1908
1909 SideEffects GetSideEffects() const { return side_effects_; }
1910
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001911 size_t GetLifetimePosition() const { return lifetime_position_; }
1912 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1913 LiveInterval* GetLiveInterval() const { return live_interval_; }
1914 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1915 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1916
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001917 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1918
1919 // Returns whether the code generation of the instruction will require to have access
1920 // to the current method. Such instructions are:
1921 // (1): Instructions that require an environment, as calling the runtime requires
1922 // to walk the stack and have the current method stored at a specific stack address.
1923 // (2): Object literals like classes and strings, that are loaded from the dex cache
1924 // fields of the current method.
1925 bool NeedsCurrentMethod() const {
1926 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1927 }
1928
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001929 virtual bool NeedsDexCache() const { return false; }
1930
Mark Mendellc4701932015-04-10 13:18:51 -04001931 // Does this instruction have any use in an environment before
1932 // control flow hits 'other'?
1933 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1934
1935 // Remove all references to environment uses of this instruction.
1936 // The caller must ensure that this is safe to do.
1937 void RemoveEnvironmentUsers();
1938
David Brazdil1abb4192015-02-17 18:33:36 +00001939 protected:
1940 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1941 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1942
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001943 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001944 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1945
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001946 HInstruction* previous_;
1947 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001948 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001949 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001950
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001951 // An instruction gets an id when it is added to the graph.
1952 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001953 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001954 int id_;
1955
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001956 // When doing liveness analysis, instructions that have uses get an SSA index.
1957 int ssa_index_;
1958
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001959 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001960 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001961
1962 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001963 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001964
Nicolas Geoffray39468442014-09-02 15:17:15 +01001965 // The environment associated with this instruction. Not null if the instruction
1966 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001967 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001968
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001969 // Set by the code generator.
1970 LocationSummary* locations_;
1971
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001972 // Set by the liveness analysis.
1973 LiveInterval* live_interval_;
1974
1975 // Set by the liveness analysis, this is the position in a linear
1976 // order of blocks where this instruction's live interval start.
1977 size_t lifetime_position_;
1978
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001979 const SideEffects side_effects_;
1980
Calin Juravleacf735c2015-02-12 15:25:22 +00001981 // TODO: for primitive types this should be marked as invalid.
1982 ReferenceTypeInfo reference_type_info_;
1983
David Brazdil1abb4192015-02-17 18:33:36 +00001984 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001985 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001986 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001987 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001988 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001989
1990 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1991};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001992std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001993
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001994class HInputIterator : public ValueObject {
1995 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001996 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001997
1998 bool Done() const { return index_ == instruction_->InputCount(); }
1999 HInstruction* Current() const { return instruction_->InputAt(index_); }
2000 void Advance() { index_++; }
2001
2002 private:
2003 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002004 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002005
2006 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2007};
2008
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002009class HInstructionIterator : public ValueObject {
2010 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002011 explicit HInstructionIterator(const HInstructionList& instructions)
2012 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002013 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002014 }
2015
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002016 bool Done() const { return instruction_ == nullptr; }
2017 HInstruction* Current() const { return instruction_; }
2018 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002019 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002020 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002021 }
2022
2023 private:
2024 HInstruction* instruction_;
2025 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002026
2027 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002028};
2029
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002030class HBackwardInstructionIterator : public ValueObject {
2031 public:
2032 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2033 : instruction_(instructions.last_instruction_) {
2034 next_ = Done() ? nullptr : instruction_->GetPrevious();
2035 }
2036
2037 bool Done() const { return instruction_ == nullptr; }
2038 HInstruction* Current() const { return instruction_; }
2039 void Advance() {
2040 instruction_ = next_;
2041 next_ = Done() ? nullptr : instruction_->GetPrevious();
2042 }
2043
2044 private:
2045 HInstruction* instruction_;
2046 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002047
2048 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002049};
2050
Vladimir Markof9f64412015-09-02 14:05:49 +01002051template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002052class HTemplateInstruction: public HInstruction {
2053 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002054 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2055 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002056 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002057
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002058 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002059
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002060 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002061 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2062 DCHECK_LT(i, N);
2063 return inputs_[i];
2064 }
David Brazdil1abb4192015-02-17 18:33:36 +00002065
2066 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002067 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002068 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002069 }
2070
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002071 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002072 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002073
2074 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002075};
2076
Vladimir Markof9f64412015-09-02 14:05:49 +01002077// HTemplateInstruction specialization for N=0.
2078template<>
2079class HTemplateInstruction<0>: public HInstruction {
2080 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002081 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2082 : HInstruction(side_effects, dex_pc) {}
2083
Vladimir Markof9f64412015-09-02 14:05:49 +01002084 virtual ~HTemplateInstruction() {}
2085
2086 size_t InputCount() const OVERRIDE { return 0; }
2087
2088 protected:
2089 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2090 LOG(FATAL) << "Unreachable";
2091 UNREACHABLE();
2092 }
2093
2094 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2095 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2096 LOG(FATAL) << "Unreachable";
2097 UNREACHABLE();
2098 }
2099
2100 private:
2101 friend class SsaBuilder;
2102};
2103
Dave Allison20dfc792014-06-16 20:44:29 -07002104template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002105class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002106 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002107 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc = kNoDexPc)
2108 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002109 virtual ~HExpression() {}
2110
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002111 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002112
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002113 protected:
2114 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002115};
2116
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002117// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2118// instruction that branches to the exit block.
2119class HReturnVoid : public HTemplateInstruction<0> {
2120 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002121 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2122 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002123
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002124 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002125
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002126 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002127
2128 private:
2129 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2130};
2131
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002132// Represents dex's RETURN opcodes. A HReturn is a control flow
2133// instruction that branches to the exit block.
2134class HReturn : public HTemplateInstruction<1> {
2135 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002136 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2137 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002138 SetRawInputAt(0, value);
2139 }
2140
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002141 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002142
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002143 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002144
2145 private:
2146 DISALLOW_COPY_AND_ASSIGN(HReturn);
2147};
2148
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002149// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002150// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002151// exit block.
2152class HExit : public HTemplateInstruction<0> {
2153 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002154 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002155
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002156 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002157
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002158 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002159
2160 private:
2161 DISALLOW_COPY_AND_ASSIGN(HExit);
2162};
2163
2164// Jumps from one block to another.
2165class HGoto : public HTemplateInstruction<0> {
2166 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002167 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002168
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002169 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002170
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002171 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002172 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002173 }
2174
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002175 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002176
2177 private:
2178 DISALLOW_COPY_AND_ASSIGN(HGoto);
2179};
2180
Roland Levillain9867bc72015-08-05 10:21:34 +01002181class HConstant : public HExpression<0> {
2182 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002183 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2184 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002185
2186 bool CanBeMoved() const OVERRIDE { return true; }
2187
2188 virtual bool IsMinusOne() const { return false; }
2189 virtual bool IsZero() const { return false; }
2190 virtual bool IsOne() const { return false; }
2191
David Brazdil77a48ae2015-09-15 12:34:04 +00002192 virtual uint64_t GetValueAsUint64() const = 0;
2193
Roland Levillain9867bc72015-08-05 10:21:34 +01002194 DECLARE_INSTRUCTION(Constant);
2195
2196 private:
2197 DISALLOW_COPY_AND_ASSIGN(HConstant);
2198};
2199
2200class HNullConstant : public HConstant {
2201 public:
2202 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2203 return true;
2204 }
2205
David Brazdil77a48ae2015-09-15 12:34:04 +00002206 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2207
Roland Levillain9867bc72015-08-05 10:21:34 +01002208 size_t ComputeHashCode() const OVERRIDE { return 0; }
2209
2210 DECLARE_INSTRUCTION(NullConstant);
2211
2212 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002213 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002214
2215 friend class HGraph;
2216 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2217};
2218
2219// Constants of the type int. Those can be from Dex instructions, or
2220// synthesized (for example with the if-eqz instruction).
2221class HIntConstant : public HConstant {
2222 public:
2223 int32_t GetValue() const { return value_; }
2224
David Brazdil77a48ae2015-09-15 12:34:04 +00002225 uint64_t GetValueAsUint64() const OVERRIDE { return static_cast<uint64_t>(value_); }
2226
Roland Levillain9867bc72015-08-05 10:21:34 +01002227 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2228 DCHECK(other->IsIntConstant());
2229 return other->AsIntConstant()->value_ == value_;
2230 }
2231
2232 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2233
2234 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2235 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2236 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2237
2238 DECLARE_INSTRUCTION(IntConstant);
2239
2240 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002241 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2242 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2243 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2244 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002245
2246 const int32_t value_;
2247
2248 friend class HGraph;
2249 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2250 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2251 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2252};
2253
2254class HLongConstant : public HConstant {
2255 public:
2256 int64_t GetValue() const { return value_; }
2257
David Brazdil77a48ae2015-09-15 12:34:04 +00002258 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2259
Roland Levillain9867bc72015-08-05 10:21:34 +01002260 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2261 DCHECK(other->IsLongConstant());
2262 return other->AsLongConstant()->value_ == value_;
2263 }
2264
2265 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2266
2267 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2268 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2269 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2270
2271 DECLARE_INSTRUCTION(LongConstant);
2272
2273 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002274 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2275 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002276
2277 const int64_t value_;
2278
2279 friend class HGraph;
2280 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2281};
Dave Allison20dfc792014-06-16 20:44:29 -07002282
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002283// Conditional branch. A block ending with an HIf instruction must have
2284// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002285class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002286 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002287 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2288 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002289 SetRawInputAt(0, input);
2290 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002291
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002292 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002293
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002294 HBasicBlock* IfTrueSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002295 return GetBlock()->GetSuccessor(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002296 }
2297
2298 HBasicBlock* IfFalseSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002299 return GetBlock()->GetSuccessor(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002300 }
2301
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002302 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002303
2304 private:
2305 DISALLOW_COPY_AND_ASSIGN(HIf);
2306};
2307
David Brazdilfc6a86a2015-06-26 10:33:45 +00002308
2309// Abstract instruction which marks the beginning and/or end of a try block and
2310// links it to the respective exception handlers. Behaves the same as a Goto in
2311// non-exceptional control flow.
2312// Normal-flow successor is stored at index zero, exception handlers under
2313// higher indices in no particular order.
2314class HTryBoundary : public HTemplateInstruction<0> {
2315 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002316 enum BoundaryKind {
2317 kEntry,
2318 kExit,
2319 };
2320
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002321 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2322 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002323
2324 bool IsControlFlow() const OVERRIDE { return true; }
2325
2326 // Returns the block's non-exceptional successor (index zero).
Vladimir Marko60584552015-09-03 13:35:12 +00002327 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessor(0); }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002328
2329 // Returns whether `handler` is among its exception handlers (non-zero index
2330 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002331 bool HasExceptionHandler(const HBasicBlock& handler) const {
2332 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002333 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002334 }
2335
2336 // If not present already, adds `handler` to its block's list of exception
2337 // handlers.
2338 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002339 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002340 GetBlock()->AddSuccessor(handler);
2341 }
2342 }
2343
David Brazdil56e1acc2015-06-30 15:41:36 +01002344 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002345
David Brazdilffee3d32015-07-06 11:48:53 +01002346 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2347
David Brazdilfc6a86a2015-06-26 10:33:45 +00002348 DECLARE_INSTRUCTION(TryBoundary);
2349
2350 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002351 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002352
2353 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2354};
2355
David Brazdilffee3d32015-07-06 11:48:53 +01002356// Iterator over exception handlers of a given HTryBoundary, i.e. over
2357// exceptional successors of its basic block.
2358class HExceptionHandlerIterator : public ValueObject {
2359 public:
2360 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2361 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2362
Vladimir Marko60584552015-09-03 13:35:12 +00002363 bool Done() const { return index_ == block_.GetSuccessors().size(); }
2364 HBasicBlock* Current() const { return block_.GetSuccessor(index_); }
David Brazdilffee3d32015-07-06 11:48:53 +01002365 size_t CurrentSuccessorIndex() const { return index_; }
2366 void Advance() { ++index_; }
2367
2368 private:
2369 const HBasicBlock& block_;
2370 size_t index_;
2371
2372 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2373};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002374
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002375// Deoptimize to interpreter, upon checking a condition.
2376class HDeoptimize : public HTemplateInstruction<1> {
2377 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002378 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2379 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002380 SetRawInputAt(0, cond);
2381 }
2382
2383 bool NeedsEnvironment() const OVERRIDE { return true; }
2384 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002385
2386 DECLARE_INSTRUCTION(Deoptimize);
2387
2388 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002389 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2390};
2391
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002392// Represents the ArtMethod that was passed as a first argument to
2393// the method. It is used by instructions that depend on it, like
2394// instructions that work with the dex cache.
2395class HCurrentMethod : public HExpression<0> {
2396 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002397 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2398 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002399
2400 DECLARE_INSTRUCTION(CurrentMethod);
2401
2402 private:
2403 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2404};
2405
Mark Mendellfe57faa2015-09-18 09:26:15 -04002406// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2407// have one successor for each entry in the switch table, and the final successor
2408// will be the block containing the next Dex opcode.
2409class HPackedSwitch : public HTemplateInstruction<1> {
2410 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002411 HPackedSwitch(int32_t start_value,
2412 uint32_t num_entries,
2413 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002414 uint32_t dex_pc = kNoDexPc)
2415 : HTemplateInstruction(SideEffects::None(), dex_pc),
2416 start_value_(start_value),
2417 num_entries_(num_entries) {
2418 SetRawInputAt(0, input);
2419 }
2420
2421 bool IsControlFlow() const OVERRIDE { return true; }
2422
2423 int32_t GetStartValue() const { return start_value_; }
2424
Vladimir Marko211c2112015-09-24 16:52:33 +01002425 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002426
2427 HBasicBlock* GetDefaultBlock() const {
2428 // Last entry is the default block.
2429 return GetBlock()->GetSuccessor(num_entries_);
2430 }
2431 DECLARE_INSTRUCTION(PackedSwitch);
2432
2433 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002434 const int32_t start_value_;
2435 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002436
2437 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2438};
2439
Roland Levillain88cb1752014-10-20 16:36:47 +01002440class HUnaryOperation : public HExpression<1> {
2441 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002442 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2443 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002444 SetRawInputAt(0, input);
2445 }
2446
2447 HInstruction* GetInput() const { return InputAt(0); }
2448 Primitive::Type GetResultType() const { return GetType(); }
2449
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002450 bool CanBeMoved() const OVERRIDE { return true; }
2451 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002452 UNUSED(other);
2453 return true;
2454 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002455
Roland Levillain9240d6a2014-10-20 16:47:04 +01002456 // Try to statically evaluate `operation` and return a HConstant
2457 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002458 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002459 HConstant* TryStaticEvaluation() const;
2460
2461 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002462 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2463 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002464
Roland Levillain88cb1752014-10-20 16:36:47 +01002465 DECLARE_INSTRUCTION(UnaryOperation);
2466
2467 private:
2468 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2469};
2470
Dave Allison20dfc792014-06-16 20:44:29 -07002471class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002472 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002473 HBinaryOperation(Primitive::Type result_type,
2474 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002475 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002476 SideEffects side_effects = SideEffects::None(),
2477 uint32_t dex_pc = kNoDexPc)
2478 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002479 SetRawInputAt(0, left);
2480 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002481 }
2482
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002483 HInstruction* GetLeft() const { return InputAt(0); }
2484 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002485 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002486
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002487 virtual bool IsCommutative() const { return false; }
2488
2489 // Put constant on the right.
2490 // Returns whether order is changed.
2491 bool OrderInputsWithConstantOnTheRight() {
2492 HInstruction* left = InputAt(0);
2493 HInstruction* right = InputAt(1);
2494 if (left->IsConstant() && !right->IsConstant()) {
2495 ReplaceInput(right, 0);
2496 ReplaceInput(left, 1);
2497 return true;
2498 }
2499 return false;
2500 }
2501
2502 // Order inputs by instruction id, but favor constant on the right side.
2503 // This helps GVN for commutative ops.
2504 void OrderInputs() {
2505 DCHECK(IsCommutative());
2506 HInstruction* left = InputAt(0);
2507 HInstruction* right = InputAt(1);
2508 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2509 return;
2510 }
2511 if (OrderInputsWithConstantOnTheRight()) {
2512 return;
2513 }
2514 // Order according to instruction id.
2515 if (left->GetId() > right->GetId()) {
2516 ReplaceInput(right, 0);
2517 ReplaceInput(left, 1);
2518 }
2519 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002520
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002521 bool CanBeMoved() const OVERRIDE { return true; }
2522 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002523 UNUSED(other);
2524 return true;
2525 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002526
Roland Levillain9240d6a2014-10-20 16:47:04 +01002527 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002528 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002529 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002530 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002531
2532 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002533 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2534 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2535 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2536 HLongConstant* y ATTRIBUTE_UNUSED) const {
2537 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2538 return nullptr;
2539 }
2540 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2541 HIntConstant* y ATTRIBUTE_UNUSED) const {
2542 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2543 return nullptr;
2544 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002545
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002546 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002547 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002548 HConstant* GetConstantRight() const;
2549
2550 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002551 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002552 HInstruction* GetLeastConstantLeft() const;
2553
Roland Levillainccc07a92014-09-16 14:48:16 +01002554 DECLARE_INSTRUCTION(BinaryOperation);
2555
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002556 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002557 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2558};
2559
Mark Mendellc4701932015-04-10 13:18:51 -04002560// The comparison bias applies for floating point operations and indicates how NaN
2561// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002562enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002563 kNoBias, // bias is not applicable (i.e. for long operation)
2564 kGtBias, // return 1 for NaN comparisons
2565 kLtBias, // return -1 for NaN comparisons
2566};
2567
Dave Allison20dfc792014-06-16 20:44:29 -07002568class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002569 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002570 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2571 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002572 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002573 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002574
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002575 bool NeedsMaterialization() const { return needs_materialization_; }
2576 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002577
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002578 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002579 // `instruction`, and disregard moves in between.
2580 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002581
Dave Allison20dfc792014-06-16 20:44:29 -07002582 DECLARE_INSTRUCTION(Condition);
2583
2584 virtual IfCondition GetCondition() const = 0;
2585
Mark Mendellc4701932015-04-10 13:18:51 -04002586 virtual IfCondition GetOppositeCondition() const = 0;
2587
Roland Levillain4fa13f62015-07-06 18:11:54 +01002588 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002589
2590 void SetBias(ComparisonBias bias) { bias_ = bias; }
2591
2592 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2593 return bias_ == other->AsCondition()->bias_;
2594 }
2595
Roland Levillain4fa13f62015-07-06 18:11:54 +01002596 bool IsFPConditionTrueIfNaN() const {
2597 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2598 IfCondition if_cond = GetCondition();
2599 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2600 }
2601
2602 bool IsFPConditionFalseIfNaN() const {
2603 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2604 IfCondition if_cond = GetCondition();
2605 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2606 }
2607
Dave Allison20dfc792014-06-16 20:44:29 -07002608 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002609 // For register allocation purposes, returns whether this instruction needs to be
2610 // materialized (that is, not just be in the processor flags).
2611 bool needs_materialization_;
2612
Mark Mendellc4701932015-04-10 13:18:51 -04002613 // Needed if we merge a HCompare into a HCondition.
2614 ComparisonBias bias_;
2615
Dave Allison20dfc792014-06-16 20:44:29 -07002616 DISALLOW_COPY_AND_ASSIGN(HCondition);
2617};
2618
2619// Instruction to check if two inputs are equal to each other.
2620class HEqual : public HCondition {
2621 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002622 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2623 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002624
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002625 bool IsCommutative() const OVERRIDE { return true; }
2626
Roland Levillain9867bc72015-08-05 10:21:34 +01002627 template <typename T> bool Compute(T x, T y) const { return x == y; }
2628
2629 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002630 return GetBlock()->GetGraph()->GetIntConstant(
2631 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002632 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002633 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002634 return GetBlock()->GetGraph()->GetIntConstant(
2635 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002636 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002637
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002638 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002639
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002640 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002641 return kCondEQ;
2642 }
2643
Mark Mendellc4701932015-04-10 13:18:51 -04002644 IfCondition GetOppositeCondition() const OVERRIDE {
2645 return kCondNE;
2646 }
2647
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002648 private:
2649 DISALLOW_COPY_AND_ASSIGN(HEqual);
2650};
2651
Dave Allison20dfc792014-06-16 20:44:29 -07002652class HNotEqual : public HCondition {
2653 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002654 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2655 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002656
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002657 bool IsCommutative() const OVERRIDE { return true; }
2658
Roland Levillain9867bc72015-08-05 10:21:34 +01002659 template <typename T> bool Compute(T x, T y) const { return x != y; }
2660
2661 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002662 return GetBlock()->GetGraph()->GetIntConstant(
2663 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002664 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002665 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002666 return GetBlock()->GetGraph()->GetIntConstant(
2667 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002668 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002669
Dave Allison20dfc792014-06-16 20:44:29 -07002670 DECLARE_INSTRUCTION(NotEqual);
2671
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002672 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002673 return kCondNE;
2674 }
2675
Mark Mendellc4701932015-04-10 13:18:51 -04002676 IfCondition GetOppositeCondition() const OVERRIDE {
2677 return kCondEQ;
2678 }
2679
Dave Allison20dfc792014-06-16 20:44:29 -07002680 private:
2681 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2682};
2683
2684class HLessThan : public HCondition {
2685 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002686 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2687 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002688
Roland Levillain9867bc72015-08-05 10:21:34 +01002689 template <typename T> bool Compute(T x, T y) const { return x < y; }
2690
2691 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002692 return GetBlock()->GetGraph()->GetIntConstant(
2693 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002694 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002695 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002696 return GetBlock()->GetGraph()->GetIntConstant(
2697 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002698 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002699
Dave Allison20dfc792014-06-16 20:44:29 -07002700 DECLARE_INSTRUCTION(LessThan);
2701
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002702 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002703 return kCondLT;
2704 }
2705
Mark Mendellc4701932015-04-10 13:18:51 -04002706 IfCondition GetOppositeCondition() const OVERRIDE {
2707 return kCondGE;
2708 }
2709
Dave Allison20dfc792014-06-16 20:44:29 -07002710 private:
2711 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2712};
2713
2714class HLessThanOrEqual : public HCondition {
2715 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002716 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2717 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002718
Roland Levillain9867bc72015-08-05 10:21:34 +01002719 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2720
2721 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002722 return GetBlock()->GetGraph()->GetIntConstant(
2723 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002724 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002725 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002726 return GetBlock()->GetGraph()->GetIntConstant(
2727 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002728 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002729
Dave Allison20dfc792014-06-16 20:44:29 -07002730 DECLARE_INSTRUCTION(LessThanOrEqual);
2731
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002732 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002733 return kCondLE;
2734 }
2735
Mark Mendellc4701932015-04-10 13:18:51 -04002736 IfCondition GetOppositeCondition() const OVERRIDE {
2737 return kCondGT;
2738 }
2739
Dave Allison20dfc792014-06-16 20:44:29 -07002740 private:
2741 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2742};
2743
2744class HGreaterThan : public HCondition {
2745 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002746 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2747 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002748
Roland Levillain9867bc72015-08-05 10:21:34 +01002749 template <typename T> bool Compute(T x, T y) const { return x > y; }
2750
2751 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002752 return GetBlock()->GetGraph()->GetIntConstant(
2753 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002754 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002755 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002756 return GetBlock()->GetGraph()->GetIntConstant(
2757 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002758 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002759
Dave Allison20dfc792014-06-16 20:44:29 -07002760 DECLARE_INSTRUCTION(GreaterThan);
2761
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002762 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002763 return kCondGT;
2764 }
2765
Mark Mendellc4701932015-04-10 13:18:51 -04002766 IfCondition GetOppositeCondition() const OVERRIDE {
2767 return kCondLE;
2768 }
2769
Dave Allison20dfc792014-06-16 20:44:29 -07002770 private:
2771 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2772};
2773
2774class HGreaterThanOrEqual : public HCondition {
2775 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002776 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2777 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002778
Roland Levillain9867bc72015-08-05 10:21:34 +01002779 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2780
2781 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002782 return GetBlock()->GetGraph()->GetIntConstant(
2783 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002784 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002785 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002786 return GetBlock()->GetGraph()->GetIntConstant(
2787 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002788 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002789
Dave Allison20dfc792014-06-16 20:44:29 -07002790 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2791
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002792 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002793 return kCondGE;
2794 }
2795
Mark Mendellc4701932015-04-10 13:18:51 -04002796 IfCondition GetOppositeCondition() const OVERRIDE {
2797 return kCondLT;
2798 }
2799
Dave Allison20dfc792014-06-16 20:44:29 -07002800 private:
2801 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2802};
2803
2804
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002805// Instruction to check how two inputs compare to each other.
2806// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2807class HCompare : public HBinaryOperation {
2808 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002809 HCompare(Primitive::Type type,
2810 HInstruction* first,
2811 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002812 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002813 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002814 : HBinaryOperation(Primitive::kPrimInt,
2815 first,
2816 second,
2817 SideEffectsForArchRuntimeCalls(type),
2818 dex_pc),
2819 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002820 DCHECK_EQ(type, first->GetType());
2821 DCHECK_EQ(type, second->GetType());
2822 }
2823
Roland Levillain9867bc72015-08-05 10:21:34 +01002824 template <typename T>
2825 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002826
Roland Levillain9867bc72015-08-05 10:21:34 +01002827 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002828 return GetBlock()->GetGraph()->GetIntConstant(
2829 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002830 }
2831 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002832 return GetBlock()->GetGraph()->GetIntConstant(
2833 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002834 }
2835
Calin Juravleddb7df22014-11-25 20:56:51 +00002836 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2837 return bias_ == other->AsCompare()->bias_;
2838 }
2839
Mark Mendellc4701932015-04-10 13:18:51 -04002840 ComparisonBias GetBias() const { return bias_; }
2841
Roland Levillain4fa13f62015-07-06 18:11:54 +01002842 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002843
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002844
2845 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2846 // MIPS64 uses a runtime call for FP comparisons.
2847 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2848 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002849
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002850 DECLARE_INSTRUCTION(Compare);
2851
2852 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002853 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002854
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002855 DISALLOW_COPY_AND_ASSIGN(HCompare);
2856};
2857
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002858// A local in the graph. Corresponds to a Dex register.
2859class HLocal : public HTemplateInstruction<0> {
2860 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002861 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002862 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002863
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002864 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002865
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002866 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002867
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002868 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002869 // The Dex register number.
2870 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002871
2872 DISALLOW_COPY_AND_ASSIGN(HLocal);
2873};
2874
2875// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002876class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002877 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002878 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2879 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002880 SetRawInputAt(0, local);
2881 }
2882
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002883 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2884
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002885 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002886
2887 private:
2888 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2889};
2890
2891// Store a value in a given local. This instruction has two inputs: the value
2892// and the local.
2893class HStoreLocal : public HTemplateInstruction<2> {
2894 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002895 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
2896 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002897 SetRawInputAt(0, local);
2898 SetRawInputAt(1, value);
2899 }
2900
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002901 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2902
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002903 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002904
2905 private:
2906 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2907};
2908
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002909class HFloatConstant : public HConstant {
2910 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002911 float GetValue() const { return value_; }
2912
David Brazdil77a48ae2015-09-15 12:34:04 +00002913 uint64_t GetValueAsUint64() const OVERRIDE {
2914 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2915 }
2916
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002917 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002918 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002919 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002920 }
2921
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002922 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002923
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002924 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002925 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002926 }
2927 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002928 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002929 }
2930 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002931 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2932 }
2933 bool IsNaN() const {
2934 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002935 }
2936
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002937 DECLARE_INSTRUCTION(FloatConstant);
2938
2939 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002940 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2941 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2942 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2943 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002944
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002945 const float value_;
2946
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002947 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002948 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002949 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002950 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2951};
2952
2953class HDoubleConstant : public HConstant {
2954 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002955 double GetValue() const { return value_; }
2956
David Brazdil77a48ae2015-09-15 12:34:04 +00002957 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2958
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002959 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002960 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002961 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002962 }
2963
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002964 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002965
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002966 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002967 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002968 }
2969 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002970 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002971 }
2972 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002973 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2974 }
2975 bool IsNaN() const {
2976 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002977 }
2978
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002979 DECLARE_INSTRUCTION(DoubleConstant);
2980
2981 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002982 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2983 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2984 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2985 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002986
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002987 const double value_;
2988
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002989 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002990 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002991 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002992 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2993};
2994
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002995enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07002996#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002997#include "intrinsics_list.h"
2998 kNone,
2999 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3000#undef INTRINSICS_LIST
3001#undef OPTIMIZING_INTRINSICS
3002};
3003std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3004
Agi Csaki05f20562015-08-19 14:58:14 -07003005enum IntrinsicNeedsEnvironmentOrCache {
3006 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3007 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003008};
3009
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003010class HInvoke : public HInstruction {
3011 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003012 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003013
3014 // Runtime needs to walk the stack, so Dex -> Dex calls need to
3015 // know their environment.
Agi Csaki05f20562015-08-19 14:58:14 -07003016 bool NeedsEnvironment() const OVERRIDE {
3017 return needs_environment_or_cache_ == kNeedsEnvironmentOrCache;
3018 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003019
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003020 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003021 SetRawInputAt(index, argument);
3022 }
3023
Roland Levillain3e3d7332015-04-28 11:00:54 +01003024 // Return the number of arguments. This number can be lower than
3025 // the number of inputs returned by InputCount(), as some invoke
3026 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3027 // inputs at the end of their list of inputs.
3028 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3029
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003030 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003031
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003032
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003033 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003034 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003035
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003036 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3037
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003038 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003039 return intrinsic_;
3040 }
3041
Agi Csaki05f20562015-08-19 14:58:14 -07003042 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003043 intrinsic_ = intrinsic;
Agi Csaki05f20562015-08-19 14:58:14 -07003044 needs_environment_or_cache_ = needs_env_or_cache;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003045 }
3046
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003047 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003048 return GetEnvironment()->GetParent() != nullptr;
3049 }
3050
3051 bool CanThrow() const OVERRIDE { return true; }
3052
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003053 DECLARE_INSTRUCTION(Invoke);
3054
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003055 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003056 HInvoke(ArenaAllocator* arena,
3057 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003058 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003059 Primitive::Type return_type,
3060 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003061 uint32_t dex_method_index,
3062 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003063 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003064 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003065 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003066 inputs_(number_of_arguments + number_of_other_inputs,
3067 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003068 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003069 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003070 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003071 intrinsic_(Intrinsics::kNone),
Agi Csaki05f20562015-08-19 14:58:14 -07003072 needs_environment_or_cache_(kNeedsEnvironmentOrCache) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003073 }
3074
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003075 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
3076 DCHECK_LT(index, InputCount());
3077 return inputs_[index];
3078 }
3079
David Brazdil1abb4192015-02-17 18:33:36 +00003080 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003081 DCHECK_LT(index, InputCount());
3082 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003083 }
3084
Roland Levillain3e3d7332015-04-28 11:00:54 +01003085 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003086 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003087 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003088 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003089 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003090 Intrinsics intrinsic_;
Agi Csaki05f20562015-08-19 14:58:14 -07003091 IntrinsicNeedsEnvironmentOrCache needs_environment_or_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003092
3093 private:
3094 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3095};
3096
Calin Juravle175dc732015-08-25 15:42:32 +01003097class HInvokeUnresolved : public HInvoke {
3098 public:
3099 HInvokeUnresolved(ArenaAllocator* arena,
3100 uint32_t number_of_arguments,
3101 Primitive::Type return_type,
3102 uint32_t dex_pc,
3103 uint32_t dex_method_index,
3104 InvokeType invoke_type)
3105 : HInvoke(arena,
3106 number_of_arguments,
3107 0u /* number_of_other_inputs */,
3108 return_type,
3109 dex_pc,
3110 dex_method_index,
3111 invoke_type) {
3112 }
3113
3114 DECLARE_INSTRUCTION(InvokeUnresolved);
3115
3116 private:
3117 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3118};
3119
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003120class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003121 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003122 // Requirements of this method call regarding the class
3123 // initialization (clinit) check of its declaring class.
3124 enum class ClinitCheckRequirement {
3125 kNone, // Class already initialized.
3126 kExplicit, // Static call having explicit clinit check as last input.
3127 kImplicit, // Static call implicitly requiring a clinit check.
3128 };
3129
Vladimir Marko58155012015-08-19 12:49:41 +00003130 // Determines how to load the target ArtMethod*.
3131 enum class MethodLoadKind {
3132 // Use a String init ArtMethod* loaded from Thread entrypoints.
3133 kStringInit,
3134
3135 // Use the method's own ArtMethod* loaded by the register allocator.
3136 kRecursive,
3137
3138 // Use ArtMethod* at a known address, embed the direct address in the code.
3139 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3140 kDirectAddress,
3141
3142 // Use ArtMethod* at an address that will be known at link time, embed the direct
3143 // address in the code. If the image is relocatable, emit .patch_oat entry.
3144 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3145 // the image relocatable or not.
3146 kDirectAddressWithFixup,
3147
3148 // Load from resoved methods array in the dex cache using a PC-relative load.
3149 // Used when we need to use the dex cache, for example for invoke-static that
3150 // may cause class initialization (the entry may point to a resolution method),
3151 // and we know that we can access the dex cache arrays using a PC-relative load.
3152 kDexCachePcRelative,
3153
3154 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3155 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3156 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3157 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3158 kDexCacheViaMethod,
3159 };
3160
3161 // Determines the location of the code pointer.
3162 enum class CodePtrLocation {
3163 // Recursive call, use local PC-relative call instruction.
3164 kCallSelf,
3165
3166 // Use PC-relative call instruction patched at link time.
3167 // Used for calls within an oat file, boot->boot or app->app.
3168 kCallPCRelative,
3169
3170 // Call to a known target address, embed the direct address in code.
3171 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3172 kCallDirect,
3173
3174 // Call to a target address that will be known at link time, embed the direct
3175 // address in code. If the image is relocatable, emit .patch_oat entry.
3176 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3177 // the image relocatable or not.
3178 kCallDirectWithFixup,
3179
3180 // Use code pointer from the ArtMethod*.
3181 // Used when we don't know the target code. This is also the last-resort-kind used when
3182 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3183 kCallArtMethod,
3184 };
3185
3186 struct DispatchInfo {
3187 const MethodLoadKind method_load_kind;
3188 const CodePtrLocation code_ptr_location;
3189 // The method load data holds
3190 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3191 // Note that there are multiple string init methods, each having its own offset.
3192 // - the method address for kDirectAddress
3193 // - the dex cache arrays offset for kDexCachePcRel.
3194 const uint64_t method_load_data;
3195 const uint64_t direct_code_ptr;
3196 };
3197
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003198 HInvokeStaticOrDirect(ArenaAllocator* arena,
3199 uint32_t number_of_arguments,
3200 Primitive::Type return_type,
3201 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003202 uint32_t method_index,
3203 MethodReference target_method,
3204 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003205 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003206 InvokeType invoke_type,
3207 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003208 : HInvoke(arena,
3209 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003210 // There is one extra argument for the HCurrentMethod node, and
3211 // potentially one other if the clinit check is explicit, and one other
3212 // if the method is a string factory.
3213 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003214 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003215 return_type,
3216 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003217 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003218 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003219 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003220 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003221 target_method_(target_method),
3222 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003223
Calin Juravle641547a2015-04-21 22:08:51 +01003224 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3225 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003226 // We access the method via the dex cache so we can't do an implicit null check.
3227 // TODO: for intrinsics we can generate implicit null checks.
3228 return false;
3229 }
3230
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003231 bool CanBeNull() const OVERRIDE {
3232 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3233 }
3234
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003235 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003236 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3237 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3238 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Agi Csaki05f20562015-08-19 14:58:14 -07003239 bool NeedsDexCache() const OVERRIDE {
3240 if (intrinsic_ != Intrinsics::kNone) { return needs_environment_or_cache_; }
3241 return !IsRecursive() && !IsStringInit();
3242 }
Vladimir Marko58155012015-08-19 12:49:41 +00003243 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003244 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003245 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3246 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3247 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3248 MethodReference GetTargetMethod() const { return target_method_; }
3249
3250 int32_t GetStringInitOffset() const {
3251 DCHECK(IsStringInit());
3252 return dispatch_info_.method_load_data;
3253 }
3254
3255 uint64_t GetMethodAddress() const {
3256 DCHECK(HasMethodAddress());
3257 return dispatch_info_.method_load_data;
3258 }
3259
3260 uint32_t GetDexCacheArrayOffset() const {
3261 DCHECK(HasPcRelDexCache());
3262 return dispatch_info_.method_load_data;
3263 }
3264
3265 uint64_t GetDirectCodePtr() const {
3266 DCHECK(HasDirectCodePtr());
3267 return dispatch_info_.direct_code_ptr;
3268 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003269
Calin Juravle68ad6492015-08-18 17:08:12 +01003270 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3271
Roland Levillain4c0eb422015-04-24 16:43:49 +01003272 // Is this instruction a call to a static method?
3273 bool IsStatic() const {
3274 return GetInvokeType() == kStatic;
3275 }
3276
Roland Levillain3e3d7332015-04-28 11:00:54 +01003277 // Remove the art::HLoadClass instruction set as last input by
3278 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3279 // the initial art::HClinitCheck instruction (only relevant for
3280 // static calls with explicit clinit check).
3281 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003282 DCHECK(IsStaticWithExplicitClinitCheck());
3283 size_t last_input_index = InputCount() - 1;
3284 HInstruction* last_input = InputAt(last_input_index);
3285 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003286 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003287 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003288 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003289 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3290 DCHECK(IsStaticWithImplicitClinitCheck());
3291 }
3292
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003293 bool IsStringFactoryFor(HFakeString* str) const {
3294 if (!IsStringInit()) return false;
3295 // +1 for the current method.
3296 if (InputCount() == (number_of_arguments_ + 1)) return false;
3297 return InputAt(InputCount() - 1)->AsFakeString() == str;
3298 }
3299
3300 void RemoveFakeStringArgumentAsLastInput() {
3301 DCHECK(IsStringInit());
3302 size_t last_input_index = InputCount() - 1;
3303 HInstruction* last_input = InputAt(last_input_index);
3304 DCHECK(last_input != nullptr);
3305 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3306 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003307 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003308 }
3309
Roland Levillain4c0eb422015-04-24 16:43:49 +01003310 // Is this a call to a static method whose declaring class has an
3311 // explicit intialization check in the graph?
3312 bool IsStaticWithExplicitClinitCheck() const {
3313 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3314 }
3315
3316 // Is this a call to a static method whose declaring class has an
3317 // implicit intialization check requirement?
3318 bool IsStaticWithImplicitClinitCheck() const {
3319 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3320 }
3321
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003322 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003323
Roland Levillain4c0eb422015-04-24 16:43:49 +01003324 protected:
3325 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3326 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3327 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3328 HInstruction* input = input_record.GetInstruction();
3329 // `input` is the last input of a static invoke marked as having
3330 // an explicit clinit check. It must either be:
3331 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3332 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3333 DCHECK(input != nullptr);
3334 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3335 }
3336 return input_record;
3337 }
3338
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003339 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003340 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003341 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003342 // The target method may refer to different dex file or method index than the original
3343 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3344 // in derived class to increase visibility.
3345 MethodReference target_method_;
3346 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003347
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003348 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003349};
3350
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003351class HInvokeVirtual : public HInvoke {
3352 public:
3353 HInvokeVirtual(ArenaAllocator* arena,
3354 uint32_t number_of_arguments,
3355 Primitive::Type return_type,
3356 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003357 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003358 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003359 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003360 vtable_index_(vtable_index) {}
3361
Calin Juravle641547a2015-04-21 22:08:51 +01003362 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003363 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003364 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003365 }
3366
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003367 uint32_t GetVTableIndex() const { return vtable_index_; }
3368
3369 DECLARE_INSTRUCTION(InvokeVirtual);
3370
3371 private:
3372 const uint32_t vtable_index_;
3373
3374 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3375};
3376
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003377class HInvokeInterface : public HInvoke {
3378 public:
3379 HInvokeInterface(ArenaAllocator* arena,
3380 uint32_t number_of_arguments,
3381 Primitive::Type return_type,
3382 uint32_t dex_pc,
3383 uint32_t dex_method_index,
3384 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003385 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003386 imt_index_(imt_index) {}
3387
Calin Juravle641547a2015-04-21 22:08:51 +01003388 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003389 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003390 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003391 }
3392
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003393 uint32_t GetImtIndex() const { return imt_index_; }
3394 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3395
3396 DECLARE_INSTRUCTION(InvokeInterface);
3397
3398 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003399 const uint32_t imt_index_;
3400
3401 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3402};
3403
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003404class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003405 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003406 HNewInstance(HCurrentMethod* current_method,
3407 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003408 uint16_t type_index,
3409 const DexFile& dex_file,
3410 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003411 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003412 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003413 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003414 entrypoint_(entrypoint) {
3415 SetRawInputAt(0, current_method);
3416 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003417
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003418 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003419 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003420
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003421 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003422 bool NeedsEnvironment() const OVERRIDE { return true; }
3423 // It may throw when called on:
3424 // - interfaces
3425 // - abstract/innaccessible/unknown classes
3426 // TODO: optimize when possible.
3427 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003428
Calin Juravle10e244f2015-01-26 18:54:32 +00003429 bool CanBeNull() const OVERRIDE { return false; }
3430
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003431 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3432
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003433 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003434
3435 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003436 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003437 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003438 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003439
3440 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3441};
3442
Roland Levillain88cb1752014-10-20 16:36:47 +01003443class HNeg : public HUnaryOperation {
3444 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003445 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3446 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003447
Roland Levillain9867bc72015-08-05 10:21:34 +01003448 template <typename T> T Compute(T x) const { return -x; }
3449
3450 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003451 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003452 }
3453 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003454 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003455 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003456
Roland Levillain88cb1752014-10-20 16:36:47 +01003457 DECLARE_INSTRUCTION(Neg);
3458
3459 private:
3460 DISALLOW_COPY_AND_ASSIGN(HNeg);
3461};
3462
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003463class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003464 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003465 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003466 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003467 uint32_t dex_pc,
3468 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003469 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003470 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003471 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003472 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003473 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003474 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003475 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003476 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003477 }
3478
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003479 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003480 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003481
3482 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003483 bool NeedsEnvironment() const OVERRIDE { return true; }
3484
Mingyao Yang0c365e62015-03-31 15:09:29 -07003485 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3486 bool CanThrow() const OVERRIDE { return true; }
3487
Calin Juravle10e244f2015-01-26 18:54:32 +00003488 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003489
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003490 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3491
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003492 DECLARE_INSTRUCTION(NewArray);
3493
3494 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003495 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003496 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003497 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003498
3499 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3500};
3501
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003502class HAdd : public HBinaryOperation {
3503 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003504 HAdd(Primitive::Type result_type,
3505 HInstruction* left,
3506 HInstruction* right,
3507 uint32_t dex_pc = kNoDexPc)
3508 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003509
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003510 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003511
Roland Levillain9867bc72015-08-05 10:21:34 +01003512 template <typename T> T Compute(T x, T y) const { return x + y; }
3513
3514 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003515 return GetBlock()->GetGraph()->GetIntConstant(
3516 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003517 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003518 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003519 return GetBlock()->GetGraph()->GetLongConstant(
3520 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003521 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003522
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003523 DECLARE_INSTRUCTION(Add);
3524
3525 private:
3526 DISALLOW_COPY_AND_ASSIGN(HAdd);
3527};
3528
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003529class HSub : public HBinaryOperation {
3530 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003531 HSub(Primitive::Type result_type,
3532 HInstruction* left,
3533 HInstruction* right,
3534 uint32_t dex_pc = kNoDexPc)
3535 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003536
Roland Levillain9867bc72015-08-05 10:21:34 +01003537 template <typename T> T Compute(T x, T y) const { return x - y; }
3538
3539 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003540 return GetBlock()->GetGraph()->GetIntConstant(
3541 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003542 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003543 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003544 return GetBlock()->GetGraph()->GetLongConstant(
3545 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003546 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003547
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003548 DECLARE_INSTRUCTION(Sub);
3549
3550 private:
3551 DISALLOW_COPY_AND_ASSIGN(HSub);
3552};
3553
Calin Juravle34bacdf2014-10-07 20:23:36 +01003554class HMul : public HBinaryOperation {
3555 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003556 HMul(Primitive::Type result_type,
3557 HInstruction* left,
3558 HInstruction* right,
3559 uint32_t dex_pc = kNoDexPc)
3560 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003561
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003562 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003563
Roland Levillain9867bc72015-08-05 10:21:34 +01003564 template <typename T> T Compute(T x, T y) const { return x * y; }
3565
3566 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003567 return GetBlock()->GetGraph()->GetIntConstant(
3568 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003569 }
3570 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003571 return GetBlock()->GetGraph()->GetLongConstant(
3572 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003573 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003574
3575 DECLARE_INSTRUCTION(Mul);
3576
3577 private:
3578 DISALLOW_COPY_AND_ASSIGN(HMul);
3579};
3580
Calin Juravle7c4954d2014-10-28 16:57:40 +00003581class HDiv : public HBinaryOperation {
3582 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003583 HDiv(Primitive::Type result_type,
3584 HInstruction* left,
3585 HInstruction* right,
3586 uint32_t dex_pc)
3587 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003588
Roland Levillain9867bc72015-08-05 10:21:34 +01003589 template <typename T>
3590 T Compute(T x, T y) const {
3591 // Our graph structure ensures we never have 0 for `y` during
3592 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003593 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003594 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003595 return (y == -1) ? -x : x / y;
3596 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003597
Roland Levillain9867bc72015-08-05 10:21:34 +01003598 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003599 return GetBlock()->GetGraph()->GetIntConstant(
3600 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003601 }
3602 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003603 return GetBlock()->GetGraph()->GetLongConstant(
3604 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003605 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003606
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003607 static SideEffects SideEffectsForArchRuntimeCalls() {
3608 // The generated code can use a runtime call.
3609 return SideEffects::CanTriggerGC();
3610 }
3611
Calin Juravle7c4954d2014-10-28 16:57:40 +00003612 DECLARE_INSTRUCTION(Div);
3613
3614 private:
3615 DISALLOW_COPY_AND_ASSIGN(HDiv);
3616};
3617
Calin Juravlebacfec32014-11-14 15:54:36 +00003618class HRem : public HBinaryOperation {
3619 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003620 HRem(Primitive::Type result_type,
3621 HInstruction* left,
3622 HInstruction* right,
3623 uint32_t dex_pc)
3624 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003625
Roland Levillain9867bc72015-08-05 10:21:34 +01003626 template <typename T>
3627 T Compute(T x, T y) const {
3628 // Our graph structure ensures we never have 0 for `y` during
3629 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003630 DCHECK_NE(y, 0);
3631 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3632 return (y == -1) ? 0 : x % y;
3633 }
3634
Roland Levillain9867bc72015-08-05 10:21:34 +01003635 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003636 return GetBlock()->GetGraph()->GetIntConstant(
3637 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003638 }
3639 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003640 return GetBlock()->GetGraph()->GetLongConstant(
3641 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003642 }
3643
Calin Juravlebacfec32014-11-14 15:54:36 +00003644
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003645 static SideEffects SideEffectsForArchRuntimeCalls() {
3646 return SideEffects::CanTriggerGC();
3647 }
3648
Calin Juravlebacfec32014-11-14 15:54:36 +00003649 DECLARE_INSTRUCTION(Rem);
3650
3651 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003652 DISALLOW_COPY_AND_ASSIGN(HRem);
3653};
3654
Calin Juravled0d48522014-11-04 16:40:20 +00003655class HDivZeroCheck : public HExpression<1> {
3656 public:
3657 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003658 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003659 SetRawInputAt(0, value);
3660 }
3661
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003662 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3663
Calin Juravled0d48522014-11-04 16:40:20 +00003664 bool CanBeMoved() const OVERRIDE { return true; }
3665
3666 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3667 UNUSED(other);
3668 return true;
3669 }
3670
3671 bool NeedsEnvironment() const OVERRIDE { return true; }
3672 bool CanThrow() const OVERRIDE { return true; }
3673
Calin Juravled0d48522014-11-04 16:40:20 +00003674 DECLARE_INSTRUCTION(DivZeroCheck);
3675
3676 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003677 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3678};
3679
Calin Juravle9aec02f2014-11-18 23:06:35 +00003680class HShl : public HBinaryOperation {
3681 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003682 HShl(Primitive::Type result_type,
3683 HInstruction* left,
3684 HInstruction* right,
3685 uint32_t dex_pc = kNoDexPc)
3686 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003687
Roland Levillain9867bc72015-08-05 10:21:34 +01003688 template <typename T, typename U, typename V>
3689 T Compute(T x, U y, V max_shift_value) const {
3690 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3691 "V is not the unsigned integer type corresponding to T");
3692 return x << (y & max_shift_value);
3693 }
3694
3695 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3696 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003697 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003698 }
3699 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3700 // case is handled as `x << static_cast<int>(y)`.
3701 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3702 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003703 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003704 }
3705 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3706 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003707 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003708 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003709
3710 DECLARE_INSTRUCTION(Shl);
3711
3712 private:
3713 DISALLOW_COPY_AND_ASSIGN(HShl);
3714};
3715
3716class HShr : public HBinaryOperation {
3717 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003718 HShr(Primitive::Type result_type,
3719 HInstruction* left,
3720 HInstruction* right,
3721 uint32_t dex_pc = kNoDexPc)
3722 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003723
Roland Levillain9867bc72015-08-05 10:21:34 +01003724 template <typename T, typename U, typename V>
3725 T Compute(T x, U y, V max_shift_value) const {
3726 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3727 "V is not the unsigned integer type corresponding to T");
3728 return x >> (y & max_shift_value);
3729 }
3730
3731 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3732 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003733 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003734 }
3735 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3736 // case is handled as `x >> static_cast<int>(y)`.
3737 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3738 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003739 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003740 }
3741 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3742 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003743 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003744 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003745
3746 DECLARE_INSTRUCTION(Shr);
3747
3748 private:
3749 DISALLOW_COPY_AND_ASSIGN(HShr);
3750};
3751
3752class HUShr : public HBinaryOperation {
3753 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003754 HUShr(Primitive::Type result_type,
3755 HInstruction* left,
3756 HInstruction* right,
3757 uint32_t dex_pc = kNoDexPc)
3758 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003759
Roland Levillain9867bc72015-08-05 10:21:34 +01003760 template <typename T, typename U, typename V>
3761 T Compute(T x, U y, V max_shift_value) const {
3762 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3763 "V is not the unsigned integer type corresponding to T");
3764 V ux = static_cast<V>(x);
3765 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003766 }
3767
Roland Levillain9867bc72015-08-05 10:21:34 +01003768 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3769 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003770 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003771 }
3772 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3773 // case is handled as `x >>> static_cast<int>(y)`.
3774 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3775 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003776 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003777 }
3778 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3779 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003780 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003781 }
3782
3783 DECLARE_INSTRUCTION(UShr);
3784
3785 private:
3786 DISALLOW_COPY_AND_ASSIGN(HUShr);
3787};
3788
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003789class HAnd : public HBinaryOperation {
3790 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003791 HAnd(Primitive::Type result_type,
3792 HInstruction* left,
3793 HInstruction* right,
3794 uint32_t dex_pc = kNoDexPc)
3795 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003796
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003797 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003798
Roland Levillain9867bc72015-08-05 10:21:34 +01003799 template <typename T, typename U>
3800 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3801
3802 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003803 return GetBlock()->GetGraph()->GetIntConstant(
3804 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003805 }
3806 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003807 return GetBlock()->GetGraph()->GetLongConstant(
3808 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003809 }
3810 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003811 return GetBlock()->GetGraph()->GetLongConstant(
3812 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003813 }
3814 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003815 return GetBlock()->GetGraph()->GetLongConstant(
3816 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003817 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003818
3819 DECLARE_INSTRUCTION(And);
3820
3821 private:
3822 DISALLOW_COPY_AND_ASSIGN(HAnd);
3823};
3824
3825class HOr : public HBinaryOperation {
3826 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003827 HOr(Primitive::Type result_type,
3828 HInstruction* left,
3829 HInstruction* right,
3830 uint32_t dex_pc = kNoDexPc)
3831 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003832
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003833 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003834
Roland Levillain9867bc72015-08-05 10:21:34 +01003835 template <typename T, typename U>
3836 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3837
3838 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003839 return GetBlock()->GetGraph()->GetIntConstant(
3840 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003841 }
3842 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003843 return GetBlock()->GetGraph()->GetLongConstant(
3844 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003845 }
3846 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003847 return GetBlock()->GetGraph()->GetLongConstant(
3848 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003849 }
3850 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003851 return GetBlock()->GetGraph()->GetLongConstant(
3852 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003853 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003854
3855 DECLARE_INSTRUCTION(Or);
3856
3857 private:
3858 DISALLOW_COPY_AND_ASSIGN(HOr);
3859};
3860
3861class HXor : public HBinaryOperation {
3862 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003863 HXor(Primitive::Type result_type,
3864 HInstruction* left,
3865 HInstruction* right,
3866 uint32_t dex_pc = kNoDexPc)
3867 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003868
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003869 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003870
Roland Levillain9867bc72015-08-05 10:21:34 +01003871 template <typename T, typename U>
3872 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3873
3874 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003875 return GetBlock()->GetGraph()->GetIntConstant(
3876 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003877 }
3878 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003879 return GetBlock()->GetGraph()->GetLongConstant(
3880 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003881 }
3882 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003883 return GetBlock()->GetGraph()->GetLongConstant(
3884 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003885 }
3886 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003887 return GetBlock()->GetGraph()->GetLongConstant(
3888 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003889 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003890
3891 DECLARE_INSTRUCTION(Xor);
3892
3893 private:
3894 DISALLOW_COPY_AND_ASSIGN(HXor);
3895};
3896
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003897// The value of a parameter in this method. Its location depends on
3898// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003899class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003900 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003901 HParameterValue(uint8_t index,
3902 Primitive::Type parameter_type,
3903 bool is_this = false)
3904 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003905 index_(index),
3906 is_this_(is_this),
3907 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003908
3909 uint8_t GetIndex() const { return index_; }
3910
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003911 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3912 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003913
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003914 bool IsThis() const { return is_this_; }
3915
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003916 DECLARE_INSTRUCTION(ParameterValue);
3917
3918 private:
3919 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003920 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003921 const uint8_t index_;
3922
Calin Juravle10e244f2015-01-26 18:54:32 +00003923 // Whether or not the parameter value corresponds to 'this' argument.
3924 const bool is_this_;
3925
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003926 bool can_be_null_;
3927
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003928 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3929};
3930
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003931class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003932 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003933 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3934 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003935
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003936 bool CanBeMoved() const OVERRIDE { return true; }
3937 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003938 UNUSED(other);
3939 return true;
3940 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003941
Roland Levillain9867bc72015-08-05 10:21:34 +01003942 template <typename T> T Compute(T x) const { return ~x; }
3943
3944 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003945 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003946 }
3947 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003948 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003949 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003950
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003951 DECLARE_INSTRUCTION(Not);
3952
3953 private:
3954 DISALLOW_COPY_AND_ASSIGN(HNot);
3955};
3956
David Brazdil66d126e2015-04-03 16:02:44 +01003957class HBooleanNot : public HUnaryOperation {
3958 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003959 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3960 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01003961
3962 bool CanBeMoved() const OVERRIDE { return true; }
3963 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3964 UNUSED(other);
3965 return true;
3966 }
3967
Roland Levillain9867bc72015-08-05 10:21:34 +01003968 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003969 DCHECK(IsUint<1>(x));
3970 return !x;
3971 }
3972
Roland Levillain9867bc72015-08-05 10:21:34 +01003973 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003974 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003975 }
3976 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
3977 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01003978 UNREACHABLE();
3979 }
3980
3981 DECLARE_INSTRUCTION(BooleanNot);
3982
3983 private:
3984 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3985};
3986
Roland Levillaindff1f282014-11-05 14:15:05 +00003987class HTypeConversion : public HExpression<1> {
3988 public:
3989 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003990 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003991 : HExpression(result_type,
3992 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
3993 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003994 SetRawInputAt(0, input);
3995 DCHECK_NE(input->GetType(), result_type);
3996 }
3997
3998 HInstruction* GetInput() const { return InputAt(0); }
3999 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4000 Primitive::Type GetResultType() const { return GetType(); }
4001
Roland Levillain624279f2014-12-04 11:54:28 +00004002 // Required by the x86 and ARM code generators when producing calls
4003 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004004
Roland Levillaindff1f282014-11-05 14:15:05 +00004005 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004006 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004007
Mark Mendelle82549b2015-05-06 10:55:34 -04004008 // Try to statically evaluate the conversion and return a HConstant
4009 // containing the result. If the input cannot be converted, return nullptr.
4010 HConstant* TryStaticEvaluation() const;
4011
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004012 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4013 Primitive::Type result_type) {
4014 // Some architectures may not require the 'GC' side effects, but at this point
4015 // in the compilation process we do not know what architecture we will
4016 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004017 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4018 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004019 return SideEffects::CanTriggerGC();
4020 }
4021 return SideEffects::None();
4022 }
4023
Roland Levillaindff1f282014-11-05 14:15:05 +00004024 DECLARE_INSTRUCTION(TypeConversion);
4025
4026 private:
4027 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4028};
4029
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004030static constexpr uint32_t kNoRegNumber = -1;
4031
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004032class HPhi : public HInstruction {
4033 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004034 HPhi(ArenaAllocator* arena,
4035 uint32_t reg_number,
4036 size_t number_of_inputs,
4037 Primitive::Type type,
4038 uint32_t dex_pc = kNoDexPc)
4039 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004040 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004041 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004042 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004043 is_live_(false),
4044 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004045 }
4046
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004047 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4048 static Primitive::Type ToPhiType(Primitive::Type type) {
4049 switch (type) {
4050 case Primitive::kPrimBoolean:
4051 case Primitive::kPrimByte:
4052 case Primitive::kPrimShort:
4053 case Primitive::kPrimChar:
4054 return Primitive::kPrimInt;
4055 default:
4056 return type;
4057 }
4058 }
4059
David Brazdilffee3d32015-07-06 11:48:53 +01004060 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4061
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004062 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004063
4064 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004065 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004066
Calin Juravle10e244f2015-01-26 18:54:32 +00004067 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004068 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004069
Calin Juravle10e244f2015-01-26 18:54:32 +00004070 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4071 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4072
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004073 uint32_t GetRegNumber() const { return reg_number_; }
4074
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004075 void SetDead() { is_live_ = false; }
4076 void SetLive() { is_live_ = true; }
4077 bool IsDead() const { return !is_live_; }
4078 bool IsLive() const { return is_live_; }
4079
David Brazdil77a48ae2015-09-15 12:34:04 +00004080 bool IsVRegEquivalentOf(HInstruction* other) const {
4081 return other != nullptr
4082 && other->IsPhi()
4083 && other->AsPhi()->GetBlock() == GetBlock()
4084 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4085 }
4086
Calin Juravlea4f88312015-04-16 12:57:19 +01004087 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4088 // An equivalent phi is a phi having the same dex register and type.
4089 // It assumes that phis with the same dex register are adjacent.
4090 HPhi* GetNextEquivalentPhiWithSameType() {
4091 HInstruction* next = GetNext();
4092 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4093 if (next->GetType() == GetType()) {
4094 return next->AsPhi();
4095 }
4096 next = next->GetNext();
4097 }
4098 return nullptr;
4099 }
4100
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004101 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004102
David Brazdil1abb4192015-02-17 18:33:36 +00004103 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004104 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
4105 DCHECK_LE(index, InputCount());
4106 return inputs_[index];
4107 }
David Brazdil1abb4192015-02-17 18:33:36 +00004108
4109 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004110 DCHECK_LE(index, InputCount());
4111 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004112 }
4113
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004114 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004115 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004116 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004117 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004118 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004119 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004120
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004121 DISALLOW_COPY_AND_ASSIGN(HPhi);
4122};
4123
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004124class HNullCheck : public HExpression<1> {
4125 public:
4126 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004127 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004128 SetRawInputAt(0, value);
4129 }
4130
Calin Juravle10e244f2015-01-26 18:54:32 +00004131 bool CanBeMoved() const OVERRIDE { return true; }
4132 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004133 UNUSED(other);
4134 return true;
4135 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004136
Calin Juravle10e244f2015-01-26 18:54:32 +00004137 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004138
Calin Juravle10e244f2015-01-26 18:54:32 +00004139 bool CanThrow() const OVERRIDE { return true; }
4140
4141 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004142
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004143
4144 DECLARE_INSTRUCTION(NullCheck);
4145
4146 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004147 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4148};
4149
4150class FieldInfo : public ValueObject {
4151 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004152 FieldInfo(MemberOffset field_offset,
4153 Primitive::Type field_type,
4154 bool is_volatile,
4155 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004156 const DexFile& dex_file,
4157 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004158 : field_offset_(field_offset),
4159 field_type_(field_type),
4160 is_volatile_(is_volatile),
4161 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004162 dex_file_(dex_file),
4163 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004164
4165 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004166 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004167 uint32_t GetFieldIndex() const { return index_; }
4168 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004169 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004170 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004171
4172 private:
4173 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004174 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004175 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004176 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004177 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004178 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004179};
4180
4181class HInstanceFieldGet : public HExpression<1> {
4182 public:
4183 HInstanceFieldGet(HInstruction* value,
4184 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004185 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004186 bool is_volatile,
4187 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004188 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004189 Handle<mirror::DexCache> dex_cache,
4190 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004191 : HExpression(
4192 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004193 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004194 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004195 SetRawInputAt(0, value);
4196 }
4197
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004198 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004199
4200 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4201 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4202 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004203 }
4204
Calin Juravle641547a2015-04-21 22:08:51 +01004205 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4206 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004207 }
4208
4209 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004210 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4211 }
4212
Calin Juravle52c48962014-12-16 17:02:57 +00004213 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004214 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004215 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004216 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004217
4218 DECLARE_INSTRUCTION(InstanceFieldGet);
4219
4220 private:
4221 const FieldInfo field_info_;
4222
4223 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4224};
4225
4226class HInstanceFieldSet : public HTemplateInstruction<2> {
4227 public:
4228 HInstanceFieldSet(HInstruction* object,
4229 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004230 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004231 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004232 bool is_volatile,
4233 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004234 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004235 Handle<mirror::DexCache> dex_cache,
4236 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004237 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004238 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004239 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004240 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004241 SetRawInputAt(0, object);
4242 SetRawInputAt(1, value);
4243 }
4244
Calin Juravle641547a2015-04-21 22:08:51 +01004245 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4246 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004247 }
4248
Calin Juravle52c48962014-12-16 17:02:57 +00004249 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004250 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004251 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004252 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004253 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004254 bool GetValueCanBeNull() const { return value_can_be_null_; }
4255 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004256
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004257 DECLARE_INSTRUCTION(InstanceFieldSet);
4258
4259 private:
4260 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004261 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004262
4263 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4264};
4265
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004266class HArrayGet : public HExpression<2> {
4267 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004268 HArrayGet(HInstruction* array,
4269 HInstruction* index,
4270 Primitive::Type type,
4271 uint32_t dex_pc = kNoDexPc)
4272 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004273 SetRawInputAt(0, array);
4274 SetRawInputAt(1, index);
4275 }
4276
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004277 bool CanBeMoved() const OVERRIDE { return true; }
4278 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004279 UNUSED(other);
4280 return true;
4281 }
Calin Juravle641547a2015-04-21 22:08:51 +01004282 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4283 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004284 // TODO: We can be smarter here.
4285 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4286 // which generates the implicit null check. There are cases when these can be removed
4287 // to produce better code. If we ever add optimizations to do so we should allow an
4288 // implicit check here (as long as the address falls in the first page).
4289 return false;
4290 }
4291
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004292 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004293
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004294 HInstruction* GetArray() const { return InputAt(0); }
4295 HInstruction* GetIndex() const { return InputAt(1); }
4296
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004297 DECLARE_INSTRUCTION(ArrayGet);
4298
4299 private:
4300 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4301};
4302
4303class HArraySet : public HTemplateInstruction<3> {
4304 public:
4305 HArraySet(HInstruction* array,
4306 HInstruction* index,
4307 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004308 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004309 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004310 : HTemplateInstruction(
4311 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004312 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004313 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004314 needs_type_check_(value->GetType() == Primitive::kPrimNot),
4315 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004316 SetRawInputAt(0, array);
4317 SetRawInputAt(1, index);
4318 SetRawInputAt(2, value);
4319 }
4320
Calin Juravle77520bc2015-01-12 18:45:46 +00004321 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004322 // We currently always call a runtime method to catch array store
4323 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004324 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004325 }
4326
Mingyao Yang81014cb2015-06-02 03:16:27 -07004327 // Can throw ArrayStoreException.
4328 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4329
Calin Juravle641547a2015-04-21 22:08:51 +01004330 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4331 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004332 // TODO: Same as for ArrayGet.
4333 return false;
4334 }
4335
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004336 void ClearNeedsTypeCheck() {
4337 needs_type_check_ = false;
4338 }
4339
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004340 void ClearValueCanBeNull() {
4341 value_can_be_null_ = false;
4342 }
4343
4344 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004345 bool NeedsTypeCheck() const { return needs_type_check_; }
4346
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004347 HInstruction* GetArray() const { return InputAt(0); }
4348 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004349 HInstruction* GetValue() const { return InputAt(2); }
4350
4351 Primitive::Type GetComponentType() const {
4352 // The Dex format does not type floating point index operations. Since the
4353 // `expected_component_type_` is set during building and can therefore not
4354 // be correct, we also check what is the value type. If it is a floating
4355 // point type, we must use that type.
4356 Primitive::Type value_type = GetValue()->GetType();
4357 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4358 ? value_type
4359 : expected_component_type_;
4360 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004361
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004362 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4363 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4364 }
4365
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004366 DECLARE_INSTRUCTION(ArraySet);
4367
4368 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004369 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004370 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004371 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004372
4373 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4374};
4375
4376class HArrayLength : public HExpression<1> {
4377 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004378 explicit HArrayLength(HInstruction* array, uint32_t dex_pc = kNoDexPc)
4379 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004380 // Note that arrays do not change length, so the instruction does not
4381 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004382 SetRawInputAt(0, array);
4383 }
4384
Calin Juravle77520bc2015-01-12 18:45:46 +00004385 bool CanBeMoved() const OVERRIDE { return true; }
4386 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004387 UNUSED(other);
4388 return true;
4389 }
Calin Juravle641547a2015-04-21 22:08:51 +01004390 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4391 return obj == InputAt(0);
4392 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004393
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004394 DECLARE_INSTRUCTION(ArrayLength);
4395
4396 private:
4397 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4398};
4399
4400class HBoundsCheck : public HExpression<2> {
4401 public:
4402 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004403 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004404 DCHECK(index->GetType() == Primitive::kPrimInt);
4405 SetRawInputAt(0, index);
4406 SetRawInputAt(1, length);
4407 }
4408
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004409 bool CanBeMoved() const OVERRIDE { return true; }
4410 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004411 UNUSED(other);
4412 return true;
4413 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004414
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004415 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004416
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004417 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004418
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004419
4420 DECLARE_INSTRUCTION(BoundsCheck);
4421
4422 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004423 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4424};
4425
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004426/**
4427 * Some DEX instructions are folded into multiple HInstructions that need
4428 * to stay live until the last HInstruction. This class
4429 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004430 * HInstruction stays live. `index` represents the stack location index of the
4431 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004432 */
4433class HTemporary : public HTemplateInstruction<0> {
4434 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004435 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4436 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004437
4438 size_t GetIndex() const { return index_; }
4439
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004440 Primitive::Type GetType() const OVERRIDE {
4441 // The previous instruction is the one that will be stored in the temporary location.
4442 DCHECK(GetPrevious() != nullptr);
4443 return GetPrevious()->GetType();
4444 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004445
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004446 DECLARE_INSTRUCTION(Temporary);
4447
4448 private:
4449 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004450 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4451};
4452
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004453class HSuspendCheck : public HTemplateInstruction<0> {
4454 public:
4455 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004456 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004457
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004458 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004459 return true;
4460 }
4461
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004462 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4463 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004464
4465 DECLARE_INSTRUCTION(SuspendCheck);
4466
4467 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004468 // Only used for code generation, in order to share the same slow path between back edges
4469 // of a same loop.
4470 SlowPathCode* slow_path_;
4471
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004472 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4473};
4474
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004475/**
4476 * Instruction to load a Class object.
4477 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004478class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004479 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004480 HLoadClass(HCurrentMethod* current_method,
4481 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004482 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004483 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004484 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004485 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004486 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004487 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004488 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004489 generate_clinit_check_(false),
Calin Juravle2e768302015-07-28 14:41:11 +00004490 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004491 SetRawInputAt(0, current_method);
4492 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004493
4494 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004495
4496 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4497 return other->AsLoadClass()->type_index_ == type_index_;
4498 }
4499
4500 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4501
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004502 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004503 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004504 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004505
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004506 bool NeedsEnvironment() const OVERRIDE {
4507 // Will call runtime and load the class if the class is not loaded yet.
4508 // TODO: finer grain decision.
4509 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004510 }
4511
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004512 bool MustGenerateClinitCheck() const {
4513 return generate_clinit_check_;
4514 }
4515
Calin Juravle0ba218d2015-05-19 18:46:01 +01004516 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4517 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004518 }
4519
4520 bool CanCallRuntime() const {
4521 return MustGenerateClinitCheck() || !is_referrers_class_;
4522 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004523
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004524 bool CanThrow() const OVERRIDE {
4525 // May call runtime and and therefore can throw.
4526 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004527 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004528 }
4529
Calin Juravleacf735c2015-02-12 15:25:22 +00004530 ReferenceTypeInfo GetLoadedClassRTI() {
4531 return loaded_class_rti_;
4532 }
4533
4534 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4535 // Make sure we only set exact types (the loaded class should never be merged).
4536 DCHECK(rti.IsExact());
4537 loaded_class_rti_ = rti;
4538 }
4539
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004540 const DexFile& GetDexFile() { return dex_file_; }
4541
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004542 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4543
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004544 static SideEffects SideEffectsForArchRuntimeCalls() {
4545 return SideEffects::CanTriggerGC();
4546 }
4547
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004548 DECLARE_INSTRUCTION(LoadClass);
4549
4550 private:
4551 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004552 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004553 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004554 // Whether this instruction must generate the initialization check.
4555 // Used for code generation.
4556 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004557
Calin Juravleacf735c2015-02-12 15:25:22 +00004558 ReferenceTypeInfo loaded_class_rti_;
4559
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004560 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4561};
4562
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004563class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004564 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004565 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004566 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4567 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004568 SetRawInputAt(0, current_method);
4569 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004570
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004571 bool CanBeMoved() const OVERRIDE { return true; }
4572
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004573 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4574 return other->AsLoadString()->string_index_ == string_index_;
4575 }
4576
4577 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4578
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004579 uint32_t GetStringIndex() const { return string_index_; }
4580
4581 // TODO: Can we deopt or debug when we resolve a string?
4582 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004583 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004584 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004585
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004586 static SideEffects SideEffectsForArchRuntimeCalls() {
4587 return SideEffects::CanTriggerGC();
4588 }
4589
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004590 DECLARE_INSTRUCTION(LoadString);
4591
4592 private:
4593 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004594
4595 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4596};
4597
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004598/**
4599 * Performs an initialization check on its Class object input.
4600 */
4601class HClinitCheck : public HExpression<1> {
4602 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004603 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004604 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004605 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004606 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4607 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004608 SetRawInputAt(0, constant);
4609 }
4610
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004611 bool CanBeMoved() const OVERRIDE { return true; }
4612 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4613 UNUSED(other);
4614 return true;
4615 }
4616
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004617 bool NeedsEnvironment() const OVERRIDE {
4618 // May call runtime to initialize the class.
4619 return true;
4620 }
4621
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004622
4623 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4624
4625 DECLARE_INSTRUCTION(ClinitCheck);
4626
4627 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004628 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4629};
4630
4631class HStaticFieldGet : public HExpression<1> {
4632 public:
4633 HStaticFieldGet(HInstruction* cls,
4634 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004635 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004636 bool is_volatile,
4637 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004638 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004639 Handle<mirror::DexCache> dex_cache,
4640 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004641 : HExpression(
4642 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004643 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004644 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004645 SetRawInputAt(0, cls);
4646 }
4647
Calin Juravle52c48962014-12-16 17:02:57 +00004648
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004649 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004650
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004651 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004652 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4653 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004654 }
4655
4656 size_t ComputeHashCode() const OVERRIDE {
4657 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4658 }
4659
Calin Juravle52c48962014-12-16 17:02:57 +00004660 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004661 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4662 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004663 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004664
4665 DECLARE_INSTRUCTION(StaticFieldGet);
4666
4667 private:
4668 const FieldInfo field_info_;
4669
4670 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4671};
4672
4673class HStaticFieldSet : public HTemplateInstruction<2> {
4674 public:
4675 HStaticFieldSet(HInstruction* cls,
4676 HInstruction* value,
4677 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004678 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004679 bool is_volatile,
4680 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004681 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004682 Handle<mirror::DexCache> dex_cache,
4683 uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004684 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004685 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004686 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004687 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004688 SetRawInputAt(0, cls);
4689 SetRawInputAt(1, value);
4690 }
4691
Calin Juravle52c48962014-12-16 17:02:57 +00004692 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004693 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4694 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004695 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004696
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004697 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004698 bool GetValueCanBeNull() const { return value_can_be_null_; }
4699 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004700
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004701 DECLARE_INSTRUCTION(StaticFieldSet);
4702
4703 private:
4704 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004705 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004706
4707 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4708};
4709
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004710// Implement the move-exception DEX instruction.
4711class HLoadException : public HExpression<0> {
4712 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004713 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4714 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004715
David Brazdilbbd733e2015-08-18 17:48:17 +01004716 bool CanBeNull() const OVERRIDE { return false; }
4717
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004718 DECLARE_INSTRUCTION(LoadException);
4719
4720 private:
4721 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4722};
4723
David Brazdilcb1c0552015-08-04 16:22:25 +01004724// Implicit part of move-exception which clears thread-local exception storage.
4725// Must not be removed because the runtime expects the TLS to get cleared.
4726class HClearException : public HTemplateInstruction<0> {
4727 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004728 explicit HClearException(uint32_t dex_pc = kNoDexPc)
4729 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01004730
4731 DECLARE_INSTRUCTION(ClearException);
4732
4733 private:
4734 DISALLOW_COPY_AND_ASSIGN(HClearException);
4735};
4736
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004737class HThrow : public HTemplateInstruction<1> {
4738 public:
4739 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004740 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004741 SetRawInputAt(0, exception);
4742 }
4743
4744 bool IsControlFlow() const OVERRIDE { return true; }
4745
4746 bool NeedsEnvironment() const OVERRIDE { return true; }
4747
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004748 bool CanThrow() const OVERRIDE { return true; }
4749
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004750
4751 DECLARE_INSTRUCTION(Throw);
4752
4753 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004754 DISALLOW_COPY_AND_ASSIGN(HThrow);
4755};
4756
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004757/**
4758 * Implementation strategies for the code generator of a HInstanceOf
4759 * or `HCheckCast`.
4760 */
4761enum class TypeCheckKind {
4762 kExactCheck, // Can do a single class compare.
4763 kClassHierarchyCheck, // Can just walk the super class chain.
4764 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
4765 kInterfaceCheck, // No optimization yet when checking against an interface.
4766 kArrayObjectCheck, // Can just check if the array is not primitive.
4767 kArrayCheck // No optimization yet when checking against a generic array.
4768};
4769
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004770class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004771 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004772 HInstanceOf(HInstruction* object,
4773 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004774 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004775 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004776 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004777 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004778 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004779 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004780 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004781 SetRawInputAt(0, object);
4782 SetRawInputAt(1, constant);
4783 }
4784
4785 bool CanBeMoved() const OVERRIDE { return true; }
4786
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004787 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004788 return true;
4789 }
4790
4791 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004792 return false;
4793 }
4794
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004795 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
4796
4797 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004798
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004799 // Used only in code generation.
4800 bool MustDoNullCheck() const { return must_do_null_check_; }
4801 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4802
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004803 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
4804 return (check_kind == TypeCheckKind::kExactCheck)
4805 ? SideEffects::None()
4806 // Mips currently does runtime calls for any other checks.
4807 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004808 }
4809
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004810 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004811
4812 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004813 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004814 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004815
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004816 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4817};
4818
Calin Juravleb1498f62015-02-16 13:13:29 +00004819class HBoundType : public HExpression<1> {
4820 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004821 // Constructs an HBoundType with the given upper_bound.
4822 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004823 HBoundType(HInstruction* input,
4824 ReferenceTypeInfo upper_bound,
4825 bool upper_can_be_null,
4826 uint32_t dex_pc = kNoDexPc)
4827 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004828 upper_bound_(upper_bound),
4829 upper_can_be_null_(upper_can_be_null),
4830 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004831 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004832 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004833 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004834 }
4835
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004836 // GetUpper* should only be used in reference type propagation.
4837 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4838 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004839
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004840 void SetCanBeNull(bool can_be_null) {
4841 DCHECK(upper_can_be_null_ || !can_be_null);
4842 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004843 }
4844
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004845 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4846
Calin Juravleb1498f62015-02-16 13:13:29 +00004847 DECLARE_INSTRUCTION(BoundType);
4848
4849 private:
4850 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004851 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4852 // It is used to bound the type in cases like:
4853 // if (x instanceof ClassX) {
4854 // // uper_bound_ will be ClassX
4855 // }
4856 const ReferenceTypeInfo upper_bound_;
4857 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4858 // is false then can_be_null_ cannot be true).
4859 const bool upper_can_be_null_;
4860 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004861
4862 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4863};
4864
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004865class HCheckCast : public HTemplateInstruction<2> {
4866 public:
4867 HCheckCast(HInstruction* object,
4868 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004869 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004870 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004871 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004872 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004873 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004874 SetRawInputAt(0, object);
4875 SetRawInputAt(1, constant);
4876 }
4877
4878 bool CanBeMoved() const OVERRIDE { return true; }
4879
4880 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4881 return true;
4882 }
4883
4884 bool NeedsEnvironment() const OVERRIDE {
4885 // Instruction may throw a CheckCastError.
4886 return true;
4887 }
4888
4889 bool CanThrow() const OVERRIDE { return true; }
4890
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004891 bool MustDoNullCheck() const { return must_do_null_check_; }
4892 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004893 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004894
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004895 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004896
4897 DECLARE_INSTRUCTION(CheckCast);
4898
4899 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004900 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004901 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004902
4903 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004904};
4905
Calin Juravle27df7582015-04-17 19:12:31 +01004906class HMemoryBarrier : public HTemplateInstruction<0> {
4907 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004908 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004909 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004910 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01004911 barrier_kind_(barrier_kind) {}
4912
4913 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4914
4915 DECLARE_INSTRUCTION(MemoryBarrier);
4916
4917 private:
4918 const MemBarrierKind barrier_kind_;
4919
4920 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4921};
4922
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004923class HMonitorOperation : public HTemplateInstruction<1> {
4924 public:
4925 enum OperationKind {
4926 kEnter,
4927 kExit,
4928 };
4929
4930 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004931 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004932 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
4933 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004934 SetRawInputAt(0, object);
4935 }
4936
4937 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004938 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4939
4940 bool CanThrow() const OVERRIDE {
4941 // Verifier guarantees that monitor-exit cannot throw.
4942 // This is important because it allows the HGraphBuilder to remove
4943 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4944 return IsEnter();
4945 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004946
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004947
4948 bool IsEnter() const { return kind_ == kEnter; }
4949
4950 DECLARE_INSTRUCTION(MonitorOperation);
4951
Calin Juravle52c48962014-12-16 17:02:57 +00004952 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004953 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004954
4955 private:
4956 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4957};
4958
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004959/**
4960 * A HInstruction used as a marker for the replacement of new + <init>
4961 * of a String to a call to a StringFactory. Only baseline will see
4962 * the node at code generation, where it will be be treated as null.
4963 * When compiling non-baseline, `HFakeString` instructions are being removed
4964 * in the instruction simplifier.
4965 */
4966class HFakeString : public HTemplateInstruction<0> {
4967 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004968 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
4969 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004970
4971 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4972
4973 DECLARE_INSTRUCTION(FakeString);
4974
4975 private:
4976 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4977};
4978
Vladimir Markof9f64412015-09-02 14:05:49 +01004979class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004980 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004981 MoveOperands(Location source,
4982 Location destination,
4983 Primitive::Type type,
4984 HInstruction* instruction)
4985 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004986
4987 Location GetSource() const { return source_; }
4988 Location GetDestination() const { return destination_; }
4989
4990 void SetSource(Location value) { source_ = value; }
4991 void SetDestination(Location value) { destination_ = value; }
4992
4993 // The parallel move resolver marks moves as "in-progress" by clearing the
4994 // destination (but not the source).
4995 Location MarkPending() {
4996 DCHECK(!IsPending());
4997 Location dest = destination_;
4998 destination_ = Location::NoLocation();
4999 return dest;
5000 }
5001
5002 void ClearPending(Location dest) {
5003 DCHECK(IsPending());
5004 destination_ = dest;
5005 }
5006
5007 bool IsPending() const {
5008 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5009 return destination_.IsInvalid() && !source_.IsInvalid();
5010 }
5011
5012 // True if this blocks a move from the given location.
5013 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005014 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005015 }
5016
5017 // A move is redundant if it's been eliminated, if its source and
5018 // destination are the same, or if its destination is unneeded.
5019 bool IsRedundant() const {
5020 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5021 }
5022
5023 // We clear both operands to indicate move that's been eliminated.
5024 void Eliminate() {
5025 source_ = destination_ = Location::NoLocation();
5026 }
5027
5028 bool IsEliminated() const {
5029 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5030 return source_.IsInvalid();
5031 }
5032
Alexey Frunze4dda3372015-06-01 18:31:49 -07005033 Primitive::Type GetType() const { return type_; }
5034
Nicolas Geoffray90218252015-04-15 11:56:51 +01005035 bool Is64BitMove() const {
5036 return Primitive::Is64BitType(type_);
5037 }
5038
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005039 HInstruction* GetInstruction() const { return instruction_; }
5040
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005041 private:
5042 Location source_;
5043 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005044 // The type this move is for.
5045 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005046 // The instruction this move is assocatied with. Null when this move is
5047 // for moving an input in the expected locations of user (including a phi user).
5048 // This is only used in debug mode, to ensure we do not connect interval siblings
5049 // in the same parallel move.
5050 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005051};
5052
5053static constexpr size_t kDefaultNumberOfMoves = 4;
5054
5055class HParallelMove : public HTemplateInstruction<0> {
5056 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005057 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005058 : HTemplateInstruction(SideEffects::None(), dex_pc),
5059 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5060 moves_.reserve(kDefaultNumberOfMoves);
5061 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005062
Nicolas Geoffray90218252015-04-15 11:56:51 +01005063 void AddMove(Location source,
5064 Location destination,
5065 Primitive::Type type,
5066 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005067 DCHECK(source.IsValid());
5068 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005069 if (kIsDebugBuild) {
5070 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005071 for (const MoveOperands& move : moves_) {
5072 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005073 // Special case the situation where the move is for the spill slot
5074 // of the instruction.
5075 if ((GetPrevious() == instruction)
5076 || ((GetPrevious() == nullptr)
5077 && instruction->IsPhi()
5078 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005079 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005080 << "Doing parallel moves for the same instruction.";
5081 } else {
5082 DCHECK(false) << "Doing parallel moves for the same instruction.";
5083 }
5084 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005085 }
5086 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005087 for (const MoveOperands& move : moves_) {
5088 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005089 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005090 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005091 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005092 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005093 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005094 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005095 }
5096
Vladimir Marko225b6462015-09-28 12:17:40 +01005097 MoveOperands* MoveOperandsAt(size_t index) {
5098 DCHECK_LT(index, moves_.size());
5099 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005100 }
5101
Vladimir Marko225b6462015-09-28 12:17:40 +01005102 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005103
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005104 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005105
5106 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005107 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005108
5109 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5110};
5111
Mark Mendell0616ae02015-04-17 12:49:27 -04005112} // namespace art
5113
5114#ifdef ART_ENABLE_CODEGEN_x86
5115#include "nodes_x86.h"
5116#endif
5117
5118namespace art {
5119
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005120class HGraphVisitor : public ValueObject {
5121 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005122 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5123 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005124
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005125 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005126 virtual void VisitBasicBlock(HBasicBlock* block);
5127
Roland Levillain633021e2014-10-01 14:12:25 +01005128 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005129 void VisitInsertionOrder();
5130
Roland Levillain633021e2014-10-01 14:12:25 +01005131 // Visit the graph following dominator tree reverse post-order.
5132 void VisitReversePostOrder();
5133
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005134 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005135
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005136 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005137#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005138 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5139
5140 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5141
5142#undef DECLARE_VISIT_INSTRUCTION
5143
5144 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005145 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005146
5147 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5148};
5149
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005150class HGraphDelegateVisitor : public HGraphVisitor {
5151 public:
5152 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5153 virtual ~HGraphDelegateVisitor() {}
5154
5155 // Visit functions that delegate to to super class.
5156#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005157 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005158
5159 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5160
5161#undef DECLARE_VISIT_INSTRUCTION
5162
5163 private:
5164 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5165};
5166
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005167class HInsertionOrderIterator : public ValueObject {
5168 public:
5169 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5170
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005171 bool Done() const { return index_ == graph_.GetBlocks().size(); }
5172 HBasicBlock* Current() const { return graph_.GetBlock(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005173 void Advance() { ++index_; }
5174
5175 private:
5176 const HGraph& graph_;
5177 size_t index_;
5178
5179 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5180};
5181
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005182class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005183 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005184 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5185 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005186 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005187 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005188
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005189 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5190 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005191 void Advance() { ++index_; }
5192
5193 private:
5194 const HGraph& graph_;
5195 size_t index_;
5196
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005197 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005198};
5199
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005200class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005201 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005202 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005203 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005204 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005205 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005206 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005207
5208 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005209 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005210 void Advance() { --index_; }
5211
5212 private:
5213 const HGraph& graph_;
5214 size_t index_;
5215
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005216 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005217};
5218
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005219class HLinearPostOrderIterator : public ValueObject {
5220 public:
5221 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005222 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005223
5224 bool Done() const { return index_ == 0; }
5225
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005226 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005227
5228 void Advance() {
5229 --index_;
5230 DCHECK_GE(index_, 0U);
5231 }
5232
5233 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005234 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005235 size_t index_;
5236
5237 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5238};
5239
5240class HLinearOrderIterator : public ValueObject {
5241 public:
5242 explicit HLinearOrderIterator(const HGraph& graph)
5243 : order_(graph.GetLinearOrder()), index_(0) {}
5244
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005245 bool Done() const { return index_ == order_.size(); }
5246 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005247 void Advance() { ++index_; }
5248
5249 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005250 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005251 size_t index_;
5252
5253 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5254};
5255
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005256// Iterator over the blocks that art part of the loop. Includes blocks part
5257// of an inner loop. The order in which the blocks are iterated is on their
5258// block id.
5259class HBlocksInLoopIterator : public ValueObject {
5260 public:
5261 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5262 : blocks_in_loop_(info.GetBlocks()),
5263 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5264 index_(0) {
5265 if (!blocks_in_loop_.IsBitSet(index_)) {
5266 Advance();
5267 }
5268 }
5269
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005270 bool Done() const { return index_ == blocks_.size(); }
5271 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005272 void Advance() {
5273 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005274 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005275 if (blocks_in_loop_.IsBitSet(index_)) {
5276 break;
5277 }
5278 }
5279 }
5280
5281 private:
5282 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005283 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005284 size_t index_;
5285
5286 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5287};
5288
Mingyao Yang3584bce2015-05-19 16:01:59 -07005289// Iterator over the blocks that art part of the loop. Includes blocks part
5290// of an inner loop. The order in which the blocks are iterated is reverse
5291// post order.
5292class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5293 public:
5294 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5295 : blocks_in_loop_(info.GetBlocks()),
5296 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5297 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005298 DCHECK(!blocks_.empty());
5299 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005300 Advance();
5301 }
5302 }
5303
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005304 bool Done() const { return index_ == blocks_.size(); }
5305 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005306 void Advance() {
5307 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005308 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5309 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005310 break;
5311 }
5312 }
5313 }
5314
5315 private:
5316 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005317 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005318 size_t index_;
5319
5320 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5321};
5322
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005323inline int64_t Int64FromConstant(HConstant* constant) {
5324 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5325 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5326 : constant->AsLongConstant()->GetValue();
5327}
5328
Vladimir Marko58155012015-08-19 12:49:41 +00005329inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5330 // For the purposes of the compiler, the dex files must actually be the same object
5331 // if we want to safely treat them as the same. This is especially important for JIT
5332 // as custom class loaders can open the same underlying file (or memory) multiple
5333 // times and provide different class resolution but no two class loaders should ever
5334 // use the same DexFile object - doing so is an unsupported hack that can lead to
5335 // all sorts of weird failures.
5336 return &lhs == &rhs;
5337}
5338
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005339} // namespace art
5340
5341#endif // ART_COMPILER_OPTIMIZING_NODES_H_