blob: 82909c41b6b40e7421cbb227081f36210f7dd95b [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
David Brazdil69ba7b72015-06-23 18:27:30 +0100180 bool IsInSsaForm() const { return in_ssa_form_; }
181
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000182 HBasicBlock* GetEntryBlock() const { return entry_block_; }
183 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100184 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000185
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000186 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
187 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000188
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000189 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100190
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000191 // Try building the SSA form of this graph, with dominance computation and loop
192 // recognition. Returns whether it was successful in doing all these steps.
193 bool TryBuildingSsa() {
194 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000195 // The SSA builder requires loops to all be natural. Specifically, the dead phi
196 // elimination phase checks the consistency of the graph when doing a post-order
197 // visit for eliminating dead phis: a dead phi can only have loop header phi
198 // users remaining when being visited.
199 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100200 // Precompute per-block try membership before entering the SSA builder,
201 // which needs the information to build catch block phis from values of
202 // locals at throwing instructions inside try blocks.
203 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000204 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100205 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000206 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000207 }
208
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100209 void ComputeDominanceInformation();
210 void ClearDominanceInformation();
211
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000212 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100214 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100215 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000216
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000217 // Analyze all natural loops in this graph. Returns false if one
218 // loop is not natural, that is the header does not dominate the
219 // back edge.
220 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100221
David Brazdilffee3d32015-07-06 11:48:53 +0100222 // Iterate over blocks to compute try block membership. Needs reverse post
223 // order and loop information.
224 void ComputeTryBlockInformation();
225
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000226 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000227 // Returns the instruction used to replace the invoke expression or null if the
228 // invoke is for a void method.
229 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000230
Mingyao Yang3584bce2015-05-19 16:01:59 -0700231 // Need to add a couple of blocks to test if the loop body is entered and
232 // put deoptimization instructions, etc.
233 void TransformLoopHeaderForBCE(HBasicBlock* header);
234
David Brazdil2d7352b2015-04-20 14:52:42 +0100235 // Removes `block` from the graph.
236 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000237
David Brazdilfc6a86a2015-06-26 10:33:45 +0000238 // Splits the edge between `block` and `successor` while preserving the
239 // indices in the predecessor/successor lists. If there are multiple edges
240 // between the blocks, the lowest indices are used.
241 // Returns the new block which is empty and has the same dex pc as `successor`.
242 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
243
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100244 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
245 void SimplifyLoop(HBasicBlock* header);
246
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000247 int32_t GetNextInstructionId() {
248 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000249 return current_instruction_id_++;
250 }
251
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000252 int32_t GetCurrentInstructionId() const {
253 return current_instruction_id_;
254 }
255
256 void SetCurrentInstructionId(int32_t id) {
257 current_instruction_id_ = id;
258 }
259
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100260 uint16_t GetMaximumNumberOfOutVRegs() const {
261 return maximum_number_of_out_vregs_;
262 }
263
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000264 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
265 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100266 }
267
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100268 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
269 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
270 }
271
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000272 void UpdateTemporariesVRegSlots(size_t slots) {
273 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100274 }
275
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000276 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100277 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000278 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100279 }
280
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100281 void SetNumberOfVRegs(uint16_t number_of_vregs) {
282 number_of_vregs_ = number_of_vregs;
283 }
284
285 uint16_t GetNumberOfVRegs() const {
286 return number_of_vregs_;
287 }
288
289 void SetNumberOfInVRegs(uint16_t value) {
290 number_of_in_vregs_ = value;
291 }
292
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100293 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100294 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100295 return number_of_vregs_ - number_of_in_vregs_;
296 }
297
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100298 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100299 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100300 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100301
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100302 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100303 return linear_order_;
304 }
305
Mark Mendell1152c922015-04-24 17:06:35 -0400306 bool HasBoundsChecks() const {
307 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800308 }
309
Mark Mendell1152c922015-04-24 17:06:35 -0400310 void SetHasBoundsChecks(bool value) {
311 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800312 }
313
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100314 bool ShouldGenerateConstructorBarrier() const {
315 return should_generate_constructor_barrier_;
316 }
317
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000318 bool IsDebuggable() const { return debuggable_; }
319
David Brazdil8d5b8b22015-03-24 10:51:52 +0000320 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000321 // already, it is created and inserted into the graph. This method is only for
322 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600323 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000324
325 // TODO: This is problematic for the consistency of reference type propagation
326 // because it can be created anytime after the pass and thus it will be left
327 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600328 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000329
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600330 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
331 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000332 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600333 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
334 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000335 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600336 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
337 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000338 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600339 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
340 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000341 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000342
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100343 HCurrentMethod* GetCurrentMethod();
344
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000345 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100346
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100347 const DexFile& GetDexFile() const {
348 return dex_file_;
349 }
350
351 uint32_t GetMethodIdx() const {
352 return method_idx_;
353 }
354
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100355 InvokeType GetInvokeType() const {
356 return invoke_type_;
357 }
358
Mark Mendellc4701932015-04-10 13:18:51 -0400359 InstructionSet GetInstructionSet() const {
360 return instruction_set_;
361 }
362
David Brazdil77a48ae2015-09-15 12:34:04 +0000363 bool HasTryCatch() const { return has_try_catch_; }
364 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100365
David Brazdil2d7352b2015-04-20 14:52:42 +0100366 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100367 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000368 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100369 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000370
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000371 template <class InstructionType, typename ValueType>
372 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600373 ArenaSafeMap<ValueType, InstructionType*>* cache,
374 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000375 // Try to find an existing constant of the given value.
376 InstructionType* constant = nullptr;
377 auto cached_constant = cache->find(value);
378 if (cached_constant != cache->end()) {
379 constant = cached_constant->second;
380 }
381
382 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100383 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000384 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600385 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000386 cache->Overwrite(value, constant);
387 InsertConstant(constant);
388 }
389 return constant;
390 }
391
David Brazdil8d5b8b22015-03-24 10:51:52 +0000392 void InsertConstant(HConstant* instruction);
393
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000394 // Cache a float constant into the graph. This method should only be
395 // called by the SsaBuilder when creating "equivalent" instructions.
396 void CacheFloatConstant(HFloatConstant* constant);
397
398 // See CacheFloatConstant comment.
399 void CacheDoubleConstant(HDoubleConstant* constant);
400
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000401 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000402
403 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100404 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000405
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100406 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100407 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000408
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100409 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100410 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100411
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000412 HBasicBlock* entry_block_;
413 HBasicBlock* exit_block_;
414
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100415 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100416 uint16_t maximum_number_of_out_vregs_;
417
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100418 // The number of virtual registers in this method. Contains the parameters.
419 uint16_t number_of_vregs_;
420
421 // The number of virtual registers used by parameters of this method.
422 uint16_t number_of_in_vregs_;
423
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000424 // Number of vreg size slots that the temporaries use (used in baseline compiler).
425 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100426
Mark Mendell1152c922015-04-24 17:06:35 -0400427 // Has bounds checks. We can totally skip BCE if it's false.
428 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800429
David Brazdil77a48ae2015-09-15 12:34:04 +0000430 // Flag whether there are any try/catch blocks in the graph. We will skip
431 // try/catch-related passes if false.
432 bool has_try_catch_;
433
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000434 // Indicates whether the graph should be compiled in a way that
435 // ensures full debuggability. If false, we can apply more
436 // aggressive optimizations that may limit the level of debugging.
437 const bool debuggable_;
438
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000439 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000440 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000441
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100442 // The dex file from which the method is from.
443 const DexFile& dex_file_;
444
445 // The method index in the dex file.
446 const uint32_t method_idx_;
447
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100448 // If inlined, this encodes how the callee is being invoked.
449 const InvokeType invoke_type_;
450
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100451 // Whether the graph has been transformed to SSA form. Only used
452 // in debug mode to ensure we are not using properties only valid
453 // for non-SSA form (like the number of temporaries).
454 bool in_ssa_form_;
455
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100456 const bool should_generate_constructor_barrier_;
457
Mathieu Chartiere401d142015-04-22 13:56:20 -0700458 const InstructionSet instruction_set_;
459
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000460 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000461 HNullConstant* cached_null_constant_;
462 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000463 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000464 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000465 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000466
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100467 HCurrentMethod* cached_current_method_;
468
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000469 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100470 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000471 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000472 DISALLOW_COPY_AND_ASSIGN(HGraph);
473};
474
Vladimir Markof9f64412015-09-02 14:05:49 +0100475class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000476 public:
477 HLoopInformation(HBasicBlock* header, HGraph* graph)
478 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100479 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100480 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100481 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100482 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
483 back_edges_.reserve(kDefaultNumberOfBackEdges);
484 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100485
486 HBasicBlock* GetHeader() const {
487 return header_;
488 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000489
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000490 void SetHeader(HBasicBlock* block) {
491 header_ = block;
492 }
493
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100494 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
495 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
496 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
497
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000498 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100499 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000500 }
501
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100502 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100503 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100504 }
505
David Brazdil46e2a392015-03-16 17:31:52 +0000506 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100507 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100508 }
509
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000510 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100511 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000512 }
513
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100514 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100515
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100516 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100517 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100518 }
519
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100520 // Returns the lifetime position of the back edge that has the
521 // greatest lifetime position.
522 size_t GetLifetimeEnd() const;
523
524 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100525 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100526 }
527
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100528 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100529 // that is the header dominates the back edge.
530 bool Populate();
531
David Brazdila4b8c212015-05-07 09:59:30 +0100532 // Reanalyzes the loop by removing loop info from its blocks and re-running
533 // Populate(). If there are no back edges left, the loop info is completely
534 // removed as well as its SuspendCheck instruction. It must be run on nested
535 // inner loops first.
536 void Update();
537
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100538 // Returns whether this loop information contains `block`.
539 // Note that this loop information *must* be populated before entering this function.
540 bool Contains(const HBasicBlock& block) const;
541
542 // Returns whether this loop information is an inner loop of `other`.
543 // Note that `other` *must* be populated before entering this function.
544 bool IsIn(const HLoopInformation& other) const;
545
546 const ArenaBitVector& GetBlocks() const { return blocks_; }
547
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000548 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000549 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000550
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000551 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100552 // Internal recursive implementation of `Populate`.
553 void PopulateRecursive(HBasicBlock* block);
554
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000555 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100556 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100557 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100558 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000559
560 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
561};
562
David Brazdilec16f792015-08-19 15:04:01 +0100563// Stores try/catch information for basic blocks.
564// Note that HGraph is constructed so that catch blocks cannot simultaneously
565// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100566class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100567 public:
568 // Try block information constructor.
569 explicit TryCatchInformation(const HTryBoundary& try_entry)
570 : try_entry_(&try_entry),
571 catch_dex_file_(nullptr),
572 catch_type_index_(DexFile::kDexNoIndex16) {
573 DCHECK(try_entry_ != nullptr);
574 }
575
576 // Catch block information constructor.
577 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
578 : try_entry_(nullptr),
579 catch_dex_file_(&dex_file),
580 catch_type_index_(catch_type_index) {}
581
582 bool IsTryBlock() const { return try_entry_ != nullptr; }
583
584 const HTryBoundary& GetTryEntry() const {
585 DCHECK(IsTryBlock());
586 return *try_entry_;
587 }
588
589 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
590
591 bool IsCatchAllTypeIndex() const {
592 DCHECK(IsCatchBlock());
593 return catch_type_index_ == DexFile::kDexNoIndex16;
594 }
595
596 uint16_t GetCatchTypeIndex() const {
597 DCHECK(IsCatchBlock());
598 return catch_type_index_;
599 }
600
601 const DexFile& GetCatchDexFile() const {
602 DCHECK(IsCatchBlock());
603 return *catch_dex_file_;
604 }
605
606 private:
607 // One of possibly several TryBoundary instructions entering the block's try.
608 // Only set for try blocks.
609 const HTryBoundary* try_entry_;
610
611 // Exception type information. Only set for catch blocks.
612 const DexFile* catch_dex_file_;
613 const uint16_t catch_type_index_;
614};
615
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100616static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100617static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100618
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000619// A block in a method. Contains the list of instructions represented
620// as a double linked list. Each block knows its predecessors and
621// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100622
Vladimir Markof9f64412015-09-02 14:05:49 +0100623class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000624 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600625 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000626 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000627 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
628 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000629 loop_information_(nullptr),
630 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000631 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100632 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100633 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100634 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000635 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000636 try_catch_information_(nullptr) {
637 predecessors_.reserve(kDefaultNumberOfPredecessors);
638 successors_.reserve(kDefaultNumberOfSuccessors);
639 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
640 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641
Vladimir Marko60584552015-09-03 13:35:12 +0000642 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100643 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000644 }
645
Vladimir Marko60584552015-09-03 13:35:12 +0000646 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100647 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000648 }
649
Vladimir Marko60584552015-09-03 13:35:12 +0000650 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
651 return ContainsElement(successors_, block, start_from);
652 }
653
654 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100655 return dominated_blocks_;
656 }
657
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100658 bool IsEntryBlock() const {
659 return graph_->GetEntryBlock() == this;
660 }
661
662 bool IsExitBlock() const {
663 return graph_->GetExitBlock() == this;
664 }
665
David Brazdil46e2a392015-03-16 17:31:52 +0000666 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000667 bool IsSingleTryBoundary() const;
668
669 // Returns true if this block emits nothing but a jump.
670 bool IsSingleJump() const {
671 HLoopInformation* loop_info = GetLoopInformation();
672 return (IsSingleGoto() || IsSingleTryBoundary())
673 // Back edges generate a suspend check.
674 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
675 }
David Brazdil46e2a392015-03-16 17:31:52 +0000676
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000677 void AddBackEdge(HBasicBlock* back_edge) {
678 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000679 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000680 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100681 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000682 loop_information_->AddBackEdge(back_edge);
683 }
684
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000685 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000686 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000687
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100688 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000689 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600690 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000691
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000692 HBasicBlock* GetDominator() const { return dominator_; }
693 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000694 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
695
696 void RemoveDominatedBlock(HBasicBlock* block) {
697 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100698 }
Vladimir Marko60584552015-09-03 13:35:12 +0000699
700 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
701 ReplaceElement(dominated_blocks_, existing, new_block);
702 }
703
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100704 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000705
706 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100707 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000708 }
709
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100710 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
711 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100712 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100713 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100714 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
715 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000716
717 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000718 successors_.push_back(block);
719 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000720 }
721
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100722 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
723 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100724 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000725 new_block->predecessors_.push_back(this);
726 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000727 }
728
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000729 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
730 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000731 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000732 new_block->successors_.push_back(this);
733 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000734 }
735
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100736 // Insert `this` between `predecessor` and `successor. This method
737 // preserves the indicies, and will update the first edge found between
738 // `predecessor` and `successor`.
739 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
740 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100741 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000742 successor->predecessors_[predecessor_index] = this;
743 predecessor->successors_[successor_index] = this;
744 successors_.push_back(successor);
745 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100746 }
747
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100748 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000749 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100750 }
751
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000752 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000753 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000754 }
755
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100756 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000757 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100758 }
759
760 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000761 predecessors_.push_back(block);
762 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100763 }
764
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100765 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000766 DCHECK_EQ(predecessors_.size(), 2u);
767 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100768 }
769
David Brazdil769c9e52015-04-27 13:54:09 +0100770 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000771 DCHECK_EQ(successors_.size(), 2u);
772 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100773 }
774
David Brazdilfc6a86a2015-06-26 10:33:45 +0000775 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000776 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100777 }
778
David Brazdilfc6a86a2015-06-26 10:33:45 +0000779 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000780 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100781 }
782
David Brazdilfc6a86a2015-06-26 10:33:45 +0000783 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000784 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100785 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000786 }
787
788 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000789 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100790 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000791 }
792
793 // Returns whether the first occurrence of `predecessor` in the list of
794 // predecessors is at index `idx`.
795 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100796 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000797 return GetPredecessorIndexOf(predecessor) == idx;
798 }
799
David Brazdilffee3d32015-07-06 11:48:53 +0100800 // Returns the number of non-exceptional successors. SsaChecker ensures that
801 // these are stored at the beginning of the successor list.
802 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000803 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100804 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000805
David Brazdild7558da2015-09-22 13:04:14 +0100806 // Create a new block between this block and its predecessors. The new block
807 // is added to the graph, all predecessor edges are relinked to it and an edge
808 // is created to `this`. Returns the new empty block. Reverse post order or
809 // loop and try/catch information are not updated.
810 HBasicBlock* CreateImmediateDominator();
811
David Brazdilfc6a86a2015-06-26 10:33:45 +0000812 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100813 // created, latter block. Note that this method will add the block to the
814 // graph, create a Goto at the end of the former block and will create an edge
815 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100816 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000817 HBasicBlock* SplitBefore(HInstruction* cursor);
818
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000819 // Split the block into two blocks just after `cursor`. Returns the newly
820 // created block. Note that this method just updates raw block information,
821 // like predecessors, successors, dominators, and instruction list. It does not
822 // update the graph, reverse post order, loop information, nor make sure the
823 // blocks are consistent (for example ending with a control flow instruction).
824 HBasicBlock* SplitAfter(HInstruction* cursor);
825
826 // Merge `other` at the end of `this`. Successors and dominated blocks of
827 // `other` are changed to be successors and dominated blocks of `this`. Note
828 // that this method does not update the graph, reverse post order, loop
829 // information, nor make sure the blocks are consistent (for example ending
830 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100831 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000832
833 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
834 // of `this` are moved to `other`.
835 // Note that this method does not update the graph, reverse post order, loop
836 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000837 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000838 void ReplaceWith(HBasicBlock* other);
839
David Brazdil2d7352b2015-04-20 14:52:42 +0100840 // Merge `other` at the end of `this`. This method updates loops, reverse post
841 // order, links to predecessors, successors, dominators and deletes the block
842 // from the graph. The two blocks must be successive, i.e. `this` the only
843 // predecessor of `other` and vice versa.
844 void MergeWith(HBasicBlock* other);
845
846 // Disconnects `this` from all its predecessors, successors and dominator,
847 // removes it from all loops it is included in and eventually from the graph.
848 // The block must not dominate any other block. Predecessors and successors
849 // are safely updated.
850 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000851
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000852 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100853 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100854 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100855 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100856 // Replace instruction `initial` with `replacement` within this block.
857 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
858 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100859 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100860 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000861 // RemoveInstruction and RemovePhi delete a given instruction from the respective
862 // instruction list. With 'ensure_safety' set to true, it verifies that the
863 // instruction is not in use and removes it from the use lists of its inputs.
864 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
865 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100866 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100867
868 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100869 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100870 }
871
Roland Levillain6b879dd2014-09-22 17:13:44 +0100872 bool IsLoopPreHeaderFirstPredecessor() const {
873 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100874 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100875 }
876
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100877 HLoopInformation* GetLoopInformation() const {
878 return loop_information_;
879 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000880
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000881 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100882 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000883 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100884 void SetInLoop(HLoopInformation* info) {
885 if (IsLoopHeader()) {
886 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100887 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100888 loop_information_ = info;
889 } else if (loop_information_->Contains(*info->GetHeader())) {
890 // Block is currently part of an outer loop. Make it part of this inner loop.
891 // Note that a non loop header having a loop information means this loop information
892 // has already been populated
893 loop_information_ = info;
894 } else {
895 // Block is part of an inner loop. Do not update the loop information.
896 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
897 // at this point, because this method is being called while populating `info`.
898 }
899 }
900
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000901 // Raw update of the loop information.
902 void SetLoopInformation(HLoopInformation* info) {
903 loop_information_ = info;
904 }
905
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100906 bool IsInLoop() const { return loop_information_ != nullptr; }
907
David Brazdilec16f792015-08-19 15:04:01 +0100908 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
909
910 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
911 try_catch_information_ = try_catch_information;
912 }
913
914 bool IsTryBlock() const {
915 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
916 }
917
918 bool IsCatchBlock() const {
919 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
920 }
David Brazdilffee3d32015-07-06 11:48:53 +0100921
922 // Returns the try entry that this block's successors should have. They will
923 // be in the same try, unless the block ends in a try boundary. In that case,
924 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100925 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100926
David Brazdild7558da2015-09-22 13:04:14 +0100927 bool HasThrowingInstructions() const;
928
David Brazdila4b8c212015-05-07 09:59:30 +0100929 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100930 bool Dominates(HBasicBlock* block) const;
931
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100932 size_t GetLifetimeStart() const { return lifetime_start_; }
933 size_t GetLifetimeEnd() const { return lifetime_end_; }
934
935 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
936 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
937
David Brazdil8d5b8b22015-03-24 10:51:52 +0000938 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000939 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100940 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000941 bool HasSinglePhi() const;
942
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000943 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000944 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000945 ArenaVector<HBasicBlock*> predecessors_;
946 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100947 HInstructionList instructions_;
948 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000949 HLoopInformation* loop_information_;
950 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000951 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100952 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100953 // The dex program counter of the first instruction of this block.
954 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100955 size_t lifetime_start_;
956 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100957 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100958
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000959 friend class HGraph;
960 friend class HInstruction;
961
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000962 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
963};
964
David Brazdilb2bd1c52015-03-25 11:17:37 +0000965// Iterates over the LoopInformation of all loops which contain 'block'
966// from the innermost to the outermost.
967class HLoopInformationOutwardIterator : public ValueObject {
968 public:
969 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
970 : current_(block.GetLoopInformation()) {}
971
972 bool Done() const { return current_ == nullptr; }
973
974 void Advance() {
975 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100976 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000977 }
978
979 HLoopInformation* Current() const {
980 DCHECK(!Done());
981 return current_;
982 }
983
984 private:
985 HLoopInformation* current_;
986
987 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
988};
989
Alexandre Ramesef20f712015-06-09 10:29:30 +0100990#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100991 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000992 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000993 M(ArrayGet, Instruction) \
994 M(ArrayLength, Instruction) \
995 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100996 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000997 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000998 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000999 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001000 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001001 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001002 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001003 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001004 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001005 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001006 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001007 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001008 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001009 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001010 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001011 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001012 M(FloatConstant, Constant) \
1013 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001014 M(GreaterThan, Condition) \
1015 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001016 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001017 M(InstanceFieldGet, Instruction) \
1018 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001019 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001020 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001021 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001022 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001023 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001024 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001025 M(LessThan, Condition) \
1026 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001027 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001028 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001029 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001030 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001031 M(Local, Instruction) \
1032 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001033 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001034 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001035 M(Mul, BinaryOperation) \
1036 M(Neg, UnaryOperation) \
1037 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001038 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001039 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001040 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001041 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001042 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001043 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001044 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001045 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001046 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001047 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001048 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001049 M(Return, Instruction) \
1050 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001051 M(Shl, BinaryOperation) \
1052 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001053 M(StaticFieldGet, Instruction) \
1054 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001055 M(UnresolvedInstanceFieldGet, Instruction) \
1056 M(UnresolvedInstanceFieldSet, Instruction) \
1057 M(UnresolvedStaticFieldGet, Instruction) \
1058 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001059 M(StoreLocal, Instruction) \
1060 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001061 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001062 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001063 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001064 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001065 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001066 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001067 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001068
Alexandre Ramesef20f712015-06-09 10:29:30 +01001069#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1070
1071#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1072
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001073#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1074
Mark Mendell0616ae02015-04-17 12:49:27 -04001075#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1076 M(X86ComputeBaseMethodAddress, Instruction) \
1077 M(X86LoadFromConstantTable, Instruction)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001078
1079#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1080
1081#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1082 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1083 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1084 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001085 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001086 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1087 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1088
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001089#define FOR_EACH_INSTRUCTION(M) \
1090 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1091 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001092 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001093 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001094 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001095
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001096#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001097FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1098#undef FORWARD_DECLARATION
1099
Roland Levillainccc07a92014-09-16 14:48:16 +01001100#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001101 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1102 const char* DebugName() const OVERRIDE { return #type; } \
1103 const H##type* As##type() const OVERRIDE { return this; } \
1104 H##type* As##type() OVERRIDE { return this; } \
1105 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001106 return other->Is##type(); \
1107 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001108 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001109
David Brazdiled596192015-01-23 10:39:45 +00001110template <typename T> class HUseList;
1111
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001112template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001113class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001114 public:
David Brazdiled596192015-01-23 10:39:45 +00001115 HUseListNode* GetPrevious() const { return prev_; }
1116 HUseListNode* GetNext() const { return next_; }
1117 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001118 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001119 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001120
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001121 private:
David Brazdiled596192015-01-23 10:39:45 +00001122 HUseListNode(T user, size_t index)
1123 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1124
1125 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001126 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001127 HUseListNode<T>* prev_;
1128 HUseListNode<T>* next_;
1129
1130 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001131
1132 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1133};
1134
David Brazdiled596192015-01-23 10:39:45 +00001135template <typename T>
1136class HUseList : public ValueObject {
1137 public:
1138 HUseList() : first_(nullptr) {}
1139
1140 void Clear() {
1141 first_ = nullptr;
1142 }
1143
1144 // Adds a new entry at the beginning of the use list and returns
1145 // the newly created node.
1146 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001147 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001148 if (IsEmpty()) {
1149 first_ = new_node;
1150 } else {
1151 first_->prev_ = new_node;
1152 new_node->next_ = first_;
1153 first_ = new_node;
1154 }
1155 return new_node;
1156 }
1157
1158 HUseListNode<T>* GetFirst() const {
1159 return first_;
1160 }
1161
1162 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001163 DCHECK(node != nullptr);
1164 DCHECK(Contains(node));
1165
David Brazdiled596192015-01-23 10:39:45 +00001166 if (node->prev_ != nullptr) {
1167 node->prev_->next_ = node->next_;
1168 }
1169 if (node->next_ != nullptr) {
1170 node->next_->prev_ = node->prev_;
1171 }
1172 if (node == first_) {
1173 first_ = node->next_;
1174 }
1175 }
1176
David Brazdil1abb4192015-02-17 18:33:36 +00001177 bool Contains(const HUseListNode<T>* node) const {
1178 if (node == nullptr) {
1179 return false;
1180 }
1181 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1182 if (current == node) {
1183 return true;
1184 }
1185 }
1186 return false;
1187 }
1188
David Brazdiled596192015-01-23 10:39:45 +00001189 bool IsEmpty() const {
1190 return first_ == nullptr;
1191 }
1192
1193 bool HasOnlyOneUse() const {
1194 return first_ != nullptr && first_->next_ == nullptr;
1195 }
1196
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001197 size_t SizeSlow() const {
1198 size_t count = 0;
1199 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1200 ++count;
1201 }
1202 return count;
1203 }
1204
David Brazdiled596192015-01-23 10:39:45 +00001205 private:
1206 HUseListNode<T>* first_;
1207};
1208
1209template<typename T>
1210class HUseIterator : public ValueObject {
1211 public:
1212 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1213
1214 bool Done() const { return current_ == nullptr; }
1215
1216 void Advance() {
1217 DCHECK(!Done());
1218 current_ = current_->GetNext();
1219 }
1220
1221 HUseListNode<T>* Current() const {
1222 DCHECK(!Done());
1223 return current_;
1224 }
1225
1226 private:
1227 HUseListNode<T>* current_;
1228
1229 friend class HValue;
1230};
1231
David Brazdil1abb4192015-02-17 18:33:36 +00001232// This class is used by HEnvironment and HInstruction classes to record the
1233// instructions they use and pointers to the corresponding HUseListNodes kept
1234// by the used instructions.
1235template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001236class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001237 public:
1238 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1239 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1240
1241 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1242 : instruction_(old_record.instruction_), use_node_(use_node) {
1243 DCHECK(instruction_ != nullptr);
1244 DCHECK(use_node_ != nullptr);
1245 DCHECK(old_record.use_node_ == nullptr);
1246 }
1247
1248 HInstruction* GetInstruction() const { return instruction_; }
1249 HUseListNode<T>* GetUseNode() const { return use_node_; }
1250
1251 private:
1252 // Instruction used by the user.
1253 HInstruction* instruction_;
1254
1255 // Corresponding entry in the use list kept by 'instruction_'.
1256 HUseListNode<T>* use_node_;
1257};
1258
Aart Bik854a02b2015-07-14 16:07:00 -07001259/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001260 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001261 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001262 * For write/read dependences on fields/arrays, the dependence analysis uses
1263 * type disambiguation (e.g. a float field write cannot modify the value of an
1264 * integer field read) and the access type (e.g. a reference array write cannot
1265 * modify the value of a reference field read [although it may modify the
1266 * reference fetch prior to reading the field, which is represented by its own
1267 * write/read dependence]). The analysis makes conservative points-to
1268 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1269 * the same, and any reference read depends on any reference read without
1270 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001271 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001272 * The internal representation uses 38-bit and is described in the table below.
1273 * The first line indicates the side effect, and for field/array accesses the
1274 * second line indicates the type of the access (in the order of the
1275 * Primitive::Type enum).
1276 * The two numbered lines below indicate the bit position in the bitfield (read
1277 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001278 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001279 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1280 * +-------------+---------+---------+--------------+---------+---------+
1281 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1282 * | 3 |333333322|222222221| 1 |111111110|000000000|
1283 * | 7 |654321098|765432109| 8 |765432109|876543210|
1284 *
1285 * Note that, to ease the implementation, 'changes' bits are least significant
1286 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001287 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001288class SideEffects : public ValueObject {
1289 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001290 SideEffects() : flags_(0) {}
1291
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001292 static SideEffects None() {
1293 return SideEffects(0);
1294 }
1295
1296 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001297 return SideEffects(kAllChangeBits | kAllDependOnBits);
1298 }
1299
1300 static SideEffects AllChanges() {
1301 return SideEffects(kAllChangeBits);
1302 }
1303
1304 static SideEffects AllDependencies() {
1305 return SideEffects(kAllDependOnBits);
1306 }
1307
1308 static SideEffects AllExceptGCDependency() {
1309 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1310 }
1311
1312 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001313 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001314 }
1315
Aart Bik34c3ba92015-07-20 14:08:59 -07001316 static SideEffects AllWrites() {
1317 return SideEffects(kAllWrites);
1318 }
1319
1320 static SideEffects AllReads() {
1321 return SideEffects(kAllReads);
1322 }
1323
1324 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1325 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001326 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001327 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001328 }
1329
Aart Bik854a02b2015-07-14 16:07:00 -07001330 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1331 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001332 }
1333
Aart Bik34c3ba92015-07-20 14:08:59 -07001334 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1335 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001336 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001337 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001338 }
1339
1340 static SideEffects ArrayReadOfType(Primitive::Type type) {
1341 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1342 }
1343
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001344 static SideEffects CanTriggerGC() {
1345 return SideEffects(1ULL << kCanTriggerGCBit);
1346 }
1347
1348 static SideEffects DependsOnGC() {
1349 return SideEffects(1ULL << kDependsOnGCBit);
1350 }
1351
Aart Bik854a02b2015-07-14 16:07:00 -07001352 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001353 SideEffects Union(SideEffects other) const {
1354 return SideEffects(flags_ | other.flags_);
1355 }
1356
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001357 SideEffects Exclusion(SideEffects other) const {
1358 return SideEffects(flags_ & ~other.flags_);
1359 }
1360
1361 bool Includes(SideEffects other) const {
1362 return (other.flags_ & flags_) == other.flags_;
1363 }
1364
1365 bool HasSideEffects() const {
1366 return (flags_ & kAllChangeBits);
1367 }
1368
1369 bool HasDependencies() const {
1370 return (flags_ & kAllDependOnBits);
1371 }
1372
1373 // Returns true if there are no side effects or dependencies.
1374 bool DoesNothing() const {
1375 return flags_ == 0;
1376 }
1377
Aart Bik854a02b2015-07-14 16:07:00 -07001378 // Returns true if something is written.
1379 bool DoesAnyWrite() const {
1380 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001381 }
1382
Aart Bik854a02b2015-07-14 16:07:00 -07001383 // Returns true if something is read.
1384 bool DoesAnyRead() const {
1385 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001386 }
1387
Aart Bik854a02b2015-07-14 16:07:00 -07001388 // Returns true if potentially everything is written and read
1389 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001390 bool DoesAllReadWrite() const {
1391 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1392 }
1393
Aart Bik854a02b2015-07-14 16:07:00 -07001394 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001395 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001396 }
1397
1398 // Returns true if this may read something written by other.
1399 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001400 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1401 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001402 }
1403
1404 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001405 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001406 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001407 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001408 for (int s = kLastBit; s >= 0; s--) {
1409 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1410 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1411 // This is a bit for the GC side effect.
1412 if (current_bit_is_set) {
1413 flags += "GC";
1414 }
Aart Bik854a02b2015-07-14 16:07:00 -07001415 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001416 } else {
1417 // This is a bit for the array/field analysis.
1418 // The underscore character stands for the 'can trigger GC' bit.
1419 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1420 if (current_bit_is_set) {
1421 flags += kDebug[s];
1422 }
1423 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1424 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1425 flags += "|";
1426 }
1427 }
Aart Bik854a02b2015-07-14 16:07:00 -07001428 }
1429 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001430 }
1431
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001432 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001433
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001434 private:
1435 static constexpr int kFieldArrayAnalysisBits = 9;
1436
1437 static constexpr int kFieldWriteOffset = 0;
1438 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1439 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1440 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1441
1442 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1443
1444 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1445 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1446 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1447 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1448
1449 static constexpr int kLastBit = kDependsOnGCBit;
1450 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1451
1452 // Aliases.
1453
1454 static_assert(kChangeBits == kDependOnBits,
1455 "the 'change' bits should match the 'depend on' bits.");
1456
1457 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1458 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1459 static constexpr uint64_t kAllWrites =
1460 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1461 static constexpr uint64_t kAllReads =
1462 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001463
Aart Bik854a02b2015-07-14 16:07:00 -07001464 // Work around the fact that HIR aliases I/F and J/D.
1465 // TODO: remove this interceptor once HIR types are clean
1466 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1467 switch (type) {
1468 case Primitive::kPrimInt:
1469 case Primitive::kPrimFloat:
1470 return TypeFlag(Primitive::kPrimInt, offset) |
1471 TypeFlag(Primitive::kPrimFloat, offset);
1472 case Primitive::kPrimLong:
1473 case Primitive::kPrimDouble:
1474 return TypeFlag(Primitive::kPrimLong, offset) |
1475 TypeFlag(Primitive::kPrimDouble, offset);
1476 default:
1477 return TypeFlag(type, offset);
1478 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001479 }
1480
Aart Bik854a02b2015-07-14 16:07:00 -07001481 // Translates type to bit flag.
1482 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1483 CHECK_NE(type, Primitive::kPrimVoid);
1484 const uint64_t one = 1;
1485 const int shift = type; // 0-based consecutive enum
1486 DCHECK_LE(kFieldWriteOffset, shift);
1487 DCHECK_LT(shift, kArrayWriteOffset);
1488 return one << (type + offset);
1489 }
1490
1491 // Private constructor on direct flags value.
1492 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1493
1494 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001495};
1496
David Brazdiled596192015-01-23 10:39:45 +00001497// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001498class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001499 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001500 HEnvironment(ArenaAllocator* arena,
1501 size_t number_of_vregs,
1502 const DexFile& dex_file,
1503 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001504 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001505 InvokeType invoke_type,
1506 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001507 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1508 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001509 parent_(nullptr),
1510 dex_file_(dex_file),
1511 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001512 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001513 invoke_type_(invoke_type),
1514 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001515 }
1516
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001517 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001518 : HEnvironment(arena,
1519 to_copy.Size(),
1520 to_copy.GetDexFile(),
1521 to_copy.GetMethodIdx(),
1522 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001523 to_copy.GetInvokeType(),
1524 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001525
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001526 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001527 if (parent_ != nullptr) {
1528 parent_->SetAndCopyParentChain(allocator, parent);
1529 } else {
1530 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1531 parent_->CopyFrom(parent);
1532 if (parent->GetParent() != nullptr) {
1533 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1534 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001535 }
David Brazdiled596192015-01-23 10:39:45 +00001536 }
1537
Vladimir Marko71bf8092015-09-15 15:33:14 +01001538 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001539 void CopyFrom(HEnvironment* environment);
1540
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001541 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1542 // input to the loop phi instead. This is for inserting instructions that
1543 // require an environment (like HDeoptimization) in the loop pre-header.
1544 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001545
1546 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001547 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001548 }
1549
1550 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001551 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001552 }
1553
David Brazdil1abb4192015-02-17 18:33:36 +00001554 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001555
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001556 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001557
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001558 HEnvironment* GetParent() const { return parent_; }
1559
1560 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001561 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001562 }
1563
1564 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001565 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001566 }
1567
1568 uint32_t GetDexPc() const {
1569 return dex_pc_;
1570 }
1571
1572 uint32_t GetMethodIdx() const {
1573 return method_idx_;
1574 }
1575
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001576 InvokeType GetInvokeType() const {
1577 return invoke_type_;
1578 }
1579
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001580 const DexFile& GetDexFile() const {
1581 return dex_file_;
1582 }
1583
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001584 HInstruction* GetHolder() const {
1585 return holder_;
1586 }
1587
David Brazdiled596192015-01-23 10:39:45 +00001588 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001589 // Record instructions' use entries of this environment for constant-time removal.
1590 // It should only be called by HInstruction when a new environment use is added.
1591 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1592 DCHECK(env_use->GetUser() == this);
1593 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001594 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001595 }
David Brazdiled596192015-01-23 10:39:45 +00001596
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001597 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1598 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001599 HEnvironment* parent_;
1600 const DexFile& dex_file_;
1601 const uint32_t method_idx_;
1602 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001603 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001604
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001605 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001606 HInstruction* const holder_;
1607
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001608 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001609
1610 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1611};
1612
Calin Juravleacf735c2015-02-12 15:25:22 +00001613class ReferenceTypeInfo : ValueObject {
1614 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001615 typedef Handle<mirror::Class> TypeHandle;
1616
Calin Juravle2e768302015-07-28 14:41:11 +00001617 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1618 // The constructor will check that the type_handle is valid.
1619 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001620 }
1621
Calin Juravle2e768302015-07-28 14:41:11 +00001622 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1623
1624 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1625 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001626 }
1627
Calin Juravle2e768302015-07-28 14:41:11 +00001628 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1629 return IsValidHandle(type_handle_);
1630 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001631
Calin Juravleacf735c2015-02-12 15:25:22 +00001632 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001633
1634 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1635 DCHECK(IsValid());
1636 return GetTypeHandle()->IsObjectClass();
1637 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001638
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001639 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001640 DCHECK(IsValid());
1641 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001642 }
1643
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001644 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1645 DCHECK(IsValid());
1646 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1647 }
1648
Mathieu Chartier90443472015-07-16 20:32:27 -07001649 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001650 DCHECK(IsValid());
1651 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001652 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001653
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001654 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001655 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001656 return GetTypeHandle()->IsArrayClass();
1657 }
1658
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001659 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1660 DCHECK(IsValid());
1661 return GetTypeHandle()->IsPrimitiveArray();
1662 }
1663
1664 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1665 DCHECK(IsValid());
1666 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1667 }
1668
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001669 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001670 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001671 if (!IsExact()) return false;
1672 if (!IsArrayClass()) return false;
1673 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1674 }
1675
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001676 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1677 DCHECK(IsValid());
1678 if (!IsExact()) return false;
1679 if (!IsArrayClass()) return false;
1680 if (!rti.IsArrayClass()) return false;
1681 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1682 rti.GetTypeHandle()->GetComponentType());
1683 }
1684
Calin Juravleacf735c2015-02-12 15:25:22 +00001685 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1686
Mathieu Chartier90443472015-07-16 20:32:27 -07001687 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001688 DCHECK(IsValid());
1689 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001690 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1691 }
1692
1693 // Returns true if the type information provide the same amount of details.
1694 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001695 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001696 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001697 if (!IsValid() && !rti.IsValid()) {
1698 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001699 return true;
1700 }
Calin Juravle2e768302015-07-28 14:41:11 +00001701 if (!IsValid() || !rti.IsValid()) {
1702 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001703 return false;
1704 }
Calin Juravle2e768302015-07-28 14:41:11 +00001705 return IsExact() == rti.IsExact()
1706 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001707 }
1708
1709 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001710 ReferenceTypeInfo();
1711 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001712
Calin Juravleacf735c2015-02-12 15:25:22 +00001713 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001714 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001715 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001716 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001717 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001718};
1719
1720std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1721
Vladimir Markof9f64412015-09-02 14:05:49 +01001722class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001723 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001724 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001725 : previous_(nullptr),
1726 next_(nullptr),
1727 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001728 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001729 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001730 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001731 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001732 locations_(nullptr),
1733 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001734 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001735 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001736 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001737
Dave Allison20dfc792014-06-16 20:44:29 -07001738 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001739
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001740#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001741 enum InstructionKind {
1742 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1743 };
1744#undef DECLARE_KIND
1745
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001746 HInstruction* GetNext() const { return next_; }
1747 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001748
Calin Juravle77520bc2015-01-12 18:45:46 +00001749 HInstruction* GetNextDisregardingMoves() const;
1750 HInstruction* GetPreviousDisregardingMoves() const;
1751
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001752 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001753 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001754 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001755 bool IsInBlock() const { return block_ != nullptr; }
1756 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001757 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001758
Roland Levillain6b879dd2014-09-22 17:13:44 +01001759 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001760 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001761
1762 virtual void Accept(HGraphVisitor* visitor) = 0;
1763 virtual const char* DebugName() const = 0;
1764
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001765 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001766 void SetRawInputAt(size_t index, HInstruction* input) {
1767 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1768 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001769
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001770 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001771
1772 uint32_t GetDexPc() const { return dex_pc_; }
1773
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001774 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001775
Roland Levillaine161a2a2014-10-03 12:45:18 +01001776 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001777 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001778
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001779 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001780 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001781
Calin Juravle10e244f2015-01-26 18:54:32 +00001782 // Does not apply for all instructions, but having this at top level greatly
1783 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001784 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001785 virtual bool CanBeNull() const {
1786 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1787 return true;
1788 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001789
Calin Juravle641547a2015-04-21 22:08:51 +01001790 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1791 UNUSED(obj);
1792 return false;
1793 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001794
Calin Juravle2e768302015-07-28 14:41:11 +00001795 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001796
Calin Juravle61d544b2015-02-23 16:46:57 +00001797 ReferenceTypeInfo GetReferenceTypeInfo() const {
1798 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1799 return reference_type_info_;
1800 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001801
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001802 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001803 DCHECK(user != nullptr);
1804 HUseListNode<HInstruction*>* use =
1805 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1806 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001807 }
1808
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001809 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001810 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001811 HUseListNode<HEnvironment*>* env_use =
1812 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1813 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001814 }
1815
David Brazdil1abb4192015-02-17 18:33:36 +00001816 void RemoveAsUserOfInput(size_t input) {
1817 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1818 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1819 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001820
David Brazdil1abb4192015-02-17 18:33:36 +00001821 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1822 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001823
David Brazdiled596192015-01-23 10:39:45 +00001824 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1825 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001826 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001827 bool HasOnlyOneNonEnvironmentUse() const {
1828 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1829 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001830
Roland Levillain6c82d402014-10-13 16:10:27 +01001831 // Does this instruction strictly dominate `other_instruction`?
1832 // Returns false if this instruction and `other_instruction` are the same.
1833 // Aborts if this instruction and `other_instruction` are both phis.
1834 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001835
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001836 int GetId() const { return id_; }
1837 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001838
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001839 int GetSsaIndex() const { return ssa_index_; }
1840 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1841 bool HasSsaIndex() const { return ssa_index_ != -1; }
1842
1843 bool HasEnvironment() const { return environment_ != nullptr; }
1844 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001845 // Set the `environment_` field. Raw because this method does not
1846 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001847 void SetRawEnvironment(HEnvironment* environment) {
1848 DCHECK(environment_ == nullptr);
1849 DCHECK_EQ(environment->GetHolder(), this);
1850 environment_ = environment;
1851 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001852
1853 // Set the environment of this instruction, copying it from `environment`. While
1854 // copying, the uses lists are being updated.
1855 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001856 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001857 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001858 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001859 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001860 if (environment->GetParent() != nullptr) {
1861 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1862 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001863 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001864
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001865 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1866 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001867 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001868 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001869 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001870 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001871 if (environment->GetParent() != nullptr) {
1872 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1873 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001874 }
1875
Nicolas Geoffray39468442014-09-02 15:17:15 +01001876 // Returns the number of entries in the environment. Typically, that is the
1877 // number of dex registers in a method. It could be more in case of inlining.
1878 size_t EnvironmentSize() const;
1879
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001880 LocationSummary* GetLocations() const { return locations_; }
1881 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001882
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001883 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001884 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001885
Alexandre Rames188d4312015-04-09 18:30:21 +01001886 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1887 // uses of this instruction by `other` are *not* updated.
1888 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1889 ReplaceWith(other);
1890 other->ReplaceInput(this, use_index);
1891 }
1892
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001893 // Move `this` instruction before `cursor`.
1894 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001895
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001896#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001897 bool Is##type() const { return (As##type() != nullptr); } \
1898 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001899 virtual H##type* As##type() { return nullptr; }
1900
1901 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1902#undef INSTRUCTION_TYPE_CHECK
1903
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001904 // Returns whether the instruction can be moved within the graph.
1905 virtual bool CanBeMoved() const { return false; }
1906
1907 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001908 virtual bool InstructionTypeEquals(HInstruction* other) const {
1909 UNUSED(other);
1910 return false;
1911 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001912
1913 // Returns whether any data encoded in the two instructions is equal.
1914 // This method does not look at the inputs. Both instructions must be
1915 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001916 virtual bool InstructionDataEquals(HInstruction* other) const {
1917 UNUSED(other);
1918 return false;
1919 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001920
1921 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001922 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001923 // 2) Their inputs are identical.
1924 bool Equals(HInstruction* other) const;
1925
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001926 virtual InstructionKind GetKind() const = 0;
1927
1928 virtual size_t ComputeHashCode() const {
1929 size_t result = GetKind();
1930 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1931 result = (result * 31) + InputAt(i)->GetId();
1932 }
1933 return result;
1934 }
1935
1936 SideEffects GetSideEffects() const { return side_effects_; }
1937
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001938 size_t GetLifetimePosition() const { return lifetime_position_; }
1939 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1940 LiveInterval* GetLiveInterval() const { return live_interval_; }
1941 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1942 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1943
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001944 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1945
1946 // Returns whether the code generation of the instruction will require to have access
1947 // to the current method. Such instructions are:
1948 // (1): Instructions that require an environment, as calling the runtime requires
1949 // to walk the stack and have the current method stored at a specific stack address.
1950 // (2): Object literals like classes and strings, that are loaded from the dex cache
1951 // fields of the current method.
1952 bool NeedsCurrentMethod() const {
1953 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1954 }
1955
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001956 virtual bool NeedsDexCache() const { return false; }
1957
Mark Mendellc4701932015-04-10 13:18:51 -04001958 // Does this instruction have any use in an environment before
1959 // control flow hits 'other'?
1960 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1961
1962 // Remove all references to environment uses of this instruction.
1963 // The caller must ensure that this is safe to do.
1964 void RemoveEnvironmentUsers();
1965
David Brazdil1abb4192015-02-17 18:33:36 +00001966 protected:
1967 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1968 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1969
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001970 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001971 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1972
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001973 HInstruction* previous_;
1974 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001975 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001976 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001977
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001978 // An instruction gets an id when it is added to the graph.
1979 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001980 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001981 int id_;
1982
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001983 // When doing liveness analysis, instructions that have uses get an SSA index.
1984 int ssa_index_;
1985
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001986 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001987 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001988
1989 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001990 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001991
Nicolas Geoffray39468442014-09-02 15:17:15 +01001992 // The environment associated with this instruction. Not null if the instruction
1993 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001994 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001995
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001996 // Set by the code generator.
1997 LocationSummary* locations_;
1998
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001999 // Set by the liveness analysis.
2000 LiveInterval* live_interval_;
2001
2002 // Set by the liveness analysis, this is the position in a linear
2003 // order of blocks where this instruction's live interval start.
2004 size_t lifetime_position_;
2005
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002006 const SideEffects side_effects_;
2007
Calin Juravleacf735c2015-02-12 15:25:22 +00002008 // TODO: for primitive types this should be marked as invalid.
2009 ReferenceTypeInfo reference_type_info_;
2010
David Brazdil1abb4192015-02-17 18:33:36 +00002011 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002012 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002013 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002014 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002015 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002016
2017 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2018};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002019std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002020
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002021class HInputIterator : public ValueObject {
2022 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002023 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002024
2025 bool Done() const { return index_ == instruction_->InputCount(); }
2026 HInstruction* Current() const { return instruction_->InputAt(index_); }
2027 void Advance() { index_++; }
2028
2029 private:
2030 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002031 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002032
2033 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2034};
2035
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002036class HInstructionIterator : public ValueObject {
2037 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002038 explicit HInstructionIterator(const HInstructionList& instructions)
2039 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002040 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002041 }
2042
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002043 bool Done() const { return instruction_ == nullptr; }
2044 HInstruction* Current() const { return instruction_; }
2045 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002046 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002047 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002048 }
2049
2050 private:
2051 HInstruction* instruction_;
2052 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002053
2054 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002055};
2056
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002057class HBackwardInstructionIterator : public ValueObject {
2058 public:
2059 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2060 : instruction_(instructions.last_instruction_) {
2061 next_ = Done() ? nullptr : instruction_->GetPrevious();
2062 }
2063
2064 bool Done() const { return instruction_ == nullptr; }
2065 HInstruction* Current() const { return instruction_; }
2066 void Advance() {
2067 instruction_ = next_;
2068 next_ = Done() ? nullptr : instruction_->GetPrevious();
2069 }
2070
2071 private:
2072 HInstruction* instruction_;
2073 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002074
2075 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002076};
2077
Vladimir Markof9f64412015-09-02 14:05:49 +01002078template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002079class HTemplateInstruction: public HInstruction {
2080 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002081 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002082 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002083 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002084
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002085 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002086
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002087 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002088 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2089 DCHECK_LT(i, N);
2090 return inputs_[i];
2091 }
David Brazdil1abb4192015-02-17 18:33:36 +00002092
2093 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002094 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002095 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002096 }
2097
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002098 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002099 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002100
2101 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002102};
2103
Vladimir Markof9f64412015-09-02 14:05:49 +01002104// HTemplateInstruction specialization for N=0.
2105template<>
2106class HTemplateInstruction<0>: public HInstruction {
2107 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002108 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002109 : HInstruction(side_effects, dex_pc) {}
2110
Vladimir Markof9f64412015-09-02 14:05:49 +01002111 virtual ~HTemplateInstruction() {}
2112
2113 size_t InputCount() const OVERRIDE { return 0; }
2114
2115 protected:
2116 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2117 LOG(FATAL) << "Unreachable";
2118 UNREACHABLE();
2119 }
2120
2121 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2122 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2123 LOG(FATAL) << "Unreachable";
2124 UNREACHABLE();
2125 }
2126
2127 private:
2128 friend class SsaBuilder;
2129};
2130
Dave Allison20dfc792014-06-16 20:44:29 -07002131template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002132class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002133 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002134 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002135 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002136 virtual ~HExpression() {}
2137
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002138 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002139
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002140 protected:
2141 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002142};
2143
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002144// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2145// instruction that branches to the exit block.
2146class HReturnVoid : public HTemplateInstruction<0> {
2147 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002148 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2149 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002150
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002151 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002152
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002153 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002154
2155 private:
2156 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2157};
2158
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002159// Represents dex's RETURN opcodes. A HReturn is a control flow
2160// instruction that branches to the exit block.
2161class HReturn : public HTemplateInstruction<1> {
2162 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002163 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2164 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002165 SetRawInputAt(0, value);
2166 }
2167
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002168 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002169
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002170 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002171
2172 private:
2173 DISALLOW_COPY_AND_ASSIGN(HReturn);
2174};
2175
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002176// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002177// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002178// exit block.
2179class HExit : public HTemplateInstruction<0> {
2180 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002181 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002182
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002183 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002184
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002185 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002186
2187 private:
2188 DISALLOW_COPY_AND_ASSIGN(HExit);
2189};
2190
2191// Jumps from one block to another.
2192class HGoto : public HTemplateInstruction<0> {
2193 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002194 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002195
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002196 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002197
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002198 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002199 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002200 }
2201
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002202 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002203
2204 private:
2205 DISALLOW_COPY_AND_ASSIGN(HGoto);
2206};
2207
Roland Levillain9867bc72015-08-05 10:21:34 +01002208class HConstant : public HExpression<0> {
2209 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002210 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2211 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002212
2213 bool CanBeMoved() const OVERRIDE { return true; }
2214
2215 virtual bool IsMinusOne() const { return false; }
2216 virtual bool IsZero() const { return false; }
2217 virtual bool IsOne() const { return false; }
2218
David Brazdil77a48ae2015-09-15 12:34:04 +00002219 virtual uint64_t GetValueAsUint64() const = 0;
2220
Roland Levillain9867bc72015-08-05 10:21:34 +01002221 DECLARE_INSTRUCTION(Constant);
2222
2223 private:
2224 DISALLOW_COPY_AND_ASSIGN(HConstant);
2225};
2226
2227class HNullConstant : public HConstant {
2228 public:
2229 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2230 return true;
2231 }
2232
David Brazdil77a48ae2015-09-15 12:34:04 +00002233 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2234
Roland Levillain9867bc72015-08-05 10:21:34 +01002235 size_t ComputeHashCode() const OVERRIDE { return 0; }
2236
2237 DECLARE_INSTRUCTION(NullConstant);
2238
2239 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002240 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002241
2242 friend class HGraph;
2243 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2244};
2245
2246// Constants of the type int. Those can be from Dex instructions, or
2247// synthesized (for example with the if-eqz instruction).
2248class HIntConstant : public HConstant {
2249 public:
2250 int32_t GetValue() const { return value_; }
2251
David Brazdil9f389d42015-10-01 14:32:56 +01002252 uint64_t GetValueAsUint64() const OVERRIDE {
2253 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2254 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002255
Roland Levillain9867bc72015-08-05 10:21:34 +01002256 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2257 DCHECK(other->IsIntConstant());
2258 return other->AsIntConstant()->value_ == value_;
2259 }
2260
2261 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2262
2263 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2264 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2265 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2266
2267 DECLARE_INSTRUCTION(IntConstant);
2268
2269 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002270 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2271 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2272 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2273 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002274
2275 const int32_t value_;
2276
2277 friend class HGraph;
2278 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2279 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2280 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2281};
2282
2283class HLongConstant : public HConstant {
2284 public:
2285 int64_t GetValue() const { return value_; }
2286
David Brazdil77a48ae2015-09-15 12:34:04 +00002287 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2288
Roland Levillain9867bc72015-08-05 10:21:34 +01002289 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2290 DCHECK(other->IsLongConstant());
2291 return other->AsLongConstant()->value_ == value_;
2292 }
2293
2294 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2295
2296 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2297 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2298 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2299
2300 DECLARE_INSTRUCTION(LongConstant);
2301
2302 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002303 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2304 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002305
2306 const int64_t value_;
2307
2308 friend class HGraph;
2309 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2310};
Dave Allison20dfc792014-06-16 20:44:29 -07002311
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002312// Conditional branch. A block ending with an HIf instruction must have
2313// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002314class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002315 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002316 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2317 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002318 SetRawInputAt(0, input);
2319 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002320
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002321 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002322
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002323 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002324 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002325 }
2326
2327 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002328 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002329 }
2330
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002331 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002332
2333 private:
2334 DISALLOW_COPY_AND_ASSIGN(HIf);
2335};
2336
David Brazdilfc6a86a2015-06-26 10:33:45 +00002337
2338// Abstract instruction which marks the beginning and/or end of a try block and
2339// links it to the respective exception handlers. Behaves the same as a Goto in
2340// non-exceptional control flow.
2341// Normal-flow successor is stored at index zero, exception handlers under
2342// higher indices in no particular order.
2343class HTryBoundary : public HTemplateInstruction<0> {
2344 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002345 enum BoundaryKind {
2346 kEntry,
2347 kExit,
2348 };
2349
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002350 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2351 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002352
2353 bool IsControlFlow() const OVERRIDE { return true; }
2354
2355 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002356 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002357
2358 // Returns whether `handler` is among its exception handlers (non-zero index
2359 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002360 bool HasExceptionHandler(const HBasicBlock& handler) const {
2361 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002362 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002363 }
2364
2365 // If not present already, adds `handler` to its block's list of exception
2366 // handlers.
2367 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002368 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002369 GetBlock()->AddSuccessor(handler);
2370 }
2371 }
2372
David Brazdil56e1acc2015-06-30 15:41:36 +01002373 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002374
David Brazdilffee3d32015-07-06 11:48:53 +01002375 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2376
David Brazdilfc6a86a2015-06-26 10:33:45 +00002377 DECLARE_INSTRUCTION(TryBoundary);
2378
2379 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002380 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002381
2382 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2383};
2384
David Brazdilffee3d32015-07-06 11:48:53 +01002385// Iterator over exception handlers of a given HTryBoundary, i.e. over
2386// exceptional successors of its basic block.
2387class HExceptionHandlerIterator : public ValueObject {
2388 public:
2389 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2390 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2391
Vladimir Marko60584552015-09-03 13:35:12 +00002392 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002393 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002394 size_t CurrentSuccessorIndex() const { return index_; }
2395 void Advance() { ++index_; }
2396
2397 private:
2398 const HBasicBlock& block_;
2399 size_t index_;
2400
2401 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2402};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002403
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002404// Deoptimize to interpreter, upon checking a condition.
2405class HDeoptimize : public HTemplateInstruction<1> {
2406 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002407 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2408 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002409 SetRawInputAt(0, cond);
2410 }
2411
2412 bool NeedsEnvironment() const OVERRIDE { return true; }
2413 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002414
2415 DECLARE_INSTRUCTION(Deoptimize);
2416
2417 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002418 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2419};
2420
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002421// Represents the ArtMethod that was passed as a first argument to
2422// the method. It is used by instructions that depend on it, like
2423// instructions that work with the dex cache.
2424class HCurrentMethod : public HExpression<0> {
2425 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002426 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2427 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002428
2429 DECLARE_INSTRUCTION(CurrentMethod);
2430
2431 private:
2432 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2433};
2434
Mark Mendellfe57faa2015-09-18 09:26:15 -04002435// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2436// have one successor for each entry in the switch table, and the final successor
2437// will be the block containing the next Dex opcode.
2438class HPackedSwitch : public HTemplateInstruction<1> {
2439 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002440 HPackedSwitch(int32_t start_value,
2441 uint32_t num_entries,
2442 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002443 uint32_t dex_pc = kNoDexPc)
2444 : HTemplateInstruction(SideEffects::None(), dex_pc),
2445 start_value_(start_value),
2446 num_entries_(num_entries) {
2447 SetRawInputAt(0, input);
2448 }
2449
2450 bool IsControlFlow() const OVERRIDE { return true; }
2451
2452 int32_t GetStartValue() const { return start_value_; }
2453
Vladimir Marko211c2112015-09-24 16:52:33 +01002454 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002455
2456 HBasicBlock* GetDefaultBlock() const {
2457 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002458 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002459 }
2460 DECLARE_INSTRUCTION(PackedSwitch);
2461
2462 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002463 const int32_t start_value_;
2464 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002465
2466 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2467};
2468
Roland Levillain88cb1752014-10-20 16:36:47 +01002469class HUnaryOperation : public HExpression<1> {
2470 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002471 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2472 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002473 SetRawInputAt(0, input);
2474 }
2475
2476 HInstruction* GetInput() const { return InputAt(0); }
2477 Primitive::Type GetResultType() const { return GetType(); }
2478
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002479 bool CanBeMoved() const OVERRIDE { return true; }
2480 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002481 UNUSED(other);
2482 return true;
2483 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002484
Roland Levillain9240d6a2014-10-20 16:47:04 +01002485 // Try to statically evaluate `operation` and return a HConstant
2486 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002487 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002488 HConstant* TryStaticEvaluation() const;
2489
2490 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002491 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2492 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002493
Roland Levillain88cb1752014-10-20 16:36:47 +01002494 DECLARE_INSTRUCTION(UnaryOperation);
2495
2496 private:
2497 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2498};
2499
Dave Allison20dfc792014-06-16 20:44:29 -07002500class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002501 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002502 HBinaryOperation(Primitive::Type result_type,
2503 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002504 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002505 SideEffects side_effects = SideEffects::None(),
2506 uint32_t dex_pc = kNoDexPc)
2507 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002508 SetRawInputAt(0, left);
2509 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002510 }
2511
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002512 HInstruction* GetLeft() const { return InputAt(0); }
2513 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002514 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002515
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002516 virtual bool IsCommutative() const { return false; }
2517
2518 // Put constant on the right.
2519 // Returns whether order is changed.
2520 bool OrderInputsWithConstantOnTheRight() {
2521 HInstruction* left = InputAt(0);
2522 HInstruction* right = InputAt(1);
2523 if (left->IsConstant() && !right->IsConstant()) {
2524 ReplaceInput(right, 0);
2525 ReplaceInput(left, 1);
2526 return true;
2527 }
2528 return false;
2529 }
2530
2531 // Order inputs by instruction id, but favor constant on the right side.
2532 // This helps GVN for commutative ops.
2533 void OrderInputs() {
2534 DCHECK(IsCommutative());
2535 HInstruction* left = InputAt(0);
2536 HInstruction* right = InputAt(1);
2537 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2538 return;
2539 }
2540 if (OrderInputsWithConstantOnTheRight()) {
2541 return;
2542 }
2543 // Order according to instruction id.
2544 if (left->GetId() > right->GetId()) {
2545 ReplaceInput(right, 0);
2546 ReplaceInput(left, 1);
2547 }
2548 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002549
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002550 bool CanBeMoved() const OVERRIDE { return true; }
2551 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002552 UNUSED(other);
2553 return true;
2554 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002555
Roland Levillain9240d6a2014-10-20 16:47:04 +01002556 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002557 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002558 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002559 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002560
2561 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002562 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2563 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2564 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2565 HLongConstant* y ATTRIBUTE_UNUSED) const {
2566 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2567 return nullptr;
2568 }
2569 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2570 HIntConstant* y ATTRIBUTE_UNUSED) const {
2571 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2572 return nullptr;
2573 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002574
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002575 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002576 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002577 HConstant* GetConstantRight() const;
2578
2579 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002580 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002581 HInstruction* GetLeastConstantLeft() const;
2582
Roland Levillainccc07a92014-09-16 14:48:16 +01002583 DECLARE_INSTRUCTION(BinaryOperation);
2584
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002585 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002586 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2587};
2588
Mark Mendellc4701932015-04-10 13:18:51 -04002589// The comparison bias applies for floating point operations and indicates how NaN
2590// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002591enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002592 kNoBias, // bias is not applicable (i.e. for long operation)
2593 kGtBias, // return 1 for NaN comparisons
2594 kLtBias, // return -1 for NaN comparisons
2595};
2596
Dave Allison20dfc792014-06-16 20:44:29 -07002597class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002598 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002599 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2600 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002601 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002602 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002603
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002604 bool NeedsMaterialization() const { return needs_materialization_; }
2605 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002606
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002607 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002608 // `instruction`, and disregard moves in between.
2609 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002610
Dave Allison20dfc792014-06-16 20:44:29 -07002611 DECLARE_INSTRUCTION(Condition);
2612
2613 virtual IfCondition GetCondition() const = 0;
2614
Mark Mendellc4701932015-04-10 13:18:51 -04002615 virtual IfCondition GetOppositeCondition() const = 0;
2616
Roland Levillain4fa13f62015-07-06 18:11:54 +01002617 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002618
2619 void SetBias(ComparisonBias bias) { bias_ = bias; }
2620
2621 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2622 return bias_ == other->AsCondition()->bias_;
2623 }
2624
Roland Levillain4fa13f62015-07-06 18:11:54 +01002625 bool IsFPConditionTrueIfNaN() const {
2626 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2627 IfCondition if_cond = GetCondition();
2628 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2629 }
2630
2631 bool IsFPConditionFalseIfNaN() const {
2632 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2633 IfCondition if_cond = GetCondition();
2634 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2635 }
2636
Dave Allison20dfc792014-06-16 20:44:29 -07002637 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002638 // For register allocation purposes, returns whether this instruction needs to be
2639 // materialized (that is, not just be in the processor flags).
2640 bool needs_materialization_;
2641
Mark Mendellc4701932015-04-10 13:18:51 -04002642 // Needed if we merge a HCompare into a HCondition.
2643 ComparisonBias bias_;
2644
Dave Allison20dfc792014-06-16 20:44:29 -07002645 DISALLOW_COPY_AND_ASSIGN(HCondition);
2646};
2647
2648// Instruction to check if two inputs are equal to each other.
2649class HEqual : public HCondition {
2650 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002651 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2652 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002653
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002654 bool IsCommutative() const OVERRIDE { return true; }
2655
Roland Levillain9867bc72015-08-05 10:21:34 +01002656 template <typename T> bool Compute(T x, T y) const { return x == y; }
2657
2658 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002659 return GetBlock()->GetGraph()->GetIntConstant(
2660 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002661 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002662 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002663 return GetBlock()->GetGraph()->GetIntConstant(
2664 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002665 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002666
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002667 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002668
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002669 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002670 return kCondEQ;
2671 }
2672
Mark Mendellc4701932015-04-10 13:18:51 -04002673 IfCondition GetOppositeCondition() const OVERRIDE {
2674 return kCondNE;
2675 }
2676
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002677 private:
2678 DISALLOW_COPY_AND_ASSIGN(HEqual);
2679};
2680
Dave Allison20dfc792014-06-16 20:44:29 -07002681class HNotEqual : public HCondition {
2682 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002683 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2684 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002685
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002686 bool IsCommutative() const OVERRIDE { return true; }
2687
Roland Levillain9867bc72015-08-05 10:21:34 +01002688 template <typename T> bool Compute(T x, T y) const { return x != y; }
2689
2690 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002691 return GetBlock()->GetGraph()->GetIntConstant(
2692 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002693 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002694 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002695 return GetBlock()->GetGraph()->GetIntConstant(
2696 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002697 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002698
Dave Allison20dfc792014-06-16 20:44:29 -07002699 DECLARE_INSTRUCTION(NotEqual);
2700
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002701 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002702 return kCondNE;
2703 }
2704
Mark Mendellc4701932015-04-10 13:18:51 -04002705 IfCondition GetOppositeCondition() const OVERRIDE {
2706 return kCondEQ;
2707 }
2708
Dave Allison20dfc792014-06-16 20:44:29 -07002709 private:
2710 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2711};
2712
2713class HLessThan : public HCondition {
2714 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002715 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2716 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002717
Roland Levillain9867bc72015-08-05 10:21:34 +01002718 template <typename T> bool Compute(T x, T y) const { return x < y; }
2719
2720 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002721 return GetBlock()->GetGraph()->GetIntConstant(
2722 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002723 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002724 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002725 return GetBlock()->GetGraph()->GetIntConstant(
2726 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002727 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002728
Dave Allison20dfc792014-06-16 20:44:29 -07002729 DECLARE_INSTRUCTION(LessThan);
2730
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002731 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002732 return kCondLT;
2733 }
2734
Mark Mendellc4701932015-04-10 13:18:51 -04002735 IfCondition GetOppositeCondition() const OVERRIDE {
2736 return kCondGE;
2737 }
2738
Dave Allison20dfc792014-06-16 20:44:29 -07002739 private:
2740 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2741};
2742
2743class HLessThanOrEqual : public HCondition {
2744 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002745 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2746 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002747
Roland Levillain9867bc72015-08-05 10:21:34 +01002748 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2749
2750 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002751 return GetBlock()->GetGraph()->GetIntConstant(
2752 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002753 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002754 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002755 return GetBlock()->GetGraph()->GetIntConstant(
2756 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002757 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002758
Dave Allison20dfc792014-06-16 20:44:29 -07002759 DECLARE_INSTRUCTION(LessThanOrEqual);
2760
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002761 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002762 return kCondLE;
2763 }
2764
Mark Mendellc4701932015-04-10 13:18:51 -04002765 IfCondition GetOppositeCondition() const OVERRIDE {
2766 return kCondGT;
2767 }
2768
Dave Allison20dfc792014-06-16 20:44:29 -07002769 private:
2770 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2771};
2772
2773class HGreaterThan : public HCondition {
2774 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002775 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2776 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002777
Roland Levillain9867bc72015-08-05 10:21:34 +01002778 template <typename T> bool Compute(T x, T y) const { return x > y; }
2779
2780 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002781 return GetBlock()->GetGraph()->GetIntConstant(
2782 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002783 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002784 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002785 return GetBlock()->GetGraph()->GetIntConstant(
2786 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002787 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002788
Dave Allison20dfc792014-06-16 20:44:29 -07002789 DECLARE_INSTRUCTION(GreaterThan);
2790
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002791 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002792 return kCondGT;
2793 }
2794
Mark Mendellc4701932015-04-10 13:18:51 -04002795 IfCondition GetOppositeCondition() const OVERRIDE {
2796 return kCondLE;
2797 }
2798
Dave Allison20dfc792014-06-16 20:44:29 -07002799 private:
2800 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2801};
2802
2803class HGreaterThanOrEqual : public HCondition {
2804 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002805 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2806 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002807
Roland Levillain9867bc72015-08-05 10:21:34 +01002808 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2809
2810 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002811 return GetBlock()->GetGraph()->GetIntConstant(
2812 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002813 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002814 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002815 return GetBlock()->GetGraph()->GetIntConstant(
2816 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002817 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002818
Dave Allison20dfc792014-06-16 20:44:29 -07002819 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2820
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002821 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002822 return kCondGE;
2823 }
2824
Mark Mendellc4701932015-04-10 13:18:51 -04002825 IfCondition GetOppositeCondition() const OVERRIDE {
2826 return kCondLT;
2827 }
2828
Dave Allison20dfc792014-06-16 20:44:29 -07002829 private:
2830 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2831};
2832
2833
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002834// Instruction to check how two inputs compare to each other.
2835// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2836class HCompare : public HBinaryOperation {
2837 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002838 HCompare(Primitive::Type type,
2839 HInstruction* first,
2840 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002841 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002842 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002843 : HBinaryOperation(Primitive::kPrimInt,
2844 first,
2845 second,
2846 SideEffectsForArchRuntimeCalls(type),
2847 dex_pc),
2848 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002849 DCHECK_EQ(type, first->GetType());
2850 DCHECK_EQ(type, second->GetType());
2851 }
2852
Roland Levillain9867bc72015-08-05 10:21:34 +01002853 template <typename T>
2854 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002855
Roland Levillain9867bc72015-08-05 10:21:34 +01002856 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002857 return GetBlock()->GetGraph()->GetIntConstant(
2858 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002859 }
2860 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002861 return GetBlock()->GetGraph()->GetIntConstant(
2862 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002863 }
2864
Calin Juravleddb7df22014-11-25 20:56:51 +00002865 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2866 return bias_ == other->AsCompare()->bias_;
2867 }
2868
Mark Mendellc4701932015-04-10 13:18:51 -04002869 ComparisonBias GetBias() const { return bias_; }
2870
Roland Levillain4fa13f62015-07-06 18:11:54 +01002871 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002872
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002873
2874 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2875 // MIPS64 uses a runtime call for FP comparisons.
2876 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2877 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002878
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002879 DECLARE_INSTRUCTION(Compare);
2880
2881 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002882 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002883
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002884 DISALLOW_COPY_AND_ASSIGN(HCompare);
2885};
2886
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002887// A local in the graph. Corresponds to a Dex register.
2888class HLocal : public HTemplateInstruction<0> {
2889 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002890 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002891 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002892
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002893 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002894
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002895 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002896
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002897 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002898 // The Dex register number.
2899 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002900
2901 DISALLOW_COPY_AND_ASSIGN(HLocal);
2902};
2903
2904// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002905class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002906 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002907 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2908 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002909 SetRawInputAt(0, local);
2910 }
2911
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002912 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2913
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002914 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002915
2916 private:
2917 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2918};
2919
2920// Store a value in a given local. This instruction has two inputs: the value
2921// and the local.
2922class HStoreLocal : public HTemplateInstruction<2> {
2923 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002924 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
2925 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002926 SetRawInputAt(0, local);
2927 SetRawInputAt(1, value);
2928 }
2929
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002930 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2931
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002932 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002933
2934 private:
2935 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2936};
2937
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002938class HFloatConstant : public HConstant {
2939 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002940 float GetValue() const { return value_; }
2941
David Brazdil77a48ae2015-09-15 12:34:04 +00002942 uint64_t GetValueAsUint64() const OVERRIDE {
2943 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2944 }
2945
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002946 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002947 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002948 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002949 }
2950
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002951 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002952
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002953 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002954 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002955 }
2956 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002957 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002958 }
2959 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002960 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2961 }
2962 bool IsNaN() const {
2963 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002964 }
2965
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002966 DECLARE_INSTRUCTION(FloatConstant);
2967
2968 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002969 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2970 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2971 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2972 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002973
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002974 const float value_;
2975
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002976 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002977 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002978 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002979 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2980};
2981
2982class HDoubleConstant : public HConstant {
2983 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002984 double GetValue() const { return value_; }
2985
David Brazdil77a48ae2015-09-15 12:34:04 +00002986 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2987
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002988 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002989 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002990 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002991 }
2992
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002993 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002994
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002995 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002996 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002997 }
2998 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002999 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003000 }
3001 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003002 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3003 }
3004 bool IsNaN() const {
3005 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003006 }
3007
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003008 DECLARE_INSTRUCTION(DoubleConstant);
3009
3010 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003011 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3012 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3013 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3014 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003015
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003016 const double value_;
3017
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003018 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003019 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003020 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003021 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3022};
3023
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003024enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003025#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003026#include "intrinsics_list.h"
3027 kNone,
3028 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3029#undef INTRINSICS_LIST
3030#undef OPTIMIZING_INTRINSICS
3031};
3032std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3033
Agi Csaki05f20562015-08-19 14:58:14 -07003034enum IntrinsicNeedsEnvironmentOrCache {
3035 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3036 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003037};
3038
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003039class HInvoke : public HInstruction {
3040 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003041 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003042
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003043 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003044
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003045 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003046 SetRawInputAt(index, argument);
3047 }
3048
Roland Levillain3e3d7332015-04-28 11:00:54 +01003049 // Return the number of arguments. This number can be lower than
3050 // the number of inputs returned by InputCount(), as some invoke
3051 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3052 // inputs at the end of their list of inputs.
3053 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3054
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003055 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003056
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003057
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003058 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003059 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003060
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003061 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3062
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003063 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003064 return intrinsic_;
3065 }
3066
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003067 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003068
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003069 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003070 return GetEnvironment()->GetParent() != nullptr;
3071 }
3072
3073 bool CanThrow() const OVERRIDE { return true; }
3074
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003075 uint32_t* GetIntrinsicOptimizations() {
3076 return &intrinsic_optimizations_;
3077 }
3078
3079 const uint32_t* GetIntrinsicOptimizations() const {
3080 return &intrinsic_optimizations_;
3081 }
3082
3083 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3084
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003085 DECLARE_INSTRUCTION(Invoke);
3086
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003087 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003088 HInvoke(ArenaAllocator* arena,
3089 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003090 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003091 Primitive::Type return_type,
3092 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003093 uint32_t dex_method_index,
3094 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003095 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003096 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003097 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003098 inputs_(number_of_arguments + number_of_other_inputs,
3099 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003100 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003101 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003102 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003103 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003104 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003105 }
3106
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003107 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003108 return inputs_[index];
3109 }
3110
David Brazdil1abb4192015-02-17 18:33:36 +00003111 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003112 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:
Calin Juravlec05aca72015-10-13 13:10:33 +00003930 HParameterValue(uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003931 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 Juravlec05aca72015-10-13 13:10:33 +00003943 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 {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004134 return inputs_[index];
4135 }
David Brazdil1abb4192015-02-17 18:33:36 +00004136
4137 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004138 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004139 }
4140
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004141 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004142 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004143 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004144 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004145 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004146 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004147
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004148 DISALLOW_COPY_AND_ASSIGN(HPhi);
4149};
4150
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004151class HNullCheck : public HExpression<1> {
4152 public:
4153 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004154 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004155 SetRawInputAt(0, value);
4156 }
4157
Calin Juravle10e244f2015-01-26 18:54:32 +00004158 bool CanBeMoved() const OVERRIDE { return true; }
4159 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004160 UNUSED(other);
4161 return true;
4162 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004163
Calin Juravle10e244f2015-01-26 18:54:32 +00004164 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004165
Calin Juravle10e244f2015-01-26 18:54:32 +00004166 bool CanThrow() const OVERRIDE { return true; }
4167
4168 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004169
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004170
4171 DECLARE_INSTRUCTION(NullCheck);
4172
4173 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004174 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4175};
4176
4177class FieldInfo : public ValueObject {
4178 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004179 FieldInfo(MemberOffset field_offset,
4180 Primitive::Type field_type,
4181 bool is_volatile,
4182 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004183 const DexFile& dex_file,
4184 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004185 : field_offset_(field_offset),
4186 field_type_(field_type),
4187 is_volatile_(is_volatile),
4188 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004189 dex_file_(dex_file),
4190 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004191
4192 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004193 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004194 uint32_t GetFieldIndex() const { return index_; }
4195 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004196 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004197 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004198
4199 private:
4200 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004201 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004202 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004203 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004204 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004205 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004206};
4207
4208class HInstanceFieldGet : public HExpression<1> {
4209 public:
4210 HInstanceFieldGet(HInstruction* value,
4211 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004212 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004213 bool is_volatile,
4214 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004215 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004216 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004217 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004218 : HExpression(
4219 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004220 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004221 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004222 SetRawInputAt(0, value);
4223 }
4224
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004225 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004226
4227 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4228 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4229 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004230 }
4231
Calin Juravle641547a2015-04-21 22:08:51 +01004232 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4233 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004234 }
4235
4236 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004237 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4238 }
4239
Calin Juravle52c48962014-12-16 17:02:57 +00004240 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004241 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004242 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004243 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004244
4245 DECLARE_INSTRUCTION(InstanceFieldGet);
4246
4247 private:
4248 const FieldInfo field_info_;
4249
4250 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4251};
4252
4253class HInstanceFieldSet : public HTemplateInstruction<2> {
4254 public:
4255 HInstanceFieldSet(HInstruction* object,
4256 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004257 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004258 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004259 bool is_volatile,
4260 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004261 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004262 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004263 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004264 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004265 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004266 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004267 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004268 SetRawInputAt(0, object);
4269 SetRawInputAt(1, value);
4270 }
4271
Calin Juravle641547a2015-04-21 22:08:51 +01004272 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4273 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004274 }
4275
Calin Juravle52c48962014-12-16 17:02:57 +00004276 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004277 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004278 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004279 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004280 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004281 bool GetValueCanBeNull() const { return value_can_be_null_; }
4282 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004283
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004284 DECLARE_INSTRUCTION(InstanceFieldSet);
4285
4286 private:
4287 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004288 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004289
4290 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4291};
4292
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004293class HArrayGet : public HExpression<2> {
4294 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004295 HArrayGet(HInstruction* array,
4296 HInstruction* index,
4297 Primitive::Type type,
Calin Juravle154746b2015-10-06 15:46:54 +01004298 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004299 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004300 SetRawInputAt(0, array);
4301 SetRawInputAt(1, index);
4302 }
4303
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004304 bool CanBeMoved() const OVERRIDE { return true; }
4305 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004306 UNUSED(other);
4307 return true;
4308 }
Calin Juravle641547a2015-04-21 22:08:51 +01004309 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4310 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004311 // TODO: We can be smarter here.
4312 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4313 // which generates the implicit null check. There are cases when these can be removed
4314 // to produce better code. If we ever add optimizations to do so we should allow an
4315 // implicit check here (as long as the address falls in the first page).
4316 return false;
4317 }
4318
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004319 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004320
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004321 HInstruction* GetArray() const { return InputAt(0); }
4322 HInstruction* GetIndex() const { return InputAt(1); }
4323
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004324 DECLARE_INSTRUCTION(ArrayGet);
4325
4326 private:
4327 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4328};
4329
4330class HArraySet : public HTemplateInstruction<3> {
4331 public:
4332 HArraySet(HInstruction* array,
4333 HInstruction* index,
4334 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004335 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004336 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004337 : HTemplateInstruction(
4338 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004339 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004340 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004341 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004342 value_can_be_null_(true),
4343 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004344 SetRawInputAt(0, array);
4345 SetRawInputAt(1, index);
4346 SetRawInputAt(2, value);
4347 }
4348
Calin Juravle77520bc2015-01-12 18:45:46 +00004349 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004350 // We currently always call a runtime method to catch array store
4351 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004352 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004353 }
4354
Mingyao Yang81014cb2015-06-02 03:16:27 -07004355 // Can throw ArrayStoreException.
4356 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4357
Calin Juravle641547a2015-04-21 22:08:51 +01004358 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4359 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004360 // TODO: Same as for ArrayGet.
4361 return false;
4362 }
4363
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004364 void ClearNeedsTypeCheck() {
4365 needs_type_check_ = false;
4366 }
4367
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004368 void ClearValueCanBeNull() {
4369 value_can_be_null_ = false;
4370 }
4371
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004372 void SetStaticTypeOfArrayIsObjectArray() {
4373 static_type_of_array_is_object_array_ = true;
4374 }
4375
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004376 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004377 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004378 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004379
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004380 HInstruction* GetArray() const { return InputAt(0); }
4381 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004382 HInstruction* GetValue() const { return InputAt(2); }
4383
4384 Primitive::Type GetComponentType() const {
4385 // The Dex format does not type floating point index operations. Since the
4386 // `expected_component_type_` is set during building and can therefore not
4387 // be correct, we also check what is the value type. If it is a floating
4388 // point type, we must use that type.
4389 Primitive::Type value_type = GetValue()->GetType();
4390 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4391 ? value_type
4392 : expected_component_type_;
4393 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004394
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004395 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4396 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4397 }
4398
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004399 DECLARE_INSTRUCTION(ArraySet);
4400
4401 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004402 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004403 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004404 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004405 // Cached information for the reference_type_info_ so that codegen
4406 // does not need to inspect the static type.
4407 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004408
4409 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4410};
4411
4412class HArrayLength : public HExpression<1> {
4413 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004414 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004415 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004416 // Note that arrays do not change length, so the instruction does not
4417 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004418 SetRawInputAt(0, array);
4419 }
4420
Calin Juravle77520bc2015-01-12 18:45:46 +00004421 bool CanBeMoved() const OVERRIDE { return true; }
4422 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004423 UNUSED(other);
4424 return true;
4425 }
Calin Juravle641547a2015-04-21 22:08:51 +01004426 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4427 return obj == InputAt(0);
4428 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004429
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004430 DECLARE_INSTRUCTION(ArrayLength);
4431
4432 private:
4433 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4434};
4435
4436class HBoundsCheck : public HExpression<2> {
4437 public:
4438 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004439 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004440 DCHECK(index->GetType() == Primitive::kPrimInt);
4441 SetRawInputAt(0, index);
4442 SetRawInputAt(1, length);
4443 }
4444
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004445 bool CanBeMoved() const OVERRIDE { return true; }
4446 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004447 UNUSED(other);
4448 return true;
4449 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004450
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004451 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004452
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004453 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004454
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004455
4456 DECLARE_INSTRUCTION(BoundsCheck);
4457
4458 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004459 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4460};
4461
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004462/**
4463 * Some DEX instructions are folded into multiple HInstructions that need
4464 * to stay live until the last HInstruction. This class
4465 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004466 * HInstruction stays live. `index` represents the stack location index of the
4467 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004468 */
4469class HTemporary : public HTemplateInstruction<0> {
4470 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004471 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4472 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004473
4474 size_t GetIndex() const { return index_; }
4475
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004476 Primitive::Type GetType() const OVERRIDE {
4477 // The previous instruction is the one that will be stored in the temporary location.
4478 DCHECK(GetPrevious() != nullptr);
4479 return GetPrevious()->GetType();
4480 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004481
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004482 DECLARE_INSTRUCTION(Temporary);
4483
4484 private:
4485 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004486 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4487};
4488
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004489class HSuspendCheck : public HTemplateInstruction<0> {
4490 public:
4491 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004492 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004493
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004494 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004495 return true;
4496 }
4497
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004498 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4499 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004500
4501 DECLARE_INSTRUCTION(SuspendCheck);
4502
4503 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004504 // Only used for code generation, in order to share the same slow path between back edges
4505 // of a same loop.
4506 SlowPathCode* slow_path_;
4507
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004508 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4509};
4510
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004511/**
4512 * Instruction to load a Class object.
4513 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004514class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004515 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004516 HLoadClass(HCurrentMethod* current_method,
4517 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004518 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004519 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004520 uint32_t dex_pc,
4521 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004522 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004523 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004524 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004525 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004526 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004527 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004528 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004529 // Referrers class should not need access check. We never inline unverified
4530 // methods so we can't possibly end up in this situation.
4531 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004532 SetRawInputAt(0, current_method);
4533 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004534
4535 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004536
4537 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004538 // Note that we don't need to test for generate_clinit_check_.
4539 // Whether or not we need to generate the clinit check is processed in
4540 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004541 return other->AsLoadClass()->type_index_ == type_index_ &&
4542 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004543 }
4544
4545 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4546
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004547 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004548 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004549 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004550
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004551 bool NeedsEnvironment() const OVERRIDE {
4552 // Will call runtime and load the class if the class is not loaded yet.
4553 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004554 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004555 }
4556
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004557 bool MustGenerateClinitCheck() const {
4558 return generate_clinit_check_;
4559 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004560 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4561 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004562 }
4563
4564 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004565 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4566 }
4567
4568 bool NeedsAccessCheck() const {
4569 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004570 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004571
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004572 bool CanThrow() const OVERRIDE {
4573 // May call runtime and and therefore can throw.
4574 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004575 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004576 }
4577
Calin Juravleacf735c2015-02-12 15:25:22 +00004578 ReferenceTypeInfo GetLoadedClassRTI() {
4579 return loaded_class_rti_;
4580 }
4581
4582 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4583 // Make sure we only set exact types (the loaded class should never be merged).
4584 DCHECK(rti.IsExact());
4585 loaded_class_rti_ = rti;
4586 }
4587
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004588 const DexFile& GetDexFile() { return dex_file_; }
4589
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004590 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4591
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004592 static SideEffects SideEffectsForArchRuntimeCalls() {
4593 return SideEffects::CanTriggerGC();
4594 }
4595
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004596 DECLARE_INSTRUCTION(LoadClass);
4597
4598 private:
4599 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004600 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004601 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004602 // Whether this instruction must generate the initialization check.
4603 // Used for code generation.
4604 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004605 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004606
Calin Juravleacf735c2015-02-12 15:25:22 +00004607 ReferenceTypeInfo loaded_class_rti_;
4608
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004609 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4610};
4611
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004612class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004613 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004614 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004615 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4616 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004617 SetRawInputAt(0, current_method);
4618 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004619
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004620 bool CanBeMoved() const OVERRIDE { return true; }
4621
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004622 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4623 return other->AsLoadString()->string_index_ == string_index_;
4624 }
4625
4626 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4627
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004628 uint32_t GetStringIndex() const { return string_index_; }
4629
4630 // TODO: Can we deopt or debug when we resolve a string?
4631 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004632 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004633 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004634
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004635 static SideEffects SideEffectsForArchRuntimeCalls() {
4636 return SideEffects::CanTriggerGC();
4637 }
4638
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004639 DECLARE_INSTRUCTION(LoadString);
4640
4641 private:
4642 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004643
4644 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4645};
4646
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004647/**
4648 * Performs an initialization check on its Class object input.
4649 */
4650class HClinitCheck : public HExpression<1> {
4651 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004652 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004653 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004654 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004655 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4656 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004657 SetRawInputAt(0, constant);
4658 }
4659
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004660 bool CanBeMoved() const OVERRIDE { return true; }
4661 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4662 UNUSED(other);
4663 return true;
4664 }
4665
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004666 bool NeedsEnvironment() const OVERRIDE {
4667 // May call runtime to initialize the class.
4668 return true;
4669 }
4670
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004671
4672 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4673
4674 DECLARE_INSTRUCTION(ClinitCheck);
4675
4676 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004677 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4678};
4679
4680class HStaticFieldGet : public HExpression<1> {
4681 public:
4682 HStaticFieldGet(HInstruction* cls,
4683 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004684 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004685 bool is_volatile,
4686 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004687 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004688 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004689 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004690 : HExpression(
4691 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004692 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004693 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004694 SetRawInputAt(0, cls);
4695 }
4696
Calin Juravle52c48962014-12-16 17:02:57 +00004697
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004698 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004699
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004700 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004701 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4702 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004703 }
4704
4705 size_t ComputeHashCode() const OVERRIDE {
4706 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4707 }
4708
Calin Juravle52c48962014-12-16 17:02:57 +00004709 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004710 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4711 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004712 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004713
4714 DECLARE_INSTRUCTION(StaticFieldGet);
4715
4716 private:
4717 const FieldInfo field_info_;
4718
4719 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4720};
4721
4722class HStaticFieldSet : public HTemplateInstruction<2> {
4723 public:
4724 HStaticFieldSet(HInstruction* cls,
4725 HInstruction* value,
4726 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004727 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004728 bool is_volatile,
4729 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004730 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004731 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004732 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004733 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004734 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004735 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004736 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004737 SetRawInputAt(0, cls);
4738 SetRawInputAt(1, value);
4739 }
4740
Calin Juravle52c48962014-12-16 17:02:57 +00004741 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004742 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4743 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004744 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004745
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004746 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004747 bool GetValueCanBeNull() const { return value_can_be_null_; }
4748 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004749
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004750 DECLARE_INSTRUCTION(StaticFieldSet);
4751
4752 private:
4753 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004754 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004755
4756 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4757};
4758
Calin Juravlee460d1d2015-09-29 04:52:17 +01004759class HUnresolvedInstanceFieldGet : public HExpression<1> {
4760 public:
4761 HUnresolvedInstanceFieldGet(HInstruction* obj,
4762 Primitive::Type field_type,
4763 uint32_t field_index,
4764 uint32_t dex_pc)
4765 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4766 field_index_(field_index) {
4767 SetRawInputAt(0, obj);
4768 }
4769
4770 bool NeedsEnvironment() const OVERRIDE { return true; }
4771 bool CanThrow() const OVERRIDE { return true; }
4772
4773 Primitive::Type GetFieldType() const { return GetType(); }
4774 uint32_t GetFieldIndex() const { return field_index_; }
4775
4776 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
4777
4778 private:
4779 const uint32_t field_index_;
4780
4781 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
4782};
4783
4784class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
4785 public:
4786 HUnresolvedInstanceFieldSet(HInstruction* obj,
4787 HInstruction* value,
4788 Primitive::Type field_type,
4789 uint32_t field_index,
4790 uint32_t dex_pc)
4791 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4792 field_type_(field_type),
4793 field_index_(field_index) {
4794 DCHECK_EQ(field_type, value->GetType());
4795 SetRawInputAt(0, obj);
4796 SetRawInputAt(1, value);
4797 }
4798
4799 bool NeedsEnvironment() const OVERRIDE { return true; }
4800 bool CanThrow() const OVERRIDE { return true; }
4801
4802 Primitive::Type GetFieldType() const { return field_type_; }
4803 uint32_t GetFieldIndex() const { return field_index_; }
4804
4805 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
4806
4807 private:
4808 const Primitive::Type field_type_;
4809 const uint32_t field_index_;
4810
4811 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
4812};
4813
4814class HUnresolvedStaticFieldGet : public HExpression<0> {
4815 public:
4816 HUnresolvedStaticFieldGet(Primitive::Type field_type,
4817 uint32_t field_index,
4818 uint32_t dex_pc)
4819 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4820 field_index_(field_index) {
4821 }
4822
4823 bool NeedsEnvironment() const OVERRIDE { return true; }
4824 bool CanThrow() const OVERRIDE { return true; }
4825
4826 Primitive::Type GetFieldType() const { return GetType(); }
4827 uint32_t GetFieldIndex() const { return field_index_; }
4828
4829 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
4830
4831 private:
4832 const uint32_t field_index_;
4833
4834 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
4835};
4836
4837class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
4838 public:
4839 HUnresolvedStaticFieldSet(HInstruction* value,
4840 Primitive::Type field_type,
4841 uint32_t field_index,
4842 uint32_t dex_pc)
4843 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4844 field_type_(field_type),
4845 field_index_(field_index) {
4846 DCHECK_EQ(field_type, value->GetType());
4847 SetRawInputAt(0, value);
4848 }
4849
4850 bool NeedsEnvironment() const OVERRIDE { return true; }
4851 bool CanThrow() const OVERRIDE { return true; }
4852
4853 Primitive::Type GetFieldType() const { return field_type_; }
4854 uint32_t GetFieldIndex() const { return field_index_; }
4855
4856 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
4857
4858 private:
4859 const Primitive::Type field_type_;
4860 const uint32_t field_index_;
4861
4862 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
4863};
4864
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004865// Implement the move-exception DEX instruction.
4866class HLoadException : public HExpression<0> {
4867 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004868 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4869 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004870
David Brazdilbbd733e2015-08-18 17:48:17 +01004871 bool CanBeNull() const OVERRIDE { return false; }
4872
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004873 DECLARE_INSTRUCTION(LoadException);
4874
4875 private:
4876 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4877};
4878
David Brazdilcb1c0552015-08-04 16:22:25 +01004879// Implicit part of move-exception which clears thread-local exception storage.
4880// Must not be removed because the runtime expects the TLS to get cleared.
4881class HClearException : public HTemplateInstruction<0> {
4882 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004883 explicit HClearException(uint32_t dex_pc = kNoDexPc)
4884 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01004885
4886 DECLARE_INSTRUCTION(ClearException);
4887
4888 private:
4889 DISALLOW_COPY_AND_ASSIGN(HClearException);
4890};
4891
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004892class HThrow : public HTemplateInstruction<1> {
4893 public:
4894 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004895 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004896 SetRawInputAt(0, exception);
4897 }
4898
4899 bool IsControlFlow() const OVERRIDE { return true; }
4900
4901 bool NeedsEnvironment() const OVERRIDE { return true; }
4902
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004903 bool CanThrow() const OVERRIDE { return true; }
4904
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004905
4906 DECLARE_INSTRUCTION(Throw);
4907
4908 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004909 DISALLOW_COPY_AND_ASSIGN(HThrow);
4910};
4911
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004912/**
4913 * Implementation strategies for the code generator of a HInstanceOf
4914 * or `HCheckCast`.
4915 */
4916enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01004917 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004918 kExactCheck, // Can do a single class compare.
4919 kClassHierarchyCheck, // Can just walk the super class chain.
4920 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
4921 kInterfaceCheck, // No optimization yet when checking against an interface.
4922 kArrayObjectCheck, // Can just check if the array is not primitive.
4923 kArrayCheck // No optimization yet when checking against a generic array.
4924};
4925
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004926class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004927 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004928 HInstanceOf(HInstruction* object,
4929 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004930 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004931 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004932 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004933 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004934 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004935 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004936 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004937 SetRawInputAt(0, object);
4938 SetRawInputAt(1, constant);
4939 }
4940
4941 bool CanBeMoved() const OVERRIDE { return true; }
4942
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004943 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004944 return true;
4945 }
4946
4947 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004948 return false;
4949 }
4950
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004951 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
4952
4953 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004954
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004955 // Used only in code generation.
4956 bool MustDoNullCheck() const { return must_do_null_check_; }
4957 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4958
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004959 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
4960 return (check_kind == TypeCheckKind::kExactCheck)
4961 ? SideEffects::None()
4962 // Mips currently does runtime calls for any other checks.
4963 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004964 }
4965
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004966 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004967
4968 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004969 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004970 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004971
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004972 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4973};
4974
Calin Juravleb1498f62015-02-16 13:13:29 +00004975class HBoundType : public HExpression<1> {
4976 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004977 // Constructs an HBoundType with the given upper_bound.
4978 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004979 HBoundType(HInstruction* input,
4980 ReferenceTypeInfo upper_bound,
4981 bool upper_can_be_null,
4982 uint32_t dex_pc = kNoDexPc)
4983 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004984 upper_bound_(upper_bound),
4985 upper_can_be_null_(upper_can_be_null),
4986 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004987 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004988 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004989 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004990 }
4991
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004992 // GetUpper* should only be used in reference type propagation.
4993 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4994 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004995
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004996 void SetCanBeNull(bool can_be_null) {
4997 DCHECK(upper_can_be_null_ || !can_be_null);
4998 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004999 }
5000
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005001 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5002
Calin Juravleb1498f62015-02-16 13:13:29 +00005003 DECLARE_INSTRUCTION(BoundType);
5004
5005 private:
5006 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005007 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5008 // It is used to bound the type in cases like:
5009 // if (x instanceof ClassX) {
5010 // // uper_bound_ will be ClassX
5011 // }
5012 const ReferenceTypeInfo upper_bound_;
5013 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5014 // is false then can_be_null_ cannot be true).
5015 const bool upper_can_be_null_;
5016 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005017
5018 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5019};
5020
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005021class HCheckCast : public HTemplateInstruction<2> {
5022 public:
5023 HCheckCast(HInstruction* object,
5024 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005025 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005026 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005027 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005028 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005029 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005030 SetRawInputAt(0, object);
5031 SetRawInputAt(1, constant);
5032 }
5033
5034 bool CanBeMoved() const OVERRIDE { return true; }
5035
5036 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5037 return true;
5038 }
5039
5040 bool NeedsEnvironment() const OVERRIDE {
5041 // Instruction may throw a CheckCastError.
5042 return true;
5043 }
5044
5045 bool CanThrow() const OVERRIDE { return true; }
5046
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005047 bool MustDoNullCheck() const { return must_do_null_check_; }
5048 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005049 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005050
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005051 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005052
5053 DECLARE_INSTRUCTION(CheckCast);
5054
5055 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005056 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005057 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005058
5059 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005060};
5061
Calin Juravle27df7582015-04-17 19:12:31 +01005062class HMemoryBarrier : public HTemplateInstruction<0> {
5063 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005064 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005065 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005066 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005067 barrier_kind_(barrier_kind) {}
5068
5069 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5070
5071 DECLARE_INSTRUCTION(MemoryBarrier);
5072
5073 private:
5074 const MemBarrierKind barrier_kind_;
5075
5076 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5077};
5078
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005079class HMonitorOperation : public HTemplateInstruction<1> {
5080 public:
5081 enum OperationKind {
5082 kEnter,
5083 kExit,
5084 };
5085
5086 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005087 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005088 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5089 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005090 SetRawInputAt(0, object);
5091 }
5092
5093 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005094 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5095
5096 bool CanThrow() const OVERRIDE {
5097 // Verifier guarantees that monitor-exit cannot throw.
5098 // This is important because it allows the HGraphBuilder to remove
5099 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5100 return IsEnter();
5101 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005102
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005103
5104 bool IsEnter() const { return kind_ == kEnter; }
5105
5106 DECLARE_INSTRUCTION(MonitorOperation);
5107
Calin Juravle52c48962014-12-16 17:02:57 +00005108 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005109 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005110
5111 private:
5112 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5113};
5114
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005115/**
5116 * A HInstruction used as a marker for the replacement of new + <init>
5117 * of a String to a call to a StringFactory. Only baseline will see
5118 * the node at code generation, where it will be be treated as null.
5119 * When compiling non-baseline, `HFakeString` instructions are being removed
5120 * in the instruction simplifier.
5121 */
5122class HFakeString : public HTemplateInstruction<0> {
5123 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005124 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5125 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005126
5127 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5128
5129 DECLARE_INSTRUCTION(FakeString);
5130
5131 private:
5132 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5133};
5134
Vladimir Markof9f64412015-09-02 14:05:49 +01005135class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005136 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005137 MoveOperands(Location source,
5138 Location destination,
5139 Primitive::Type type,
5140 HInstruction* instruction)
5141 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005142
5143 Location GetSource() const { return source_; }
5144 Location GetDestination() const { return destination_; }
5145
5146 void SetSource(Location value) { source_ = value; }
5147 void SetDestination(Location value) { destination_ = value; }
5148
5149 // The parallel move resolver marks moves as "in-progress" by clearing the
5150 // destination (but not the source).
5151 Location MarkPending() {
5152 DCHECK(!IsPending());
5153 Location dest = destination_;
5154 destination_ = Location::NoLocation();
5155 return dest;
5156 }
5157
5158 void ClearPending(Location dest) {
5159 DCHECK(IsPending());
5160 destination_ = dest;
5161 }
5162
5163 bool IsPending() const {
5164 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5165 return destination_.IsInvalid() && !source_.IsInvalid();
5166 }
5167
5168 // True if this blocks a move from the given location.
5169 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005170 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005171 }
5172
5173 // A move is redundant if it's been eliminated, if its source and
5174 // destination are the same, or if its destination is unneeded.
5175 bool IsRedundant() const {
5176 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5177 }
5178
5179 // We clear both operands to indicate move that's been eliminated.
5180 void Eliminate() {
5181 source_ = destination_ = Location::NoLocation();
5182 }
5183
5184 bool IsEliminated() const {
5185 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5186 return source_.IsInvalid();
5187 }
5188
Alexey Frunze4dda3372015-06-01 18:31:49 -07005189 Primitive::Type GetType() const { return type_; }
5190
Nicolas Geoffray90218252015-04-15 11:56:51 +01005191 bool Is64BitMove() const {
5192 return Primitive::Is64BitType(type_);
5193 }
5194
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005195 HInstruction* GetInstruction() const { return instruction_; }
5196
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005197 private:
5198 Location source_;
5199 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005200 // The type this move is for.
5201 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005202 // The instruction this move is assocatied with. Null when this move is
5203 // for moving an input in the expected locations of user (including a phi user).
5204 // This is only used in debug mode, to ensure we do not connect interval siblings
5205 // in the same parallel move.
5206 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005207};
5208
5209static constexpr size_t kDefaultNumberOfMoves = 4;
5210
5211class HParallelMove : public HTemplateInstruction<0> {
5212 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005213 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005214 : HTemplateInstruction(SideEffects::None(), dex_pc),
5215 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5216 moves_.reserve(kDefaultNumberOfMoves);
5217 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005218
Nicolas Geoffray90218252015-04-15 11:56:51 +01005219 void AddMove(Location source,
5220 Location destination,
5221 Primitive::Type type,
5222 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005223 DCHECK(source.IsValid());
5224 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005225 if (kIsDebugBuild) {
5226 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005227 for (const MoveOperands& move : moves_) {
5228 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005229 // Special case the situation where the move is for the spill slot
5230 // of the instruction.
5231 if ((GetPrevious() == instruction)
5232 || ((GetPrevious() == nullptr)
5233 && instruction->IsPhi()
5234 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005235 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005236 << "Doing parallel moves for the same instruction.";
5237 } else {
5238 DCHECK(false) << "Doing parallel moves for the same instruction.";
5239 }
5240 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005241 }
5242 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005243 for (const MoveOperands& move : moves_) {
5244 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005245 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005246 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005247 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005248 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005249 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005250 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005251 }
5252
Vladimir Marko225b6462015-09-28 12:17:40 +01005253 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005254 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005255 }
5256
Vladimir Marko225b6462015-09-28 12:17:40 +01005257 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005258
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005259 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005260
5261 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005262 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005263
5264 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5265};
5266
Mark Mendell0616ae02015-04-17 12:49:27 -04005267} // namespace art
5268
5269#ifdef ART_ENABLE_CODEGEN_x86
5270#include "nodes_x86.h"
5271#endif
5272
5273namespace art {
5274
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005275class HGraphVisitor : public ValueObject {
5276 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005277 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5278 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005279
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005280 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005281 virtual void VisitBasicBlock(HBasicBlock* block);
5282
Roland Levillain633021e2014-10-01 14:12:25 +01005283 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005284 void VisitInsertionOrder();
5285
Roland Levillain633021e2014-10-01 14:12:25 +01005286 // Visit the graph following dominator tree reverse post-order.
5287 void VisitReversePostOrder();
5288
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005289 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005290
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005291 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005292#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005293 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5294
5295 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5296
5297#undef DECLARE_VISIT_INSTRUCTION
5298
5299 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005300 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005301
5302 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5303};
5304
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005305class HGraphDelegateVisitor : public HGraphVisitor {
5306 public:
5307 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5308 virtual ~HGraphDelegateVisitor() {}
5309
5310 // Visit functions that delegate to to super class.
5311#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005312 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005313
5314 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5315
5316#undef DECLARE_VISIT_INSTRUCTION
5317
5318 private:
5319 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5320};
5321
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005322class HInsertionOrderIterator : public ValueObject {
5323 public:
5324 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5325
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005326 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005327 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005328 void Advance() { ++index_; }
5329
5330 private:
5331 const HGraph& graph_;
5332 size_t index_;
5333
5334 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5335};
5336
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005337class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005338 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005339 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5340 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005341 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005342 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005343
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005344 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5345 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005346 void Advance() { ++index_; }
5347
5348 private:
5349 const HGraph& graph_;
5350 size_t index_;
5351
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005352 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005353};
5354
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005355class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005356 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005357 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005358 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005359 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005360 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005361 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005362
5363 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005364 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005365 void Advance() { --index_; }
5366
5367 private:
5368 const HGraph& graph_;
5369 size_t index_;
5370
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005371 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005372};
5373
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005374class HLinearPostOrderIterator : public ValueObject {
5375 public:
5376 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005377 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005378
5379 bool Done() const { return index_ == 0; }
5380
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005381 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005382
5383 void Advance() {
5384 --index_;
5385 DCHECK_GE(index_, 0U);
5386 }
5387
5388 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005389 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005390 size_t index_;
5391
5392 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5393};
5394
5395class HLinearOrderIterator : public ValueObject {
5396 public:
5397 explicit HLinearOrderIterator(const HGraph& graph)
5398 : order_(graph.GetLinearOrder()), index_(0) {}
5399
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005400 bool Done() const { return index_ == order_.size(); }
5401 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005402 void Advance() { ++index_; }
5403
5404 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005405 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005406 size_t index_;
5407
5408 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5409};
5410
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005411// Iterator over the blocks that art part of the loop. Includes blocks part
5412// of an inner loop. The order in which the blocks are iterated is on their
5413// block id.
5414class HBlocksInLoopIterator : public ValueObject {
5415 public:
5416 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5417 : blocks_in_loop_(info.GetBlocks()),
5418 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5419 index_(0) {
5420 if (!blocks_in_loop_.IsBitSet(index_)) {
5421 Advance();
5422 }
5423 }
5424
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005425 bool Done() const { return index_ == blocks_.size(); }
5426 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005427 void Advance() {
5428 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005429 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005430 if (blocks_in_loop_.IsBitSet(index_)) {
5431 break;
5432 }
5433 }
5434 }
5435
5436 private:
5437 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005438 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005439 size_t index_;
5440
5441 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5442};
5443
Mingyao Yang3584bce2015-05-19 16:01:59 -07005444// Iterator over the blocks that art part of the loop. Includes blocks part
5445// of an inner loop. The order in which the blocks are iterated is reverse
5446// post order.
5447class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5448 public:
5449 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5450 : blocks_in_loop_(info.GetBlocks()),
5451 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5452 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005453 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005454 Advance();
5455 }
5456 }
5457
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005458 bool Done() const { return index_ == blocks_.size(); }
5459 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005460 void Advance() {
5461 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005462 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5463 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005464 break;
5465 }
5466 }
5467 }
5468
5469 private:
5470 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005471 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005472 size_t index_;
5473
5474 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5475};
5476
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005477inline int64_t Int64FromConstant(HConstant* constant) {
5478 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5479 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5480 : constant->AsLongConstant()->GetValue();
5481}
5482
Vladimir Marko58155012015-08-19 12:49:41 +00005483inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5484 // For the purposes of the compiler, the dex files must actually be the same object
5485 // if we want to safely treat them as the same. This is especially important for JIT
5486 // as custom class loaders can open the same underlying file (or memory) multiple
5487 // times and provide different class resolution but no two class loaders should ever
5488 // use the same DexFile object - doing so is an unsupported hack that can lead to
5489 // all sorts of weird failures.
5490 return &lhs == &rhs;
5491}
5492
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005493} // namespace art
5494
5495#endif // ART_COMPILER_OPTIMIZING_NODES_H_