blob: 95dd03970d7078e493bd7b1fad6e1b096205e27b [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) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001070 M(UnresolvedInstanceFieldGet, Instruction) \
1071 M(UnresolvedInstanceFieldSet, Instruction) \
1072 M(UnresolvedStaticFieldGet, Instruction) \
1073 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001074 M(StoreLocal, Instruction) \
1075 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001076 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001077 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001078 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001079 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001080 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001081 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001082 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001083
Alexandre Ramesef20f712015-06-09 10:29:30 +01001084#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1085
1086#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1087
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001088#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1089
Mark Mendell0616ae02015-04-17 12:49:27 -04001090#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1091 M(X86ComputeBaseMethodAddress, Instruction) \
1092 M(X86LoadFromConstantTable, Instruction)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001093
1094#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1095
1096#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1097 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1098 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1099 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001100 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001101 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1102 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1103
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001104#define FOR_EACH_INSTRUCTION(M) \
1105 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1106 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001107 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001108 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001109 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001110
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001111#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001112FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1113#undef FORWARD_DECLARATION
1114
Roland Levillainccc07a92014-09-16 14:48:16 +01001115#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001116 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1117 const char* DebugName() const OVERRIDE { return #type; } \
1118 const H##type* As##type() const OVERRIDE { return this; } \
1119 H##type* As##type() OVERRIDE { return this; } \
1120 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001121 return other->Is##type(); \
1122 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001123 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001124
David Brazdiled596192015-01-23 10:39:45 +00001125template <typename T> class HUseList;
1126
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001127template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001128class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001129 public:
David Brazdiled596192015-01-23 10:39:45 +00001130 HUseListNode* GetPrevious() const { return prev_; }
1131 HUseListNode* GetNext() const { return next_; }
1132 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001133 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001134 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001135
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001136 private:
David Brazdiled596192015-01-23 10:39:45 +00001137 HUseListNode(T user, size_t index)
1138 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1139
1140 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001141 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001142 HUseListNode<T>* prev_;
1143 HUseListNode<T>* next_;
1144
1145 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001146
1147 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1148};
1149
David Brazdiled596192015-01-23 10:39:45 +00001150template <typename T>
1151class HUseList : public ValueObject {
1152 public:
1153 HUseList() : first_(nullptr) {}
1154
1155 void Clear() {
1156 first_ = nullptr;
1157 }
1158
1159 // Adds a new entry at the beginning of the use list and returns
1160 // the newly created node.
1161 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001162 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001163 if (IsEmpty()) {
1164 first_ = new_node;
1165 } else {
1166 first_->prev_ = new_node;
1167 new_node->next_ = first_;
1168 first_ = new_node;
1169 }
1170 return new_node;
1171 }
1172
1173 HUseListNode<T>* GetFirst() const {
1174 return first_;
1175 }
1176
1177 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001178 DCHECK(node != nullptr);
1179 DCHECK(Contains(node));
1180
David Brazdiled596192015-01-23 10:39:45 +00001181 if (node->prev_ != nullptr) {
1182 node->prev_->next_ = node->next_;
1183 }
1184 if (node->next_ != nullptr) {
1185 node->next_->prev_ = node->prev_;
1186 }
1187 if (node == first_) {
1188 first_ = node->next_;
1189 }
1190 }
1191
David Brazdil1abb4192015-02-17 18:33:36 +00001192 bool Contains(const HUseListNode<T>* node) const {
1193 if (node == nullptr) {
1194 return false;
1195 }
1196 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1197 if (current == node) {
1198 return true;
1199 }
1200 }
1201 return false;
1202 }
1203
David Brazdiled596192015-01-23 10:39:45 +00001204 bool IsEmpty() const {
1205 return first_ == nullptr;
1206 }
1207
1208 bool HasOnlyOneUse() const {
1209 return first_ != nullptr && first_->next_ == nullptr;
1210 }
1211
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001212 size_t SizeSlow() const {
1213 size_t count = 0;
1214 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1215 ++count;
1216 }
1217 return count;
1218 }
1219
David Brazdiled596192015-01-23 10:39:45 +00001220 private:
1221 HUseListNode<T>* first_;
1222};
1223
1224template<typename T>
1225class HUseIterator : public ValueObject {
1226 public:
1227 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1228
1229 bool Done() const { return current_ == nullptr; }
1230
1231 void Advance() {
1232 DCHECK(!Done());
1233 current_ = current_->GetNext();
1234 }
1235
1236 HUseListNode<T>* Current() const {
1237 DCHECK(!Done());
1238 return current_;
1239 }
1240
1241 private:
1242 HUseListNode<T>* current_;
1243
1244 friend class HValue;
1245};
1246
David Brazdil1abb4192015-02-17 18:33:36 +00001247// This class is used by HEnvironment and HInstruction classes to record the
1248// instructions they use and pointers to the corresponding HUseListNodes kept
1249// by the used instructions.
1250template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001251class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001252 public:
1253 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1254 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1255
1256 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1257 : instruction_(old_record.instruction_), use_node_(use_node) {
1258 DCHECK(instruction_ != nullptr);
1259 DCHECK(use_node_ != nullptr);
1260 DCHECK(old_record.use_node_ == nullptr);
1261 }
1262
1263 HInstruction* GetInstruction() const { return instruction_; }
1264 HUseListNode<T>* GetUseNode() const { return use_node_; }
1265
1266 private:
1267 // Instruction used by the user.
1268 HInstruction* instruction_;
1269
1270 // Corresponding entry in the use list kept by 'instruction_'.
1271 HUseListNode<T>* use_node_;
1272};
1273
Aart Bik854a02b2015-07-14 16:07:00 -07001274/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001275 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001276 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001277 * For write/read dependences on fields/arrays, the dependence analysis uses
1278 * type disambiguation (e.g. a float field write cannot modify the value of an
1279 * integer field read) and the access type (e.g. a reference array write cannot
1280 * modify the value of a reference field read [although it may modify the
1281 * reference fetch prior to reading the field, which is represented by its own
1282 * write/read dependence]). The analysis makes conservative points-to
1283 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1284 * the same, and any reference read depends on any reference read without
1285 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001286 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001287 * The internal representation uses 38-bit and is described in the table below.
1288 * The first line indicates the side effect, and for field/array accesses the
1289 * second line indicates the type of the access (in the order of the
1290 * Primitive::Type enum).
1291 * The two numbered lines below indicate the bit position in the bitfield (read
1292 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001293 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001294 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1295 * +-------------+---------+---------+--------------+---------+---------+
1296 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1297 * | 3 |333333322|222222221| 1 |111111110|000000000|
1298 * | 7 |654321098|765432109| 8 |765432109|876543210|
1299 *
1300 * Note that, to ease the implementation, 'changes' bits are least significant
1301 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001302 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001303class SideEffects : public ValueObject {
1304 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001305 SideEffects() : flags_(0) {}
1306
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001307 static SideEffects None() {
1308 return SideEffects(0);
1309 }
1310
1311 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001312 return SideEffects(kAllChangeBits | kAllDependOnBits);
1313 }
1314
1315 static SideEffects AllChanges() {
1316 return SideEffects(kAllChangeBits);
1317 }
1318
1319 static SideEffects AllDependencies() {
1320 return SideEffects(kAllDependOnBits);
1321 }
1322
1323 static SideEffects AllExceptGCDependency() {
1324 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1325 }
1326
1327 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001328 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001329 }
1330
Aart Bik34c3ba92015-07-20 14:08:59 -07001331 static SideEffects AllWrites() {
1332 return SideEffects(kAllWrites);
1333 }
1334
1335 static SideEffects AllReads() {
1336 return SideEffects(kAllReads);
1337 }
1338
1339 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1340 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001341 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001342 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001343 }
1344
Aart Bik854a02b2015-07-14 16:07:00 -07001345 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1346 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001347 }
1348
Aart Bik34c3ba92015-07-20 14:08:59 -07001349 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1350 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001351 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001352 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001353 }
1354
1355 static SideEffects ArrayReadOfType(Primitive::Type type) {
1356 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1357 }
1358
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001359 static SideEffects CanTriggerGC() {
1360 return SideEffects(1ULL << kCanTriggerGCBit);
1361 }
1362
1363 static SideEffects DependsOnGC() {
1364 return SideEffects(1ULL << kDependsOnGCBit);
1365 }
1366
Aart Bik854a02b2015-07-14 16:07:00 -07001367 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001368 SideEffects Union(SideEffects other) const {
1369 return SideEffects(flags_ | other.flags_);
1370 }
1371
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001372 SideEffects Exclusion(SideEffects other) const {
1373 return SideEffects(flags_ & ~other.flags_);
1374 }
1375
1376 bool Includes(SideEffects other) const {
1377 return (other.flags_ & flags_) == other.flags_;
1378 }
1379
1380 bool HasSideEffects() const {
1381 return (flags_ & kAllChangeBits);
1382 }
1383
1384 bool HasDependencies() const {
1385 return (flags_ & kAllDependOnBits);
1386 }
1387
1388 // Returns true if there are no side effects or dependencies.
1389 bool DoesNothing() const {
1390 return flags_ == 0;
1391 }
1392
Aart Bik854a02b2015-07-14 16:07:00 -07001393 // Returns true if something is written.
1394 bool DoesAnyWrite() const {
1395 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001396 }
1397
Aart Bik854a02b2015-07-14 16:07:00 -07001398 // Returns true if something is read.
1399 bool DoesAnyRead() const {
1400 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001401 }
1402
Aart Bik854a02b2015-07-14 16:07:00 -07001403 // Returns true if potentially everything is written and read
1404 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001405 bool DoesAllReadWrite() const {
1406 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1407 }
1408
Aart Bik854a02b2015-07-14 16:07:00 -07001409 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001410 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001411 }
1412
1413 // Returns true if this may read something written by other.
1414 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001415 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1416 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001417 }
1418
1419 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001420 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001421 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001422 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001423 for (int s = kLastBit; s >= 0; s--) {
1424 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1425 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1426 // This is a bit for the GC side effect.
1427 if (current_bit_is_set) {
1428 flags += "GC";
1429 }
Aart Bik854a02b2015-07-14 16:07:00 -07001430 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001431 } else {
1432 // This is a bit for the array/field analysis.
1433 // The underscore character stands for the 'can trigger GC' bit.
1434 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1435 if (current_bit_is_set) {
1436 flags += kDebug[s];
1437 }
1438 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1439 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1440 flags += "|";
1441 }
1442 }
Aart Bik854a02b2015-07-14 16:07:00 -07001443 }
1444 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001445 }
1446
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001447 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001448
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001449 private:
1450 static constexpr int kFieldArrayAnalysisBits = 9;
1451
1452 static constexpr int kFieldWriteOffset = 0;
1453 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1454 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1455 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1456
1457 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1458
1459 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1460 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1461 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1462 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1463
1464 static constexpr int kLastBit = kDependsOnGCBit;
1465 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1466
1467 // Aliases.
1468
1469 static_assert(kChangeBits == kDependOnBits,
1470 "the 'change' bits should match the 'depend on' bits.");
1471
1472 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1473 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1474 static constexpr uint64_t kAllWrites =
1475 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1476 static constexpr uint64_t kAllReads =
1477 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001478
Aart Bik854a02b2015-07-14 16:07:00 -07001479 // Work around the fact that HIR aliases I/F and J/D.
1480 // TODO: remove this interceptor once HIR types are clean
1481 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1482 switch (type) {
1483 case Primitive::kPrimInt:
1484 case Primitive::kPrimFloat:
1485 return TypeFlag(Primitive::kPrimInt, offset) |
1486 TypeFlag(Primitive::kPrimFloat, offset);
1487 case Primitive::kPrimLong:
1488 case Primitive::kPrimDouble:
1489 return TypeFlag(Primitive::kPrimLong, offset) |
1490 TypeFlag(Primitive::kPrimDouble, offset);
1491 default:
1492 return TypeFlag(type, offset);
1493 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001494 }
1495
Aart Bik854a02b2015-07-14 16:07:00 -07001496 // Translates type to bit flag.
1497 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1498 CHECK_NE(type, Primitive::kPrimVoid);
1499 const uint64_t one = 1;
1500 const int shift = type; // 0-based consecutive enum
1501 DCHECK_LE(kFieldWriteOffset, shift);
1502 DCHECK_LT(shift, kArrayWriteOffset);
1503 return one << (type + offset);
1504 }
1505
1506 // Private constructor on direct flags value.
1507 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1508
1509 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001510};
1511
David Brazdiled596192015-01-23 10:39:45 +00001512// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001513class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001514 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001515 HEnvironment(ArenaAllocator* arena,
1516 size_t number_of_vregs,
1517 const DexFile& dex_file,
1518 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001519 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001520 InvokeType invoke_type,
1521 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001522 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1523 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001524 parent_(nullptr),
1525 dex_file_(dex_file),
1526 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001527 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001528 invoke_type_(invoke_type),
1529 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001530 }
1531
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001532 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001533 : HEnvironment(arena,
1534 to_copy.Size(),
1535 to_copy.GetDexFile(),
1536 to_copy.GetMethodIdx(),
1537 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001538 to_copy.GetInvokeType(),
1539 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001540
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001541 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001542 if (parent_ != nullptr) {
1543 parent_->SetAndCopyParentChain(allocator, parent);
1544 } else {
1545 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1546 parent_->CopyFrom(parent);
1547 if (parent->GetParent() != nullptr) {
1548 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1549 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001550 }
David Brazdiled596192015-01-23 10:39:45 +00001551 }
1552
Vladimir Marko71bf8092015-09-15 15:33:14 +01001553 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001554 void CopyFrom(HEnvironment* environment);
1555
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001556 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1557 // input to the loop phi instead. This is for inserting instructions that
1558 // require an environment (like HDeoptimization) in the loop pre-header.
1559 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001560
1561 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001562 DCHECK_LT(index, Size());
1563 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001564 }
1565
1566 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001567 DCHECK_LT(index, Size());
1568 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001569 }
1570
David Brazdil1abb4192015-02-17 18:33:36 +00001571 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001572
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001573 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001574
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001575 HEnvironment* GetParent() const { return parent_; }
1576
1577 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001578 DCHECK_LT(index, Size());
1579 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001580 }
1581
1582 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001583 DCHECK_LT(index, Size());
1584 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001585 }
1586
1587 uint32_t GetDexPc() const {
1588 return dex_pc_;
1589 }
1590
1591 uint32_t GetMethodIdx() const {
1592 return method_idx_;
1593 }
1594
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001595 InvokeType GetInvokeType() const {
1596 return invoke_type_;
1597 }
1598
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001599 const DexFile& GetDexFile() const {
1600 return dex_file_;
1601 }
1602
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001603 HInstruction* GetHolder() const {
1604 return holder_;
1605 }
1606
David Brazdiled596192015-01-23 10:39:45 +00001607 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001608 // Record instructions' use entries of this environment for constant-time removal.
1609 // It should only be called by HInstruction when a new environment use is added.
1610 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1611 DCHECK(env_use->GetUser() == this);
1612 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001613 DCHECK_LT(index, Size());
1614 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001615 }
David Brazdiled596192015-01-23 10:39:45 +00001616
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001617 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1618 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001619 HEnvironment* parent_;
1620 const DexFile& dex_file_;
1621 const uint32_t method_idx_;
1622 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001623 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001624
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001625 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001626 HInstruction* const holder_;
1627
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001628 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001629
1630 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1631};
1632
Calin Juravleacf735c2015-02-12 15:25:22 +00001633class ReferenceTypeInfo : ValueObject {
1634 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001635 typedef Handle<mirror::Class> TypeHandle;
1636
Calin Juravle2e768302015-07-28 14:41:11 +00001637 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1638 // The constructor will check that the type_handle is valid.
1639 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001640 }
1641
Calin Juravle2e768302015-07-28 14:41:11 +00001642 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1643
1644 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1645 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001646 }
1647
Calin Juravle2e768302015-07-28 14:41:11 +00001648 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1649 return IsValidHandle(type_handle_);
1650 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001651
Calin Juravleacf735c2015-02-12 15:25:22 +00001652 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001653
1654 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1655 DCHECK(IsValid());
1656 return GetTypeHandle()->IsObjectClass();
1657 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001658
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001659 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1660 return IsValid() && GetTypeHandle()->IsStringClass();
1661 }
1662
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001663 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1664 DCHECK(IsValid());
1665 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1666 }
1667
Mathieu Chartier90443472015-07-16 20:32:27 -07001668 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001669 DCHECK(IsValid());
1670 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001671 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001672
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001673 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1674 return GetTypeHandle()->IsArrayClass();
1675 }
1676
1677 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1678 if (!IsExact()) return false;
1679 if (!IsArrayClass()) return false;
1680 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1681 }
1682
Calin Juravleacf735c2015-02-12 15:25:22 +00001683 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1684
Mathieu Chartier90443472015-07-16 20:32:27 -07001685 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001686 DCHECK(IsValid());
1687 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001688 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1689 }
1690
1691 // Returns true if the type information provide the same amount of details.
1692 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001693 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001694 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001695 if (!IsValid() && !rti.IsValid()) {
1696 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001697 return true;
1698 }
Calin Juravle2e768302015-07-28 14:41:11 +00001699 if (!IsValid() || !rti.IsValid()) {
1700 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001701 return false;
1702 }
Calin Juravle2e768302015-07-28 14:41:11 +00001703 return IsExact() == rti.IsExact()
1704 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001705 }
1706
1707 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001708 ReferenceTypeInfo();
1709 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001710
Calin Juravleacf735c2015-02-12 15:25:22 +00001711 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001712 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001713 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001714 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001715 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001716};
1717
1718std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1719
Vladimir Markof9f64412015-09-02 14:05:49 +01001720class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001721 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001722 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001723 : previous_(nullptr),
1724 next_(nullptr),
1725 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001726 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001727 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001728 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001729 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001730 locations_(nullptr),
1731 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001732 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001733 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001734 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001735
Dave Allison20dfc792014-06-16 20:44:29 -07001736 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001737
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001738#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001739 enum InstructionKind {
1740 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1741 };
1742#undef DECLARE_KIND
1743
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001744 HInstruction* GetNext() const { return next_; }
1745 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001746
Calin Juravle77520bc2015-01-12 18:45:46 +00001747 HInstruction* GetNextDisregardingMoves() const;
1748 HInstruction* GetPreviousDisregardingMoves() const;
1749
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001750 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001751 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001752 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001753 bool IsInBlock() const { return block_ != nullptr; }
1754 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001755 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001756
Roland Levillain6b879dd2014-09-22 17:13:44 +01001757 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001758 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001759
1760 virtual void Accept(HGraphVisitor* visitor) = 0;
1761 virtual const char* DebugName() const = 0;
1762
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001763 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001764 void SetRawInputAt(size_t index, HInstruction* input) {
1765 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1766 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001767
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001768 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001769
1770 uint32_t GetDexPc() const { return dex_pc_; }
1771
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001772 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001773
Roland Levillaine161a2a2014-10-03 12:45:18 +01001774 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001775 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001776
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001777 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001778 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001779
Calin Juravle10e244f2015-01-26 18:54:32 +00001780 // Does not apply for all instructions, but having this at top level greatly
1781 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001782 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001783 virtual bool CanBeNull() const {
1784 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1785 return true;
1786 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001787
Calin Juravle641547a2015-04-21 22:08:51 +01001788 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1789 UNUSED(obj);
1790 return false;
1791 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001792
Calin Juravle2e768302015-07-28 14:41:11 +00001793 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001794
Calin Juravle61d544b2015-02-23 16:46:57 +00001795 ReferenceTypeInfo GetReferenceTypeInfo() const {
1796 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1797 return reference_type_info_;
1798 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001799
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001800 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001801 DCHECK(user != nullptr);
1802 HUseListNode<HInstruction*>* use =
1803 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1804 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001805 }
1806
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001807 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001808 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001809 HUseListNode<HEnvironment*>* env_use =
1810 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1811 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001812 }
1813
David Brazdil1abb4192015-02-17 18:33:36 +00001814 void RemoveAsUserOfInput(size_t input) {
1815 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1816 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1817 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001818
David Brazdil1abb4192015-02-17 18:33:36 +00001819 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1820 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001821
David Brazdiled596192015-01-23 10:39:45 +00001822 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1823 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001824 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001825 bool HasOnlyOneNonEnvironmentUse() const {
1826 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1827 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001828
Roland Levillain6c82d402014-10-13 16:10:27 +01001829 // Does this instruction strictly dominate `other_instruction`?
1830 // Returns false if this instruction and `other_instruction` are the same.
1831 // Aborts if this instruction and `other_instruction` are both phis.
1832 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001833
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001834 int GetId() const { return id_; }
1835 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001836
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001837 int GetSsaIndex() const { return ssa_index_; }
1838 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1839 bool HasSsaIndex() const { return ssa_index_ != -1; }
1840
1841 bool HasEnvironment() const { return environment_ != nullptr; }
1842 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001843 // Set the `environment_` field. Raw because this method does not
1844 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001845 void SetRawEnvironment(HEnvironment* environment) {
1846 DCHECK(environment_ == nullptr);
1847 DCHECK_EQ(environment->GetHolder(), this);
1848 environment_ = environment;
1849 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001850
1851 // Set the environment of this instruction, copying it from `environment`. While
1852 // copying, the uses lists are being updated.
1853 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001854 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001855 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001856 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001857 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001858 if (environment->GetParent() != nullptr) {
1859 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1860 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001861 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001862
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001863 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1864 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001865 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001866 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001867 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001868 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001869 if (environment->GetParent() != nullptr) {
1870 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1871 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001872 }
1873
Nicolas Geoffray39468442014-09-02 15:17:15 +01001874 // Returns the number of entries in the environment. Typically, that is the
1875 // number of dex registers in a method. It could be more in case of inlining.
1876 size_t EnvironmentSize() const;
1877
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001878 LocationSummary* GetLocations() const { return locations_; }
1879 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001880
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001881 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001882 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001883
Alexandre Rames188d4312015-04-09 18:30:21 +01001884 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1885 // uses of this instruction by `other` are *not* updated.
1886 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1887 ReplaceWith(other);
1888 other->ReplaceInput(this, use_index);
1889 }
1890
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001891 // Move `this` instruction before `cursor`.
1892 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001893
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001894#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001895 bool Is##type() const { return (As##type() != nullptr); } \
1896 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001897 virtual H##type* As##type() { return nullptr; }
1898
1899 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1900#undef INSTRUCTION_TYPE_CHECK
1901
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001902 // Returns whether the instruction can be moved within the graph.
1903 virtual bool CanBeMoved() const { return false; }
1904
1905 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001906 virtual bool InstructionTypeEquals(HInstruction* other) const {
1907 UNUSED(other);
1908 return false;
1909 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001910
1911 // Returns whether any data encoded in the two instructions is equal.
1912 // This method does not look at the inputs. Both instructions must be
1913 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001914 virtual bool InstructionDataEquals(HInstruction* other) const {
1915 UNUSED(other);
1916 return false;
1917 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001918
1919 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001920 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001921 // 2) Their inputs are identical.
1922 bool Equals(HInstruction* other) const;
1923
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001924 virtual InstructionKind GetKind() const = 0;
1925
1926 virtual size_t ComputeHashCode() const {
1927 size_t result = GetKind();
1928 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1929 result = (result * 31) + InputAt(i)->GetId();
1930 }
1931 return result;
1932 }
1933
1934 SideEffects GetSideEffects() const { return side_effects_; }
1935
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001936 size_t GetLifetimePosition() const { return lifetime_position_; }
1937 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1938 LiveInterval* GetLiveInterval() const { return live_interval_; }
1939 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1940 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1941
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001942 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1943
1944 // Returns whether the code generation of the instruction will require to have access
1945 // to the current method. Such instructions are:
1946 // (1): Instructions that require an environment, as calling the runtime requires
1947 // to walk the stack and have the current method stored at a specific stack address.
1948 // (2): Object literals like classes and strings, that are loaded from the dex cache
1949 // fields of the current method.
1950 bool NeedsCurrentMethod() const {
1951 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1952 }
1953
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001954 virtual bool NeedsDexCache() const { return false; }
1955
Mark Mendellc4701932015-04-10 13:18:51 -04001956 // Does this instruction have any use in an environment before
1957 // control flow hits 'other'?
1958 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1959
1960 // Remove all references to environment uses of this instruction.
1961 // The caller must ensure that this is safe to do.
1962 void RemoveEnvironmentUsers();
1963
David Brazdil1abb4192015-02-17 18:33:36 +00001964 protected:
1965 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1966 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1967
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001968 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001969 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1970
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001971 HInstruction* previous_;
1972 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001973 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001974 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001975
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001976 // An instruction gets an id when it is added to the graph.
1977 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001978 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001979 int id_;
1980
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001981 // When doing liveness analysis, instructions that have uses get an SSA index.
1982 int ssa_index_;
1983
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001984 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001985 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001986
1987 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001988 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001989
Nicolas Geoffray39468442014-09-02 15:17:15 +01001990 // The environment associated with this instruction. Not null if the instruction
1991 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001992 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001993
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001994 // Set by the code generator.
1995 LocationSummary* locations_;
1996
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001997 // Set by the liveness analysis.
1998 LiveInterval* live_interval_;
1999
2000 // Set by the liveness analysis, this is the position in a linear
2001 // order of blocks where this instruction's live interval start.
2002 size_t lifetime_position_;
2003
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002004 const SideEffects side_effects_;
2005
Calin Juravleacf735c2015-02-12 15:25:22 +00002006 // TODO: for primitive types this should be marked as invalid.
2007 ReferenceTypeInfo reference_type_info_;
2008
David Brazdil1abb4192015-02-17 18:33:36 +00002009 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002010 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002011 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002012 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002013 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002014
2015 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2016};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002017std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002018
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002019class HInputIterator : public ValueObject {
2020 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002021 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002022
2023 bool Done() const { return index_ == instruction_->InputCount(); }
2024 HInstruction* Current() const { return instruction_->InputAt(index_); }
2025 void Advance() { index_++; }
2026
2027 private:
2028 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002029 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002030
2031 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2032};
2033
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002034class HInstructionIterator : public ValueObject {
2035 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002036 explicit HInstructionIterator(const HInstructionList& instructions)
2037 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002038 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002039 }
2040
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002041 bool Done() const { return instruction_ == nullptr; }
2042 HInstruction* Current() const { return instruction_; }
2043 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002044 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002045 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002046 }
2047
2048 private:
2049 HInstruction* instruction_;
2050 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002051
2052 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002053};
2054
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002055class HBackwardInstructionIterator : public ValueObject {
2056 public:
2057 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2058 : instruction_(instructions.last_instruction_) {
2059 next_ = Done() ? nullptr : instruction_->GetPrevious();
2060 }
2061
2062 bool Done() const { return instruction_ == nullptr; }
2063 HInstruction* Current() const { return instruction_; }
2064 void Advance() {
2065 instruction_ = next_;
2066 next_ = Done() ? nullptr : instruction_->GetPrevious();
2067 }
2068
2069 private:
2070 HInstruction* instruction_;
2071 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002072
2073 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002074};
2075
Vladimir Markof9f64412015-09-02 14:05:49 +01002076template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002077class HTemplateInstruction: public HInstruction {
2078 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002079 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002080 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002081 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002082
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002083 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002084
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002085 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002086 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2087 DCHECK_LT(i, N);
2088 return inputs_[i];
2089 }
David Brazdil1abb4192015-02-17 18:33:36 +00002090
2091 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002092 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002093 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002094 }
2095
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002096 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002097 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002098
2099 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002100};
2101
Vladimir Markof9f64412015-09-02 14:05:49 +01002102// HTemplateInstruction specialization for N=0.
2103template<>
2104class HTemplateInstruction<0>: public HInstruction {
2105 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002106 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002107 : HInstruction(side_effects, dex_pc) {}
2108
Vladimir Markof9f64412015-09-02 14:05:49 +01002109 virtual ~HTemplateInstruction() {}
2110
2111 size_t InputCount() const OVERRIDE { return 0; }
2112
2113 protected:
2114 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2115 LOG(FATAL) << "Unreachable";
2116 UNREACHABLE();
2117 }
2118
2119 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2120 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2121 LOG(FATAL) << "Unreachable";
2122 UNREACHABLE();
2123 }
2124
2125 private:
2126 friend class SsaBuilder;
2127};
2128
Dave Allison20dfc792014-06-16 20:44:29 -07002129template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002130class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002131 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002132 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002133 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002134 virtual ~HExpression() {}
2135
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002136 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002137
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002138 protected:
2139 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002140};
2141
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002142// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2143// instruction that branches to the exit block.
2144class HReturnVoid : public HTemplateInstruction<0> {
2145 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002146 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2147 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002148
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002149 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002150
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002151 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002152
2153 private:
2154 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2155};
2156
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002157// Represents dex's RETURN opcodes. A HReturn is a control flow
2158// instruction that branches to the exit block.
2159class HReturn : public HTemplateInstruction<1> {
2160 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002161 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2162 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002163 SetRawInputAt(0, value);
2164 }
2165
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002166 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002167
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002168 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002169
2170 private:
2171 DISALLOW_COPY_AND_ASSIGN(HReturn);
2172};
2173
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002174// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002175// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002176// exit block.
2177class HExit : public HTemplateInstruction<0> {
2178 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002179 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002180
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002181 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002182
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002183 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002184
2185 private:
2186 DISALLOW_COPY_AND_ASSIGN(HExit);
2187};
2188
2189// Jumps from one block to another.
2190class HGoto : public HTemplateInstruction<0> {
2191 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002192 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002193
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002194 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002195
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002196 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002197 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002198 }
2199
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002200 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002201
2202 private:
2203 DISALLOW_COPY_AND_ASSIGN(HGoto);
2204};
2205
Roland Levillain9867bc72015-08-05 10:21:34 +01002206class HConstant : public HExpression<0> {
2207 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002208 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2209 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002210
2211 bool CanBeMoved() const OVERRIDE { return true; }
2212
2213 virtual bool IsMinusOne() const { return false; }
2214 virtual bool IsZero() const { return false; }
2215 virtual bool IsOne() const { return false; }
2216
David Brazdil77a48ae2015-09-15 12:34:04 +00002217 virtual uint64_t GetValueAsUint64() const = 0;
2218
Roland Levillain9867bc72015-08-05 10:21:34 +01002219 DECLARE_INSTRUCTION(Constant);
2220
2221 private:
2222 DISALLOW_COPY_AND_ASSIGN(HConstant);
2223};
2224
2225class HNullConstant : public HConstant {
2226 public:
2227 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2228 return true;
2229 }
2230
David Brazdil77a48ae2015-09-15 12:34:04 +00002231 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2232
Roland Levillain9867bc72015-08-05 10:21:34 +01002233 size_t ComputeHashCode() const OVERRIDE { return 0; }
2234
2235 DECLARE_INSTRUCTION(NullConstant);
2236
2237 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002238 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002239
2240 friend class HGraph;
2241 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2242};
2243
2244// Constants of the type int. Those can be from Dex instructions, or
2245// synthesized (for example with the if-eqz instruction).
2246class HIntConstant : public HConstant {
2247 public:
2248 int32_t GetValue() const { return value_; }
2249
David Brazdil9f389d42015-10-01 14:32:56 +01002250 uint64_t GetValueAsUint64() const OVERRIDE {
2251 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2252 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002253
Roland Levillain9867bc72015-08-05 10:21:34 +01002254 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2255 DCHECK(other->IsIntConstant());
2256 return other->AsIntConstant()->value_ == value_;
2257 }
2258
2259 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2260
2261 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2262 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2263 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2264
2265 DECLARE_INSTRUCTION(IntConstant);
2266
2267 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002268 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2269 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2270 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2271 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002272
2273 const int32_t value_;
2274
2275 friend class HGraph;
2276 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2277 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2278 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2279};
2280
2281class HLongConstant : public HConstant {
2282 public:
2283 int64_t GetValue() const { return value_; }
2284
David Brazdil77a48ae2015-09-15 12:34:04 +00002285 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2286
Roland Levillain9867bc72015-08-05 10:21:34 +01002287 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2288 DCHECK(other->IsLongConstant());
2289 return other->AsLongConstant()->value_ == value_;
2290 }
2291
2292 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2293
2294 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2295 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2296 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2297
2298 DECLARE_INSTRUCTION(LongConstant);
2299
2300 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002301 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2302 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002303
2304 const int64_t value_;
2305
2306 friend class HGraph;
2307 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2308};
Dave Allison20dfc792014-06-16 20:44:29 -07002309
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002310// Conditional branch. A block ending with an HIf instruction must have
2311// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002312class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002313 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002314 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2315 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002316 SetRawInputAt(0, input);
2317 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002318
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002319 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002320
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002321 HBasicBlock* IfTrueSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002322 return GetBlock()->GetSuccessor(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002323 }
2324
2325 HBasicBlock* IfFalseSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00002326 return GetBlock()->GetSuccessor(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002327 }
2328
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002329 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002330
2331 private:
2332 DISALLOW_COPY_AND_ASSIGN(HIf);
2333};
2334
David Brazdilfc6a86a2015-06-26 10:33:45 +00002335
2336// Abstract instruction which marks the beginning and/or end of a try block and
2337// links it to the respective exception handlers. Behaves the same as a Goto in
2338// non-exceptional control flow.
2339// Normal-flow successor is stored at index zero, exception handlers under
2340// higher indices in no particular order.
2341class HTryBoundary : public HTemplateInstruction<0> {
2342 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002343 enum BoundaryKind {
2344 kEntry,
2345 kExit,
2346 };
2347
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002348 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2349 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002350
2351 bool IsControlFlow() const OVERRIDE { return true; }
2352
2353 // Returns the block's non-exceptional successor (index zero).
Vladimir Marko60584552015-09-03 13:35:12 +00002354 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessor(0); }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002355
2356 // Returns whether `handler` is among its exception handlers (non-zero index
2357 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002358 bool HasExceptionHandler(const HBasicBlock& handler) const {
2359 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002360 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002361 }
2362
2363 // If not present already, adds `handler` to its block's list of exception
2364 // handlers.
2365 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002366 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002367 GetBlock()->AddSuccessor(handler);
2368 }
2369 }
2370
David Brazdil56e1acc2015-06-30 15:41:36 +01002371 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002372
David Brazdilffee3d32015-07-06 11:48:53 +01002373 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2374
David Brazdilfc6a86a2015-06-26 10:33:45 +00002375 DECLARE_INSTRUCTION(TryBoundary);
2376
2377 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002378 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002379
2380 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2381};
2382
David Brazdilffee3d32015-07-06 11:48:53 +01002383// Iterator over exception handlers of a given HTryBoundary, i.e. over
2384// exceptional successors of its basic block.
2385class HExceptionHandlerIterator : public ValueObject {
2386 public:
2387 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2388 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2389
Vladimir Marko60584552015-09-03 13:35:12 +00002390 bool Done() const { return index_ == block_.GetSuccessors().size(); }
2391 HBasicBlock* Current() const { return block_.GetSuccessor(index_); }
David Brazdilffee3d32015-07-06 11:48:53 +01002392 size_t CurrentSuccessorIndex() const { return index_; }
2393 void Advance() { ++index_; }
2394
2395 private:
2396 const HBasicBlock& block_;
2397 size_t index_;
2398
2399 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2400};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002401
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002402// Deoptimize to interpreter, upon checking a condition.
2403class HDeoptimize : public HTemplateInstruction<1> {
2404 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002405 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2406 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002407 SetRawInputAt(0, cond);
2408 }
2409
2410 bool NeedsEnvironment() const OVERRIDE { return true; }
2411 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002412
2413 DECLARE_INSTRUCTION(Deoptimize);
2414
2415 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002416 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2417};
2418
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002419// Represents the ArtMethod that was passed as a first argument to
2420// the method. It is used by instructions that depend on it, like
2421// instructions that work with the dex cache.
2422class HCurrentMethod : public HExpression<0> {
2423 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002424 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2425 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002426
2427 DECLARE_INSTRUCTION(CurrentMethod);
2428
2429 private:
2430 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2431};
2432
Mark Mendellfe57faa2015-09-18 09:26:15 -04002433// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2434// have one successor for each entry in the switch table, and the final successor
2435// will be the block containing the next Dex opcode.
2436class HPackedSwitch : public HTemplateInstruction<1> {
2437 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002438 HPackedSwitch(int32_t start_value,
2439 uint32_t num_entries,
2440 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002441 uint32_t dex_pc = kNoDexPc)
2442 : HTemplateInstruction(SideEffects::None(), dex_pc),
2443 start_value_(start_value),
2444 num_entries_(num_entries) {
2445 SetRawInputAt(0, input);
2446 }
2447
2448 bool IsControlFlow() const OVERRIDE { return true; }
2449
2450 int32_t GetStartValue() const { return start_value_; }
2451
Vladimir Marko211c2112015-09-24 16:52:33 +01002452 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002453
2454 HBasicBlock* GetDefaultBlock() const {
2455 // Last entry is the default block.
2456 return GetBlock()->GetSuccessor(num_entries_);
2457 }
2458 DECLARE_INSTRUCTION(PackedSwitch);
2459
2460 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002461 const int32_t start_value_;
2462 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002463
2464 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2465};
2466
Roland Levillain88cb1752014-10-20 16:36:47 +01002467class HUnaryOperation : public HExpression<1> {
2468 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002469 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2470 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002471 SetRawInputAt(0, input);
2472 }
2473
2474 HInstruction* GetInput() const { return InputAt(0); }
2475 Primitive::Type GetResultType() const { return GetType(); }
2476
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002477 bool CanBeMoved() const OVERRIDE { return true; }
2478 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002479 UNUSED(other);
2480 return true;
2481 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002482
Roland Levillain9240d6a2014-10-20 16:47:04 +01002483 // Try to statically evaluate `operation` and return a HConstant
2484 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002485 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002486 HConstant* TryStaticEvaluation() const;
2487
2488 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002489 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2490 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002491
Roland Levillain88cb1752014-10-20 16:36:47 +01002492 DECLARE_INSTRUCTION(UnaryOperation);
2493
2494 private:
2495 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2496};
2497
Dave Allison20dfc792014-06-16 20:44:29 -07002498class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002499 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002500 HBinaryOperation(Primitive::Type result_type,
2501 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002502 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002503 SideEffects side_effects = SideEffects::None(),
2504 uint32_t dex_pc = kNoDexPc)
2505 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002506 SetRawInputAt(0, left);
2507 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002508 }
2509
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002510 HInstruction* GetLeft() const { return InputAt(0); }
2511 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002512 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002513
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002514 virtual bool IsCommutative() const { return false; }
2515
2516 // Put constant on the right.
2517 // Returns whether order is changed.
2518 bool OrderInputsWithConstantOnTheRight() {
2519 HInstruction* left = InputAt(0);
2520 HInstruction* right = InputAt(1);
2521 if (left->IsConstant() && !right->IsConstant()) {
2522 ReplaceInput(right, 0);
2523 ReplaceInput(left, 1);
2524 return true;
2525 }
2526 return false;
2527 }
2528
2529 // Order inputs by instruction id, but favor constant on the right side.
2530 // This helps GVN for commutative ops.
2531 void OrderInputs() {
2532 DCHECK(IsCommutative());
2533 HInstruction* left = InputAt(0);
2534 HInstruction* right = InputAt(1);
2535 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2536 return;
2537 }
2538 if (OrderInputsWithConstantOnTheRight()) {
2539 return;
2540 }
2541 // Order according to instruction id.
2542 if (left->GetId() > right->GetId()) {
2543 ReplaceInput(right, 0);
2544 ReplaceInput(left, 1);
2545 }
2546 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002547
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002548 bool CanBeMoved() const OVERRIDE { return true; }
2549 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002550 UNUSED(other);
2551 return true;
2552 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002553
Roland Levillain9240d6a2014-10-20 16:47:04 +01002554 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002555 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002556 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002557 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002558
2559 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002560 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2561 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2562 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2563 HLongConstant* y ATTRIBUTE_UNUSED) const {
2564 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2565 return nullptr;
2566 }
2567 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2568 HIntConstant* y ATTRIBUTE_UNUSED) const {
2569 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2570 return nullptr;
2571 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002572
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002573 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002574 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002575 HConstant* GetConstantRight() const;
2576
2577 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002578 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002579 HInstruction* GetLeastConstantLeft() const;
2580
Roland Levillainccc07a92014-09-16 14:48:16 +01002581 DECLARE_INSTRUCTION(BinaryOperation);
2582
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002583 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002584 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2585};
2586
Mark Mendellc4701932015-04-10 13:18:51 -04002587// The comparison bias applies for floating point operations and indicates how NaN
2588// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002589enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002590 kNoBias, // bias is not applicable (i.e. for long operation)
2591 kGtBias, // return 1 for NaN comparisons
2592 kLtBias, // return -1 for NaN comparisons
2593};
2594
Dave Allison20dfc792014-06-16 20:44:29 -07002595class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002596 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002597 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2598 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002599 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002600 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002601
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002602 bool NeedsMaterialization() const { return needs_materialization_; }
2603 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002604
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002605 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002606 // `instruction`, and disregard moves in between.
2607 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002608
Dave Allison20dfc792014-06-16 20:44:29 -07002609 DECLARE_INSTRUCTION(Condition);
2610
2611 virtual IfCondition GetCondition() const = 0;
2612
Mark Mendellc4701932015-04-10 13:18:51 -04002613 virtual IfCondition GetOppositeCondition() const = 0;
2614
Roland Levillain4fa13f62015-07-06 18:11:54 +01002615 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002616
2617 void SetBias(ComparisonBias bias) { bias_ = bias; }
2618
2619 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2620 return bias_ == other->AsCondition()->bias_;
2621 }
2622
Roland Levillain4fa13f62015-07-06 18:11:54 +01002623 bool IsFPConditionTrueIfNaN() const {
2624 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2625 IfCondition if_cond = GetCondition();
2626 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2627 }
2628
2629 bool IsFPConditionFalseIfNaN() const {
2630 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2631 IfCondition if_cond = GetCondition();
2632 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2633 }
2634
Dave Allison20dfc792014-06-16 20:44:29 -07002635 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002636 // For register allocation purposes, returns whether this instruction needs to be
2637 // materialized (that is, not just be in the processor flags).
2638 bool needs_materialization_;
2639
Mark Mendellc4701932015-04-10 13:18:51 -04002640 // Needed if we merge a HCompare into a HCondition.
2641 ComparisonBias bias_;
2642
Dave Allison20dfc792014-06-16 20:44:29 -07002643 DISALLOW_COPY_AND_ASSIGN(HCondition);
2644};
2645
2646// Instruction to check if two inputs are equal to each other.
2647class HEqual : public HCondition {
2648 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002649 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2650 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002651
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002652 bool IsCommutative() const OVERRIDE { return true; }
2653
Roland Levillain9867bc72015-08-05 10:21:34 +01002654 template <typename T> bool Compute(T x, T y) const { return x == y; }
2655
2656 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002657 return GetBlock()->GetGraph()->GetIntConstant(
2658 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002659 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002660 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002661 return GetBlock()->GetGraph()->GetIntConstant(
2662 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002663 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002664
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002665 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002666
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002667 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002668 return kCondEQ;
2669 }
2670
Mark Mendellc4701932015-04-10 13:18:51 -04002671 IfCondition GetOppositeCondition() const OVERRIDE {
2672 return kCondNE;
2673 }
2674
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002675 private:
2676 DISALLOW_COPY_AND_ASSIGN(HEqual);
2677};
2678
Dave Allison20dfc792014-06-16 20:44:29 -07002679class HNotEqual : public HCondition {
2680 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002681 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2682 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002683
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002684 bool IsCommutative() const OVERRIDE { return true; }
2685
Roland Levillain9867bc72015-08-05 10:21:34 +01002686 template <typename T> bool Compute(T x, T y) const { return x != y; }
2687
2688 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002689 return GetBlock()->GetGraph()->GetIntConstant(
2690 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002691 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002692 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002693 return GetBlock()->GetGraph()->GetIntConstant(
2694 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002695 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002696
Dave Allison20dfc792014-06-16 20:44:29 -07002697 DECLARE_INSTRUCTION(NotEqual);
2698
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002699 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002700 return kCondNE;
2701 }
2702
Mark Mendellc4701932015-04-10 13:18:51 -04002703 IfCondition GetOppositeCondition() const OVERRIDE {
2704 return kCondEQ;
2705 }
2706
Dave Allison20dfc792014-06-16 20:44:29 -07002707 private:
2708 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2709};
2710
2711class HLessThan : public HCondition {
2712 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002713 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2714 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002715
Roland Levillain9867bc72015-08-05 10:21:34 +01002716 template <typename T> bool Compute(T x, T y) const { return x < y; }
2717
2718 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002719 return GetBlock()->GetGraph()->GetIntConstant(
2720 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002721 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002722 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002723 return GetBlock()->GetGraph()->GetIntConstant(
2724 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002725 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002726
Dave Allison20dfc792014-06-16 20:44:29 -07002727 DECLARE_INSTRUCTION(LessThan);
2728
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002729 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002730 return kCondLT;
2731 }
2732
Mark Mendellc4701932015-04-10 13:18:51 -04002733 IfCondition GetOppositeCondition() const OVERRIDE {
2734 return kCondGE;
2735 }
2736
Dave Allison20dfc792014-06-16 20:44:29 -07002737 private:
2738 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2739};
2740
2741class HLessThanOrEqual : public HCondition {
2742 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002743 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2744 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002745
Roland Levillain9867bc72015-08-05 10:21:34 +01002746 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2747
2748 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002749 return GetBlock()->GetGraph()->GetIntConstant(
2750 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002751 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002752 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002753 return GetBlock()->GetGraph()->GetIntConstant(
2754 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002755 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002756
Dave Allison20dfc792014-06-16 20:44:29 -07002757 DECLARE_INSTRUCTION(LessThanOrEqual);
2758
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002759 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002760 return kCondLE;
2761 }
2762
Mark Mendellc4701932015-04-10 13:18:51 -04002763 IfCondition GetOppositeCondition() const OVERRIDE {
2764 return kCondGT;
2765 }
2766
Dave Allison20dfc792014-06-16 20:44:29 -07002767 private:
2768 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2769};
2770
2771class HGreaterThan : public HCondition {
2772 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002773 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2774 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002775
Roland Levillain9867bc72015-08-05 10:21:34 +01002776 template <typename T> bool Compute(T x, T y) const { return x > y; }
2777
2778 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002779 return GetBlock()->GetGraph()->GetIntConstant(
2780 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002781 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002782 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002783 return GetBlock()->GetGraph()->GetIntConstant(
2784 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002785 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002786
Dave Allison20dfc792014-06-16 20:44:29 -07002787 DECLARE_INSTRUCTION(GreaterThan);
2788
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002789 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002790 return kCondGT;
2791 }
2792
Mark Mendellc4701932015-04-10 13:18:51 -04002793 IfCondition GetOppositeCondition() const OVERRIDE {
2794 return kCondLE;
2795 }
2796
Dave Allison20dfc792014-06-16 20:44:29 -07002797 private:
2798 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2799};
2800
2801class HGreaterThanOrEqual : public HCondition {
2802 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002803 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2804 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002805
Roland Levillain9867bc72015-08-05 10:21:34 +01002806 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2807
2808 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002809 return GetBlock()->GetGraph()->GetIntConstant(
2810 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002811 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002812 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002813 return GetBlock()->GetGraph()->GetIntConstant(
2814 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002815 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002816
Dave Allison20dfc792014-06-16 20:44:29 -07002817 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2818
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002819 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002820 return kCondGE;
2821 }
2822
Mark Mendellc4701932015-04-10 13:18:51 -04002823 IfCondition GetOppositeCondition() const OVERRIDE {
2824 return kCondLT;
2825 }
2826
Dave Allison20dfc792014-06-16 20:44:29 -07002827 private:
2828 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2829};
2830
2831
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002832// Instruction to check how two inputs compare to each other.
2833// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2834class HCompare : public HBinaryOperation {
2835 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002836 HCompare(Primitive::Type type,
2837 HInstruction* first,
2838 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002839 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002840 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002841 : HBinaryOperation(Primitive::kPrimInt,
2842 first,
2843 second,
2844 SideEffectsForArchRuntimeCalls(type),
2845 dex_pc),
2846 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002847 DCHECK_EQ(type, first->GetType());
2848 DCHECK_EQ(type, second->GetType());
2849 }
2850
Roland Levillain9867bc72015-08-05 10:21:34 +01002851 template <typename T>
2852 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002853
Roland Levillain9867bc72015-08-05 10:21:34 +01002854 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002855 return GetBlock()->GetGraph()->GetIntConstant(
2856 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002857 }
2858 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002859 return GetBlock()->GetGraph()->GetIntConstant(
2860 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002861 }
2862
Calin Juravleddb7df22014-11-25 20:56:51 +00002863 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2864 return bias_ == other->AsCompare()->bias_;
2865 }
2866
Mark Mendellc4701932015-04-10 13:18:51 -04002867 ComparisonBias GetBias() const { return bias_; }
2868
Roland Levillain4fa13f62015-07-06 18:11:54 +01002869 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002870
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002871
2872 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2873 // MIPS64 uses a runtime call for FP comparisons.
2874 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2875 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002876
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002877 DECLARE_INSTRUCTION(Compare);
2878
2879 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002880 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002881
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002882 DISALLOW_COPY_AND_ASSIGN(HCompare);
2883};
2884
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002885// A local in the graph. Corresponds to a Dex register.
2886class HLocal : public HTemplateInstruction<0> {
2887 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002888 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002889 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002890
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002891 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002892
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002893 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002894
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002895 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002896 // The Dex register number.
2897 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002898
2899 DISALLOW_COPY_AND_ASSIGN(HLocal);
2900};
2901
2902// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002903class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002904 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002905 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2906 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002907 SetRawInputAt(0, local);
2908 }
2909
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002910 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2911
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002912 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002913
2914 private:
2915 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2916};
2917
2918// Store a value in a given local. This instruction has two inputs: the value
2919// and the local.
2920class HStoreLocal : public HTemplateInstruction<2> {
2921 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002922 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
2923 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002924 SetRawInputAt(0, local);
2925 SetRawInputAt(1, value);
2926 }
2927
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002928 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2929
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002930 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002931
2932 private:
2933 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2934};
2935
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002936class HFloatConstant : public HConstant {
2937 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002938 float GetValue() const { return value_; }
2939
David Brazdil77a48ae2015-09-15 12:34:04 +00002940 uint64_t GetValueAsUint64() const OVERRIDE {
2941 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2942 }
2943
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002944 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002945 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002946 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002947 }
2948
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002949 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002950
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002951 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002952 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002953 }
2954 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002955 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002956 }
2957 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002958 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2959 }
2960 bool IsNaN() const {
2961 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002962 }
2963
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002964 DECLARE_INSTRUCTION(FloatConstant);
2965
2966 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002967 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2968 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2969 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2970 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002971
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002972 const float value_;
2973
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002974 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002975 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002976 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002977 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2978};
2979
2980class HDoubleConstant : public HConstant {
2981 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002982 double GetValue() const { return value_; }
2983
David Brazdil77a48ae2015-09-15 12:34:04 +00002984 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2985
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002986 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002987 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002988 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002989 }
2990
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002991 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002992
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002993 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002994 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002995 }
2996 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002997 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002998 }
2999 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003000 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3001 }
3002 bool IsNaN() const {
3003 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003004 }
3005
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003006 DECLARE_INSTRUCTION(DoubleConstant);
3007
3008 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003009 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3010 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3011 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3012 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003013
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003014 const double value_;
3015
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003016 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003017 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003018 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003019 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3020};
3021
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003022enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003023#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003024#include "intrinsics_list.h"
3025 kNone,
3026 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3027#undef INTRINSICS_LIST
3028#undef OPTIMIZING_INTRINSICS
3029};
3030std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3031
Agi Csaki05f20562015-08-19 14:58:14 -07003032enum IntrinsicNeedsEnvironmentOrCache {
3033 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3034 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003035};
3036
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003037class HInvoke : public HInstruction {
3038 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003039 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003040
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003041 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003042
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003043 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003044 SetRawInputAt(index, argument);
3045 }
3046
Roland Levillain3e3d7332015-04-28 11:00:54 +01003047 // Return the number of arguments. This number can be lower than
3048 // the number of inputs returned by InputCount(), as some invoke
3049 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3050 // inputs at the end of their list of inputs.
3051 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3052
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003053 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003054
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003055
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003056 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003057 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003058
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003059 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3060
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003061 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003062 return intrinsic_;
3063 }
3064
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003065 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003066
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003067 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003068 return GetEnvironment()->GetParent() != nullptr;
3069 }
3070
3071 bool CanThrow() const OVERRIDE { return true; }
3072
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003073 uint32_t* GetIntrinsicOptimizations() {
3074 return &intrinsic_optimizations_;
3075 }
3076
3077 const uint32_t* GetIntrinsicOptimizations() const {
3078 return &intrinsic_optimizations_;
3079 }
3080
3081 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3082
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003083 DECLARE_INSTRUCTION(Invoke);
3084
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003085 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003086 HInvoke(ArenaAllocator* arena,
3087 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003088 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003089 Primitive::Type return_type,
3090 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003091 uint32_t dex_method_index,
3092 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003093 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003094 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003095 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003096 inputs_(number_of_arguments + number_of_other_inputs,
3097 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003098 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003099 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003100 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003101 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003102 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003103 }
3104
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003105 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
3106 DCHECK_LT(index, InputCount());
3107 return inputs_[index];
3108 }
3109
David Brazdil1abb4192015-02-17 18:33:36 +00003110 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003111 DCHECK_LT(index, InputCount());
3112 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003113 }
3114
Roland Levillain3e3d7332015-04-28 11:00:54 +01003115 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003116 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003117 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003118 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003119 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003120 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003121
3122 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3123 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003124
3125 private:
3126 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3127};
3128
Calin Juravle175dc732015-08-25 15:42:32 +01003129class HInvokeUnresolved : public HInvoke {
3130 public:
3131 HInvokeUnresolved(ArenaAllocator* arena,
3132 uint32_t number_of_arguments,
3133 Primitive::Type return_type,
3134 uint32_t dex_pc,
3135 uint32_t dex_method_index,
3136 InvokeType invoke_type)
3137 : HInvoke(arena,
3138 number_of_arguments,
3139 0u /* number_of_other_inputs */,
3140 return_type,
3141 dex_pc,
3142 dex_method_index,
3143 invoke_type) {
3144 }
3145
3146 DECLARE_INSTRUCTION(InvokeUnresolved);
3147
3148 private:
3149 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3150};
3151
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003152class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003153 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003154 // Requirements of this method call regarding the class
3155 // initialization (clinit) check of its declaring class.
3156 enum class ClinitCheckRequirement {
3157 kNone, // Class already initialized.
3158 kExplicit, // Static call having explicit clinit check as last input.
3159 kImplicit, // Static call implicitly requiring a clinit check.
3160 };
3161
Vladimir Marko58155012015-08-19 12:49:41 +00003162 // Determines how to load the target ArtMethod*.
3163 enum class MethodLoadKind {
3164 // Use a String init ArtMethod* loaded from Thread entrypoints.
3165 kStringInit,
3166
3167 // Use the method's own ArtMethod* loaded by the register allocator.
3168 kRecursive,
3169
3170 // Use ArtMethod* at a known address, embed the direct address in the code.
3171 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3172 kDirectAddress,
3173
3174 // Use ArtMethod* at an address that will be known at link time, embed the direct
3175 // address in the 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 kDirectAddressWithFixup,
3179
3180 // Load from resoved methods array in the dex cache using a PC-relative load.
3181 // Used when we need to use the dex cache, for example for invoke-static that
3182 // may cause class initialization (the entry may point to a resolution method),
3183 // and we know that we can access the dex cache arrays using a PC-relative load.
3184 kDexCachePcRelative,
3185
3186 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3187 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3188 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3189 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3190 kDexCacheViaMethod,
3191 };
3192
3193 // Determines the location of the code pointer.
3194 enum class CodePtrLocation {
3195 // Recursive call, use local PC-relative call instruction.
3196 kCallSelf,
3197
3198 // Use PC-relative call instruction patched at link time.
3199 // Used for calls within an oat file, boot->boot or app->app.
3200 kCallPCRelative,
3201
3202 // Call to a known target address, embed the direct address in code.
3203 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3204 kCallDirect,
3205
3206 // Call to a target address that will be known at link time, embed the direct
3207 // address in code. If the image is relocatable, emit .patch_oat entry.
3208 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3209 // the image relocatable or not.
3210 kCallDirectWithFixup,
3211
3212 // Use code pointer from the ArtMethod*.
3213 // Used when we don't know the target code. This is also the last-resort-kind used when
3214 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3215 kCallArtMethod,
3216 };
3217
3218 struct DispatchInfo {
3219 const MethodLoadKind method_load_kind;
3220 const CodePtrLocation code_ptr_location;
3221 // The method load data holds
3222 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3223 // Note that there are multiple string init methods, each having its own offset.
3224 // - the method address for kDirectAddress
3225 // - the dex cache arrays offset for kDexCachePcRel.
3226 const uint64_t method_load_data;
3227 const uint64_t direct_code_ptr;
3228 };
3229
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003230 HInvokeStaticOrDirect(ArenaAllocator* arena,
3231 uint32_t number_of_arguments,
3232 Primitive::Type return_type,
3233 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003234 uint32_t method_index,
3235 MethodReference target_method,
3236 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003237 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003238 InvokeType invoke_type,
3239 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003240 : HInvoke(arena,
3241 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003242 // There is one extra argument for the HCurrentMethod node, and
3243 // potentially one other if the clinit check is explicit, and one other
3244 // if the method is a string factory.
3245 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003246 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003247 return_type,
3248 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003249 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003250 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003251 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003252 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003253 target_method_(target_method),
3254 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003255
Calin Juravle641547a2015-04-21 22:08:51 +01003256 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3257 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003258 // We access the method via the dex cache so we can't do an implicit null check.
3259 // TODO: for intrinsics we can generate implicit null checks.
3260 return false;
3261 }
3262
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003263 bool CanBeNull() const OVERRIDE {
3264 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3265 }
3266
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003267 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003268 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3269 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3270 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003271 bool NeedsDexCache() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003272 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003273 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003274 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3275 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3276 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3277 MethodReference GetTargetMethod() const { return target_method_; }
3278
3279 int32_t GetStringInitOffset() const {
3280 DCHECK(IsStringInit());
3281 return dispatch_info_.method_load_data;
3282 }
3283
3284 uint64_t GetMethodAddress() const {
3285 DCHECK(HasMethodAddress());
3286 return dispatch_info_.method_load_data;
3287 }
3288
3289 uint32_t GetDexCacheArrayOffset() const {
3290 DCHECK(HasPcRelDexCache());
3291 return dispatch_info_.method_load_data;
3292 }
3293
3294 uint64_t GetDirectCodePtr() const {
3295 DCHECK(HasDirectCodePtr());
3296 return dispatch_info_.direct_code_ptr;
3297 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003298
Calin Juravle68ad6492015-08-18 17:08:12 +01003299 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3300
Roland Levillain4c0eb422015-04-24 16:43:49 +01003301 // Is this instruction a call to a static method?
3302 bool IsStatic() const {
3303 return GetInvokeType() == kStatic;
3304 }
3305
Roland Levillain3e3d7332015-04-28 11:00:54 +01003306 // Remove the art::HLoadClass instruction set as last input by
3307 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3308 // the initial art::HClinitCheck instruction (only relevant for
3309 // static calls with explicit clinit check).
3310 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003311 DCHECK(IsStaticWithExplicitClinitCheck());
3312 size_t last_input_index = InputCount() - 1;
3313 HInstruction* last_input = InputAt(last_input_index);
3314 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003315 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003316 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003317 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003318 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3319 DCHECK(IsStaticWithImplicitClinitCheck());
3320 }
3321
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003322 bool IsStringFactoryFor(HFakeString* str) const {
3323 if (!IsStringInit()) return false;
3324 // +1 for the current method.
3325 if (InputCount() == (number_of_arguments_ + 1)) return false;
3326 return InputAt(InputCount() - 1)->AsFakeString() == str;
3327 }
3328
3329 void RemoveFakeStringArgumentAsLastInput() {
3330 DCHECK(IsStringInit());
3331 size_t last_input_index = InputCount() - 1;
3332 HInstruction* last_input = InputAt(last_input_index);
3333 DCHECK(last_input != nullptr);
3334 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3335 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003336 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003337 }
3338
Roland Levillain4c0eb422015-04-24 16:43:49 +01003339 // Is this a call to a static method whose declaring class has an
3340 // explicit intialization check in the graph?
3341 bool IsStaticWithExplicitClinitCheck() const {
3342 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3343 }
3344
3345 // Is this a call to a static method whose declaring class has an
3346 // implicit intialization check requirement?
3347 bool IsStaticWithImplicitClinitCheck() const {
3348 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3349 }
3350
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003351 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003352
Roland Levillain4c0eb422015-04-24 16:43:49 +01003353 protected:
3354 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3355 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3356 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3357 HInstruction* input = input_record.GetInstruction();
3358 // `input` is the last input of a static invoke marked as having
3359 // an explicit clinit check. It must either be:
3360 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3361 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3362 DCHECK(input != nullptr);
3363 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3364 }
3365 return input_record;
3366 }
3367
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003368 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003369 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003370 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003371 // The target method may refer to different dex file or method index than the original
3372 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3373 // in derived class to increase visibility.
3374 MethodReference target_method_;
3375 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003376
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003377 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003378};
3379
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003380class HInvokeVirtual : public HInvoke {
3381 public:
3382 HInvokeVirtual(ArenaAllocator* arena,
3383 uint32_t number_of_arguments,
3384 Primitive::Type return_type,
3385 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003386 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003387 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003388 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003389 vtable_index_(vtable_index) {}
3390
Calin Juravle641547a2015-04-21 22:08:51 +01003391 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003392 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003393 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003394 }
3395
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003396 uint32_t GetVTableIndex() const { return vtable_index_; }
3397
3398 DECLARE_INSTRUCTION(InvokeVirtual);
3399
3400 private:
3401 const uint32_t vtable_index_;
3402
3403 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3404};
3405
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003406class HInvokeInterface : public HInvoke {
3407 public:
3408 HInvokeInterface(ArenaAllocator* arena,
3409 uint32_t number_of_arguments,
3410 Primitive::Type return_type,
3411 uint32_t dex_pc,
3412 uint32_t dex_method_index,
3413 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003414 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003415 imt_index_(imt_index) {}
3416
Calin Juravle641547a2015-04-21 22:08:51 +01003417 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003418 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003419 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003420 }
3421
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003422 uint32_t GetImtIndex() const { return imt_index_; }
3423 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3424
3425 DECLARE_INSTRUCTION(InvokeInterface);
3426
3427 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003428 const uint32_t imt_index_;
3429
3430 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3431};
3432
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003433class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003434 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003435 HNewInstance(HCurrentMethod* current_method,
3436 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003437 uint16_t type_index,
3438 const DexFile& dex_file,
3439 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003440 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003441 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003442 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003443 entrypoint_(entrypoint) {
3444 SetRawInputAt(0, current_method);
3445 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003446
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003447 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003448 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003449
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003450 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003451 bool NeedsEnvironment() const OVERRIDE { return true; }
3452 // It may throw when called on:
3453 // - interfaces
3454 // - abstract/innaccessible/unknown classes
3455 // TODO: optimize when possible.
3456 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003457
Calin Juravle10e244f2015-01-26 18:54:32 +00003458 bool CanBeNull() const OVERRIDE { return false; }
3459
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003460 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3461
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003462 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003463
3464 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003465 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003466 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003467 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003468
3469 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3470};
3471
Roland Levillain88cb1752014-10-20 16:36:47 +01003472class HNeg : public HUnaryOperation {
3473 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003474 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3475 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003476
Roland Levillain9867bc72015-08-05 10:21:34 +01003477 template <typename T> T Compute(T x) const { return -x; }
3478
3479 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003480 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003481 }
3482 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003483 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003484 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003485
Roland Levillain88cb1752014-10-20 16:36:47 +01003486 DECLARE_INSTRUCTION(Neg);
3487
3488 private:
3489 DISALLOW_COPY_AND_ASSIGN(HNeg);
3490};
3491
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003492class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003493 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003494 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003495 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003496 uint32_t dex_pc,
3497 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003498 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003499 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003500 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003501 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003502 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003503 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003504 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003505 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003506 }
3507
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003508 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003509 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003510
3511 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003512 bool NeedsEnvironment() const OVERRIDE { return true; }
3513
Mingyao Yang0c365e62015-03-31 15:09:29 -07003514 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3515 bool CanThrow() const OVERRIDE { return true; }
3516
Calin Juravle10e244f2015-01-26 18:54:32 +00003517 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003518
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003519 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3520
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003521 DECLARE_INSTRUCTION(NewArray);
3522
3523 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003524 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003525 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003526 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003527
3528 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3529};
3530
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003531class HAdd : public HBinaryOperation {
3532 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003533 HAdd(Primitive::Type result_type,
3534 HInstruction* left,
3535 HInstruction* right,
3536 uint32_t dex_pc = kNoDexPc)
3537 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003538
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003539 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003540
Roland Levillain9867bc72015-08-05 10:21:34 +01003541 template <typename T> T Compute(T x, T y) const { return x + y; }
3542
3543 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003544 return GetBlock()->GetGraph()->GetIntConstant(
3545 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003546 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003547 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003548 return GetBlock()->GetGraph()->GetLongConstant(
3549 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003550 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003551
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003552 DECLARE_INSTRUCTION(Add);
3553
3554 private:
3555 DISALLOW_COPY_AND_ASSIGN(HAdd);
3556};
3557
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003558class HSub : public HBinaryOperation {
3559 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003560 HSub(Primitive::Type result_type,
3561 HInstruction* left,
3562 HInstruction* right,
3563 uint32_t dex_pc = kNoDexPc)
3564 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003565
Roland Levillain9867bc72015-08-05 10:21:34 +01003566 template <typename T> T Compute(T x, T y) const { return x - y; }
3567
3568 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003569 return GetBlock()->GetGraph()->GetIntConstant(
3570 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003571 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003572 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003573 return GetBlock()->GetGraph()->GetLongConstant(
3574 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003575 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003576
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003577 DECLARE_INSTRUCTION(Sub);
3578
3579 private:
3580 DISALLOW_COPY_AND_ASSIGN(HSub);
3581};
3582
Calin Juravle34bacdf2014-10-07 20:23:36 +01003583class HMul : public HBinaryOperation {
3584 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003585 HMul(Primitive::Type result_type,
3586 HInstruction* left,
3587 HInstruction* right,
3588 uint32_t dex_pc = kNoDexPc)
3589 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003590
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003591 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003592
Roland Levillain9867bc72015-08-05 10:21:34 +01003593 template <typename T> T Compute(T x, T y) const { return x * y; }
3594
3595 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003596 return GetBlock()->GetGraph()->GetIntConstant(
3597 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003598 }
3599 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003600 return GetBlock()->GetGraph()->GetLongConstant(
3601 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003602 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003603
3604 DECLARE_INSTRUCTION(Mul);
3605
3606 private:
3607 DISALLOW_COPY_AND_ASSIGN(HMul);
3608};
3609
Calin Juravle7c4954d2014-10-28 16:57:40 +00003610class HDiv : public HBinaryOperation {
3611 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003612 HDiv(Primitive::Type result_type,
3613 HInstruction* left,
3614 HInstruction* right,
3615 uint32_t dex_pc)
3616 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003617
Roland Levillain9867bc72015-08-05 10:21:34 +01003618 template <typename T>
3619 T Compute(T x, T y) const {
3620 // Our graph structure ensures we never have 0 for `y` during
3621 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003622 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003623 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003624 return (y == -1) ? -x : x / y;
3625 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003626
Roland Levillain9867bc72015-08-05 10:21:34 +01003627 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003628 return GetBlock()->GetGraph()->GetIntConstant(
3629 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003630 }
3631 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003632 return GetBlock()->GetGraph()->GetLongConstant(
3633 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003634 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003635
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003636 static SideEffects SideEffectsForArchRuntimeCalls() {
3637 // The generated code can use a runtime call.
3638 return SideEffects::CanTriggerGC();
3639 }
3640
Calin Juravle7c4954d2014-10-28 16:57:40 +00003641 DECLARE_INSTRUCTION(Div);
3642
3643 private:
3644 DISALLOW_COPY_AND_ASSIGN(HDiv);
3645};
3646
Calin Juravlebacfec32014-11-14 15:54:36 +00003647class HRem : public HBinaryOperation {
3648 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003649 HRem(Primitive::Type result_type,
3650 HInstruction* left,
3651 HInstruction* right,
3652 uint32_t dex_pc)
3653 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003654
Roland Levillain9867bc72015-08-05 10:21:34 +01003655 template <typename T>
3656 T Compute(T x, T y) const {
3657 // Our graph structure ensures we never have 0 for `y` during
3658 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003659 DCHECK_NE(y, 0);
3660 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3661 return (y == -1) ? 0 : x % y;
3662 }
3663
Roland Levillain9867bc72015-08-05 10:21:34 +01003664 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003665 return GetBlock()->GetGraph()->GetIntConstant(
3666 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003667 }
3668 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003669 return GetBlock()->GetGraph()->GetLongConstant(
3670 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003671 }
3672
Calin Juravlebacfec32014-11-14 15:54:36 +00003673
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003674 static SideEffects SideEffectsForArchRuntimeCalls() {
3675 return SideEffects::CanTriggerGC();
3676 }
3677
Calin Juravlebacfec32014-11-14 15:54:36 +00003678 DECLARE_INSTRUCTION(Rem);
3679
3680 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003681 DISALLOW_COPY_AND_ASSIGN(HRem);
3682};
3683
Calin Juravled0d48522014-11-04 16:40:20 +00003684class HDivZeroCheck : public HExpression<1> {
3685 public:
3686 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003687 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003688 SetRawInputAt(0, value);
3689 }
3690
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003691 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3692
Calin Juravled0d48522014-11-04 16:40:20 +00003693 bool CanBeMoved() const OVERRIDE { return true; }
3694
3695 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3696 UNUSED(other);
3697 return true;
3698 }
3699
3700 bool NeedsEnvironment() const OVERRIDE { return true; }
3701 bool CanThrow() const OVERRIDE { return true; }
3702
Calin Juravled0d48522014-11-04 16:40:20 +00003703 DECLARE_INSTRUCTION(DivZeroCheck);
3704
3705 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003706 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3707};
3708
Calin Juravle9aec02f2014-11-18 23:06:35 +00003709class HShl : public HBinaryOperation {
3710 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003711 HShl(Primitive::Type result_type,
3712 HInstruction* left,
3713 HInstruction* right,
3714 uint32_t dex_pc = kNoDexPc)
3715 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003716
Roland Levillain9867bc72015-08-05 10:21:34 +01003717 template <typename T, typename U, typename V>
3718 T Compute(T x, U y, V max_shift_value) const {
3719 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3720 "V is not the unsigned integer type corresponding to T");
3721 return x << (y & max_shift_value);
3722 }
3723
3724 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3725 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003726 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003727 }
3728 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3729 // case is handled as `x << static_cast<int>(y)`.
3730 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3731 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003732 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003733 }
3734 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3735 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003736 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003737 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003738
3739 DECLARE_INSTRUCTION(Shl);
3740
3741 private:
3742 DISALLOW_COPY_AND_ASSIGN(HShl);
3743};
3744
3745class HShr : public HBinaryOperation {
3746 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003747 HShr(Primitive::Type result_type,
3748 HInstruction* left,
3749 HInstruction* right,
3750 uint32_t dex_pc = kNoDexPc)
3751 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003752
Roland Levillain9867bc72015-08-05 10:21:34 +01003753 template <typename T, typename U, typename V>
3754 T Compute(T x, U y, V max_shift_value) const {
3755 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3756 "V is not the unsigned integer type corresponding to T");
3757 return x >> (y & max_shift_value);
3758 }
3759
3760 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3761 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003762 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003763 }
3764 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3765 // case is handled as `x >> static_cast<int>(y)`.
3766 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3767 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003768 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003769 }
3770 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3771 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003772 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003773 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003774
3775 DECLARE_INSTRUCTION(Shr);
3776
3777 private:
3778 DISALLOW_COPY_AND_ASSIGN(HShr);
3779};
3780
3781class HUShr : public HBinaryOperation {
3782 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003783 HUShr(Primitive::Type result_type,
3784 HInstruction* left,
3785 HInstruction* right,
3786 uint32_t dex_pc = kNoDexPc)
3787 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003788
Roland Levillain9867bc72015-08-05 10:21:34 +01003789 template <typename T, typename U, typename V>
3790 T Compute(T x, U y, V max_shift_value) const {
3791 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3792 "V is not the unsigned integer type corresponding to T");
3793 V ux = static_cast<V>(x);
3794 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003795 }
3796
Roland Levillain9867bc72015-08-05 10:21:34 +01003797 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3798 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003799 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003800 }
3801 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3802 // case is handled as `x >>> static_cast<int>(y)`.
3803 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3804 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003805 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003806 }
3807 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3808 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003809 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003810 }
3811
3812 DECLARE_INSTRUCTION(UShr);
3813
3814 private:
3815 DISALLOW_COPY_AND_ASSIGN(HUShr);
3816};
3817
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003818class HAnd : public HBinaryOperation {
3819 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003820 HAnd(Primitive::Type result_type,
3821 HInstruction* left,
3822 HInstruction* right,
3823 uint32_t dex_pc = kNoDexPc)
3824 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003825
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003826 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003827
Roland Levillain9867bc72015-08-05 10:21:34 +01003828 template <typename T, typename U>
3829 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3830
3831 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003832 return GetBlock()->GetGraph()->GetIntConstant(
3833 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003834 }
3835 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003836 return GetBlock()->GetGraph()->GetLongConstant(
3837 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003838 }
3839 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003840 return GetBlock()->GetGraph()->GetLongConstant(
3841 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003842 }
3843 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003844 return GetBlock()->GetGraph()->GetLongConstant(
3845 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003846 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003847
3848 DECLARE_INSTRUCTION(And);
3849
3850 private:
3851 DISALLOW_COPY_AND_ASSIGN(HAnd);
3852};
3853
3854class HOr : public HBinaryOperation {
3855 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003856 HOr(Primitive::Type result_type,
3857 HInstruction* left,
3858 HInstruction* right,
3859 uint32_t dex_pc = kNoDexPc)
3860 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003861
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003862 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003863
Roland Levillain9867bc72015-08-05 10:21:34 +01003864 template <typename T, typename U>
3865 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3866
3867 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003868 return GetBlock()->GetGraph()->GetIntConstant(
3869 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003870 }
3871 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003872 return GetBlock()->GetGraph()->GetLongConstant(
3873 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003874 }
3875 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003876 return GetBlock()->GetGraph()->GetLongConstant(
3877 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003878 }
3879 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003880 return GetBlock()->GetGraph()->GetLongConstant(
3881 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003882 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003883
3884 DECLARE_INSTRUCTION(Or);
3885
3886 private:
3887 DISALLOW_COPY_AND_ASSIGN(HOr);
3888};
3889
3890class HXor : public HBinaryOperation {
3891 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003892 HXor(Primitive::Type result_type,
3893 HInstruction* left,
3894 HInstruction* right,
3895 uint32_t dex_pc = kNoDexPc)
3896 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003897
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003898 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003899
Roland Levillain9867bc72015-08-05 10:21:34 +01003900 template <typename T, typename U>
3901 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3902
3903 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003904 return GetBlock()->GetGraph()->GetIntConstant(
3905 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003906 }
3907 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003908 return GetBlock()->GetGraph()->GetLongConstant(
3909 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003910 }
3911 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003912 return GetBlock()->GetGraph()->GetLongConstant(
3913 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003914 }
3915 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003916 return GetBlock()->GetGraph()->GetLongConstant(
3917 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003918 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003919
3920 DECLARE_INSTRUCTION(Xor);
3921
3922 private:
3923 DISALLOW_COPY_AND_ASSIGN(HXor);
3924};
3925
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003926// The value of a parameter in this method. Its location depends on
3927// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003928class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003929 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003930 HParameterValue(uint8_t index,
3931 Primitive::Type parameter_type,
3932 bool is_this = false)
3933 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003934 index_(index),
3935 is_this_(is_this),
3936 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003937
3938 uint8_t GetIndex() const { return index_; }
3939
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003940 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3941 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003942
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003943 bool IsThis() const { return is_this_; }
3944
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003945 DECLARE_INSTRUCTION(ParameterValue);
3946
3947 private:
3948 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003949 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003950 const uint8_t index_;
3951
Calin Juravle10e244f2015-01-26 18:54:32 +00003952 // Whether or not the parameter value corresponds to 'this' argument.
3953 const bool is_this_;
3954
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003955 bool can_be_null_;
3956
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003957 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3958};
3959
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003960class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003961 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003962 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3963 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003964
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003965 bool CanBeMoved() const OVERRIDE { return true; }
3966 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003967 UNUSED(other);
3968 return true;
3969 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003970
Roland Levillain9867bc72015-08-05 10:21:34 +01003971 template <typename T> T Compute(T x) const { return ~x; }
3972
3973 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) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003977 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003978 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003979
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003980 DECLARE_INSTRUCTION(Not);
3981
3982 private:
3983 DISALLOW_COPY_AND_ASSIGN(HNot);
3984};
3985
David Brazdil66d126e2015-04-03 16:02:44 +01003986class HBooleanNot : public HUnaryOperation {
3987 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003988 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3989 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01003990
3991 bool CanBeMoved() const OVERRIDE { return true; }
3992 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3993 UNUSED(other);
3994 return true;
3995 }
3996
Roland Levillain9867bc72015-08-05 10:21:34 +01003997 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003998 DCHECK(IsUint<1>(x));
3999 return !x;
4000 }
4001
Roland Levillain9867bc72015-08-05 10:21:34 +01004002 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004003 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004004 }
4005 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4006 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004007 UNREACHABLE();
4008 }
4009
4010 DECLARE_INSTRUCTION(BooleanNot);
4011
4012 private:
4013 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4014};
4015
Roland Levillaindff1f282014-11-05 14:15:05 +00004016class HTypeConversion : public HExpression<1> {
4017 public:
4018 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004019 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004020 : HExpression(result_type,
4021 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4022 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004023 SetRawInputAt(0, input);
4024 DCHECK_NE(input->GetType(), result_type);
4025 }
4026
4027 HInstruction* GetInput() const { return InputAt(0); }
4028 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4029 Primitive::Type GetResultType() const { return GetType(); }
4030
Roland Levillain624279f2014-12-04 11:54:28 +00004031 // Required by the x86 and ARM code generators when producing calls
4032 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004033
Roland Levillaindff1f282014-11-05 14:15:05 +00004034 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004035 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004036
Mark Mendelle82549b2015-05-06 10:55:34 -04004037 // Try to statically evaluate the conversion and return a HConstant
4038 // containing the result. If the input cannot be converted, return nullptr.
4039 HConstant* TryStaticEvaluation() const;
4040
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004041 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4042 Primitive::Type result_type) {
4043 // Some architectures may not require the 'GC' side effects, but at this point
4044 // in the compilation process we do not know what architecture we will
4045 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004046 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4047 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004048 return SideEffects::CanTriggerGC();
4049 }
4050 return SideEffects::None();
4051 }
4052
Roland Levillaindff1f282014-11-05 14:15:05 +00004053 DECLARE_INSTRUCTION(TypeConversion);
4054
4055 private:
4056 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4057};
4058
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004059static constexpr uint32_t kNoRegNumber = -1;
4060
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004061class HPhi : public HInstruction {
4062 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004063 HPhi(ArenaAllocator* arena,
4064 uint32_t reg_number,
4065 size_t number_of_inputs,
4066 Primitive::Type type,
4067 uint32_t dex_pc = kNoDexPc)
4068 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004069 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004070 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004071 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004072 is_live_(false),
4073 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004074 }
4075
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004076 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4077 static Primitive::Type ToPhiType(Primitive::Type type) {
4078 switch (type) {
4079 case Primitive::kPrimBoolean:
4080 case Primitive::kPrimByte:
4081 case Primitive::kPrimShort:
4082 case Primitive::kPrimChar:
4083 return Primitive::kPrimInt;
4084 default:
4085 return type;
4086 }
4087 }
4088
David Brazdilffee3d32015-07-06 11:48:53 +01004089 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4090
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004091 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004092
4093 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004094 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004095
Calin Juravle10e244f2015-01-26 18:54:32 +00004096 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004097 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004098
Calin Juravle10e244f2015-01-26 18:54:32 +00004099 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4100 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4101
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004102 uint32_t GetRegNumber() const { return reg_number_; }
4103
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004104 void SetDead() { is_live_ = false; }
4105 void SetLive() { is_live_ = true; }
4106 bool IsDead() const { return !is_live_; }
4107 bool IsLive() const { return is_live_; }
4108
David Brazdil77a48ae2015-09-15 12:34:04 +00004109 bool IsVRegEquivalentOf(HInstruction* other) const {
4110 return other != nullptr
4111 && other->IsPhi()
4112 && other->AsPhi()->GetBlock() == GetBlock()
4113 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4114 }
4115
Calin Juravlea4f88312015-04-16 12:57:19 +01004116 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4117 // An equivalent phi is a phi having the same dex register and type.
4118 // It assumes that phis with the same dex register are adjacent.
4119 HPhi* GetNextEquivalentPhiWithSameType() {
4120 HInstruction* next = GetNext();
4121 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4122 if (next->GetType() == GetType()) {
4123 return next->AsPhi();
4124 }
4125 next = next->GetNext();
4126 }
4127 return nullptr;
4128 }
4129
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004130 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004131
David Brazdil1abb4192015-02-17 18:33:36 +00004132 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004133 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
4134 DCHECK_LE(index, InputCount());
4135 return inputs_[index];
4136 }
David Brazdil1abb4192015-02-17 18:33:36 +00004137
4138 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004139 DCHECK_LE(index, InputCount());
4140 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004141 }
4142
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004143 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004144 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004145 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004146 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004147 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004148 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004149
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004150 DISALLOW_COPY_AND_ASSIGN(HPhi);
4151};
4152
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004153class HNullCheck : public HExpression<1> {
4154 public:
4155 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004156 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004157 SetRawInputAt(0, value);
4158 }
4159
Calin Juravle10e244f2015-01-26 18:54:32 +00004160 bool CanBeMoved() const OVERRIDE { return true; }
4161 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004162 UNUSED(other);
4163 return true;
4164 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004165
Calin Juravle10e244f2015-01-26 18:54:32 +00004166 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004167
Calin Juravle10e244f2015-01-26 18:54:32 +00004168 bool CanThrow() const OVERRIDE { return true; }
4169
4170 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004171
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004172
4173 DECLARE_INSTRUCTION(NullCheck);
4174
4175 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004176 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4177};
4178
4179class FieldInfo : public ValueObject {
4180 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004181 FieldInfo(MemberOffset field_offset,
4182 Primitive::Type field_type,
4183 bool is_volatile,
4184 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004185 const DexFile& dex_file,
4186 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004187 : field_offset_(field_offset),
4188 field_type_(field_type),
4189 is_volatile_(is_volatile),
4190 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004191 dex_file_(dex_file),
4192 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004193
4194 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004195 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004196 uint32_t GetFieldIndex() const { return index_; }
4197 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004198 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004199 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004200
4201 private:
4202 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004203 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004204 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004205 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004206 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004207 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004208};
4209
4210class HInstanceFieldGet : public HExpression<1> {
4211 public:
4212 HInstanceFieldGet(HInstruction* value,
4213 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004214 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004215 bool is_volatile,
4216 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004217 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004218 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004219 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004220 : HExpression(
4221 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004222 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004223 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004224 SetRawInputAt(0, value);
4225 }
4226
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004227 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004228
4229 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4230 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4231 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004232 }
4233
Calin Juravle641547a2015-04-21 22:08:51 +01004234 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4235 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004236 }
4237
4238 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004239 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4240 }
4241
Calin Juravle52c48962014-12-16 17:02:57 +00004242 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004243 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004244 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004245 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004246
4247 DECLARE_INSTRUCTION(InstanceFieldGet);
4248
4249 private:
4250 const FieldInfo field_info_;
4251
4252 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4253};
4254
4255class HInstanceFieldSet : public HTemplateInstruction<2> {
4256 public:
4257 HInstanceFieldSet(HInstruction* object,
4258 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004259 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004260 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004261 bool is_volatile,
4262 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004263 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004264 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004265 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004266 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004267 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004268 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004269 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004270 SetRawInputAt(0, object);
4271 SetRawInputAt(1, value);
4272 }
4273
Calin Juravle641547a2015-04-21 22:08:51 +01004274 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4275 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004276 }
4277
Calin Juravle52c48962014-12-16 17:02:57 +00004278 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004279 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004280 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004281 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004282 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004283 bool GetValueCanBeNull() const { return value_can_be_null_; }
4284 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004285
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004286 DECLARE_INSTRUCTION(InstanceFieldSet);
4287
4288 private:
4289 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004290 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004291
4292 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4293};
4294
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004295class HArrayGet : public HExpression<2> {
4296 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004297 HArrayGet(HInstruction* array,
4298 HInstruction* index,
4299 Primitive::Type type,
Calin Juravle154746b2015-10-06 15:46:54 +01004300 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004301 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004302 SetRawInputAt(0, array);
4303 SetRawInputAt(1, index);
4304 }
4305
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004306 bool CanBeMoved() const OVERRIDE { return true; }
4307 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004308 UNUSED(other);
4309 return true;
4310 }
Calin Juravle641547a2015-04-21 22:08:51 +01004311 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4312 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004313 // TODO: We can be smarter here.
4314 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4315 // which generates the implicit null check. There are cases when these can be removed
4316 // to produce better code. If we ever add optimizations to do so we should allow an
4317 // implicit check here (as long as the address falls in the first page).
4318 return false;
4319 }
4320
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004321 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004322
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004323 HInstruction* GetArray() const { return InputAt(0); }
4324 HInstruction* GetIndex() const { return InputAt(1); }
4325
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004326 DECLARE_INSTRUCTION(ArrayGet);
4327
4328 private:
4329 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4330};
4331
4332class HArraySet : public HTemplateInstruction<3> {
4333 public:
4334 HArraySet(HInstruction* array,
4335 HInstruction* index,
4336 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004337 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004338 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004339 : HTemplateInstruction(
4340 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004341 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004342 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004343 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004344 value_can_be_null_(true),
4345 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004346 SetRawInputAt(0, array);
4347 SetRawInputAt(1, index);
4348 SetRawInputAt(2, value);
4349 }
4350
Calin Juravle77520bc2015-01-12 18:45:46 +00004351 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004352 // We currently always call a runtime method to catch array store
4353 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004354 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004355 }
4356
Mingyao Yang81014cb2015-06-02 03:16:27 -07004357 // Can throw ArrayStoreException.
4358 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4359
Calin Juravle641547a2015-04-21 22:08:51 +01004360 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4361 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004362 // TODO: Same as for ArrayGet.
4363 return false;
4364 }
4365
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004366 void ClearNeedsTypeCheck() {
4367 needs_type_check_ = false;
4368 }
4369
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004370 void ClearValueCanBeNull() {
4371 value_can_be_null_ = false;
4372 }
4373
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004374 void SetStaticTypeOfArrayIsObjectArray() {
4375 static_type_of_array_is_object_array_ = true;
4376 }
4377
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004378 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004379 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004380 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004381
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004382 HInstruction* GetArray() const { return InputAt(0); }
4383 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004384 HInstruction* GetValue() const { return InputAt(2); }
4385
4386 Primitive::Type GetComponentType() const {
4387 // The Dex format does not type floating point index operations. Since the
4388 // `expected_component_type_` is set during building and can therefore not
4389 // be correct, we also check what is the value type. If it is a floating
4390 // point type, we must use that type.
4391 Primitive::Type value_type = GetValue()->GetType();
4392 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4393 ? value_type
4394 : expected_component_type_;
4395 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004396
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004397 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4398 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4399 }
4400
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004401 DECLARE_INSTRUCTION(ArraySet);
4402
4403 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004404 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004405 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004406 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004407 // Cached information for the reference_type_info_ so that codegen
4408 // does not need to inspect the static type.
4409 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004410
4411 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4412};
4413
4414class HArrayLength : public HExpression<1> {
4415 public:
Calin Juravle154746b2015-10-06 15:46:54 +01004416 explicit HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004417 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004418 // Note that arrays do not change length, so the instruction does not
4419 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004420 SetRawInputAt(0, array);
4421 }
4422
Calin Juravle77520bc2015-01-12 18:45:46 +00004423 bool CanBeMoved() const OVERRIDE { return true; }
4424 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004425 UNUSED(other);
4426 return true;
4427 }
Calin Juravle641547a2015-04-21 22:08:51 +01004428 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4429 return obj == InputAt(0);
4430 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004431
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004432 DECLARE_INSTRUCTION(ArrayLength);
4433
4434 private:
4435 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4436};
4437
4438class HBoundsCheck : public HExpression<2> {
4439 public:
4440 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004441 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004442 DCHECK(index->GetType() == Primitive::kPrimInt);
4443 SetRawInputAt(0, index);
4444 SetRawInputAt(1, length);
4445 }
4446
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004447 bool CanBeMoved() const OVERRIDE { return true; }
4448 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004449 UNUSED(other);
4450 return true;
4451 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004452
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004453 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004454
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004455 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004456
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004457
4458 DECLARE_INSTRUCTION(BoundsCheck);
4459
4460 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004461 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4462};
4463
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004464/**
4465 * Some DEX instructions are folded into multiple HInstructions that need
4466 * to stay live until the last HInstruction. This class
4467 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004468 * HInstruction stays live. `index` represents the stack location index of the
4469 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004470 */
4471class HTemporary : public HTemplateInstruction<0> {
4472 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004473 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4474 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004475
4476 size_t GetIndex() const { return index_; }
4477
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004478 Primitive::Type GetType() const OVERRIDE {
4479 // The previous instruction is the one that will be stored in the temporary location.
4480 DCHECK(GetPrevious() != nullptr);
4481 return GetPrevious()->GetType();
4482 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004483
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004484 DECLARE_INSTRUCTION(Temporary);
4485
4486 private:
4487 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004488 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4489};
4490
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004491class HSuspendCheck : public HTemplateInstruction<0> {
4492 public:
4493 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004494 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004495
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004496 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004497 return true;
4498 }
4499
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004500 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4501 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004502
4503 DECLARE_INSTRUCTION(SuspendCheck);
4504
4505 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004506 // Only used for code generation, in order to share the same slow path between back edges
4507 // of a same loop.
4508 SlowPathCode* slow_path_;
4509
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004510 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4511};
4512
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004513/**
4514 * Instruction to load a Class object.
4515 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004516class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004517 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004518 HLoadClass(HCurrentMethod* current_method,
4519 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004520 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004521 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004522 uint32_t dex_pc,
4523 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004524 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004525 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004526 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004527 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004528 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004529 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004530 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004531 SetRawInputAt(0, current_method);
4532 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004533
4534 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004535
4536 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4537 return other->AsLoadClass()->type_index_ == type_index_;
4538 }
4539
4540 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4541
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004542 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004543 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004544 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004545
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004546 bool NeedsEnvironment() const OVERRIDE {
4547 // Will call runtime and load the class if the class is not loaded yet.
4548 // TODO: finer grain decision.
Calin Juravle98893e12015-10-02 21:05:03 +01004549 return !is_referrers_class_ || needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004550 }
4551
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004552 bool MustGenerateClinitCheck() const {
4553 return generate_clinit_check_;
4554 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004555 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4556 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004557 }
4558
4559 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004560 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4561 }
4562
4563 bool NeedsAccessCheck() const {
4564 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004565 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004566
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004567 bool CanThrow() const OVERRIDE {
4568 // May call runtime and and therefore can throw.
4569 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004570 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004571 }
4572
Calin Juravleacf735c2015-02-12 15:25:22 +00004573 ReferenceTypeInfo GetLoadedClassRTI() {
4574 return loaded_class_rti_;
4575 }
4576
4577 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4578 // Make sure we only set exact types (the loaded class should never be merged).
4579 DCHECK(rti.IsExact());
4580 loaded_class_rti_ = rti;
4581 }
4582
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004583 const DexFile& GetDexFile() { return dex_file_; }
4584
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004585 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4586
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004587 static SideEffects SideEffectsForArchRuntimeCalls() {
4588 return SideEffects::CanTriggerGC();
4589 }
4590
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004591 DECLARE_INSTRUCTION(LoadClass);
4592
4593 private:
4594 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004595 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004596 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004597 // Whether this instruction must generate the initialization check.
4598 // Used for code generation.
4599 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004600 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004601
Calin Juravleacf735c2015-02-12 15:25:22 +00004602 ReferenceTypeInfo loaded_class_rti_;
4603
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004604 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4605};
4606
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004607class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004608 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004609 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004610 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4611 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004612 SetRawInputAt(0, current_method);
4613 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004614
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004615 bool CanBeMoved() const OVERRIDE { return true; }
4616
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004617 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4618 return other->AsLoadString()->string_index_ == string_index_;
4619 }
4620
4621 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4622
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004623 uint32_t GetStringIndex() const { return string_index_; }
4624
4625 // TODO: Can we deopt or debug when we resolve a string?
4626 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004627 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004628 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004629
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004630 static SideEffects SideEffectsForArchRuntimeCalls() {
4631 return SideEffects::CanTriggerGC();
4632 }
4633
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004634 DECLARE_INSTRUCTION(LoadString);
4635
4636 private:
4637 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004638
4639 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4640};
4641
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004642/**
4643 * Performs an initialization check on its Class object input.
4644 */
4645class HClinitCheck : public HExpression<1> {
4646 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004647 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004648 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004649 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004650 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4651 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004652 SetRawInputAt(0, constant);
4653 }
4654
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004655 bool CanBeMoved() const OVERRIDE { return true; }
4656 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4657 UNUSED(other);
4658 return true;
4659 }
4660
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004661 bool NeedsEnvironment() const OVERRIDE {
4662 // May call runtime to initialize the class.
4663 return true;
4664 }
4665
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004666
4667 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4668
4669 DECLARE_INSTRUCTION(ClinitCheck);
4670
4671 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004672 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4673};
4674
4675class HStaticFieldGet : public HExpression<1> {
4676 public:
4677 HStaticFieldGet(HInstruction* cls,
4678 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004679 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004680 bool is_volatile,
4681 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004682 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004683 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004684 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004685 : HExpression(
4686 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004687 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004688 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004689 SetRawInputAt(0, cls);
4690 }
4691
Calin Juravle52c48962014-12-16 17:02:57 +00004692
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004693 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004694
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004695 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004696 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4697 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004698 }
4699
4700 size_t ComputeHashCode() const OVERRIDE {
4701 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4702 }
4703
Calin Juravle52c48962014-12-16 17:02:57 +00004704 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004705 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4706 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004707 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004708
4709 DECLARE_INSTRUCTION(StaticFieldGet);
4710
4711 private:
4712 const FieldInfo field_info_;
4713
4714 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4715};
4716
4717class HStaticFieldSet : public HTemplateInstruction<2> {
4718 public:
4719 HStaticFieldSet(HInstruction* cls,
4720 HInstruction* value,
4721 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004722 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004723 bool is_volatile,
4724 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004725 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004726 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004727 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004728 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004729 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004730 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004731 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004732 SetRawInputAt(0, cls);
4733 SetRawInputAt(1, value);
4734 }
4735
Calin Juravle52c48962014-12-16 17:02:57 +00004736 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004737 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4738 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004739 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004740
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004741 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004742 bool GetValueCanBeNull() const { return value_can_be_null_; }
4743 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004744
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004745 DECLARE_INSTRUCTION(StaticFieldSet);
4746
4747 private:
4748 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004749 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004750
4751 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4752};
4753
Calin Juravlee460d1d2015-09-29 04:52:17 +01004754class HUnresolvedInstanceFieldGet : public HExpression<1> {
4755 public:
4756 HUnresolvedInstanceFieldGet(HInstruction* obj,
4757 Primitive::Type field_type,
4758 uint32_t field_index,
4759 uint32_t dex_pc)
4760 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4761 field_index_(field_index) {
4762 SetRawInputAt(0, obj);
4763 }
4764
4765 bool NeedsEnvironment() const OVERRIDE { return true; }
4766 bool CanThrow() const OVERRIDE { return true; }
4767
4768 Primitive::Type GetFieldType() const { return GetType(); }
4769 uint32_t GetFieldIndex() const { return field_index_; }
4770
4771 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
4772
4773 private:
4774 const uint32_t field_index_;
4775
4776 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
4777};
4778
4779class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
4780 public:
4781 HUnresolvedInstanceFieldSet(HInstruction* obj,
4782 HInstruction* value,
4783 Primitive::Type field_type,
4784 uint32_t field_index,
4785 uint32_t dex_pc)
4786 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4787 field_type_(field_type),
4788 field_index_(field_index) {
4789 DCHECK_EQ(field_type, value->GetType());
4790 SetRawInputAt(0, obj);
4791 SetRawInputAt(1, value);
4792 }
4793
4794 bool NeedsEnvironment() const OVERRIDE { return true; }
4795 bool CanThrow() const OVERRIDE { return true; }
4796
4797 Primitive::Type GetFieldType() const { return field_type_; }
4798 uint32_t GetFieldIndex() const { return field_index_; }
4799
4800 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
4801
4802 private:
4803 const Primitive::Type field_type_;
4804 const uint32_t field_index_;
4805
4806 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
4807};
4808
4809class HUnresolvedStaticFieldGet : public HExpression<0> {
4810 public:
4811 HUnresolvedStaticFieldGet(Primitive::Type field_type,
4812 uint32_t field_index,
4813 uint32_t dex_pc)
4814 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4815 field_index_(field_index) {
4816 }
4817
4818 bool NeedsEnvironment() const OVERRIDE { return true; }
4819 bool CanThrow() const OVERRIDE { return true; }
4820
4821 Primitive::Type GetFieldType() const { return GetType(); }
4822 uint32_t GetFieldIndex() const { return field_index_; }
4823
4824 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
4825
4826 private:
4827 const uint32_t field_index_;
4828
4829 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
4830};
4831
4832class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
4833 public:
4834 HUnresolvedStaticFieldSet(HInstruction* value,
4835 Primitive::Type field_type,
4836 uint32_t field_index,
4837 uint32_t dex_pc)
4838 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4839 field_type_(field_type),
4840 field_index_(field_index) {
4841 DCHECK_EQ(field_type, value->GetType());
4842 SetRawInputAt(0, value);
4843 }
4844
4845 bool NeedsEnvironment() const OVERRIDE { return true; }
4846 bool CanThrow() const OVERRIDE { return true; }
4847
4848 Primitive::Type GetFieldType() const { return field_type_; }
4849 uint32_t GetFieldIndex() const { return field_index_; }
4850
4851 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
4852
4853 private:
4854 const Primitive::Type field_type_;
4855 const uint32_t field_index_;
4856
4857 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
4858};
4859
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004860// Implement the move-exception DEX instruction.
4861class HLoadException : public HExpression<0> {
4862 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004863 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4864 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004865
David Brazdilbbd733e2015-08-18 17:48:17 +01004866 bool CanBeNull() const OVERRIDE { return false; }
4867
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004868 DECLARE_INSTRUCTION(LoadException);
4869
4870 private:
4871 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4872};
4873
David Brazdilcb1c0552015-08-04 16:22:25 +01004874// Implicit part of move-exception which clears thread-local exception storage.
4875// Must not be removed because the runtime expects the TLS to get cleared.
4876class HClearException : public HTemplateInstruction<0> {
4877 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004878 explicit HClearException(uint32_t dex_pc = kNoDexPc)
4879 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01004880
4881 DECLARE_INSTRUCTION(ClearException);
4882
4883 private:
4884 DISALLOW_COPY_AND_ASSIGN(HClearException);
4885};
4886
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004887class HThrow : public HTemplateInstruction<1> {
4888 public:
4889 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004890 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004891 SetRawInputAt(0, exception);
4892 }
4893
4894 bool IsControlFlow() const OVERRIDE { return true; }
4895
4896 bool NeedsEnvironment() const OVERRIDE { return true; }
4897
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004898 bool CanThrow() const OVERRIDE { return true; }
4899
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004900
4901 DECLARE_INSTRUCTION(Throw);
4902
4903 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004904 DISALLOW_COPY_AND_ASSIGN(HThrow);
4905};
4906
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004907/**
4908 * Implementation strategies for the code generator of a HInstanceOf
4909 * or `HCheckCast`.
4910 */
4911enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01004912 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004913 kExactCheck, // Can do a single class compare.
4914 kClassHierarchyCheck, // Can just walk the super class chain.
4915 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
4916 kInterfaceCheck, // No optimization yet when checking against an interface.
4917 kArrayObjectCheck, // Can just check if the array is not primitive.
4918 kArrayCheck // No optimization yet when checking against a generic array.
4919};
4920
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004921class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004922 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004923 HInstanceOf(HInstruction* object,
4924 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004925 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004926 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004927 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004928 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004929 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004930 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004931 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004932 SetRawInputAt(0, object);
4933 SetRawInputAt(1, constant);
4934 }
4935
4936 bool CanBeMoved() const OVERRIDE { return true; }
4937
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004938 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004939 return true;
4940 }
4941
4942 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004943 return false;
4944 }
4945
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004946 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
4947
4948 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004949
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004950 // Used only in code generation.
4951 bool MustDoNullCheck() const { return must_do_null_check_; }
4952 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4953
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004954 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
4955 return (check_kind == TypeCheckKind::kExactCheck)
4956 ? SideEffects::None()
4957 // Mips currently does runtime calls for any other checks.
4958 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004959 }
4960
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004961 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004962
4963 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004964 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004965 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004966
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004967 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4968};
4969
Calin Juravleb1498f62015-02-16 13:13:29 +00004970class HBoundType : public HExpression<1> {
4971 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004972 // Constructs an HBoundType with the given upper_bound.
4973 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004974 HBoundType(HInstruction* input,
4975 ReferenceTypeInfo upper_bound,
4976 bool upper_can_be_null,
4977 uint32_t dex_pc = kNoDexPc)
4978 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004979 upper_bound_(upper_bound),
4980 upper_can_be_null_(upper_can_be_null),
4981 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004982 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004983 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004984 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004985 }
4986
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004987 // GetUpper* should only be used in reference type propagation.
4988 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4989 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004990
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004991 void SetCanBeNull(bool can_be_null) {
4992 DCHECK(upper_can_be_null_ || !can_be_null);
4993 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004994 }
4995
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004996 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4997
Calin Juravleb1498f62015-02-16 13:13:29 +00004998 DECLARE_INSTRUCTION(BoundType);
4999
5000 private:
5001 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005002 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5003 // It is used to bound the type in cases like:
5004 // if (x instanceof ClassX) {
5005 // // uper_bound_ will be ClassX
5006 // }
5007 const ReferenceTypeInfo upper_bound_;
5008 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5009 // is false then can_be_null_ cannot be true).
5010 const bool upper_can_be_null_;
5011 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005012
5013 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5014};
5015
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005016class HCheckCast : public HTemplateInstruction<2> {
5017 public:
5018 HCheckCast(HInstruction* object,
5019 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005020 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005021 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005022 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005023 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005024 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005025 SetRawInputAt(0, object);
5026 SetRawInputAt(1, constant);
5027 }
5028
5029 bool CanBeMoved() const OVERRIDE { return true; }
5030
5031 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5032 return true;
5033 }
5034
5035 bool NeedsEnvironment() const OVERRIDE {
5036 // Instruction may throw a CheckCastError.
5037 return true;
5038 }
5039
5040 bool CanThrow() const OVERRIDE { return true; }
5041
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005042 bool MustDoNullCheck() const { return must_do_null_check_; }
5043 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005044 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005045
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005046 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005047
5048 DECLARE_INSTRUCTION(CheckCast);
5049
5050 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005051 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005052 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005053
5054 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005055};
5056
Calin Juravle27df7582015-04-17 19:12:31 +01005057class HMemoryBarrier : public HTemplateInstruction<0> {
5058 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005059 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005060 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005061 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005062 barrier_kind_(barrier_kind) {}
5063
5064 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5065
5066 DECLARE_INSTRUCTION(MemoryBarrier);
5067
5068 private:
5069 const MemBarrierKind barrier_kind_;
5070
5071 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5072};
5073
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005074class HMonitorOperation : public HTemplateInstruction<1> {
5075 public:
5076 enum OperationKind {
5077 kEnter,
5078 kExit,
5079 };
5080
5081 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005082 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005083 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5084 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005085 SetRawInputAt(0, object);
5086 }
5087
5088 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005089 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5090
5091 bool CanThrow() const OVERRIDE {
5092 // Verifier guarantees that monitor-exit cannot throw.
5093 // This is important because it allows the HGraphBuilder to remove
5094 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5095 return IsEnter();
5096 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005097
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005098
5099 bool IsEnter() const { return kind_ == kEnter; }
5100
5101 DECLARE_INSTRUCTION(MonitorOperation);
5102
Calin Juravle52c48962014-12-16 17:02:57 +00005103 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005104 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005105
5106 private:
5107 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5108};
5109
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005110/**
5111 * A HInstruction used as a marker for the replacement of new + <init>
5112 * of a String to a call to a StringFactory. Only baseline will see
5113 * the node at code generation, where it will be be treated as null.
5114 * When compiling non-baseline, `HFakeString` instructions are being removed
5115 * in the instruction simplifier.
5116 */
5117class HFakeString : public HTemplateInstruction<0> {
5118 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005119 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5120 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005121
5122 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5123
5124 DECLARE_INSTRUCTION(FakeString);
5125
5126 private:
5127 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5128};
5129
Vladimir Markof9f64412015-09-02 14:05:49 +01005130class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005131 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005132 MoveOperands(Location source,
5133 Location destination,
5134 Primitive::Type type,
5135 HInstruction* instruction)
5136 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005137
5138 Location GetSource() const { return source_; }
5139 Location GetDestination() const { return destination_; }
5140
5141 void SetSource(Location value) { source_ = value; }
5142 void SetDestination(Location value) { destination_ = value; }
5143
5144 // The parallel move resolver marks moves as "in-progress" by clearing the
5145 // destination (but not the source).
5146 Location MarkPending() {
5147 DCHECK(!IsPending());
5148 Location dest = destination_;
5149 destination_ = Location::NoLocation();
5150 return dest;
5151 }
5152
5153 void ClearPending(Location dest) {
5154 DCHECK(IsPending());
5155 destination_ = dest;
5156 }
5157
5158 bool IsPending() const {
5159 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5160 return destination_.IsInvalid() && !source_.IsInvalid();
5161 }
5162
5163 // True if this blocks a move from the given location.
5164 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005165 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005166 }
5167
5168 // A move is redundant if it's been eliminated, if its source and
5169 // destination are the same, or if its destination is unneeded.
5170 bool IsRedundant() const {
5171 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5172 }
5173
5174 // We clear both operands to indicate move that's been eliminated.
5175 void Eliminate() {
5176 source_ = destination_ = Location::NoLocation();
5177 }
5178
5179 bool IsEliminated() const {
5180 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5181 return source_.IsInvalid();
5182 }
5183
Alexey Frunze4dda3372015-06-01 18:31:49 -07005184 Primitive::Type GetType() const { return type_; }
5185
Nicolas Geoffray90218252015-04-15 11:56:51 +01005186 bool Is64BitMove() const {
5187 return Primitive::Is64BitType(type_);
5188 }
5189
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005190 HInstruction* GetInstruction() const { return instruction_; }
5191
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005192 private:
5193 Location source_;
5194 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005195 // The type this move is for.
5196 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005197 // The instruction this move is assocatied with. Null when this move is
5198 // for moving an input in the expected locations of user (including a phi user).
5199 // This is only used in debug mode, to ensure we do not connect interval siblings
5200 // in the same parallel move.
5201 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005202};
5203
5204static constexpr size_t kDefaultNumberOfMoves = 4;
5205
5206class HParallelMove : public HTemplateInstruction<0> {
5207 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005208 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005209 : HTemplateInstruction(SideEffects::None(), dex_pc),
5210 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5211 moves_.reserve(kDefaultNumberOfMoves);
5212 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005213
Nicolas Geoffray90218252015-04-15 11:56:51 +01005214 void AddMove(Location source,
5215 Location destination,
5216 Primitive::Type type,
5217 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005218 DCHECK(source.IsValid());
5219 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005220 if (kIsDebugBuild) {
5221 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005222 for (const MoveOperands& move : moves_) {
5223 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005224 // Special case the situation where the move is for the spill slot
5225 // of the instruction.
5226 if ((GetPrevious() == instruction)
5227 || ((GetPrevious() == nullptr)
5228 && instruction->IsPhi()
5229 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005230 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005231 << "Doing parallel moves for the same instruction.";
5232 } else {
5233 DCHECK(false) << "Doing parallel moves for the same instruction.";
5234 }
5235 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005236 }
5237 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005238 for (const MoveOperands& move : moves_) {
5239 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005240 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005241 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005242 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005243 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005244 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005245 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005246 }
5247
Vladimir Marko225b6462015-09-28 12:17:40 +01005248 MoveOperands* MoveOperandsAt(size_t index) {
5249 DCHECK_LT(index, moves_.size());
5250 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005251 }
5252
Vladimir Marko225b6462015-09-28 12:17:40 +01005253 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005254
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005255 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005256
5257 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005258 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005259
5260 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5261};
5262
Mark Mendell0616ae02015-04-17 12:49:27 -04005263} // namespace art
5264
5265#ifdef ART_ENABLE_CODEGEN_x86
5266#include "nodes_x86.h"
5267#endif
5268
5269namespace art {
5270
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005271class HGraphVisitor : public ValueObject {
5272 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005273 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5274 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005275
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005276 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005277 virtual void VisitBasicBlock(HBasicBlock* block);
5278
Roland Levillain633021e2014-10-01 14:12:25 +01005279 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005280 void VisitInsertionOrder();
5281
Roland Levillain633021e2014-10-01 14:12:25 +01005282 // Visit the graph following dominator tree reverse post-order.
5283 void VisitReversePostOrder();
5284
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005285 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005286
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005287 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005288#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005289 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5290
5291 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5292
5293#undef DECLARE_VISIT_INSTRUCTION
5294
5295 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005296 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005297
5298 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5299};
5300
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005301class HGraphDelegateVisitor : public HGraphVisitor {
5302 public:
5303 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5304 virtual ~HGraphDelegateVisitor() {}
5305
5306 // Visit functions that delegate to to super class.
5307#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005308 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005309
5310 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5311
5312#undef DECLARE_VISIT_INSTRUCTION
5313
5314 private:
5315 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5316};
5317
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005318class HInsertionOrderIterator : public ValueObject {
5319 public:
5320 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5321
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005322 bool Done() const { return index_ == graph_.GetBlocks().size(); }
5323 HBasicBlock* Current() const { return graph_.GetBlock(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005324 void Advance() { ++index_; }
5325
5326 private:
5327 const HGraph& graph_;
5328 size_t index_;
5329
5330 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5331};
5332
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005333class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005334 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005335 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5336 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005337 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005338 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005339
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005340 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5341 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005342 void Advance() { ++index_; }
5343
5344 private:
5345 const HGraph& graph_;
5346 size_t index_;
5347
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005348 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005349};
5350
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005351class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005352 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005353 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005354 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005355 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005356 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005357 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005358
5359 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005360 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005361 void Advance() { --index_; }
5362
5363 private:
5364 const HGraph& graph_;
5365 size_t index_;
5366
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005367 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005368};
5369
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005370class HLinearPostOrderIterator : public ValueObject {
5371 public:
5372 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005373 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005374
5375 bool Done() const { return index_ == 0; }
5376
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005377 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005378
5379 void Advance() {
5380 --index_;
5381 DCHECK_GE(index_, 0U);
5382 }
5383
5384 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005385 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005386 size_t index_;
5387
5388 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5389};
5390
5391class HLinearOrderIterator : public ValueObject {
5392 public:
5393 explicit HLinearOrderIterator(const HGraph& graph)
5394 : order_(graph.GetLinearOrder()), index_(0) {}
5395
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005396 bool Done() const { return index_ == order_.size(); }
5397 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005398 void Advance() { ++index_; }
5399
5400 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005401 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005402 size_t index_;
5403
5404 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5405};
5406
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005407// Iterator over the blocks that art part of the loop. Includes blocks part
5408// of an inner loop. The order in which the blocks are iterated is on their
5409// block id.
5410class HBlocksInLoopIterator : public ValueObject {
5411 public:
5412 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5413 : blocks_in_loop_(info.GetBlocks()),
5414 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5415 index_(0) {
5416 if (!blocks_in_loop_.IsBitSet(index_)) {
5417 Advance();
5418 }
5419 }
5420
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005421 bool Done() const { return index_ == blocks_.size(); }
5422 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005423 void Advance() {
5424 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005425 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005426 if (blocks_in_loop_.IsBitSet(index_)) {
5427 break;
5428 }
5429 }
5430 }
5431
5432 private:
5433 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005434 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005435 size_t index_;
5436
5437 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5438};
5439
Mingyao Yang3584bce2015-05-19 16:01:59 -07005440// Iterator over the blocks that art part of the loop. Includes blocks part
5441// of an inner loop. The order in which the blocks are iterated is reverse
5442// post order.
5443class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5444 public:
5445 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5446 : blocks_in_loop_(info.GetBlocks()),
5447 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5448 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005449 DCHECK(!blocks_.empty());
5450 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005451 Advance();
5452 }
5453 }
5454
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005455 bool Done() const { return index_ == blocks_.size(); }
5456 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005457 void Advance() {
5458 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005459 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5460 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005461 break;
5462 }
5463 }
5464 }
5465
5466 private:
5467 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005468 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005469 size_t index_;
5470
5471 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5472};
5473
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005474inline int64_t Int64FromConstant(HConstant* constant) {
5475 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5476 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5477 : constant->AsLongConstant()->GetValue();
5478}
5479
Vladimir Marko58155012015-08-19 12:49:41 +00005480inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5481 // For the purposes of the compiler, the dex files must actually be the same object
5482 // if we want to safely treat them as the same. This is especially important for JIT
5483 // as custom class loaders can open the same underlying file (or memory) multiple
5484 // times and provide different class resolution but no two class loaders should ever
5485 // use the same DexFile object - doing so is an unsupported hack that can lead to
5486 // all sorts of weird failures.
5487 return &lhs == &rhs;
5488}
5489
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005490} // namespace art
5491
5492#endif // ART_COMPILER_OPTIMIZING_NODES_H_