blob: 0d668e8cf7f0f9e291e46520cf6e05af90a98b52 [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) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001077 M(X86LoadFromConstantTable, Instruction) \
1078 M(X86PackedSwitch, Instruction)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001079
1080#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1081
1082#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1083 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1084 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1085 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001086 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001087 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1088 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1089
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001090#define FOR_EACH_INSTRUCTION(M) \
1091 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1092 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001093 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001094 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001095 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001096
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001097#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001098FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1099#undef FORWARD_DECLARATION
1100
Roland Levillainccc07a92014-09-16 14:48:16 +01001101#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001102 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1103 const char* DebugName() const OVERRIDE { return #type; } \
1104 const H##type* As##type() const OVERRIDE { return this; } \
1105 H##type* As##type() OVERRIDE { return this; } \
1106 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001107 return other->Is##type(); \
1108 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001109 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001110
David Brazdiled596192015-01-23 10:39:45 +00001111template <typename T> class HUseList;
1112
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001113template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001114class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001115 public:
David Brazdiled596192015-01-23 10:39:45 +00001116 HUseListNode* GetPrevious() const { return prev_; }
1117 HUseListNode* GetNext() const { return next_; }
1118 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001119 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001120 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001121
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001122 private:
David Brazdiled596192015-01-23 10:39:45 +00001123 HUseListNode(T user, size_t index)
1124 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1125
1126 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001127 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001128 HUseListNode<T>* prev_;
1129 HUseListNode<T>* next_;
1130
1131 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001132
1133 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1134};
1135
David Brazdiled596192015-01-23 10:39:45 +00001136template <typename T>
1137class HUseList : public ValueObject {
1138 public:
1139 HUseList() : first_(nullptr) {}
1140
1141 void Clear() {
1142 first_ = nullptr;
1143 }
1144
1145 // Adds a new entry at the beginning of the use list and returns
1146 // the newly created node.
1147 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001148 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001149 if (IsEmpty()) {
1150 first_ = new_node;
1151 } else {
1152 first_->prev_ = new_node;
1153 new_node->next_ = first_;
1154 first_ = new_node;
1155 }
1156 return new_node;
1157 }
1158
1159 HUseListNode<T>* GetFirst() const {
1160 return first_;
1161 }
1162
1163 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001164 DCHECK(node != nullptr);
1165 DCHECK(Contains(node));
1166
David Brazdiled596192015-01-23 10:39:45 +00001167 if (node->prev_ != nullptr) {
1168 node->prev_->next_ = node->next_;
1169 }
1170 if (node->next_ != nullptr) {
1171 node->next_->prev_ = node->prev_;
1172 }
1173 if (node == first_) {
1174 first_ = node->next_;
1175 }
1176 }
1177
David Brazdil1abb4192015-02-17 18:33:36 +00001178 bool Contains(const HUseListNode<T>* node) const {
1179 if (node == nullptr) {
1180 return false;
1181 }
1182 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1183 if (current == node) {
1184 return true;
1185 }
1186 }
1187 return false;
1188 }
1189
David Brazdiled596192015-01-23 10:39:45 +00001190 bool IsEmpty() const {
1191 return first_ == nullptr;
1192 }
1193
1194 bool HasOnlyOneUse() const {
1195 return first_ != nullptr && first_->next_ == nullptr;
1196 }
1197
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001198 size_t SizeSlow() const {
1199 size_t count = 0;
1200 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1201 ++count;
1202 }
1203 return count;
1204 }
1205
David Brazdiled596192015-01-23 10:39:45 +00001206 private:
1207 HUseListNode<T>* first_;
1208};
1209
1210template<typename T>
1211class HUseIterator : public ValueObject {
1212 public:
1213 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1214
1215 bool Done() const { return current_ == nullptr; }
1216
1217 void Advance() {
1218 DCHECK(!Done());
1219 current_ = current_->GetNext();
1220 }
1221
1222 HUseListNode<T>* Current() const {
1223 DCHECK(!Done());
1224 return current_;
1225 }
1226
1227 private:
1228 HUseListNode<T>* current_;
1229
1230 friend class HValue;
1231};
1232
David Brazdil1abb4192015-02-17 18:33:36 +00001233// This class is used by HEnvironment and HInstruction classes to record the
1234// instructions they use and pointers to the corresponding HUseListNodes kept
1235// by the used instructions.
1236template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001237class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001238 public:
1239 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1240 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1241
1242 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1243 : instruction_(old_record.instruction_), use_node_(use_node) {
1244 DCHECK(instruction_ != nullptr);
1245 DCHECK(use_node_ != nullptr);
1246 DCHECK(old_record.use_node_ == nullptr);
1247 }
1248
1249 HInstruction* GetInstruction() const { return instruction_; }
1250 HUseListNode<T>* GetUseNode() const { return use_node_; }
1251
1252 private:
1253 // Instruction used by the user.
1254 HInstruction* instruction_;
1255
1256 // Corresponding entry in the use list kept by 'instruction_'.
1257 HUseListNode<T>* use_node_;
1258};
1259
Aart Bik854a02b2015-07-14 16:07:00 -07001260/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001261 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001262 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001263 * For write/read dependences on fields/arrays, the dependence analysis uses
1264 * type disambiguation (e.g. a float field write cannot modify the value of an
1265 * integer field read) and the access type (e.g. a reference array write cannot
1266 * modify the value of a reference field read [although it may modify the
1267 * reference fetch prior to reading the field, which is represented by its own
1268 * write/read dependence]). The analysis makes conservative points-to
1269 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1270 * the same, and any reference read depends on any reference read without
1271 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001272 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001273 * The internal representation uses 38-bit and is described in the table below.
1274 * The first line indicates the side effect, and for field/array accesses the
1275 * second line indicates the type of the access (in the order of the
1276 * Primitive::Type enum).
1277 * The two numbered lines below indicate the bit position in the bitfield (read
1278 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001279 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001280 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1281 * +-------------+---------+---------+--------------+---------+---------+
1282 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1283 * | 3 |333333322|222222221| 1 |111111110|000000000|
1284 * | 7 |654321098|765432109| 8 |765432109|876543210|
1285 *
1286 * Note that, to ease the implementation, 'changes' bits are least significant
1287 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001288 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001289class SideEffects : public ValueObject {
1290 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001291 SideEffects() : flags_(0) {}
1292
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001293 static SideEffects None() {
1294 return SideEffects(0);
1295 }
1296
1297 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001298 return SideEffects(kAllChangeBits | kAllDependOnBits);
1299 }
1300
1301 static SideEffects AllChanges() {
1302 return SideEffects(kAllChangeBits);
1303 }
1304
1305 static SideEffects AllDependencies() {
1306 return SideEffects(kAllDependOnBits);
1307 }
1308
1309 static SideEffects AllExceptGCDependency() {
1310 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1311 }
1312
1313 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001314 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001315 }
1316
Aart Bik34c3ba92015-07-20 14:08:59 -07001317 static SideEffects AllWrites() {
1318 return SideEffects(kAllWrites);
1319 }
1320
1321 static SideEffects AllReads() {
1322 return SideEffects(kAllReads);
1323 }
1324
1325 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1326 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001327 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001328 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001329 }
1330
Aart Bik854a02b2015-07-14 16:07:00 -07001331 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1332 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001333 }
1334
Aart Bik34c3ba92015-07-20 14:08:59 -07001335 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1336 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001337 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001338 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001339 }
1340
1341 static SideEffects ArrayReadOfType(Primitive::Type type) {
1342 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1343 }
1344
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001345 static SideEffects CanTriggerGC() {
1346 return SideEffects(1ULL << kCanTriggerGCBit);
1347 }
1348
1349 static SideEffects DependsOnGC() {
1350 return SideEffects(1ULL << kDependsOnGCBit);
1351 }
1352
Aart Bik854a02b2015-07-14 16:07:00 -07001353 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001354 SideEffects Union(SideEffects other) const {
1355 return SideEffects(flags_ | other.flags_);
1356 }
1357
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001358 SideEffects Exclusion(SideEffects other) const {
1359 return SideEffects(flags_ & ~other.flags_);
1360 }
1361
1362 bool Includes(SideEffects other) const {
1363 return (other.flags_ & flags_) == other.flags_;
1364 }
1365
1366 bool HasSideEffects() const {
1367 return (flags_ & kAllChangeBits);
1368 }
1369
1370 bool HasDependencies() const {
1371 return (flags_ & kAllDependOnBits);
1372 }
1373
1374 // Returns true if there are no side effects or dependencies.
1375 bool DoesNothing() const {
1376 return flags_ == 0;
1377 }
1378
Aart Bik854a02b2015-07-14 16:07:00 -07001379 // Returns true if something is written.
1380 bool DoesAnyWrite() const {
1381 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001382 }
1383
Aart Bik854a02b2015-07-14 16:07:00 -07001384 // Returns true if something is read.
1385 bool DoesAnyRead() const {
1386 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001387 }
1388
Aart Bik854a02b2015-07-14 16:07:00 -07001389 // Returns true if potentially everything is written and read
1390 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001391 bool DoesAllReadWrite() const {
1392 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1393 }
1394
Aart Bik854a02b2015-07-14 16:07:00 -07001395 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001396 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001397 }
1398
1399 // Returns true if this may read something written by other.
1400 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001401 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1402 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001403 }
1404
1405 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001406 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001407 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001408 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001409 for (int s = kLastBit; s >= 0; s--) {
1410 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1411 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1412 // This is a bit for the GC side effect.
1413 if (current_bit_is_set) {
1414 flags += "GC";
1415 }
Aart Bik854a02b2015-07-14 16:07:00 -07001416 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001417 } else {
1418 // This is a bit for the array/field analysis.
1419 // The underscore character stands for the 'can trigger GC' bit.
1420 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1421 if (current_bit_is_set) {
1422 flags += kDebug[s];
1423 }
1424 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1425 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1426 flags += "|";
1427 }
1428 }
Aart Bik854a02b2015-07-14 16:07:00 -07001429 }
1430 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001431 }
1432
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001433 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001434
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001435 private:
1436 static constexpr int kFieldArrayAnalysisBits = 9;
1437
1438 static constexpr int kFieldWriteOffset = 0;
1439 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1440 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1441 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1442
1443 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1444
1445 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1446 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1447 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1448 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1449
1450 static constexpr int kLastBit = kDependsOnGCBit;
1451 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1452
1453 // Aliases.
1454
1455 static_assert(kChangeBits == kDependOnBits,
1456 "the 'change' bits should match the 'depend on' bits.");
1457
1458 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1459 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1460 static constexpr uint64_t kAllWrites =
1461 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1462 static constexpr uint64_t kAllReads =
1463 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001464
Aart Bik854a02b2015-07-14 16:07:00 -07001465 // Work around the fact that HIR aliases I/F and J/D.
1466 // TODO: remove this interceptor once HIR types are clean
1467 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1468 switch (type) {
1469 case Primitive::kPrimInt:
1470 case Primitive::kPrimFloat:
1471 return TypeFlag(Primitive::kPrimInt, offset) |
1472 TypeFlag(Primitive::kPrimFloat, offset);
1473 case Primitive::kPrimLong:
1474 case Primitive::kPrimDouble:
1475 return TypeFlag(Primitive::kPrimLong, offset) |
1476 TypeFlag(Primitive::kPrimDouble, offset);
1477 default:
1478 return TypeFlag(type, offset);
1479 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001480 }
1481
Aart Bik854a02b2015-07-14 16:07:00 -07001482 // Translates type to bit flag.
1483 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1484 CHECK_NE(type, Primitive::kPrimVoid);
1485 const uint64_t one = 1;
1486 const int shift = type; // 0-based consecutive enum
1487 DCHECK_LE(kFieldWriteOffset, shift);
1488 DCHECK_LT(shift, kArrayWriteOffset);
1489 return one << (type + offset);
1490 }
1491
1492 // Private constructor on direct flags value.
1493 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1494
1495 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001496};
1497
David Brazdiled596192015-01-23 10:39:45 +00001498// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001499class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001500 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001501 HEnvironment(ArenaAllocator* arena,
1502 size_t number_of_vregs,
1503 const DexFile& dex_file,
1504 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001505 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001506 InvokeType invoke_type,
1507 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001508 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1509 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001510 parent_(nullptr),
1511 dex_file_(dex_file),
1512 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001513 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001514 invoke_type_(invoke_type),
1515 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001516 }
1517
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001518 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001519 : HEnvironment(arena,
1520 to_copy.Size(),
1521 to_copy.GetDexFile(),
1522 to_copy.GetMethodIdx(),
1523 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001524 to_copy.GetInvokeType(),
1525 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001526
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001527 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001528 if (parent_ != nullptr) {
1529 parent_->SetAndCopyParentChain(allocator, parent);
1530 } else {
1531 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1532 parent_->CopyFrom(parent);
1533 if (parent->GetParent() != nullptr) {
1534 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1535 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001536 }
David Brazdiled596192015-01-23 10:39:45 +00001537 }
1538
Vladimir Marko71bf8092015-09-15 15:33:14 +01001539 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001540 void CopyFrom(HEnvironment* environment);
1541
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001542 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1543 // input to the loop phi instead. This is for inserting instructions that
1544 // require an environment (like HDeoptimization) in the loop pre-header.
1545 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001546
1547 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001548 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001549 }
1550
1551 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001552 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001553 }
1554
David Brazdil1abb4192015-02-17 18:33:36 +00001555 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001556
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001557 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001558
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001559 HEnvironment* GetParent() const { return parent_; }
1560
1561 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001562 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001563 }
1564
1565 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001566 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001567 }
1568
1569 uint32_t GetDexPc() const {
1570 return dex_pc_;
1571 }
1572
1573 uint32_t GetMethodIdx() const {
1574 return method_idx_;
1575 }
1576
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001577 InvokeType GetInvokeType() const {
1578 return invoke_type_;
1579 }
1580
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001581 const DexFile& GetDexFile() const {
1582 return dex_file_;
1583 }
1584
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001585 HInstruction* GetHolder() const {
1586 return holder_;
1587 }
1588
David Brazdiled596192015-01-23 10:39:45 +00001589 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001590 // Record instructions' use entries of this environment for constant-time removal.
1591 // It should only be called by HInstruction when a new environment use is added.
1592 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1593 DCHECK(env_use->GetUser() == this);
1594 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001595 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001596 }
David Brazdiled596192015-01-23 10:39:45 +00001597
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001598 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1599 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001600 HEnvironment* parent_;
1601 const DexFile& dex_file_;
1602 const uint32_t method_idx_;
1603 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001604 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001605
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001606 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001607 HInstruction* const holder_;
1608
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001609 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001610
1611 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1612};
1613
Calin Juravleacf735c2015-02-12 15:25:22 +00001614class ReferenceTypeInfo : ValueObject {
1615 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001616 typedef Handle<mirror::Class> TypeHandle;
1617
Calin Juravle2e768302015-07-28 14:41:11 +00001618 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1619 // The constructor will check that the type_handle is valid.
1620 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001621 }
1622
Calin Juravle2e768302015-07-28 14:41:11 +00001623 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1624
1625 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1626 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001627 }
1628
Calin Juravle2e768302015-07-28 14:41:11 +00001629 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1630 return IsValidHandle(type_handle_);
1631 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001632
Calin Juravleacf735c2015-02-12 15:25:22 +00001633 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001634
1635 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1636 DCHECK(IsValid());
1637 return GetTypeHandle()->IsObjectClass();
1638 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001639
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001640 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001641 DCHECK(IsValid());
1642 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001643 }
1644
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001645 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1646 DCHECK(IsValid());
1647 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1648 }
1649
Mathieu Chartier90443472015-07-16 20:32:27 -07001650 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001651 DCHECK(IsValid());
1652 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001653 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001654
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001655 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001656 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001657 return GetTypeHandle()->IsArrayClass();
1658 }
1659
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001660 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1661 DCHECK(IsValid());
1662 return GetTypeHandle()->IsPrimitiveArray();
1663 }
1664
1665 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1666 DCHECK(IsValid());
1667 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1668 }
1669
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001670 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001671 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001672 if (!IsExact()) return false;
1673 if (!IsArrayClass()) return false;
1674 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1675 }
1676
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001677 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1678 DCHECK(IsValid());
1679 if (!IsExact()) return false;
1680 if (!IsArrayClass()) return false;
1681 if (!rti.IsArrayClass()) return false;
1682 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1683 rti.GetTypeHandle()->GetComponentType());
1684 }
1685
Calin Juravleacf735c2015-02-12 15:25:22 +00001686 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1687
Mathieu Chartier90443472015-07-16 20:32:27 -07001688 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001689 DCHECK(IsValid());
1690 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001691 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1692 }
1693
1694 // Returns true if the type information provide the same amount of details.
1695 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001696 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001697 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001698 if (!IsValid() && !rti.IsValid()) {
1699 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001700 return true;
1701 }
Calin Juravle2e768302015-07-28 14:41:11 +00001702 if (!IsValid() || !rti.IsValid()) {
1703 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001704 return false;
1705 }
Calin Juravle2e768302015-07-28 14:41:11 +00001706 return IsExact() == rti.IsExact()
1707 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001708 }
1709
1710 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001711 ReferenceTypeInfo();
1712 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001713
Calin Juravleacf735c2015-02-12 15:25:22 +00001714 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001715 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001716 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001717 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001718 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001719};
1720
1721std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1722
Vladimir Markof9f64412015-09-02 14:05:49 +01001723class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001724 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001725 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001726 : previous_(nullptr),
1727 next_(nullptr),
1728 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001729 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001730 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001731 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001732 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001733 locations_(nullptr),
1734 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001735 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001736 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001737 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001738
Dave Allison20dfc792014-06-16 20:44:29 -07001739 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001740
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001741#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001742 enum InstructionKind {
1743 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1744 };
1745#undef DECLARE_KIND
1746
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001747 HInstruction* GetNext() const { return next_; }
1748 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001749
Calin Juravle77520bc2015-01-12 18:45:46 +00001750 HInstruction* GetNextDisregardingMoves() const;
1751 HInstruction* GetPreviousDisregardingMoves() const;
1752
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001753 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001754 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001755 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001756 bool IsInBlock() const { return block_ != nullptr; }
1757 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001758 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001759
Roland Levillain6b879dd2014-09-22 17:13:44 +01001760 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001761 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001762
1763 virtual void Accept(HGraphVisitor* visitor) = 0;
1764 virtual const char* DebugName() const = 0;
1765
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001766 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001767 void SetRawInputAt(size_t index, HInstruction* input) {
1768 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1769 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001770
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001771 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001772
1773 uint32_t GetDexPc() const { return dex_pc_; }
1774
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001775 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001776
Roland Levillaine161a2a2014-10-03 12:45:18 +01001777 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001778 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001779
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001780 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001781 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001782
Calin Juravle10e244f2015-01-26 18:54:32 +00001783 // Does not apply for all instructions, but having this at top level greatly
1784 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001785 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001786 virtual bool CanBeNull() const {
1787 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1788 return true;
1789 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001790
Calin Juravle641547a2015-04-21 22:08:51 +01001791 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1792 UNUSED(obj);
1793 return false;
1794 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001795
Calin Juravle2e768302015-07-28 14:41:11 +00001796 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001797
Calin Juravle61d544b2015-02-23 16:46:57 +00001798 ReferenceTypeInfo GetReferenceTypeInfo() const {
1799 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1800 return reference_type_info_;
1801 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001802
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001803 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001804 DCHECK(user != nullptr);
1805 HUseListNode<HInstruction*>* use =
1806 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1807 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001808 }
1809
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001810 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001811 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001812 HUseListNode<HEnvironment*>* env_use =
1813 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1814 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001815 }
1816
David Brazdil1abb4192015-02-17 18:33:36 +00001817 void RemoveAsUserOfInput(size_t input) {
1818 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1819 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1820 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001821
David Brazdil1abb4192015-02-17 18:33:36 +00001822 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1823 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001824
David Brazdiled596192015-01-23 10:39:45 +00001825 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1826 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001827 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001828 bool HasOnlyOneNonEnvironmentUse() const {
1829 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1830 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001831
Roland Levillain6c82d402014-10-13 16:10:27 +01001832 // Does this instruction strictly dominate `other_instruction`?
1833 // Returns false if this instruction and `other_instruction` are the same.
1834 // Aborts if this instruction and `other_instruction` are both phis.
1835 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001836
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001837 int GetId() const { return id_; }
1838 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001839
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001840 int GetSsaIndex() const { return ssa_index_; }
1841 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1842 bool HasSsaIndex() const { return ssa_index_ != -1; }
1843
1844 bool HasEnvironment() const { return environment_ != nullptr; }
1845 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001846 // Set the `environment_` field. Raw because this method does not
1847 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001848 void SetRawEnvironment(HEnvironment* environment) {
1849 DCHECK(environment_ == nullptr);
1850 DCHECK_EQ(environment->GetHolder(), this);
1851 environment_ = environment;
1852 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001853
1854 // Set the environment of this instruction, copying it from `environment`. While
1855 // copying, the uses lists are being updated.
1856 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001857 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001858 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001859 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001860 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001861 if (environment->GetParent() != nullptr) {
1862 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1863 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001864 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001865
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001866 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1867 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001868 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001869 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001870 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001871 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001872 if (environment->GetParent() != nullptr) {
1873 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1874 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001875 }
1876
Nicolas Geoffray39468442014-09-02 15:17:15 +01001877 // Returns the number of entries in the environment. Typically, that is the
1878 // number of dex registers in a method. It could be more in case of inlining.
1879 size_t EnvironmentSize() const;
1880
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001881 LocationSummary* GetLocations() const { return locations_; }
1882 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001883
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001884 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001885 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001886
Alexandre Rames188d4312015-04-09 18:30:21 +01001887 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1888 // uses of this instruction by `other` are *not* updated.
1889 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1890 ReplaceWith(other);
1891 other->ReplaceInput(this, use_index);
1892 }
1893
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001894 // Move `this` instruction before `cursor`.
1895 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001896
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001897#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001898 bool Is##type() const { return (As##type() != nullptr); } \
1899 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001900 virtual H##type* As##type() { return nullptr; }
1901
1902 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1903#undef INSTRUCTION_TYPE_CHECK
1904
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001905 // Returns whether the instruction can be moved within the graph.
1906 virtual bool CanBeMoved() const { return false; }
1907
1908 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001909 virtual bool InstructionTypeEquals(HInstruction* other) const {
1910 UNUSED(other);
1911 return false;
1912 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001913
1914 // Returns whether any data encoded in the two instructions is equal.
1915 // This method does not look at the inputs. Both instructions must be
1916 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001917 virtual bool InstructionDataEquals(HInstruction* other) const {
1918 UNUSED(other);
1919 return false;
1920 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001921
1922 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001923 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001924 // 2) Their inputs are identical.
1925 bool Equals(HInstruction* other) const;
1926
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001927 virtual InstructionKind GetKind() const = 0;
1928
1929 virtual size_t ComputeHashCode() const {
1930 size_t result = GetKind();
1931 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1932 result = (result * 31) + InputAt(i)->GetId();
1933 }
1934 return result;
1935 }
1936
1937 SideEffects GetSideEffects() const { return side_effects_; }
1938
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001939 size_t GetLifetimePosition() const { return lifetime_position_; }
1940 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1941 LiveInterval* GetLiveInterval() const { return live_interval_; }
1942 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1943 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1944
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001945 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1946
1947 // Returns whether the code generation of the instruction will require to have access
1948 // to the current method. Such instructions are:
1949 // (1): Instructions that require an environment, as calling the runtime requires
1950 // to walk the stack and have the current method stored at a specific stack address.
1951 // (2): Object literals like classes and strings, that are loaded from the dex cache
1952 // fields of the current method.
1953 bool NeedsCurrentMethod() const {
1954 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1955 }
1956
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001957 virtual bool NeedsDexCache() const { return false; }
1958
Mark Mendellc4701932015-04-10 13:18:51 -04001959 // Does this instruction have any use in an environment before
1960 // control flow hits 'other'?
1961 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1962
1963 // Remove all references to environment uses of this instruction.
1964 // The caller must ensure that this is safe to do.
1965 void RemoveEnvironmentUsers();
1966
David Brazdil1abb4192015-02-17 18:33:36 +00001967 protected:
1968 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1969 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1970
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001971 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001972 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1973
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001974 HInstruction* previous_;
1975 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001976 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001977 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001978
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001979 // An instruction gets an id when it is added to the graph.
1980 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001981 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001982 int id_;
1983
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001984 // When doing liveness analysis, instructions that have uses get an SSA index.
1985 int ssa_index_;
1986
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001987 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001988 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001989
1990 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001991 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001992
Nicolas Geoffray39468442014-09-02 15:17:15 +01001993 // The environment associated with this instruction. Not null if the instruction
1994 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001995 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001996
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001997 // Set by the code generator.
1998 LocationSummary* locations_;
1999
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002000 // Set by the liveness analysis.
2001 LiveInterval* live_interval_;
2002
2003 // Set by the liveness analysis, this is the position in a linear
2004 // order of blocks where this instruction's live interval start.
2005 size_t lifetime_position_;
2006
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002007 const SideEffects side_effects_;
2008
Calin Juravleacf735c2015-02-12 15:25:22 +00002009 // TODO: for primitive types this should be marked as invalid.
2010 ReferenceTypeInfo reference_type_info_;
2011
David Brazdil1abb4192015-02-17 18:33:36 +00002012 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002013 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002014 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002015 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002016 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002017
2018 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2019};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002020std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002021
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002022class HInputIterator : public ValueObject {
2023 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002024 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002025
2026 bool Done() const { return index_ == instruction_->InputCount(); }
2027 HInstruction* Current() const { return instruction_->InputAt(index_); }
2028 void Advance() { index_++; }
2029
2030 private:
2031 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002032 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002033
2034 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2035};
2036
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002037class HInstructionIterator : public ValueObject {
2038 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002039 explicit HInstructionIterator(const HInstructionList& instructions)
2040 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002041 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002042 }
2043
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002044 bool Done() const { return instruction_ == nullptr; }
2045 HInstruction* Current() const { return instruction_; }
2046 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002047 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002048 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002049 }
2050
2051 private:
2052 HInstruction* instruction_;
2053 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002054
2055 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002056};
2057
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002058class HBackwardInstructionIterator : public ValueObject {
2059 public:
2060 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2061 : instruction_(instructions.last_instruction_) {
2062 next_ = Done() ? nullptr : instruction_->GetPrevious();
2063 }
2064
2065 bool Done() const { return instruction_ == nullptr; }
2066 HInstruction* Current() const { return instruction_; }
2067 void Advance() {
2068 instruction_ = next_;
2069 next_ = Done() ? nullptr : instruction_->GetPrevious();
2070 }
2071
2072 private:
2073 HInstruction* instruction_;
2074 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002075
2076 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002077};
2078
Vladimir Markof9f64412015-09-02 14:05:49 +01002079template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002080class HTemplateInstruction: public HInstruction {
2081 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002082 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002083 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002084 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002085
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002086 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002087
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002088 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002089 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2090 DCHECK_LT(i, N);
2091 return inputs_[i];
2092 }
David Brazdil1abb4192015-02-17 18:33:36 +00002093
2094 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002095 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002096 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002097 }
2098
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002099 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002100 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002101
2102 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002103};
2104
Vladimir Markof9f64412015-09-02 14:05:49 +01002105// HTemplateInstruction specialization for N=0.
2106template<>
2107class HTemplateInstruction<0>: public HInstruction {
2108 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002109 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002110 : HInstruction(side_effects, dex_pc) {}
2111
Vladimir Markof9f64412015-09-02 14:05:49 +01002112 virtual ~HTemplateInstruction() {}
2113
2114 size_t InputCount() const OVERRIDE { return 0; }
2115
2116 protected:
2117 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2118 LOG(FATAL) << "Unreachable";
2119 UNREACHABLE();
2120 }
2121
2122 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2123 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2124 LOG(FATAL) << "Unreachable";
2125 UNREACHABLE();
2126 }
2127
2128 private:
2129 friend class SsaBuilder;
2130};
2131
Dave Allison20dfc792014-06-16 20:44:29 -07002132template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002133class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002134 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002135 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002136 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002137 virtual ~HExpression() {}
2138
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002139 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002140
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002141 protected:
2142 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002143};
2144
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002145// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2146// instruction that branches to the exit block.
2147class HReturnVoid : public HTemplateInstruction<0> {
2148 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002149 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2150 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002151
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002152 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002153
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002154 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002155
2156 private:
2157 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2158};
2159
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002160// Represents dex's RETURN opcodes. A HReturn is a control flow
2161// instruction that branches to the exit block.
2162class HReturn : public HTemplateInstruction<1> {
2163 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002164 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2165 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002166 SetRawInputAt(0, value);
2167 }
2168
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002169 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002170
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002171 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002172
2173 private:
2174 DISALLOW_COPY_AND_ASSIGN(HReturn);
2175};
2176
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002177// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002178// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002179// exit block.
2180class HExit : public HTemplateInstruction<0> {
2181 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002182 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002183
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002184 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002185
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002186 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002187
2188 private:
2189 DISALLOW_COPY_AND_ASSIGN(HExit);
2190};
2191
2192// Jumps from one block to another.
2193class HGoto : public HTemplateInstruction<0> {
2194 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002195 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002196
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002197 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002198
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002199 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002200 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002201 }
2202
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002203 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002204
2205 private:
2206 DISALLOW_COPY_AND_ASSIGN(HGoto);
2207};
2208
Roland Levillain9867bc72015-08-05 10:21:34 +01002209class HConstant : public HExpression<0> {
2210 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002211 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2212 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002213
2214 bool CanBeMoved() const OVERRIDE { return true; }
2215
2216 virtual bool IsMinusOne() const { return false; }
2217 virtual bool IsZero() const { return false; }
2218 virtual bool IsOne() const { return false; }
2219
David Brazdil77a48ae2015-09-15 12:34:04 +00002220 virtual uint64_t GetValueAsUint64() const = 0;
2221
Roland Levillain9867bc72015-08-05 10:21:34 +01002222 DECLARE_INSTRUCTION(Constant);
2223
2224 private:
2225 DISALLOW_COPY_AND_ASSIGN(HConstant);
2226};
2227
2228class HNullConstant : public HConstant {
2229 public:
2230 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2231 return true;
2232 }
2233
David Brazdil77a48ae2015-09-15 12:34:04 +00002234 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2235
Roland Levillain9867bc72015-08-05 10:21:34 +01002236 size_t ComputeHashCode() const OVERRIDE { return 0; }
2237
2238 DECLARE_INSTRUCTION(NullConstant);
2239
2240 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002241 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002242
2243 friend class HGraph;
2244 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2245};
2246
2247// Constants of the type int. Those can be from Dex instructions, or
2248// synthesized (for example with the if-eqz instruction).
2249class HIntConstant : public HConstant {
2250 public:
2251 int32_t GetValue() const { return value_; }
2252
David Brazdil9f389d42015-10-01 14:32:56 +01002253 uint64_t GetValueAsUint64() const OVERRIDE {
2254 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2255 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002256
Roland Levillain9867bc72015-08-05 10:21:34 +01002257 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2258 DCHECK(other->IsIntConstant());
2259 return other->AsIntConstant()->value_ == value_;
2260 }
2261
2262 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2263
2264 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2265 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2266 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2267
2268 DECLARE_INSTRUCTION(IntConstant);
2269
2270 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002271 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2272 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2273 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2274 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002275
2276 const int32_t value_;
2277
2278 friend class HGraph;
2279 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2280 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2281 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2282};
2283
2284class HLongConstant : public HConstant {
2285 public:
2286 int64_t GetValue() const { return value_; }
2287
David Brazdil77a48ae2015-09-15 12:34:04 +00002288 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2289
Roland Levillain9867bc72015-08-05 10:21:34 +01002290 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2291 DCHECK(other->IsLongConstant());
2292 return other->AsLongConstant()->value_ == value_;
2293 }
2294
2295 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2296
2297 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2298 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2299 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2300
2301 DECLARE_INSTRUCTION(LongConstant);
2302
2303 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002304 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2305 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002306
2307 const int64_t value_;
2308
2309 friend class HGraph;
2310 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2311};
Dave Allison20dfc792014-06-16 20:44:29 -07002312
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002313// Conditional branch. A block ending with an HIf instruction must have
2314// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002315class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002316 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002317 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2318 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002319 SetRawInputAt(0, input);
2320 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002321
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002322 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002323
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002324 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002325 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002326 }
2327
2328 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002329 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002330 }
2331
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002332 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002333
2334 private:
2335 DISALLOW_COPY_AND_ASSIGN(HIf);
2336};
2337
David Brazdilfc6a86a2015-06-26 10:33:45 +00002338
2339// Abstract instruction which marks the beginning and/or end of a try block and
2340// links it to the respective exception handlers. Behaves the same as a Goto in
2341// non-exceptional control flow.
2342// Normal-flow successor is stored at index zero, exception handlers under
2343// higher indices in no particular order.
2344class HTryBoundary : public HTemplateInstruction<0> {
2345 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002346 enum BoundaryKind {
2347 kEntry,
2348 kExit,
2349 };
2350
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002351 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2352 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002353
2354 bool IsControlFlow() const OVERRIDE { return true; }
2355
2356 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002357 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002358
2359 // Returns whether `handler` is among its exception handlers (non-zero index
2360 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002361 bool HasExceptionHandler(const HBasicBlock& handler) const {
2362 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002363 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002364 }
2365
2366 // If not present already, adds `handler` to its block's list of exception
2367 // handlers.
2368 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002369 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002370 GetBlock()->AddSuccessor(handler);
2371 }
2372 }
2373
David Brazdil56e1acc2015-06-30 15:41:36 +01002374 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002375
David Brazdilffee3d32015-07-06 11:48:53 +01002376 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2377
David Brazdilfc6a86a2015-06-26 10:33:45 +00002378 DECLARE_INSTRUCTION(TryBoundary);
2379
2380 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002381 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002382
2383 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2384};
2385
David Brazdilffee3d32015-07-06 11:48:53 +01002386// Iterator over exception handlers of a given HTryBoundary, i.e. over
2387// exceptional successors of its basic block.
2388class HExceptionHandlerIterator : public ValueObject {
2389 public:
2390 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2391 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2392
Vladimir Marko60584552015-09-03 13:35:12 +00002393 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002394 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002395 size_t CurrentSuccessorIndex() const { return index_; }
2396 void Advance() { ++index_; }
2397
2398 private:
2399 const HBasicBlock& block_;
2400 size_t index_;
2401
2402 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2403};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002404
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002405// Deoptimize to interpreter, upon checking a condition.
2406class HDeoptimize : public HTemplateInstruction<1> {
2407 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002408 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2409 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002410 SetRawInputAt(0, cond);
2411 }
2412
2413 bool NeedsEnvironment() const OVERRIDE { return true; }
2414 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002415
2416 DECLARE_INSTRUCTION(Deoptimize);
2417
2418 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002419 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2420};
2421
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002422// Represents the ArtMethod that was passed as a first argument to
2423// the method. It is used by instructions that depend on it, like
2424// instructions that work with the dex cache.
2425class HCurrentMethod : public HExpression<0> {
2426 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002427 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2428 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002429
2430 DECLARE_INSTRUCTION(CurrentMethod);
2431
2432 private:
2433 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2434};
2435
Mark Mendellfe57faa2015-09-18 09:26:15 -04002436// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2437// have one successor for each entry in the switch table, and the final successor
2438// will be the block containing the next Dex opcode.
2439class HPackedSwitch : public HTemplateInstruction<1> {
2440 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002441 HPackedSwitch(int32_t start_value,
2442 uint32_t num_entries,
2443 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002444 uint32_t dex_pc = kNoDexPc)
2445 : HTemplateInstruction(SideEffects::None(), dex_pc),
2446 start_value_(start_value),
2447 num_entries_(num_entries) {
2448 SetRawInputAt(0, input);
2449 }
2450
2451 bool IsControlFlow() const OVERRIDE { return true; }
2452
2453 int32_t GetStartValue() const { return start_value_; }
2454
Vladimir Marko211c2112015-09-24 16:52:33 +01002455 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002456
2457 HBasicBlock* GetDefaultBlock() const {
2458 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002459 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002460 }
2461 DECLARE_INSTRUCTION(PackedSwitch);
2462
2463 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002464 const int32_t start_value_;
2465 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002466
2467 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2468};
2469
Roland Levillain88cb1752014-10-20 16:36:47 +01002470class HUnaryOperation : public HExpression<1> {
2471 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002472 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2473 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002474 SetRawInputAt(0, input);
2475 }
2476
2477 HInstruction* GetInput() const { return InputAt(0); }
2478 Primitive::Type GetResultType() const { return GetType(); }
2479
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002480 bool CanBeMoved() const OVERRIDE { return true; }
2481 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002482 UNUSED(other);
2483 return true;
2484 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002485
Roland Levillain9240d6a2014-10-20 16:47:04 +01002486 // Try to statically evaluate `operation` and return a HConstant
2487 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002488 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002489 HConstant* TryStaticEvaluation() const;
2490
2491 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002492 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2493 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002494
Roland Levillain88cb1752014-10-20 16:36:47 +01002495 DECLARE_INSTRUCTION(UnaryOperation);
2496
2497 private:
2498 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2499};
2500
Dave Allison20dfc792014-06-16 20:44:29 -07002501class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002502 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002503 HBinaryOperation(Primitive::Type result_type,
2504 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002505 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002506 SideEffects side_effects = SideEffects::None(),
2507 uint32_t dex_pc = kNoDexPc)
2508 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002509 SetRawInputAt(0, left);
2510 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002511 }
2512
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002513 HInstruction* GetLeft() const { return InputAt(0); }
2514 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002515 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002516
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002517 virtual bool IsCommutative() const { return false; }
2518
2519 // Put constant on the right.
2520 // Returns whether order is changed.
2521 bool OrderInputsWithConstantOnTheRight() {
2522 HInstruction* left = InputAt(0);
2523 HInstruction* right = InputAt(1);
2524 if (left->IsConstant() && !right->IsConstant()) {
2525 ReplaceInput(right, 0);
2526 ReplaceInput(left, 1);
2527 return true;
2528 }
2529 return false;
2530 }
2531
2532 // Order inputs by instruction id, but favor constant on the right side.
2533 // This helps GVN for commutative ops.
2534 void OrderInputs() {
2535 DCHECK(IsCommutative());
2536 HInstruction* left = InputAt(0);
2537 HInstruction* right = InputAt(1);
2538 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2539 return;
2540 }
2541 if (OrderInputsWithConstantOnTheRight()) {
2542 return;
2543 }
2544 // Order according to instruction id.
2545 if (left->GetId() > right->GetId()) {
2546 ReplaceInput(right, 0);
2547 ReplaceInput(left, 1);
2548 }
2549 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002550
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002551 bool CanBeMoved() const OVERRIDE { return true; }
2552 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002553 UNUSED(other);
2554 return true;
2555 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002556
Roland Levillain9240d6a2014-10-20 16:47:04 +01002557 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002558 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002559 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002560 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002561
2562 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002563 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2564 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2565 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2566 HLongConstant* y ATTRIBUTE_UNUSED) const {
2567 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2568 return nullptr;
2569 }
2570 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2571 HIntConstant* y ATTRIBUTE_UNUSED) const {
2572 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2573 return nullptr;
2574 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002575
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002576 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002577 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002578 HConstant* GetConstantRight() const;
2579
2580 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002581 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002582 HInstruction* GetLeastConstantLeft() const;
2583
Roland Levillainccc07a92014-09-16 14:48:16 +01002584 DECLARE_INSTRUCTION(BinaryOperation);
2585
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002586 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002587 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2588};
2589
Mark Mendellc4701932015-04-10 13:18:51 -04002590// The comparison bias applies for floating point operations and indicates how NaN
2591// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002592enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002593 kNoBias, // bias is not applicable (i.e. for long operation)
2594 kGtBias, // return 1 for NaN comparisons
2595 kLtBias, // return -1 for NaN comparisons
2596};
2597
Dave Allison20dfc792014-06-16 20:44:29 -07002598class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002599 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002600 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2601 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002602 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002603 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002604
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002605 bool NeedsMaterialization() const { return needs_materialization_; }
2606 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002607
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002608 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002609 // `instruction`, and disregard moves in between.
2610 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002611
Dave Allison20dfc792014-06-16 20:44:29 -07002612 DECLARE_INSTRUCTION(Condition);
2613
2614 virtual IfCondition GetCondition() const = 0;
2615
Mark Mendellc4701932015-04-10 13:18:51 -04002616 virtual IfCondition GetOppositeCondition() const = 0;
2617
Roland Levillain4fa13f62015-07-06 18:11:54 +01002618 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002619
2620 void SetBias(ComparisonBias bias) { bias_ = bias; }
2621
2622 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2623 return bias_ == other->AsCondition()->bias_;
2624 }
2625
Roland Levillain4fa13f62015-07-06 18:11:54 +01002626 bool IsFPConditionTrueIfNaN() const {
2627 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2628 IfCondition if_cond = GetCondition();
2629 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2630 }
2631
2632 bool IsFPConditionFalseIfNaN() const {
2633 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2634 IfCondition if_cond = GetCondition();
2635 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2636 }
2637
Dave Allison20dfc792014-06-16 20:44:29 -07002638 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002639 // For register allocation purposes, returns whether this instruction needs to be
2640 // materialized (that is, not just be in the processor flags).
2641 bool needs_materialization_;
2642
Mark Mendellc4701932015-04-10 13:18:51 -04002643 // Needed if we merge a HCompare into a HCondition.
2644 ComparisonBias bias_;
2645
Dave Allison20dfc792014-06-16 20:44:29 -07002646 DISALLOW_COPY_AND_ASSIGN(HCondition);
2647};
2648
2649// Instruction to check if two inputs are equal to each other.
2650class HEqual : public HCondition {
2651 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002652 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2653 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002654
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002655 bool IsCommutative() const OVERRIDE { return true; }
2656
Roland Levillain9867bc72015-08-05 10:21:34 +01002657 template <typename T> bool Compute(T x, T y) const { return x == y; }
2658
2659 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002660 return GetBlock()->GetGraph()->GetIntConstant(
2661 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002662 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002663 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002664 return GetBlock()->GetGraph()->GetIntConstant(
2665 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002666 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002667
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002668 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002669
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002670 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002671 return kCondEQ;
2672 }
2673
Mark Mendellc4701932015-04-10 13:18:51 -04002674 IfCondition GetOppositeCondition() const OVERRIDE {
2675 return kCondNE;
2676 }
2677
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002678 private:
2679 DISALLOW_COPY_AND_ASSIGN(HEqual);
2680};
2681
Dave Allison20dfc792014-06-16 20:44:29 -07002682class HNotEqual : public HCondition {
2683 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002684 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2685 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002686
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002687 bool IsCommutative() const OVERRIDE { return true; }
2688
Roland Levillain9867bc72015-08-05 10:21:34 +01002689 template <typename T> bool Compute(T x, T y) const { return x != y; }
2690
2691 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002692 return GetBlock()->GetGraph()->GetIntConstant(
2693 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002694 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002695 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002696 return GetBlock()->GetGraph()->GetIntConstant(
2697 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002698 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002699
Dave Allison20dfc792014-06-16 20:44:29 -07002700 DECLARE_INSTRUCTION(NotEqual);
2701
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002702 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002703 return kCondNE;
2704 }
2705
Mark Mendellc4701932015-04-10 13:18:51 -04002706 IfCondition GetOppositeCondition() const OVERRIDE {
2707 return kCondEQ;
2708 }
2709
Dave Allison20dfc792014-06-16 20:44:29 -07002710 private:
2711 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2712};
2713
2714class HLessThan : public HCondition {
2715 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002716 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2717 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002718
Roland Levillain9867bc72015-08-05 10:21:34 +01002719 template <typename T> bool Compute(T x, T y) const { return x < y; }
2720
2721 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002722 return GetBlock()->GetGraph()->GetIntConstant(
2723 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002724 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002725 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002726 return GetBlock()->GetGraph()->GetIntConstant(
2727 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002728 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002729
Dave Allison20dfc792014-06-16 20:44:29 -07002730 DECLARE_INSTRUCTION(LessThan);
2731
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002732 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002733 return kCondLT;
2734 }
2735
Mark Mendellc4701932015-04-10 13:18:51 -04002736 IfCondition GetOppositeCondition() const OVERRIDE {
2737 return kCondGE;
2738 }
2739
Dave Allison20dfc792014-06-16 20:44:29 -07002740 private:
2741 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2742};
2743
2744class HLessThanOrEqual : public HCondition {
2745 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002746 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2747 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002748
Roland Levillain9867bc72015-08-05 10:21:34 +01002749 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2750
2751 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002752 return GetBlock()->GetGraph()->GetIntConstant(
2753 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002754 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002755 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002756 return GetBlock()->GetGraph()->GetIntConstant(
2757 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002758 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002759
Dave Allison20dfc792014-06-16 20:44:29 -07002760 DECLARE_INSTRUCTION(LessThanOrEqual);
2761
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002762 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002763 return kCondLE;
2764 }
2765
Mark Mendellc4701932015-04-10 13:18:51 -04002766 IfCondition GetOppositeCondition() const OVERRIDE {
2767 return kCondGT;
2768 }
2769
Dave Allison20dfc792014-06-16 20:44:29 -07002770 private:
2771 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2772};
2773
2774class HGreaterThan : public HCondition {
2775 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002776 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2777 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002778
Roland Levillain9867bc72015-08-05 10:21:34 +01002779 template <typename T> bool Compute(T x, T y) const { return x > y; }
2780
2781 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002782 return GetBlock()->GetGraph()->GetIntConstant(
2783 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002784 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002785 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002786 return GetBlock()->GetGraph()->GetIntConstant(
2787 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002788 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002789
Dave Allison20dfc792014-06-16 20:44:29 -07002790 DECLARE_INSTRUCTION(GreaterThan);
2791
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002792 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002793 return kCondGT;
2794 }
2795
Mark Mendellc4701932015-04-10 13:18:51 -04002796 IfCondition GetOppositeCondition() const OVERRIDE {
2797 return kCondLE;
2798 }
2799
Dave Allison20dfc792014-06-16 20:44:29 -07002800 private:
2801 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2802};
2803
2804class HGreaterThanOrEqual : public HCondition {
2805 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002806 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2807 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002808
Roland Levillain9867bc72015-08-05 10:21:34 +01002809 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2810
2811 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002812 return GetBlock()->GetGraph()->GetIntConstant(
2813 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002814 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002815 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002816 return GetBlock()->GetGraph()->GetIntConstant(
2817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002818 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002819
Dave Allison20dfc792014-06-16 20:44:29 -07002820 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2821
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002822 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002823 return kCondGE;
2824 }
2825
Mark Mendellc4701932015-04-10 13:18:51 -04002826 IfCondition GetOppositeCondition() const OVERRIDE {
2827 return kCondLT;
2828 }
2829
Dave Allison20dfc792014-06-16 20:44:29 -07002830 private:
2831 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2832};
2833
2834
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002835// Instruction to check how two inputs compare to each other.
2836// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2837class HCompare : public HBinaryOperation {
2838 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002839 HCompare(Primitive::Type type,
2840 HInstruction* first,
2841 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002842 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002843 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002844 : HBinaryOperation(Primitive::kPrimInt,
2845 first,
2846 second,
2847 SideEffectsForArchRuntimeCalls(type),
2848 dex_pc),
2849 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002850 DCHECK_EQ(type, first->GetType());
2851 DCHECK_EQ(type, second->GetType());
2852 }
2853
Roland Levillain9867bc72015-08-05 10:21:34 +01002854 template <typename T>
2855 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002856
Roland Levillain9867bc72015-08-05 10:21:34 +01002857 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002858 return GetBlock()->GetGraph()->GetIntConstant(
2859 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01002860 }
2861 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002862 return GetBlock()->GetGraph()->GetIntConstant(
2863 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01002864 }
2865
Calin Juravleddb7df22014-11-25 20:56:51 +00002866 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2867 return bias_ == other->AsCompare()->bias_;
2868 }
2869
Mark Mendellc4701932015-04-10 13:18:51 -04002870 ComparisonBias GetBias() const { return bias_; }
2871
Roland Levillain4fa13f62015-07-06 18:11:54 +01002872 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002873
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002874
2875 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
2876 // MIPS64 uses a runtime call for FP comparisons.
2877 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
2878 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002879
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002880 DECLARE_INSTRUCTION(Compare);
2881
2882 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002883 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002884
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002885 DISALLOW_COPY_AND_ASSIGN(HCompare);
2886};
2887
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002888// A local in the graph. Corresponds to a Dex register.
2889class HLocal : public HTemplateInstruction<0> {
2890 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002891 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002892 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002893
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002894 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002895
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002896 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002897
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002898 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002899 // The Dex register number.
2900 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002901
2902 DISALLOW_COPY_AND_ASSIGN(HLocal);
2903};
2904
2905// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002906class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002907 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002908 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2909 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002910 SetRawInputAt(0, local);
2911 }
2912
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002913 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2914
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002915 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002916
2917 private:
2918 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2919};
2920
2921// Store a value in a given local. This instruction has two inputs: the value
2922// and the local.
2923class HStoreLocal : public HTemplateInstruction<2> {
2924 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002925 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
2926 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002927 SetRawInputAt(0, local);
2928 SetRawInputAt(1, value);
2929 }
2930
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002931 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2932
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002933 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002934
2935 private:
2936 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2937};
2938
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002939class HFloatConstant : public HConstant {
2940 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002941 float GetValue() const { return value_; }
2942
David Brazdil77a48ae2015-09-15 12:34:04 +00002943 uint64_t GetValueAsUint64() const OVERRIDE {
2944 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2945 }
2946
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002947 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002948 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002949 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002950 }
2951
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002952 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002953
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002954 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002955 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002956 }
2957 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002958 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002959 }
2960 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002961 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2962 }
2963 bool IsNaN() const {
2964 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002965 }
2966
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002967 DECLARE_INSTRUCTION(FloatConstant);
2968
2969 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002970 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2971 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2972 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2973 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002974
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002975 const float value_;
2976
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002977 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002978 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002979 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002980 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2981};
2982
2983class HDoubleConstant : public HConstant {
2984 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002985 double GetValue() const { return value_; }
2986
David Brazdil77a48ae2015-09-15 12:34:04 +00002987 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2988
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002989 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002990 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00002991 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002992 }
2993
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002994 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002995
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002996 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002997 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002998 }
2999 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003000 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003001 }
3002 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003003 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3004 }
3005 bool IsNaN() const {
3006 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003007 }
3008
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003009 DECLARE_INSTRUCTION(DoubleConstant);
3010
3011 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003012 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3013 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3014 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3015 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003016
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003017 const double value_;
3018
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003019 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003020 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003021 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003022 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3023};
3024
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003025enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003026#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003027#include "intrinsics_list.h"
3028 kNone,
3029 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3030#undef INTRINSICS_LIST
3031#undef OPTIMIZING_INTRINSICS
3032};
3033std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3034
Agi Csaki05f20562015-08-19 14:58:14 -07003035enum IntrinsicNeedsEnvironmentOrCache {
3036 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3037 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003038};
3039
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003040class HInvoke : public HInstruction {
3041 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003042 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003043
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003044 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003045
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003046 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003047 SetRawInputAt(index, argument);
3048 }
3049
Roland Levillain3e3d7332015-04-28 11:00:54 +01003050 // Return the number of arguments. This number can be lower than
3051 // the number of inputs returned by InputCount(), as some invoke
3052 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3053 // inputs at the end of their list of inputs.
3054 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3055
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003056 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003057
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003058
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003059 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003060 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003061
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003062 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3063
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003064 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003065 return intrinsic_;
3066 }
3067
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003068 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003069
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003070 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003071 return GetEnvironment()->GetParent() != nullptr;
3072 }
3073
3074 bool CanThrow() const OVERRIDE { return true; }
3075
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003076 uint32_t* GetIntrinsicOptimizations() {
3077 return &intrinsic_optimizations_;
3078 }
3079
3080 const uint32_t* GetIntrinsicOptimizations() const {
3081 return &intrinsic_optimizations_;
3082 }
3083
3084 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3085
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003086 DECLARE_INSTRUCTION(Invoke);
3087
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003088 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003089 HInvoke(ArenaAllocator* arena,
3090 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003091 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003092 Primitive::Type return_type,
3093 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003094 uint32_t dex_method_index,
3095 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003096 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003097 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003098 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003099 inputs_(number_of_arguments + number_of_other_inputs,
3100 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003101 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003102 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003103 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003104 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003105 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003106 }
3107
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003108 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003109 return inputs_[index];
3110 }
3111
David Brazdil1abb4192015-02-17 18:33:36 +00003112 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003113 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003114 }
3115
Roland Levillain3e3d7332015-04-28 11:00:54 +01003116 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003117 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003118 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003119 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003120 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003121 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003122
3123 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3124 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003125
3126 private:
3127 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3128};
3129
Calin Juravle175dc732015-08-25 15:42:32 +01003130class HInvokeUnresolved : public HInvoke {
3131 public:
3132 HInvokeUnresolved(ArenaAllocator* arena,
3133 uint32_t number_of_arguments,
3134 Primitive::Type return_type,
3135 uint32_t dex_pc,
3136 uint32_t dex_method_index,
3137 InvokeType invoke_type)
3138 : HInvoke(arena,
3139 number_of_arguments,
3140 0u /* number_of_other_inputs */,
3141 return_type,
3142 dex_pc,
3143 dex_method_index,
3144 invoke_type) {
3145 }
3146
3147 DECLARE_INSTRUCTION(InvokeUnresolved);
3148
3149 private:
3150 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3151};
3152
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003153class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003154 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003155 // Requirements of this method call regarding the class
3156 // initialization (clinit) check of its declaring class.
3157 enum class ClinitCheckRequirement {
3158 kNone, // Class already initialized.
3159 kExplicit, // Static call having explicit clinit check as last input.
3160 kImplicit, // Static call implicitly requiring a clinit check.
3161 };
3162
Vladimir Marko58155012015-08-19 12:49:41 +00003163 // Determines how to load the target ArtMethod*.
3164 enum class MethodLoadKind {
3165 // Use a String init ArtMethod* loaded from Thread entrypoints.
3166 kStringInit,
3167
3168 // Use the method's own ArtMethod* loaded by the register allocator.
3169 kRecursive,
3170
3171 // Use ArtMethod* at a known address, embed the direct address in the code.
3172 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3173 kDirectAddress,
3174
3175 // Use ArtMethod* at an address that will be known at link time, embed the direct
3176 // address in the code. If the image is relocatable, emit .patch_oat entry.
3177 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3178 // the image relocatable or not.
3179 kDirectAddressWithFixup,
3180
3181 // Load from resoved methods array in the dex cache using a PC-relative load.
3182 // Used when we need to use the dex cache, for example for invoke-static that
3183 // may cause class initialization (the entry may point to a resolution method),
3184 // and we know that we can access the dex cache arrays using a PC-relative load.
3185 kDexCachePcRelative,
3186
3187 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3188 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3189 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3190 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3191 kDexCacheViaMethod,
3192 };
3193
3194 // Determines the location of the code pointer.
3195 enum class CodePtrLocation {
3196 // Recursive call, use local PC-relative call instruction.
3197 kCallSelf,
3198
3199 // Use PC-relative call instruction patched at link time.
3200 // Used for calls within an oat file, boot->boot or app->app.
3201 kCallPCRelative,
3202
3203 // Call to a known target address, embed the direct address in code.
3204 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3205 kCallDirect,
3206
3207 // Call to a target address that will be known at link time, embed the direct
3208 // address in code. If the image is relocatable, emit .patch_oat entry.
3209 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3210 // the image relocatable or not.
3211 kCallDirectWithFixup,
3212
3213 // Use code pointer from the ArtMethod*.
3214 // Used when we don't know the target code. This is also the last-resort-kind used when
3215 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3216 kCallArtMethod,
3217 };
3218
3219 struct DispatchInfo {
3220 const MethodLoadKind method_load_kind;
3221 const CodePtrLocation code_ptr_location;
3222 // The method load data holds
3223 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3224 // Note that there are multiple string init methods, each having its own offset.
3225 // - the method address for kDirectAddress
3226 // - the dex cache arrays offset for kDexCachePcRel.
3227 const uint64_t method_load_data;
3228 const uint64_t direct_code_ptr;
3229 };
3230
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003231 HInvokeStaticOrDirect(ArenaAllocator* arena,
3232 uint32_t number_of_arguments,
3233 Primitive::Type return_type,
3234 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003235 uint32_t method_index,
3236 MethodReference target_method,
3237 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003238 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003239 InvokeType invoke_type,
3240 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003241 : HInvoke(arena,
3242 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003243 // There is one extra argument for the HCurrentMethod node, and
3244 // potentially one other if the clinit check is explicit, and one other
3245 // if the method is a string factory.
3246 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
Vladimir Marko58155012015-08-19 12:49:41 +00003247 + (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003248 return_type,
3249 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003250 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003251 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003252 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003253 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003254 target_method_(target_method),
3255 dispatch_info_(dispatch_info) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003256
Calin Juravle641547a2015-04-21 22:08:51 +01003257 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3258 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003259 // We access the method via the dex cache so we can't do an implicit null check.
3260 // TODO: for intrinsics we can generate implicit null checks.
3261 return false;
3262 }
3263
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003264 bool CanBeNull() const OVERRIDE {
3265 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3266 }
3267
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003268 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003269 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3270 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3271 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003272 bool NeedsDexCache() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003273 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003274 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003275 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
3276 bool HasPcRelDexCache() const { return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative; }
3277 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3278 MethodReference GetTargetMethod() const { return target_method_; }
3279
3280 int32_t GetStringInitOffset() const {
3281 DCHECK(IsStringInit());
3282 return dispatch_info_.method_load_data;
3283 }
3284
3285 uint64_t GetMethodAddress() const {
3286 DCHECK(HasMethodAddress());
3287 return dispatch_info_.method_load_data;
3288 }
3289
3290 uint32_t GetDexCacheArrayOffset() const {
3291 DCHECK(HasPcRelDexCache());
3292 return dispatch_info_.method_load_data;
3293 }
3294
3295 uint64_t GetDirectCodePtr() const {
3296 DCHECK(HasDirectCodePtr());
3297 return dispatch_info_.direct_code_ptr;
3298 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003299
Calin Juravle68ad6492015-08-18 17:08:12 +01003300 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3301
Roland Levillain4c0eb422015-04-24 16:43:49 +01003302 // Is this instruction a call to a static method?
3303 bool IsStatic() const {
3304 return GetInvokeType() == kStatic;
3305 }
3306
Roland Levillain3e3d7332015-04-28 11:00:54 +01003307 // Remove the art::HLoadClass instruction set as last input by
3308 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3309 // the initial art::HClinitCheck instruction (only relevant for
3310 // static calls with explicit clinit check).
3311 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003312 DCHECK(IsStaticWithExplicitClinitCheck());
3313 size_t last_input_index = InputCount() - 1;
3314 HInstruction* last_input = InputAt(last_input_index);
3315 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003316 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003317 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003318 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003319 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3320 DCHECK(IsStaticWithImplicitClinitCheck());
3321 }
3322
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003323 bool IsStringFactoryFor(HFakeString* str) const {
3324 if (!IsStringInit()) return false;
3325 // +1 for the current method.
3326 if (InputCount() == (number_of_arguments_ + 1)) return false;
3327 return InputAt(InputCount() - 1)->AsFakeString() == str;
3328 }
3329
3330 void RemoveFakeStringArgumentAsLastInput() {
3331 DCHECK(IsStringInit());
3332 size_t last_input_index = InputCount() - 1;
3333 HInstruction* last_input = InputAt(last_input_index);
3334 DCHECK(last_input != nullptr);
3335 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3336 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003337 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003338 }
3339
Roland Levillain4c0eb422015-04-24 16:43:49 +01003340 // Is this a call to a static method whose declaring class has an
3341 // explicit intialization check in the graph?
3342 bool IsStaticWithExplicitClinitCheck() const {
3343 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3344 }
3345
3346 // Is this a call to a static method whose declaring class has an
3347 // implicit intialization check requirement?
3348 bool IsStaticWithImplicitClinitCheck() const {
3349 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3350 }
3351
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003352 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003353
Roland Levillain4c0eb422015-04-24 16:43:49 +01003354 protected:
3355 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3356 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3357 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3358 HInstruction* input = input_record.GetInstruction();
3359 // `input` is the last input of a static invoke marked as having
3360 // an explicit clinit check. It must either be:
3361 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3362 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3363 DCHECK(input != nullptr);
3364 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3365 }
3366 return input_record;
3367 }
3368
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003369 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003370 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003371 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003372 // The target method may refer to different dex file or method index than the original
3373 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3374 // in derived class to increase visibility.
3375 MethodReference target_method_;
3376 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003377
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003378 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003379};
3380
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003381class HInvokeVirtual : public HInvoke {
3382 public:
3383 HInvokeVirtual(ArenaAllocator* arena,
3384 uint32_t number_of_arguments,
3385 Primitive::Type return_type,
3386 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003387 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003388 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003389 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003390 vtable_index_(vtable_index) {}
3391
Calin Juravle641547a2015-04-21 22:08:51 +01003392 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003393 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003394 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003395 }
3396
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003397 uint32_t GetVTableIndex() const { return vtable_index_; }
3398
3399 DECLARE_INSTRUCTION(InvokeVirtual);
3400
3401 private:
3402 const uint32_t vtable_index_;
3403
3404 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3405};
3406
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003407class HInvokeInterface : public HInvoke {
3408 public:
3409 HInvokeInterface(ArenaAllocator* arena,
3410 uint32_t number_of_arguments,
3411 Primitive::Type return_type,
3412 uint32_t dex_pc,
3413 uint32_t dex_method_index,
3414 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003415 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003416 imt_index_(imt_index) {}
3417
Calin Juravle641547a2015-04-21 22:08:51 +01003418 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003419 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003420 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003421 }
3422
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003423 uint32_t GetImtIndex() const { return imt_index_; }
3424 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3425
3426 DECLARE_INSTRUCTION(InvokeInterface);
3427
3428 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003429 const uint32_t imt_index_;
3430
3431 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3432};
3433
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003434class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003435 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003436 HNewInstance(HCurrentMethod* current_method,
3437 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003438 uint16_t type_index,
3439 const DexFile& dex_file,
3440 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003441 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003442 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003443 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003444 entrypoint_(entrypoint) {
3445 SetRawInputAt(0, current_method);
3446 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003447
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003448 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003449 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003450
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003451 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003452 bool NeedsEnvironment() const OVERRIDE { return true; }
3453 // It may throw when called on:
3454 // - interfaces
3455 // - abstract/innaccessible/unknown classes
3456 // TODO: optimize when possible.
3457 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003458
Calin Juravle10e244f2015-01-26 18:54:32 +00003459 bool CanBeNull() const OVERRIDE { return false; }
3460
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003461 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3462
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003463 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003464
3465 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003466 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003467 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003468 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003469
3470 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3471};
3472
Roland Levillain88cb1752014-10-20 16:36:47 +01003473class HNeg : public HUnaryOperation {
3474 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003475 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3476 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003477
Roland Levillain9867bc72015-08-05 10:21:34 +01003478 template <typename T> T Compute(T x) const { return -x; }
3479
3480 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003481 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003482 }
3483 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003484 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003485 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003486
Roland Levillain88cb1752014-10-20 16:36:47 +01003487 DECLARE_INSTRUCTION(Neg);
3488
3489 private:
3490 DISALLOW_COPY_AND_ASSIGN(HNeg);
3491};
3492
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003493class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003494 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003495 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003496 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003497 uint32_t dex_pc,
3498 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003499 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003500 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003501 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003502 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003503 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003504 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003505 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003506 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003507 }
3508
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003509 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003510 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003511
3512 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003513 bool NeedsEnvironment() const OVERRIDE { return true; }
3514
Mingyao Yang0c365e62015-03-31 15:09:29 -07003515 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3516 bool CanThrow() const OVERRIDE { return true; }
3517
Calin Juravle10e244f2015-01-26 18:54:32 +00003518 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003519
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003520 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3521
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003522 DECLARE_INSTRUCTION(NewArray);
3523
3524 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003525 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003526 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003527 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003528
3529 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3530};
3531
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003532class HAdd : public HBinaryOperation {
3533 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003534 HAdd(Primitive::Type result_type,
3535 HInstruction* left,
3536 HInstruction* right,
3537 uint32_t dex_pc = kNoDexPc)
3538 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003539
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003540 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003541
Roland Levillain9867bc72015-08-05 10:21:34 +01003542 template <typename T> T Compute(T x, T y) const { return x + y; }
3543
3544 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003545 return GetBlock()->GetGraph()->GetIntConstant(
3546 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003547 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003548 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003549 return GetBlock()->GetGraph()->GetLongConstant(
3550 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003551 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003552
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003553 DECLARE_INSTRUCTION(Add);
3554
3555 private:
3556 DISALLOW_COPY_AND_ASSIGN(HAdd);
3557};
3558
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003559class HSub : public HBinaryOperation {
3560 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003561 HSub(Primitive::Type result_type,
3562 HInstruction* left,
3563 HInstruction* right,
3564 uint32_t dex_pc = kNoDexPc)
3565 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003566
Roland Levillain9867bc72015-08-05 10:21:34 +01003567 template <typename T> T Compute(T x, T y) const { return x - y; }
3568
3569 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003570 return GetBlock()->GetGraph()->GetIntConstant(
3571 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003572 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003573 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003574 return GetBlock()->GetGraph()->GetLongConstant(
3575 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003576 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003577
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003578 DECLARE_INSTRUCTION(Sub);
3579
3580 private:
3581 DISALLOW_COPY_AND_ASSIGN(HSub);
3582};
3583
Calin Juravle34bacdf2014-10-07 20:23:36 +01003584class HMul : public HBinaryOperation {
3585 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003586 HMul(Primitive::Type result_type,
3587 HInstruction* left,
3588 HInstruction* right,
3589 uint32_t dex_pc = kNoDexPc)
3590 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003591
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003592 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003593
Roland Levillain9867bc72015-08-05 10:21:34 +01003594 template <typename T> T Compute(T x, T y) const { return x * y; }
3595
3596 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003597 return GetBlock()->GetGraph()->GetIntConstant(
3598 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003599 }
3600 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003601 return GetBlock()->GetGraph()->GetLongConstant(
3602 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003603 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003604
3605 DECLARE_INSTRUCTION(Mul);
3606
3607 private:
3608 DISALLOW_COPY_AND_ASSIGN(HMul);
3609};
3610
Calin Juravle7c4954d2014-10-28 16:57:40 +00003611class HDiv : public HBinaryOperation {
3612 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003613 HDiv(Primitive::Type result_type,
3614 HInstruction* left,
3615 HInstruction* right,
3616 uint32_t dex_pc)
3617 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003618
Roland Levillain9867bc72015-08-05 10:21:34 +01003619 template <typename T>
3620 T Compute(T x, T y) const {
3621 // Our graph structure ensures we never have 0 for `y` during
3622 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003623 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003624 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003625 return (y == -1) ? -x : x / y;
3626 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003627
Roland Levillain9867bc72015-08-05 10:21:34 +01003628 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003629 return GetBlock()->GetGraph()->GetIntConstant(
3630 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003631 }
3632 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003633 return GetBlock()->GetGraph()->GetLongConstant(
3634 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003635 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003636
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003637 static SideEffects SideEffectsForArchRuntimeCalls() {
3638 // The generated code can use a runtime call.
3639 return SideEffects::CanTriggerGC();
3640 }
3641
Calin Juravle7c4954d2014-10-28 16:57:40 +00003642 DECLARE_INSTRUCTION(Div);
3643
3644 private:
3645 DISALLOW_COPY_AND_ASSIGN(HDiv);
3646};
3647
Calin Juravlebacfec32014-11-14 15:54:36 +00003648class HRem : public HBinaryOperation {
3649 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003650 HRem(Primitive::Type result_type,
3651 HInstruction* left,
3652 HInstruction* right,
3653 uint32_t dex_pc)
3654 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003655
Roland Levillain9867bc72015-08-05 10:21:34 +01003656 template <typename T>
3657 T Compute(T x, T y) const {
3658 // Our graph structure ensures we never have 0 for `y` during
3659 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003660 DCHECK_NE(y, 0);
3661 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3662 return (y == -1) ? 0 : x % y;
3663 }
3664
Roland Levillain9867bc72015-08-05 10:21:34 +01003665 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003666 return GetBlock()->GetGraph()->GetIntConstant(
3667 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003668 }
3669 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003670 return GetBlock()->GetGraph()->GetLongConstant(
3671 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003672 }
3673
Calin Juravlebacfec32014-11-14 15:54:36 +00003674
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003675 static SideEffects SideEffectsForArchRuntimeCalls() {
3676 return SideEffects::CanTriggerGC();
3677 }
3678
Calin Juravlebacfec32014-11-14 15:54:36 +00003679 DECLARE_INSTRUCTION(Rem);
3680
3681 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003682 DISALLOW_COPY_AND_ASSIGN(HRem);
3683};
3684
Calin Juravled0d48522014-11-04 16:40:20 +00003685class HDivZeroCheck : public HExpression<1> {
3686 public:
3687 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003688 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003689 SetRawInputAt(0, value);
3690 }
3691
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003692 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3693
Calin Juravled0d48522014-11-04 16:40:20 +00003694 bool CanBeMoved() const OVERRIDE { return true; }
3695
3696 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3697 UNUSED(other);
3698 return true;
3699 }
3700
3701 bool NeedsEnvironment() const OVERRIDE { return true; }
3702 bool CanThrow() const OVERRIDE { return true; }
3703
Calin Juravled0d48522014-11-04 16:40:20 +00003704 DECLARE_INSTRUCTION(DivZeroCheck);
3705
3706 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003707 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3708};
3709
Calin Juravle9aec02f2014-11-18 23:06:35 +00003710class HShl : public HBinaryOperation {
3711 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003712 HShl(Primitive::Type result_type,
3713 HInstruction* left,
3714 HInstruction* right,
3715 uint32_t dex_pc = kNoDexPc)
3716 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003717
Roland Levillain9867bc72015-08-05 10:21:34 +01003718 template <typename T, typename U, typename V>
3719 T Compute(T x, U y, V max_shift_value) const {
3720 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3721 "V is not the unsigned integer type corresponding to T");
3722 return x << (y & max_shift_value);
3723 }
3724
3725 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3726 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003727 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003728 }
3729 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3730 // case is handled as `x << static_cast<int>(y)`.
3731 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3732 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003733 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003734 }
3735 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3736 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003737 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003738 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003739
3740 DECLARE_INSTRUCTION(Shl);
3741
3742 private:
3743 DISALLOW_COPY_AND_ASSIGN(HShl);
3744};
3745
3746class HShr : public HBinaryOperation {
3747 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003748 HShr(Primitive::Type result_type,
3749 HInstruction* left,
3750 HInstruction* right,
3751 uint32_t dex_pc = kNoDexPc)
3752 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003753
Roland Levillain9867bc72015-08-05 10:21:34 +01003754 template <typename T, typename U, typename V>
3755 T Compute(T x, U y, V max_shift_value) const {
3756 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3757 "V is not the unsigned integer type corresponding to T");
3758 return x >> (y & max_shift_value);
3759 }
3760
3761 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3762 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003763 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003764 }
3765 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3766 // case is handled as `x >> static_cast<int>(y)`.
3767 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3768 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003769 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003770 }
3771 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3772 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003773 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003774 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003775
3776 DECLARE_INSTRUCTION(Shr);
3777
3778 private:
3779 DISALLOW_COPY_AND_ASSIGN(HShr);
3780};
3781
3782class HUShr : public HBinaryOperation {
3783 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003784 HUShr(Primitive::Type result_type,
3785 HInstruction* left,
3786 HInstruction* right,
3787 uint32_t dex_pc = kNoDexPc)
3788 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003789
Roland Levillain9867bc72015-08-05 10:21:34 +01003790 template <typename T, typename U, typename V>
3791 T Compute(T x, U y, V max_shift_value) const {
3792 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3793 "V is not the unsigned integer type corresponding to T");
3794 V ux = static_cast<V>(x);
3795 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003796 }
3797
Roland Levillain9867bc72015-08-05 10:21:34 +01003798 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3799 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003800 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003801 }
3802 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3803 // case is handled as `x >>> static_cast<int>(y)`.
3804 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3805 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003806 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003807 }
3808 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3809 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003810 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003811 }
3812
3813 DECLARE_INSTRUCTION(UShr);
3814
3815 private:
3816 DISALLOW_COPY_AND_ASSIGN(HUShr);
3817};
3818
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003819class HAnd : public HBinaryOperation {
3820 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003821 HAnd(Primitive::Type result_type,
3822 HInstruction* left,
3823 HInstruction* right,
3824 uint32_t dex_pc = kNoDexPc)
3825 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003826
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003827 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003828
Roland Levillain9867bc72015-08-05 10:21:34 +01003829 template <typename T, typename U>
3830 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
3831
3832 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003833 return GetBlock()->GetGraph()->GetIntConstant(
3834 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003835 }
3836 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003837 return GetBlock()->GetGraph()->GetLongConstant(
3838 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003839 }
3840 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003841 return GetBlock()->GetGraph()->GetLongConstant(
3842 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003843 }
3844 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003845 return GetBlock()->GetGraph()->GetLongConstant(
3846 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003847 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003848
3849 DECLARE_INSTRUCTION(And);
3850
3851 private:
3852 DISALLOW_COPY_AND_ASSIGN(HAnd);
3853};
3854
3855class HOr : public HBinaryOperation {
3856 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003857 HOr(Primitive::Type result_type,
3858 HInstruction* left,
3859 HInstruction* right,
3860 uint32_t dex_pc = kNoDexPc)
3861 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003862
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003863 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003864
Roland Levillain9867bc72015-08-05 10:21:34 +01003865 template <typename T, typename U>
3866 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
3867
3868 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003869 return GetBlock()->GetGraph()->GetIntConstant(
3870 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003871 }
3872 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003873 return GetBlock()->GetGraph()->GetLongConstant(
3874 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003875 }
3876 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003877 return GetBlock()->GetGraph()->GetLongConstant(
3878 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003879 }
3880 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003881 return GetBlock()->GetGraph()->GetLongConstant(
3882 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003883 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003884
3885 DECLARE_INSTRUCTION(Or);
3886
3887 private:
3888 DISALLOW_COPY_AND_ASSIGN(HOr);
3889};
3890
3891class HXor : public HBinaryOperation {
3892 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003893 HXor(Primitive::Type result_type,
3894 HInstruction* left,
3895 HInstruction* right,
3896 uint32_t dex_pc = kNoDexPc)
3897 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003898
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003899 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003900
Roland Levillain9867bc72015-08-05 10:21:34 +01003901 template <typename T, typename U>
3902 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
3903
3904 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003905 return GetBlock()->GetGraph()->GetIntConstant(
3906 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003907 }
3908 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003909 return GetBlock()->GetGraph()->GetLongConstant(
3910 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003911 }
3912 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003913 return GetBlock()->GetGraph()->GetLongConstant(
3914 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003915 }
3916 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003917 return GetBlock()->GetGraph()->GetLongConstant(
3918 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003919 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003920
3921 DECLARE_INSTRUCTION(Xor);
3922
3923 private:
3924 DISALLOW_COPY_AND_ASSIGN(HXor);
3925};
3926
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003927// The value of a parameter in this method. Its location depends on
3928// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003929class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003930 public:
Calin Juravlec05aca72015-10-13 13:10:33 +00003931 HParameterValue(uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003932 Primitive::Type parameter_type,
3933 bool is_this = false)
3934 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003935 index_(index),
3936 is_this_(is_this),
3937 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003938
3939 uint8_t GetIndex() const { return index_; }
3940
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003941 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3942 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00003943
Calin Juravlec05aca72015-10-13 13:10:33 +00003944 bool IsThis() const { return is_this_; }
3945
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003946 DECLARE_INSTRUCTION(ParameterValue);
3947
3948 private:
3949 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003950 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003951 const uint8_t index_;
3952
Calin Juravle10e244f2015-01-26 18:54:32 +00003953 // Whether or not the parameter value corresponds to 'this' argument.
3954 const bool is_this_;
3955
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07003956 bool can_be_null_;
3957
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003958 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3959};
3960
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003961class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003962 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003963 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3964 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003965
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003966 bool CanBeMoved() const OVERRIDE { return true; }
3967 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003968 UNUSED(other);
3969 return true;
3970 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003971
Roland Levillain9867bc72015-08-05 10:21:34 +01003972 template <typename T> T Compute(T x) const { return ~x; }
3973
3974 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003975 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003976 }
3977 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003978 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003979 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003980
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003981 DECLARE_INSTRUCTION(Not);
3982
3983 private:
3984 DISALLOW_COPY_AND_ASSIGN(HNot);
3985};
3986
David Brazdil66d126e2015-04-03 16:02:44 +01003987class HBooleanNot : public HUnaryOperation {
3988 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003989 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
3990 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01003991
3992 bool CanBeMoved() const OVERRIDE { return true; }
3993 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3994 UNUSED(other);
3995 return true;
3996 }
3997
Roland Levillain9867bc72015-08-05 10:21:34 +01003998 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01003999 DCHECK(IsUint<1>(x));
4000 return !x;
4001 }
4002
Roland Levillain9867bc72015-08-05 10:21:34 +01004003 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004004 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004005 }
4006 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4007 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004008 UNREACHABLE();
4009 }
4010
4011 DECLARE_INSTRUCTION(BooleanNot);
4012
4013 private:
4014 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4015};
4016
Roland Levillaindff1f282014-11-05 14:15:05 +00004017class HTypeConversion : public HExpression<1> {
4018 public:
4019 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004020 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004021 : HExpression(result_type,
4022 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4023 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004024 SetRawInputAt(0, input);
4025 DCHECK_NE(input->GetType(), result_type);
4026 }
4027
4028 HInstruction* GetInput() const { return InputAt(0); }
4029 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4030 Primitive::Type GetResultType() const { return GetType(); }
4031
Roland Levillain624279f2014-12-04 11:54:28 +00004032 // Required by the x86 and ARM code generators when producing calls
4033 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004034
Roland Levillaindff1f282014-11-05 14:15:05 +00004035 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004036 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004037
Mark Mendelle82549b2015-05-06 10:55:34 -04004038 // Try to statically evaluate the conversion and return a HConstant
4039 // containing the result. If the input cannot be converted, return nullptr.
4040 HConstant* TryStaticEvaluation() const;
4041
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004042 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4043 Primitive::Type result_type) {
4044 // Some architectures may not require the 'GC' side effects, but at this point
4045 // in the compilation process we do not know what architecture we will
4046 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004047 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4048 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004049 return SideEffects::CanTriggerGC();
4050 }
4051 return SideEffects::None();
4052 }
4053
Roland Levillaindff1f282014-11-05 14:15:05 +00004054 DECLARE_INSTRUCTION(TypeConversion);
4055
4056 private:
4057 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4058};
4059
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004060static constexpr uint32_t kNoRegNumber = -1;
4061
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004062class HPhi : public HInstruction {
4063 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004064 HPhi(ArenaAllocator* arena,
4065 uint32_t reg_number,
4066 size_t number_of_inputs,
4067 Primitive::Type type,
4068 uint32_t dex_pc = kNoDexPc)
4069 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004070 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004071 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004072 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00004073 is_live_(false),
4074 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004075 }
4076
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004077 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4078 static Primitive::Type ToPhiType(Primitive::Type type) {
4079 switch (type) {
4080 case Primitive::kPrimBoolean:
4081 case Primitive::kPrimByte:
4082 case Primitive::kPrimShort:
4083 case Primitive::kPrimChar:
4084 return Primitive::kPrimInt;
4085 default:
4086 return type;
4087 }
4088 }
4089
David Brazdilffee3d32015-07-06 11:48:53 +01004090 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4091
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004092 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004093
4094 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004095 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004096
Calin Juravle10e244f2015-01-26 18:54:32 +00004097 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004098 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004099
Calin Juravle10e244f2015-01-26 18:54:32 +00004100 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4101 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4102
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004103 uint32_t GetRegNumber() const { return reg_number_; }
4104
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004105 void SetDead() { is_live_ = false; }
4106 void SetLive() { is_live_ = true; }
4107 bool IsDead() const { return !is_live_; }
4108 bool IsLive() const { return is_live_; }
4109
David Brazdil77a48ae2015-09-15 12:34:04 +00004110 bool IsVRegEquivalentOf(HInstruction* other) const {
4111 return other != nullptr
4112 && other->IsPhi()
4113 && other->AsPhi()->GetBlock() == GetBlock()
4114 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4115 }
4116
Calin Juravlea4f88312015-04-16 12:57:19 +01004117 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4118 // An equivalent phi is a phi having the same dex register and type.
4119 // It assumes that phis with the same dex register are adjacent.
4120 HPhi* GetNextEquivalentPhiWithSameType() {
4121 HInstruction* next = GetNext();
4122 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4123 if (next->GetType() == GetType()) {
4124 return next->AsPhi();
4125 }
4126 next = next->GetNext();
4127 }
4128 return nullptr;
4129 }
4130
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004131 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004132
David Brazdil1abb4192015-02-17 18:33:36 +00004133 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004134 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004135 return inputs_[index];
4136 }
David Brazdil1abb4192015-02-17 18:33:36 +00004137
4138 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004139 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004140 }
4141
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004142 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004143 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004144 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004145 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004146 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004147 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004148
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004149 DISALLOW_COPY_AND_ASSIGN(HPhi);
4150};
4151
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004152class HNullCheck : public HExpression<1> {
4153 public:
4154 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004155 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004156 SetRawInputAt(0, value);
4157 }
4158
Calin Juravle10e244f2015-01-26 18:54:32 +00004159 bool CanBeMoved() const OVERRIDE { return true; }
4160 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004161 UNUSED(other);
4162 return true;
4163 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004164
Calin Juravle10e244f2015-01-26 18:54:32 +00004165 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004166
Calin Juravle10e244f2015-01-26 18:54:32 +00004167 bool CanThrow() const OVERRIDE { return true; }
4168
4169 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004170
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004171
4172 DECLARE_INSTRUCTION(NullCheck);
4173
4174 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004175 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4176};
4177
4178class FieldInfo : public ValueObject {
4179 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004180 FieldInfo(MemberOffset field_offset,
4181 Primitive::Type field_type,
4182 bool is_volatile,
4183 uint32_t index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004184 const DexFile& dex_file,
4185 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004186 : field_offset_(field_offset),
4187 field_type_(field_type),
4188 is_volatile_(is_volatile),
4189 index_(index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004190 dex_file_(dex_file),
4191 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004192
4193 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004194 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004195 uint32_t GetFieldIndex() const { return index_; }
4196 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004197 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004198 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004199
4200 private:
4201 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004202 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004203 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004204 const uint32_t index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004205 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004206 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004207};
4208
4209class HInstanceFieldGet : public HExpression<1> {
4210 public:
4211 HInstanceFieldGet(HInstruction* value,
4212 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004213 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004214 bool is_volatile,
4215 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004216 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004217 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004218 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004219 : HExpression(
4220 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004221 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004222 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004223 SetRawInputAt(0, value);
4224 }
4225
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004226 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004227
4228 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4229 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4230 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004231 }
4232
Calin Juravle641547a2015-04-21 22:08:51 +01004233 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4234 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004235 }
4236
4237 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004238 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4239 }
4240
Calin Juravle52c48962014-12-16 17:02:57 +00004241 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004242 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004243 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004244 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004245
4246 DECLARE_INSTRUCTION(InstanceFieldGet);
4247
4248 private:
4249 const FieldInfo field_info_;
4250
4251 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4252};
4253
4254class HInstanceFieldSet : public HTemplateInstruction<2> {
4255 public:
4256 HInstanceFieldSet(HInstruction* object,
4257 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004258 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004259 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004260 bool is_volatile,
4261 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004262 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004263 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004264 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004265 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004266 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004267 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004268 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004269 SetRawInputAt(0, object);
4270 SetRawInputAt(1, value);
4271 }
4272
Calin Juravle641547a2015-04-21 22:08:51 +01004273 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4274 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004275 }
4276
Calin Juravle52c48962014-12-16 17:02:57 +00004277 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004278 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004279 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004280 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004281 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004282 bool GetValueCanBeNull() const { return value_can_be_null_; }
4283 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004284
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004285 DECLARE_INSTRUCTION(InstanceFieldSet);
4286
4287 private:
4288 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004289 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004290
4291 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4292};
4293
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004294class HArrayGet : public HExpression<2> {
4295 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004296 HArrayGet(HInstruction* array,
4297 HInstruction* index,
4298 Primitive::Type type,
Calin Juravle154746b2015-10-06 15:46:54 +01004299 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004300 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004301 SetRawInputAt(0, array);
4302 SetRawInputAt(1, index);
4303 }
4304
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004305 bool CanBeMoved() const OVERRIDE { return true; }
4306 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004307 UNUSED(other);
4308 return true;
4309 }
Calin Juravle641547a2015-04-21 22:08:51 +01004310 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4311 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004312 // TODO: We can be smarter here.
4313 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4314 // which generates the implicit null check. There are cases when these can be removed
4315 // to produce better code. If we ever add optimizations to do so we should allow an
4316 // implicit check here (as long as the address falls in the first page).
4317 return false;
4318 }
4319
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004320 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004321
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004322 HInstruction* GetArray() const { return InputAt(0); }
4323 HInstruction* GetIndex() const { return InputAt(1); }
4324
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004325 DECLARE_INSTRUCTION(ArrayGet);
4326
4327 private:
4328 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4329};
4330
4331class HArraySet : public HTemplateInstruction<3> {
4332 public:
4333 HArraySet(HInstruction* array,
4334 HInstruction* index,
4335 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004336 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004337 uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004338 : HTemplateInstruction(
4339 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004340 SideEffectsForArchRuntimeCalls(value->GetType())), dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004341 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004342 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004343 value_can_be_null_(true),
4344 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004345 SetRawInputAt(0, array);
4346 SetRawInputAt(1, index);
4347 SetRawInputAt(2, value);
4348 }
4349
Calin Juravle77520bc2015-01-12 18:45:46 +00004350 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004351 // We currently always call a runtime method to catch array store
4352 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004353 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004354 }
4355
Mingyao Yang81014cb2015-06-02 03:16:27 -07004356 // Can throw ArrayStoreException.
4357 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4358
Calin Juravle641547a2015-04-21 22:08:51 +01004359 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4360 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00004361 // TODO: Same as for ArrayGet.
4362 return false;
4363 }
4364
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004365 void ClearNeedsTypeCheck() {
4366 needs_type_check_ = false;
4367 }
4368
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004369 void ClearValueCanBeNull() {
4370 value_can_be_null_ = false;
4371 }
4372
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004373 void SetStaticTypeOfArrayIsObjectArray() {
4374 static_type_of_array_is_object_array_ = true;
4375 }
4376
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004377 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004378 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004379 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004380
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004381 HInstruction* GetArray() const { return InputAt(0); }
4382 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004383 HInstruction* GetValue() const { return InputAt(2); }
4384
4385 Primitive::Type GetComponentType() const {
4386 // The Dex format does not type floating point index operations. Since the
4387 // `expected_component_type_` is set during building and can therefore not
4388 // be correct, we also check what is the value type. If it is a floating
4389 // point type, we must use that type.
4390 Primitive::Type value_type = GetValue()->GetType();
4391 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4392 ? value_type
4393 : expected_component_type_;
4394 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004395
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004396 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4397 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4398 }
4399
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004400 DECLARE_INSTRUCTION(ArraySet);
4401
4402 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004403 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004404 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004405 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004406 // Cached information for the reference_type_info_ so that codegen
4407 // does not need to inspect the static type.
4408 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004409
4410 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4411};
4412
4413class HArrayLength : public HExpression<1> {
4414 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004415 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004416 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004417 // Note that arrays do not change length, so the instruction does not
4418 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004419 SetRawInputAt(0, array);
4420 }
4421
Calin Juravle77520bc2015-01-12 18:45:46 +00004422 bool CanBeMoved() const OVERRIDE { return true; }
4423 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004424 UNUSED(other);
4425 return true;
4426 }
Calin Juravle641547a2015-04-21 22:08:51 +01004427 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4428 return obj == InputAt(0);
4429 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004430
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004431 DECLARE_INSTRUCTION(ArrayLength);
4432
4433 private:
4434 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4435};
4436
4437class HBoundsCheck : public HExpression<2> {
4438 public:
4439 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004440 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004441 DCHECK(index->GetType() == Primitive::kPrimInt);
4442 SetRawInputAt(0, index);
4443 SetRawInputAt(1, length);
4444 }
4445
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004446 bool CanBeMoved() const OVERRIDE { return true; }
4447 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004448 UNUSED(other);
4449 return true;
4450 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004451
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004452 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004453
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004454 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004455
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004456
4457 DECLARE_INSTRUCTION(BoundsCheck);
4458
4459 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004460 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4461};
4462
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004463/**
4464 * Some DEX instructions are folded into multiple HInstructions that need
4465 * to stay live until the last HInstruction. This class
4466 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004467 * HInstruction stays live. `index` represents the stack location index of the
4468 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004469 */
4470class HTemporary : public HTemplateInstruction<0> {
4471 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004472 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4473 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004474
4475 size_t GetIndex() const { return index_; }
4476
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004477 Primitive::Type GetType() const OVERRIDE {
4478 // The previous instruction is the one that will be stored in the temporary location.
4479 DCHECK(GetPrevious() != nullptr);
4480 return GetPrevious()->GetType();
4481 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004482
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004483 DECLARE_INSTRUCTION(Temporary);
4484
4485 private:
4486 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004487 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4488};
4489
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004490class HSuspendCheck : public HTemplateInstruction<0> {
4491 public:
4492 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004493 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004494
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004495 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004496 return true;
4497 }
4498
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004499 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4500 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004501
4502 DECLARE_INSTRUCTION(SuspendCheck);
4503
4504 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004505 // Only used for code generation, in order to share the same slow path between back edges
4506 // of a same loop.
4507 SlowPathCode* slow_path_;
4508
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004509 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4510};
4511
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004512/**
4513 * Instruction to load a Class object.
4514 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004515class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004516 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004517 HLoadClass(HCurrentMethod* current_method,
4518 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004519 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004520 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004521 uint32_t dex_pc,
4522 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004523 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004524 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004525 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004526 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004527 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004528 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004529 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004530 // Referrers class should not need access check. We never inline unverified
4531 // methods so we can't possibly end up in this situation.
4532 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004533 SetRawInputAt(0, current_method);
4534 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004535
4536 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004537
4538 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004539 // Note that we don't need to test for generate_clinit_check_.
4540 // Whether or not we need to generate the clinit check is processed in
4541 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004542 return other->AsLoadClass()->type_index_ == type_index_ &&
4543 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004544 }
4545
4546 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4547
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004548 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004549 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004550 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004551
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004552 bool NeedsEnvironment() const OVERRIDE {
4553 // Will call runtime and load the class if the class is not loaded yet.
4554 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004555 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004556 }
4557
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004558 bool MustGenerateClinitCheck() const {
4559 return generate_clinit_check_;
4560 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004561 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
4562 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004563 }
4564
4565 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004566 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4567 }
4568
4569 bool NeedsAccessCheck() const {
4570 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004571 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004572
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004573 bool CanThrow() const OVERRIDE {
4574 // May call runtime and and therefore can throw.
4575 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004576 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004577 }
4578
Calin Juravleacf735c2015-02-12 15:25:22 +00004579 ReferenceTypeInfo GetLoadedClassRTI() {
4580 return loaded_class_rti_;
4581 }
4582
4583 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4584 // Make sure we only set exact types (the loaded class should never be merged).
4585 DCHECK(rti.IsExact());
4586 loaded_class_rti_ = rti;
4587 }
4588
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004589 const DexFile& GetDexFile() { return dex_file_; }
4590
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004591 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
4592
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004593 static SideEffects SideEffectsForArchRuntimeCalls() {
4594 return SideEffects::CanTriggerGC();
4595 }
4596
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004597 DECLARE_INSTRUCTION(LoadClass);
4598
4599 private:
4600 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004601 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004602 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004603 // Whether this instruction must generate the initialization check.
4604 // Used for code generation.
4605 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004606 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004607
Calin Juravleacf735c2015-02-12 15:25:22 +00004608 ReferenceTypeInfo loaded_class_rti_;
4609
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004610 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4611};
4612
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004613class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004614 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004615 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004616 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4617 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004618 SetRawInputAt(0, current_method);
4619 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004620
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004621 bool CanBeMoved() const OVERRIDE { return true; }
4622
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004623 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4624 return other->AsLoadString()->string_index_ == string_index_;
4625 }
4626
4627 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4628
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004629 uint32_t GetStringIndex() const { return string_index_; }
4630
4631 // TODO: Can we deopt or debug when we resolve a string?
4632 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004633 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004634 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004635
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004636 static SideEffects SideEffectsForArchRuntimeCalls() {
4637 return SideEffects::CanTriggerGC();
4638 }
4639
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004640 DECLARE_INSTRUCTION(LoadString);
4641
4642 private:
4643 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004644
4645 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4646};
4647
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004648/**
4649 * Performs an initialization check on its Class object input.
4650 */
4651class HClinitCheck : public HExpression<1> {
4652 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004653 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004654 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004655 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004656 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4657 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004658 SetRawInputAt(0, constant);
4659 }
4660
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004661 bool CanBeMoved() const OVERRIDE { return true; }
4662 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4663 UNUSED(other);
4664 return true;
4665 }
4666
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004667 bool NeedsEnvironment() const OVERRIDE {
4668 // May call runtime to initialize the class.
4669 return true;
4670 }
4671
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004672
4673 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4674
4675 DECLARE_INSTRUCTION(ClinitCheck);
4676
4677 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004678 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4679};
4680
4681class HStaticFieldGet : public HExpression<1> {
4682 public:
4683 HStaticFieldGet(HInstruction* cls,
4684 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004685 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004686 bool is_volatile,
4687 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004688 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004689 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004690 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004691 : HExpression(
4692 field_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004693 SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004694 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004695 SetRawInputAt(0, cls);
4696 }
4697
Calin Juravle52c48962014-12-16 17:02:57 +00004698
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004699 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004700
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004701 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004702 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4703 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004704 }
4705
4706 size_t ComputeHashCode() const OVERRIDE {
4707 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4708 }
4709
Calin Juravle52c48962014-12-16 17:02:57 +00004710 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004711 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4712 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004713 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004714
4715 DECLARE_INSTRUCTION(StaticFieldGet);
4716
4717 private:
4718 const FieldInfo field_info_;
4719
4720 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4721};
4722
4723class HStaticFieldSet : public HTemplateInstruction<2> {
4724 public:
4725 HStaticFieldSet(HInstruction* cls,
4726 HInstruction* value,
4727 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004728 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004729 bool is_volatile,
4730 uint32_t field_idx,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004731 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004732 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004733 uint32_t dex_pc)
Aart Bik34c3ba92015-07-20 14:08:59 -07004734 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004735 SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004736 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file, dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004737 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004738 SetRawInputAt(0, cls);
4739 SetRawInputAt(1, value);
4740 }
4741
Calin Juravle52c48962014-12-16 17:02:57 +00004742 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004743 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4744 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004745 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004746
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004747 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004748 bool GetValueCanBeNull() const { return value_can_be_null_; }
4749 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004750
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004751 DECLARE_INSTRUCTION(StaticFieldSet);
4752
4753 private:
4754 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004755 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004756
4757 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4758};
4759
Calin Juravlee460d1d2015-09-29 04:52:17 +01004760class HUnresolvedInstanceFieldGet : public HExpression<1> {
4761 public:
4762 HUnresolvedInstanceFieldGet(HInstruction* obj,
4763 Primitive::Type field_type,
4764 uint32_t field_index,
4765 uint32_t dex_pc)
4766 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4767 field_index_(field_index) {
4768 SetRawInputAt(0, obj);
4769 }
4770
4771 bool NeedsEnvironment() const OVERRIDE { return true; }
4772 bool CanThrow() const OVERRIDE { return true; }
4773
4774 Primitive::Type GetFieldType() const { return GetType(); }
4775 uint32_t GetFieldIndex() const { return field_index_; }
4776
4777 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
4778
4779 private:
4780 const uint32_t field_index_;
4781
4782 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
4783};
4784
4785class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
4786 public:
4787 HUnresolvedInstanceFieldSet(HInstruction* obj,
4788 HInstruction* value,
4789 Primitive::Type field_type,
4790 uint32_t field_index,
4791 uint32_t dex_pc)
4792 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4793 field_type_(field_type),
4794 field_index_(field_index) {
4795 DCHECK_EQ(field_type, value->GetType());
4796 SetRawInputAt(0, obj);
4797 SetRawInputAt(1, value);
4798 }
4799
4800 bool NeedsEnvironment() const OVERRIDE { return true; }
4801 bool CanThrow() const OVERRIDE { return true; }
4802
4803 Primitive::Type GetFieldType() const { return field_type_; }
4804 uint32_t GetFieldIndex() const { return field_index_; }
4805
4806 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
4807
4808 private:
4809 const Primitive::Type field_type_;
4810 const uint32_t field_index_;
4811
4812 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
4813};
4814
4815class HUnresolvedStaticFieldGet : public HExpression<0> {
4816 public:
4817 HUnresolvedStaticFieldGet(Primitive::Type field_type,
4818 uint32_t field_index,
4819 uint32_t dex_pc)
4820 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
4821 field_index_(field_index) {
4822 }
4823
4824 bool NeedsEnvironment() const OVERRIDE { return true; }
4825 bool CanThrow() const OVERRIDE { return true; }
4826
4827 Primitive::Type GetFieldType() const { return GetType(); }
4828 uint32_t GetFieldIndex() const { return field_index_; }
4829
4830 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
4831
4832 private:
4833 const uint32_t field_index_;
4834
4835 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
4836};
4837
4838class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
4839 public:
4840 HUnresolvedStaticFieldSet(HInstruction* value,
4841 Primitive::Type field_type,
4842 uint32_t field_index,
4843 uint32_t dex_pc)
4844 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
4845 field_type_(field_type),
4846 field_index_(field_index) {
4847 DCHECK_EQ(field_type, value->GetType());
4848 SetRawInputAt(0, value);
4849 }
4850
4851 bool NeedsEnvironment() const OVERRIDE { return true; }
4852 bool CanThrow() const OVERRIDE { return true; }
4853
4854 Primitive::Type GetFieldType() const { return field_type_; }
4855 uint32_t GetFieldIndex() const { return field_index_; }
4856
4857 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
4858
4859 private:
4860 const Primitive::Type field_type_;
4861 const uint32_t field_index_;
4862
4863 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
4864};
4865
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004866// Implement the move-exception DEX instruction.
4867class HLoadException : public HExpression<0> {
4868 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004869 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
4870 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004871
David Brazdilbbd733e2015-08-18 17:48:17 +01004872 bool CanBeNull() const OVERRIDE { return false; }
4873
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004874 DECLARE_INSTRUCTION(LoadException);
4875
4876 private:
4877 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4878};
4879
David Brazdilcb1c0552015-08-04 16:22:25 +01004880// Implicit part of move-exception which clears thread-local exception storage.
4881// Must not be removed because the runtime expects the TLS to get cleared.
4882class HClearException : public HTemplateInstruction<0> {
4883 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004884 explicit HClearException(uint32_t dex_pc = kNoDexPc)
4885 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01004886
4887 DECLARE_INSTRUCTION(ClearException);
4888
4889 private:
4890 DISALLOW_COPY_AND_ASSIGN(HClearException);
4891};
4892
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004893class HThrow : public HTemplateInstruction<1> {
4894 public:
4895 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004896 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004897 SetRawInputAt(0, exception);
4898 }
4899
4900 bool IsControlFlow() const OVERRIDE { return true; }
4901
4902 bool NeedsEnvironment() const OVERRIDE { return true; }
4903
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004904 bool CanThrow() const OVERRIDE { return true; }
4905
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004906
4907 DECLARE_INSTRUCTION(Throw);
4908
4909 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004910 DISALLOW_COPY_AND_ASSIGN(HThrow);
4911};
4912
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004913/**
4914 * Implementation strategies for the code generator of a HInstanceOf
4915 * or `HCheckCast`.
4916 */
4917enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01004918 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004919 kExactCheck, // Can do a single class compare.
4920 kClassHierarchyCheck, // Can just walk the super class chain.
4921 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
4922 kInterfaceCheck, // No optimization yet when checking against an interface.
4923 kArrayObjectCheck, // Can just check if the array is not primitive.
4924 kArrayCheck // No optimization yet when checking against a generic array.
4925};
4926
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004927class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004928 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004929 HInstanceOf(HInstruction* object,
4930 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004931 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004932 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004933 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004934 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004935 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004936 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004937 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004938 SetRawInputAt(0, object);
4939 SetRawInputAt(1, constant);
4940 }
4941
4942 bool CanBeMoved() const OVERRIDE { return true; }
4943
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004944 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004945 return true;
4946 }
4947
4948 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004949 return false;
4950 }
4951
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004952 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
4953
4954 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004955
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004956 // Used only in code generation.
4957 bool MustDoNullCheck() const { return must_do_null_check_; }
4958 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4959
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004960 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
4961 return (check_kind == TypeCheckKind::kExactCheck)
4962 ? SideEffects::None()
4963 // Mips currently does runtime calls for any other checks.
4964 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004965 }
4966
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004967 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004968
4969 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004970 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004971 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004972
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004973 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4974};
4975
Calin Juravleb1498f62015-02-16 13:13:29 +00004976class HBoundType : public HExpression<1> {
4977 public:
Calin Juravle2e768302015-07-28 14:41:11 +00004978 // Constructs an HBoundType with the given upper_bound.
4979 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004980 HBoundType(HInstruction* input,
4981 ReferenceTypeInfo upper_bound,
4982 bool upper_can_be_null,
4983 uint32_t dex_pc = kNoDexPc)
4984 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004985 upper_bound_(upper_bound),
4986 upper_can_be_null_(upper_can_be_null),
4987 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004988 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004989 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00004990 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004991 }
4992
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004993 // GetUpper* should only be used in reference type propagation.
4994 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4995 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004996
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004997 void SetCanBeNull(bool can_be_null) {
4998 DCHECK(upper_can_be_null_ || !can_be_null);
4999 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005000 }
5001
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005002 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5003
Calin Juravleb1498f62015-02-16 13:13:29 +00005004 DECLARE_INSTRUCTION(BoundType);
5005
5006 private:
5007 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005008 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5009 // It is used to bound the type in cases like:
5010 // if (x instanceof ClassX) {
5011 // // uper_bound_ will be ClassX
5012 // }
5013 const ReferenceTypeInfo upper_bound_;
5014 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5015 // is false then can_be_null_ cannot be true).
5016 const bool upper_can_be_null_;
5017 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005018
5019 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5020};
5021
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005022class HCheckCast : public HTemplateInstruction<2> {
5023 public:
5024 HCheckCast(HInstruction* object,
5025 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005026 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005027 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005028 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005029 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005030 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005031 SetRawInputAt(0, object);
5032 SetRawInputAt(1, constant);
5033 }
5034
5035 bool CanBeMoved() const OVERRIDE { return true; }
5036
5037 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5038 return true;
5039 }
5040
5041 bool NeedsEnvironment() const OVERRIDE {
5042 // Instruction may throw a CheckCastError.
5043 return true;
5044 }
5045
5046 bool CanThrow() const OVERRIDE { return true; }
5047
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005048 bool MustDoNullCheck() const { return must_do_null_check_; }
5049 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005050 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005051
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005052 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005053
5054 DECLARE_INSTRUCTION(CheckCast);
5055
5056 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005057 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005058 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005059
5060 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005061};
5062
Calin Juravle27df7582015-04-17 19:12:31 +01005063class HMemoryBarrier : public HTemplateInstruction<0> {
5064 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005065 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005066 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005067 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005068 barrier_kind_(barrier_kind) {}
5069
5070 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5071
5072 DECLARE_INSTRUCTION(MemoryBarrier);
5073
5074 private:
5075 const MemBarrierKind barrier_kind_;
5076
5077 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5078};
5079
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005080class HMonitorOperation : public HTemplateInstruction<1> {
5081 public:
5082 enum OperationKind {
5083 kEnter,
5084 kExit,
5085 };
5086
5087 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005088 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005089 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5090 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005091 SetRawInputAt(0, object);
5092 }
5093
5094 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005095 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5096
5097 bool CanThrow() const OVERRIDE {
5098 // Verifier guarantees that monitor-exit cannot throw.
5099 // This is important because it allows the HGraphBuilder to remove
5100 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5101 return IsEnter();
5102 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005103
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005104
5105 bool IsEnter() const { return kind_ == kEnter; }
5106
5107 DECLARE_INSTRUCTION(MonitorOperation);
5108
Calin Juravle52c48962014-12-16 17:02:57 +00005109 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005110 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005111
5112 private:
5113 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5114};
5115
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005116/**
5117 * A HInstruction used as a marker for the replacement of new + <init>
5118 * of a String to a call to a StringFactory. Only baseline will see
5119 * the node at code generation, where it will be be treated as null.
5120 * When compiling non-baseline, `HFakeString` instructions are being removed
5121 * in the instruction simplifier.
5122 */
5123class HFakeString : public HTemplateInstruction<0> {
5124 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005125 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5126 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005127
5128 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5129
5130 DECLARE_INSTRUCTION(FakeString);
5131
5132 private:
5133 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5134};
5135
Vladimir Markof9f64412015-09-02 14:05:49 +01005136class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005137 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005138 MoveOperands(Location source,
5139 Location destination,
5140 Primitive::Type type,
5141 HInstruction* instruction)
5142 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005143
5144 Location GetSource() const { return source_; }
5145 Location GetDestination() const { return destination_; }
5146
5147 void SetSource(Location value) { source_ = value; }
5148 void SetDestination(Location value) { destination_ = value; }
5149
5150 // The parallel move resolver marks moves as "in-progress" by clearing the
5151 // destination (but not the source).
5152 Location MarkPending() {
5153 DCHECK(!IsPending());
5154 Location dest = destination_;
5155 destination_ = Location::NoLocation();
5156 return dest;
5157 }
5158
5159 void ClearPending(Location dest) {
5160 DCHECK(IsPending());
5161 destination_ = dest;
5162 }
5163
5164 bool IsPending() const {
5165 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5166 return destination_.IsInvalid() && !source_.IsInvalid();
5167 }
5168
5169 // True if this blocks a move from the given location.
5170 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005171 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005172 }
5173
5174 // A move is redundant if it's been eliminated, if its source and
5175 // destination are the same, or if its destination is unneeded.
5176 bool IsRedundant() const {
5177 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5178 }
5179
5180 // We clear both operands to indicate move that's been eliminated.
5181 void Eliminate() {
5182 source_ = destination_ = Location::NoLocation();
5183 }
5184
5185 bool IsEliminated() const {
5186 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5187 return source_.IsInvalid();
5188 }
5189
Alexey Frunze4dda3372015-06-01 18:31:49 -07005190 Primitive::Type GetType() const { return type_; }
5191
Nicolas Geoffray90218252015-04-15 11:56:51 +01005192 bool Is64BitMove() const {
5193 return Primitive::Is64BitType(type_);
5194 }
5195
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005196 HInstruction* GetInstruction() const { return instruction_; }
5197
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005198 private:
5199 Location source_;
5200 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005201 // The type this move is for.
5202 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005203 // The instruction this move is assocatied with. Null when this move is
5204 // for moving an input in the expected locations of user (including a phi user).
5205 // This is only used in debug mode, to ensure we do not connect interval siblings
5206 // in the same parallel move.
5207 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005208};
5209
5210static constexpr size_t kDefaultNumberOfMoves = 4;
5211
5212class HParallelMove : public HTemplateInstruction<0> {
5213 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005214 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005215 : HTemplateInstruction(SideEffects::None(), dex_pc),
5216 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5217 moves_.reserve(kDefaultNumberOfMoves);
5218 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005219
Nicolas Geoffray90218252015-04-15 11:56:51 +01005220 void AddMove(Location source,
5221 Location destination,
5222 Primitive::Type type,
5223 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005224 DCHECK(source.IsValid());
5225 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005226 if (kIsDebugBuild) {
5227 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005228 for (const MoveOperands& move : moves_) {
5229 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005230 // Special case the situation where the move is for the spill slot
5231 // of the instruction.
5232 if ((GetPrevious() == instruction)
5233 || ((GetPrevious() == nullptr)
5234 && instruction->IsPhi()
5235 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005236 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005237 << "Doing parallel moves for the same instruction.";
5238 } else {
5239 DCHECK(false) << "Doing parallel moves for the same instruction.";
5240 }
5241 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005242 }
5243 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005244 for (const MoveOperands& move : moves_) {
5245 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005246 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005247 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005248 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005249 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005250 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005251 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005252 }
5253
Vladimir Marko225b6462015-09-28 12:17:40 +01005254 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005255 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005256 }
5257
Vladimir Marko225b6462015-09-28 12:17:40 +01005258 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005259
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005260 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005261
5262 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005263 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005264
5265 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5266};
5267
Mark Mendell0616ae02015-04-17 12:49:27 -04005268} // namespace art
5269
5270#ifdef ART_ENABLE_CODEGEN_x86
5271#include "nodes_x86.h"
5272#endif
5273
5274namespace art {
5275
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005276class HGraphVisitor : public ValueObject {
5277 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005278 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5279 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005280
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005281 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005282 virtual void VisitBasicBlock(HBasicBlock* block);
5283
Roland Levillain633021e2014-10-01 14:12:25 +01005284 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005285 void VisitInsertionOrder();
5286
Roland Levillain633021e2014-10-01 14:12:25 +01005287 // Visit the graph following dominator tree reverse post-order.
5288 void VisitReversePostOrder();
5289
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005290 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005291
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005292 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005293#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005294 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5295
5296 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5297
5298#undef DECLARE_VISIT_INSTRUCTION
5299
5300 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005301 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005302
5303 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5304};
5305
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005306class HGraphDelegateVisitor : public HGraphVisitor {
5307 public:
5308 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5309 virtual ~HGraphDelegateVisitor() {}
5310
5311 // Visit functions that delegate to to super class.
5312#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005313 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005314
5315 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5316
5317#undef DECLARE_VISIT_INSTRUCTION
5318
5319 private:
5320 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5321};
5322
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005323class HInsertionOrderIterator : public ValueObject {
5324 public:
5325 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5326
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005327 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005328 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005329 void Advance() { ++index_; }
5330
5331 private:
5332 const HGraph& graph_;
5333 size_t index_;
5334
5335 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5336};
5337
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005338class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005339 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005340 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5341 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005342 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005343 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005344
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005345 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5346 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005347 void Advance() { ++index_; }
5348
5349 private:
5350 const HGraph& graph_;
5351 size_t index_;
5352
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005353 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005354};
5355
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005356class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005357 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005358 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005359 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005360 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005361 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005362 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005363
5364 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005365 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005366 void Advance() { --index_; }
5367
5368 private:
5369 const HGraph& graph_;
5370 size_t index_;
5371
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005372 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005373};
5374
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005375class HLinearPostOrderIterator : public ValueObject {
5376 public:
5377 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005378 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005379
5380 bool Done() const { return index_ == 0; }
5381
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005382 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005383
5384 void Advance() {
5385 --index_;
5386 DCHECK_GE(index_, 0U);
5387 }
5388
5389 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005390 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005391 size_t index_;
5392
5393 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5394};
5395
5396class HLinearOrderIterator : public ValueObject {
5397 public:
5398 explicit HLinearOrderIterator(const HGraph& graph)
5399 : order_(graph.GetLinearOrder()), index_(0) {}
5400
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005401 bool Done() const { return index_ == order_.size(); }
5402 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005403 void Advance() { ++index_; }
5404
5405 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005406 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005407 size_t index_;
5408
5409 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5410};
5411
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005412// Iterator over the blocks that art part of the loop. Includes blocks part
5413// of an inner loop. The order in which the blocks are iterated is on their
5414// block id.
5415class HBlocksInLoopIterator : public ValueObject {
5416 public:
5417 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5418 : blocks_in_loop_(info.GetBlocks()),
5419 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5420 index_(0) {
5421 if (!blocks_in_loop_.IsBitSet(index_)) {
5422 Advance();
5423 }
5424 }
5425
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005426 bool Done() const { return index_ == blocks_.size(); }
5427 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005428 void Advance() {
5429 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005430 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005431 if (blocks_in_loop_.IsBitSet(index_)) {
5432 break;
5433 }
5434 }
5435 }
5436
5437 private:
5438 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005439 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005440 size_t index_;
5441
5442 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5443};
5444
Mingyao Yang3584bce2015-05-19 16:01:59 -07005445// Iterator over the blocks that art part of the loop. Includes blocks part
5446// of an inner loop. The order in which the blocks are iterated is reverse
5447// post order.
5448class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5449 public:
5450 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5451 : blocks_in_loop_(info.GetBlocks()),
5452 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5453 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005454 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005455 Advance();
5456 }
5457 }
5458
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005459 bool Done() const { return index_ == blocks_.size(); }
5460 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005461 void Advance() {
5462 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005463 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5464 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005465 break;
5466 }
5467 }
5468 }
5469
5470 private:
5471 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005472 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005473 size_t index_;
5474
5475 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5476};
5477
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005478inline int64_t Int64FromConstant(HConstant* constant) {
5479 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5480 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5481 : constant->AsLongConstant()->GetValue();
5482}
5483
Vladimir Marko58155012015-08-19 12:49:41 +00005484inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5485 // For the purposes of the compiler, the dex files must actually be the same object
5486 // if we want to safely treat them as the same. This is especially important for JIT
5487 // as custom class loaders can open the same underlying file (or memory) multiple
5488 // times and provide different class resolution but no two class loaders should ever
5489 // use the same DexFile object - doing so is an unsupported hack that can lead to
5490 // all sorts of weird failures.
5491 return &lhs == &rhs;
5492}
5493
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005494} // namespace art
5495
5496#endif // ART_COMPILER_OPTIMIZING_NODES_H_