blob: 4421419f10fdf691733d1a1f4d840a849ed1929e [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
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.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);
Mingyao Yang8df69d42015-10-22 15:40:58 -070078static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010080static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
81
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060082static constexpr uint32_t kNoDexPc = -1;
83
Dave Allison20dfc792014-06-16 20:44:29 -070084enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070085 // All types.
86 kCondEQ, // ==
87 kCondNE, // !=
88 // Signed integers and floating-point numbers.
89 kCondLT, // <
90 kCondLE, // <=
91 kCondGT, // >
92 kCondGE, // >=
93 // Unsigned integers.
94 kCondB, // <
95 kCondBE, // <=
96 kCondA, // >
97 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070098};
99
Vladimir Markof9f64412015-09-02 14:05:49 +0100100class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100101 public:
102 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
103
104 void AddInstruction(HInstruction* instruction);
105 void RemoveInstruction(HInstruction* instruction);
106
David Brazdilc3d743f2015-04-22 13:40:50 +0100107 // Insert `instruction` before/after an existing instruction `cursor`.
108 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
109 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
110
Roland Levillain6b469232014-09-25 10:10:38 +0100111 // Return true if this list contains `instruction`.
112 bool Contains(HInstruction* instruction) const;
113
Roland Levillainccc07a92014-09-16 14:48:16 +0100114 // Return true if `instruction1` is found before `instruction2` in
115 // this instruction list and false otherwise. Abort if none
116 // of these instructions is found.
117 bool FoundBefore(const HInstruction* instruction1,
118 const HInstruction* instruction2) const;
119
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000120 bool IsEmpty() const { return first_instruction_ == nullptr; }
121 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
122
123 // Update the block of all instructions to be `block`.
124 void SetBlockOfInstructions(HBasicBlock* block) const;
125
126 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
127 void Add(const HInstructionList& instruction_list);
128
David Brazdil2d7352b2015-04-20 14:52:42 +0100129 // Return the number of instructions in the list. This is an expensive operation.
130 size_t CountSize() const;
131
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100132 private:
133 HInstruction* first_instruction_;
134 HInstruction* last_instruction_;
135
136 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000137 friend class HGraph;
138 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100139 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100140 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100141
142 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
143};
144
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000145// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100146class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000147 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100148 HGraph(ArenaAllocator* arena,
149 const DexFile& dex_file,
150 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100151 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700152 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100153 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100154 bool debuggable = false,
155 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000156 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100157 blocks_(arena->Adapter(kArenaAllocBlockList)),
158 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
159 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700160 entry_block_(nullptr),
161 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100162 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100163 number_of_vregs_(0),
164 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000165 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400166 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000167 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000168 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000169 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100170 dex_file_(dex_file),
171 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100172 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100173 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100174 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700175 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000176 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100177 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
178 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
179 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
180 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
181 cached_current_method_(nullptr) {
182 blocks_.reserve(kDefaultNumberOfBlocks);
183 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000184
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000185 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100186 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
187
David Brazdil69ba7b72015-06-23 18:27:30 +0100188 bool IsInSsaForm() const { return in_ssa_form_; }
189
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000190 HBasicBlock* GetEntryBlock() const { return entry_block_; }
191 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100192 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000193
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000194 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
195 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000196
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000197 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 // Try building the SSA form of this graph, with dominance computation and loop
200 // recognition. Returns whether it was successful in doing all these steps.
201 bool TryBuildingSsa() {
202 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000203 // The SSA builder requires loops to all be natural. Specifically, the dead phi
204 // elimination phase checks the consistency of the graph when doing a post-order
205 // visit for eliminating dead phis: a dead phi can only have loop header phi
206 // users remaining when being visited.
207 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100208 // Precompute per-block try membership before entering the SSA builder,
209 // which needs the information to build catch block phis from values of
210 // locals at throwing instructions inside try blocks.
211 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100213 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000214 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215 }
216
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100217 void ComputeDominanceInformation();
218 void ClearDominanceInformation();
219
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000220 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000221 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100222 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100223 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000224
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000225 // Analyze all natural loops in this graph. Returns false if one
226 // loop is not natural, that is the header does not dominate the
227 // back edge.
228 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100229
David Brazdilffee3d32015-07-06 11:48:53 +0100230 // Iterate over blocks to compute try block membership. Needs reverse post
231 // order and loop information.
232 void ComputeTryBlockInformation();
233
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000234 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000235 // Returns the instruction used to replace the invoke expression or null if the
236 // invoke is for a void method.
237 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000238
Mingyao Yang3584bce2015-05-19 16:01:59 -0700239 // Need to add a couple of blocks to test if the loop body is entered and
240 // put deoptimization instructions, etc.
241 void TransformLoopHeaderForBCE(HBasicBlock* header);
242
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000243 // Removes `block` from the graph. Assumes `block` has been disconnected from
244 // other blocks and has no instructions or phis.
245 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000246
David Brazdilfc6a86a2015-06-26 10:33:45 +0000247 // Splits the edge between `block` and `successor` while preserving the
248 // indices in the predecessor/successor lists. If there are multiple edges
249 // between the blocks, the lowest indices are used.
250 // Returns the new block which is empty and has the same dex pc as `successor`.
251 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
252
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100253 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
254 void SimplifyLoop(HBasicBlock* header);
255
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000256 int32_t GetNextInstructionId() {
257 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000258 return current_instruction_id_++;
259 }
260
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000261 int32_t GetCurrentInstructionId() const {
262 return current_instruction_id_;
263 }
264
265 void SetCurrentInstructionId(int32_t id) {
266 current_instruction_id_ = id;
267 }
268
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100269 uint16_t GetMaximumNumberOfOutVRegs() const {
270 return maximum_number_of_out_vregs_;
271 }
272
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000273 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
274 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100275 }
276
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100277 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
278 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
279 }
280
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000281 void UpdateTemporariesVRegSlots(size_t slots) {
282 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100283 }
284
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000285 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100286 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000287 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100288 }
289
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100290 void SetNumberOfVRegs(uint16_t number_of_vregs) {
291 number_of_vregs_ = number_of_vregs;
292 }
293
294 uint16_t GetNumberOfVRegs() const {
295 return number_of_vregs_;
296 }
297
298 void SetNumberOfInVRegs(uint16_t value) {
299 number_of_in_vregs_ = value;
300 }
301
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100302 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100303 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100304 return number_of_vregs_ - number_of_in_vregs_;
305 }
306
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100307 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100308 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100309 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100310
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100311 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100312 return linear_order_;
313 }
314
Mark Mendell1152c922015-04-24 17:06:35 -0400315 bool HasBoundsChecks() const {
316 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800317 }
318
Mark Mendell1152c922015-04-24 17:06:35 -0400319 void SetHasBoundsChecks(bool value) {
320 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800321 }
322
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100323 bool ShouldGenerateConstructorBarrier() const {
324 return should_generate_constructor_barrier_;
325 }
326
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000327 bool IsDebuggable() const { return debuggable_; }
328
David Brazdil8d5b8b22015-03-24 10:51:52 +0000329 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000330 // already, it is created and inserted into the graph. This method is only for
331 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600332 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000333
334 // TODO: This is problematic for the consistency of reference type propagation
335 // because it can be created anytime after the pass and thus it will be left
336 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600337 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000338
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600339 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
340 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000341 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600342 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
343 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000344 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600345 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
346 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000347 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600348 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
349 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000350 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000351
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100352 HCurrentMethod* GetCurrentMethod();
353
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100354 const DexFile& GetDexFile() const {
355 return dex_file_;
356 }
357
358 uint32_t GetMethodIdx() const {
359 return method_idx_;
360 }
361
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100362 InvokeType GetInvokeType() const {
363 return invoke_type_;
364 }
365
Mark Mendellc4701932015-04-10 13:18:51 -0400366 InstructionSet GetInstructionSet() const {
367 return instruction_set_;
368 }
369
David Brazdil77a48ae2015-09-15 12:34:04 +0000370 bool HasTryCatch() const { return has_try_catch_; }
371 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100372
David Brazdil2d7352b2015-04-20 14:52:42 +0100373 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100374 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000375 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100376 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000377
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000378 template <class InstructionType, typename ValueType>
379 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600380 ArenaSafeMap<ValueType, InstructionType*>* cache,
381 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000382 // Try to find an existing constant of the given value.
383 InstructionType* constant = nullptr;
384 auto cached_constant = cache->find(value);
385 if (cached_constant != cache->end()) {
386 constant = cached_constant->second;
387 }
388
389 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100390 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000391 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600392 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000393 cache->Overwrite(value, constant);
394 InsertConstant(constant);
395 }
396 return constant;
397 }
398
David Brazdil8d5b8b22015-03-24 10:51:52 +0000399 void InsertConstant(HConstant* instruction);
400
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000401 // Cache a float constant into the graph. This method should only be
402 // called by the SsaBuilder when creating "equivalent" instructions.
403 void CacheFloatConstant(HFloatConstant* constant);
404
405 // See CacheFloatConstant comment.
406 void CacheDoubleConstant(HDoubleConstant* constant);
407
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000408 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000409
410 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100411 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000412
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100413 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100414 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000415
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100416 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100417 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100418
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000419 HBasicBlock* entry_block_;
420 HBasicBlock* exit_block_;
421
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100422 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100423 uint16_t maximum_number_of_out_vregs_;
424
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100425 // The number of virtual registers in this method. Contains the parameters.
426 uint16_t number_of_vregs_;
427
428 // The number of virtual registers used by parameters of this method.
429 uint16_t number_of_in_vregs_;
430
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000431 // Number of vreg size slots that the temporaries use (used in baseline compiler).
432 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100433
Mark Mendell1152c922015-04-24 17:06:35 -0400434 // Has bounds checks. We can totally skip BCE if it's false.
435 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800436
David Brazdil77a48ae2015-09-15 12:34:04 +0000437 // Flag whether there are any try/catch blocks in the graph. We will skip
438 // try/catch-related passes if false.
439 bool has_try_catch_;
440
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000441 // Indicates whether the graph should be compiled in a way that
442 // ensures full debuggability. If false, we can apply more
443 // aggressive optimizations that may limit the level of debugging.
444 const bool debuggable_;
445
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000446 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000447 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000448
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100449 // The dex file from which the method is from.
450 const DexFile& dex_file_;
451
452 // The method index in the dex file.
453 const uint32_t method_idx_;
454
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100455 // If inlined, this encodes how the callee is being invoked.
456 const InvokeType invoke_type_;
457
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100458 // Whether the graph has been transformed to SSA form. Only used
459 // in debug mode to ensure we are not using properties only valid
460 // for non-SSA form (like the number of temporaries).
461 bool in_ssa_form_;
462
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100463 const bool should_generate_constructor_barrier_;
464
Mathieu Chartiere401d142015-04-22 13:56:20 -0700465 const InstructionSet instruction_set_;
466
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000467 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000468 HNullConstant* cached_null_constant_;
469 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000470 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000471 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000472 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000473
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100474 HCurrentMethod* cached_current_method_;
475
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000476 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100477 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000478 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000479 DISALLOW_COPY_AND_ASSIGN(HGraph);
480};
481
Vladimir Markof9f64412015-09-02 14:05:49 +0100482class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000483 public:
484 HLoopInformation(HBasicBlock* header, HGraph* graph)
485 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100486 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100487 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100488 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100489 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
490 back_edges_.reserve(kDefaultNumberOfBackEdges);
491 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100492
493 HBasicBlock* GetHeader() const {
494 return header_;
495 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000496
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000497 void SetHeader(HBasicBlock* block) {
498 header_ = block;
499 }
500
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100501 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
502 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
503 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
504
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000505 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100506 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000507 }
508
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100509 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100510 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100511 }
512
David Brazdil46e2a392015-03-16 17:31:52 +0000513 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100514 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100515 }
516
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000517 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100518 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000519 }
520
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100521 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100522
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100523 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100524 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100525 }
526
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100527 // Returns the lifetime position of the back edge that has the
528 // greatest lifetime position.
529 size_t GetLifetimeEnd() const;
530
531 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100532 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100533 }
534
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100535 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100536 // that is the header dominates the back edge.
537 bool Populate();
538
David Brazdila4b8c212015-05-07 09:59:30 +0100539 // Reanalyzes the loop by removing loop info from its blocks and re-running
540 // Populate(). If there are no back edges left, the loop info is completely
541 // removed as well as its SuspendCheck instruction. It must be run on nested
542 // inner loops first.
543 void Update();
544
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100545 // Returns whether this loop information contains `block`.
546 // Note that this loop information *must* be populated before entering this function.
547 bool Contains(const HBasicBlock& block) const;
548
549 // Returns whether this loop information is an inner loop of `other`.
550 // Note that `other` *must* be populated before entering this function.
551 bool IsIn(const HLoopInformation& other) const;
552
Aart Bik73f1f3b2015-10-28 15:28:08 -0700553 // Returns true if instruction is not defined within this loop or any loop nested inside
554 // this loop. If must_dominate is set, only definitions that actually dominate the loop
555 // header can be invariant. Otherwise, any definition outside the loop, including
556 // definitions that appear after the loop, is invariant.
557 bool IsLoopInvariant(HInstruction* instruction, bool must_dominate) const;
558
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100559 const ArenaBitVector& GetBlocks() const { return blocks_; }
560
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000561 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000562 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000563
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000564 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100565 // Internal recursive implementation of `Populate`.
566 void PopulateRecursive(HBasicBlock* block);
567
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000568 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100569 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100570 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100571 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000572
573 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
574};
575
David Brazdilec16f792015-08-19 15:04:01 +0100576// Stores try/catch information for basic blocks.
577// Note that HGraph is constructed so that catch blocks cannot simultaneously
578// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100579class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100580 public:
581 // Try block information constructor.
582 explicit TryCatchInformation(const HTryBoundary& try_entry)
583 : try_entry_(&try_entry),
584 catch_dex_file_(nullptr),
585 catch_type_index_(DexFile::kDexNoIndex16) {
586 DCHECK(try_entry_ != nullptr);
587 }
588
589 // Catch block information constructor.
590 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
591 : try_entry_(nullptr),
592 catch_dex_file_(&dex_file),
593 catch_type_index_(catch_type_index) {}
594
595 bool IsTryBlock() const { return try_entry_ != nullptr; }
596
597 const HTryBoundary& GetTryEntry() const {
598 DCHECK(IsTryBlock());
599 return *try_entry_;
600 }
601
602 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
603
604 bool IsCatchAllTypeIndex() const {
605 DCHECK(IsCatchBlock());
606 return catch_type_index_ == DexFile::kDexNoIndex16;
607 }
608
609 uint16_t GetCatchTypeIndex() const {
610 DCHECK(IsCatchBlock());
611 return catch_type_index_;
612 }
613
614 const DexFile& GetCatchDexFile() const {
615 DCHECK(IsCatchBlock());
616 return *catch_dex_file_;
617 }
618
619 private:
620 // One of possibly several TryBoundary instructions entering the block's try.
621 // Only set for try blocks.
622 const HTryBoundary* try_entry_;
623
624 // Exception type information. Only set for catch blocks.
625 const DexFile* catch_dex_file_;
626 const uint16_t catch_type_index_;
627};
628
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100629static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100630static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100631
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632// A block in a method. Contains the list of instructions represented
633// as a double linked list. Each block knows its predecessors and
634// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100635
Vladimir Markof9f64412015-09-02 14:05:49 +0100636class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000637 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600638 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000639 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000640 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
641 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000642 loop_information_(nullptr),
643 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000644 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100645 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100646 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100647 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000648 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000649 try_catch_information_(nullptr) {
650 predecessors_.reserve(kDefaultNumberOfPredecessors);
651 successors_.reserve(kDefaultNumberOfSuccessors);
652 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
653 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000654
Vladimir Marko60584552015-09-03 13:35:12 +0000655 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100656 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000657 }
658
Vladimir Marko60584552015-09-03 13:35:12 +0000659 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100660 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000661 }
662
Vladimir Marko60584552015-09-03 13:35:12 +0000663 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
664 return ContainsElement(successors_, block, start_from);
665 }
666
667 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100668 return dominated_blocks_;
669 }
670
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100671 bool IsEntryBlock() const {
672 return graph_->GetEntryBlock() == this;
673 }
674
675 bool IsExitBlock() const {
676 return graph_->GetExitBlock() == this;
677 }
678
David Brazdil46e2a392015-03-16 17:31:52 +0000679 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000680 bool IsSingleTryBoundary() const;
681
682 // Returns true if this block emits nothing but a jump.
683 bool IsSingleJump() const {
684 HLoopInformation* loop_info = GetLoopInformation();
685 return (IsSingleGoto() || IsSingleTryBoundary())
686 // Back edges generate a suspend check.
687 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
688 }
David Brazdil46e2a392015-03-16 17:31:52 +0000689
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000690 void AddBackEdge(HBasicBlock* back_edge) {
691 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000692 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000693 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100694 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000695 loop_information_->AddBackEdge(back_edge);
696 }
697
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000698 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000699 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000700
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100701 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000702 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600703 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000704
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000705 HBasicBlock* GetDominator() const { return dominator_; }
706 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000707 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
708
709 void RemoveDominatedBlock(HBasicBlock* block) {
710 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100711 }
Vladimir Marko60584552015-09-03 13:35:12 +0000712
713 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
714 ReplaceElement(dominated_blocks_, existing, new_block);
715 }
716
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100717 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000718
719 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100720 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000721 }
722
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100723 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
724 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100725 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100726 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100727 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
728 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000729
730 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000731 successors_.push_back(block);
732 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000733 }
734
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100735 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
736 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100737 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000738 new_block->predecessors_.push_back(this);
739 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000740 }
741
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000742 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
743 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000744 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000745 new_block->successors_.push_back(this);
746 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000747 }
748
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100749 // Insert `this` between `predecessor` and `successor. This method
750 // preserves the indicies, and will update the first edge found between
751 // `predecessor` and `successor`.
752 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
753 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100754 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000755 successor->predecessors_[predecessor_index] = this;
756 predecessor->successors_[successor_index] = this;
757 successors_.push_back(successor);
758 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100759 }
760
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100761 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000762 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100763 }
764
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000765 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000766 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000767 }
768
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100769 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000770 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100771 }
772
773 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000774 predecessors_.push_back(block);
775 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100776 }
777
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100778 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000779 DCHECK_EQ(predecessors_.size(), 2u);
780 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100781 }
782
David Brazdil769c9e52015-04-27 13:54:09 +0100783 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000784 DCHECK_EQ(successors_.size(), 2u);
785 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100786 }
787
David Brazdilfc6a86a2015-06-26 10:33:45 +0000788 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000789 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100790 }
791
David Brazdilfc6a86a2015-06-26 10:33:45 +0000792 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000793 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100794 }
795
David Brazdilfc6a86a2015-06-26 10:33:45 +0000796 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000797 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100798 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000799 }
800
801 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000802 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100803 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000804 }
805
806 // Returns whether the first occurrence of `predecessor` in the list of
807 // predecessors is at index `idx`.
808 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100809 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000810 return GetPredecessorIndexOf(predecessor) == idx;
811 }
812
David Brazdilffee3d32015-07-06 11:48:53 +0100813 // Returns the number of non-exceptional successors. SsaChecker ensures that
814 // these are stored at the beginning of the successor list.
815 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000816 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100817 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000818
David Brazdild7558da2015-09-22 13:04:14 +0100819 // Create a new block between this block and its predecessors. The new block
820 // is added to the graph, all predecessor edges are relinked to it and an edge
821 // is created to `this`. Returns the new empty block. Reverse post order or
822 // loop and try/catch information are not updated.
823 HBasicBlock* CreateImmediateDominator();
824
David Brazdilfc6a86a2015-06-26 10:33:45 +0000825 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100826 // created, latter block. Note that this method will add the block to the
827 // graph, create a Goto at the end of the former block and will create an edge
828 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100829 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000830 HBasicBlock* SplitBefore(HInstruction* cursor);
831
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000832 // Split the block into two blocks just after `cursor`. Returns the newly
833 // created block. Note that this method just updates raw block information,
834 // like predecessors, successors, dominators, and instruction list. It does not
835 // update the graph, reverse post order, loop information, nor make sure the
836 // blocks are consistent (for example ending with a control flow instruction).
837 HBasicBlock* SplitAfter(HInstruction* cursor);
838
David Brazdil9bc43612015-11-05 21:25:24 +0000839 // Split catch block into two blocks after the original move-exception bytecode
840 // instruction, or at the beginning if not present. Returns the newly created,
841 // latter block, or nullptr if such block could not be created (must be dead
842 // in that case). Note that this method just updates raw block information,
843 // like predecessors, successors, dominators, and instruction list. It does not
844 // update the graph, reverse post order, loop information, nor make sure the
845 // blocks are consistent (for example ending with a control flow instruction).
846 HBasicBlock* SplitCatchBlockAfterMoveException();
847
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000848 // Merge `other` at the end of `this`. Successors and dominated blocks of
849 // `other` are changed to be successors and dominated blocks of `this`. Note
850 // that this method does not update the graph, reverse post order, loop
851 // information, nor make sure the blocks are consistent (for example ending
852 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100853 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000854
855 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
856 // of `this` are moved to `other`.
857 // Note that this method does not update the graph, reverse post order, loop
858 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000859 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000860 void ReplaceWith(HBasicBlock* other);
861
David Brazdil2d7352b2015-04-20 14:52:42 +0100862 // Merge `other` at the end of `this`. This method updates loops, reverse post
863 // order, links to predecessors, successors, dominators and deletes the block
864 // from the graph. The two blocks must be successive, i.e. `this` the only
865 // predecessor of `other` and vice versa.
866 void MergeWith(HBasicBlock* other);
867
868 // Disconnects `this` from all its predecessors, successors and dominator,
869 // removes it from all loops it is included in and eventually from the graph.
870 // The block must not dominate any other block. Predecessors and successors
871 // are safely updated.
872 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000873
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000874 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100875 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100876 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100877 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100878 // Replace instruction `initial` with `replacement` within this block.
879 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
880 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100881 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100882 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000883 // RemoveInstruction and RemovePhi delete a given instruction from the respective
884 // instruction list. With 'ensure_safety' set to true, it verifies that the
885 // instruction is not in use and removes it from the use lists of its inputs.
886 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
887 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100888 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100889
890 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100891 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100892 }
893
Roland Levillain6b879dd2014-09-22 17:13:44 +0100894 bool IsLoopPreHeaderFirstPredecessor() const {
895 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100896 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100897 }
898
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100899 HLoopInformation* GetLoopInformation() const {
900 return loop_information_;
901 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000902
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000903 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100904 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000905 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100906 void SetInLoop(HLoopInformation* info) {
907 if (IsLoopHeader()) {
908 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100909 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100910 loop_information_ = info;
911 } else if (loop_information_->Contains(*info->GetHeader())) {
912 // Block is currently part of an outer loop. Make it part of this inner loop.
913 // Note that a non loop header having a loop information means this loop information
914 // has already been populated
915 loop_information_ = info;
916 } else {
917 // Block is part of an inner loop. Do not update the loop information.
918 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
919 // at this point, because this method is being called while populating `info`.
920 }
921 }
922
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000923 // Raw update of the loop information.
924 void SetLoopInformation(HLoopInformation* info) {
925 loop_information_ = info;
926 }
927
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100928 bool IsInLoop() const { return loop_information_ != nullptr; }
929
David Brazdilec16f792015-08-19 15:04:01 +0100930 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
931
932 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
933 try_catch_information_ = try_catch_information;
934 }
935
936 bool IsTryBlock() const {
937 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
938 }
939
940 bool IsCatchBlock() const {
941 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
942 }
David Brazdilffee3d32015-07-06 11:48:53 +0100943
944 // Returns the try entry that this block's successors should have. They will
945 // be in the same try, unless the block ends in a try boundary. In that case,
946 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100947 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100948
David Brazdild7558da2015-09-22 13:04:14 +0100949 bool HasThrowingInstructions() const;
950
David Brazdila4b8c212015-05-07 09:59:30 +0100951 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100952 bool Dominates(HBasicBlock* block) const;
953
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100954 size_t GetLifetimeStart() const { return lifetime_start_; }
955 size_t GetLifetimeEnd() const { return lifetime_end_; }
956
957 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
958 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
959
David Brazdil8d5b8b22015-03-24 10:51:52 +0000960 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000961 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100962 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000963 bool HasSinglePhi() const;
964
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000965 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000966 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000967 ArenaVector<HBasicBlock*> predecessors_;
968 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100969 HInstructionList instructions_;
970 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000971 HLoopInformation* loop_information_;
972 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000973 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100974 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100975 // The dex program counter of the first instruction of this block.
976 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100977 size_t lifetime_start_;
978 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100979 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100980
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000981 friend class HGraph;
982 friend class HInstruction;
983
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000984 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
985};
986
David Brazdilb2bd1c52015-03-25 11:17:37 +0000987// Iterates over the LoopInformation of all loops which contain 'block'
988// from the innermost to the outermost.
989class HLoopInformationOutwardIterator : public ValueObject {
990 public:
991 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
992 : current_(block.GetLoopInformation()) {}
993
994 bool Done() const { return current_ == nullptr; }
995
996 void Advance() {
997 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100998 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000999 }
1000
1001 HLoopInformation* Current() const {
1002 DCHECK(!Done());
1003 return current_;
1004 }
1005
1006 private:
1007 HLoopInformation* current_;
1008
1009 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1010};
1011
Alexandre Ramesef20f712015-06-09 10:29:30 +01001012#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001013 M(Above, Condition) \
1014 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001015 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001016 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001017 M(ArrayGet, Instruction) \
1018 M(ArrayLength, Instruction) \
1019 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001020 M(Below, Condition) \
1021 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001022 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001023 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001024 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001025 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001026 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001027 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001028 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001029 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001030 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001031 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001032 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001033 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001034 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001035 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001036 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001037 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001038 M(FloatConstant, Constant) \
1039 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001040 M(GreaterThan, Condition) \
1041 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001042 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001043 M(InstanceFieldGet, Instruction) \
1044 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001045 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001046 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001047 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001048 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001049 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001050 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001051 M(LessThan, Condition) \
1052 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001053 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001054 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001055 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001056 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001057 M(Local, Instruction) \
1058 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001059 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001060 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001061 M(Mul, BinaryOperation) \
1062 M(Neg, UnaryOperation) \
1063 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001064 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001065 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001066 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001067 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001068 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001069 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001070 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001071 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001072 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001073 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001074 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001075 M(Return, Instruction) \
1076 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001077 M(Shl, BinaryOperation) \
1078 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001079 M(StaticFieldGet, Instruction) \
1080 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001081 M(UnresolvedInstanceFieldGet, Instruction) \
1082 M(UnresolvedInstanceFieldSet, Instruction) \
1083 M(UnresolvedStaticFieldGet, Instruction) \
1084 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001085 M(StoreLocal, Instruction) \
1086 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001087 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001088 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001089 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001090 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001091 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001092 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001093 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001094
Alexandre Ramesef20f712015-06-09 10:29:30 +01001095#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1096
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001097#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001098#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001099#else
1100#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1101 M(Arm64IntermediateAddress, Instruction)
1102#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001103
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001104#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1105
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001106#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1107
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001108#ifndef ART_ENABLE_CODEGEN_x86
1109#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1110#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001111#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1112 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001113 M(X86LoadFromConstantTable, Instruction) \
1114 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001115#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001116
1117#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1118
1119#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1120 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1121 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1122 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001123 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001124 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001125 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1126 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1127
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001128#define FOR_EACH_INSTRUCTION(M) \
1129 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1130 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001131 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001132 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001133 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001134
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001135#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001136FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1137#undef FORWARD_DECLARATION
1138
Roland Levillainccc07a92014-09-16 14:48:16 +01001139#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001140 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1141 const char* DebugName() const OVERRIDE { return #type; } \
1142 const H##type* As##type() const OVERRIDE { return this; } \
1143 H##type* As##type() OVERRIDE { return this; } \
1144 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001145 return other->Is##type(); \
1146 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001147 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001148
David Brazdiled596192015-01-23 10:39:45 +00001149template <typename T> class HUseList;
1150
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001151template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001152class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001153 public:
David Brazdiled596192015-01-23 10:39:45 +00001154 HUseListNode* GetPrevious() const { return prev_; }
1155 HUseListNode* GetNext() const { return next_; }
1156 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001157 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001158 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001159
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001160 private:
David Brazdiled596192015-01-23 10:39:45 +00001161 HUseListNode(T user, size_t index)
1162 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1163
1164 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001165 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001166 HUseListNode<T>* prev_;
1167 HUseListNode<T>* next_;
1168
1169 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001170
1171 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1172};
1173
David Brazdiled596192015-01-23 10:39:45 +00001174template <typename T>
1175class HUseList : public ValueObject {
1176 public:
1177 HUseList() : first_(nullptr) {}
1178
1179 void Clear() {
1180 first_ = nullptr;
1181 }
1182
1183 // Adds a new entry at the beginning of the use list and returns
1184 // the newly created node.
1185 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001186 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001187 if (IsEmpty()) {
1188 first_ = new_node;
1189 } else {
1190 first_->prev_ = new_node;
1191 new_node->next_ = first_;
1192 first_ = new_node;
1193 }
1194 return new_node;
1195 }
1196
1197 HUseListNode<T>* GetFirst() const {
1198 return first_;
1199 }
1200
1201 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001202 DCHECK(node != nullptr);
1203 DCHECK(Contains(node));
1204
David Brazdiled596192015-01-23 10:39:45 +00001205 if (node->prev_ != nullptr) {
1206 node->prev_->next_ = node->next_;
1207 }
1208 if (node->next_ != nullptr) {
1209 node->next_->prev_ = node->prev_;
1210 }
1211 if (node == first_) {
1212 first_ = node->next_;
1213 }
1214 }
1215
David Brazdil1abb4192015-02-17 18:33:36 +00001216 bool Contains(const HUseListNode<T>* node) const {
1217 if (node == nullptr) {
1218 return false;
1219 }
1220 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1221 if (current == node) {
1222 return true;
1223 }
1224 }
1225 return false;
1226 }
1227
David Brazdiled596192015-01-23 10:39:45 +00001228 bool IsEmpty() const {
1229 return first_ == nullptr;
1230 }
1231
1232 bool HasOnlyOneUse() const {
1233 return first_ != nullptr && first_->next_ == nullptr;
1234 }
1235
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001236 size_t SizeSlow() const {
1237 size_t count = 0;
1238 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1239 ++count;
1240 }
1241 return count;
1242 }
1243
David Brazdiled596192015-01-23 10:39:45 +00001244 private:
1245 HUseListNode<T>* first_;
1246};
1247
1248template<typename T>
1249class HUseIterator : public ValueObject {
1250 public:
1251 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1252
1253 bool Done() const { return current_ == nullptr; }
1254
1255 void Advance() {
1256 DCHECK(!Done());
1257 current_ = current_->GetNext();
1258 }
1259
1260 HUseListNode<T>* Current() const {
1261 DCHECK(!Done());
1262 return current_;
1263 }
1264
1265 private:
1266 HUseListNode<T>* current_;
1267
1268 friend class HValue;
1269};
1270
David Brazdil1abb4192015-02-17 18:33:36 +00001271// This class is used by HEnvironment and HInstruction classes to record the
1272// instructions they use and pointers to the corresponding HUseListNodes kept
1273// by the used instructions.
1274template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001275class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001276 public:
1277 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1278 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1279
1280 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1281 : instruction_(old_record.instruction_), use_node_(use_node) {
1282 DCHECK(instruction_ != nullptr);
1283 DCHECK(use_node_ != nullptr);
1284 DCHECK(old_record.use_node_ == nullptr);
1285 }
1286
1287 HInstruction* GetInstruction() const { return instruction_; }
1288 HUseListNode<T>* GetUseNode() const { return use_node_; }
1289
1290 private:
1291 // Instruction used by the user.
1292 HInstruction* instruction_;
1293
1294 // Corresponding entry in the use list kept by 'instruction_'.
1295 HUseListNode<T>* use_node_;
1296};
1297
Aart Bik854a02b2015-07-14 16:07:00 -07001298/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001299 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001300 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001301 * For write/read dependences on fields/arrays, the dependence analysis uses
1302 * type disambiguation (e.g. a float field write cannot modify the value of an
1303 * integer field read) and the access type (e.g. a reference array write cannot
1304 * modify the value of a reference field read [although it may modify the
1305 * reference fetch prior to reading the field, which is represented by its own
1306 * write/read dependence]). The analysis makes conservative points-to
1307 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1308 * the same, and any reference read depends on any reference read without
1309 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001310 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001311 * The internal representation uses 38-bit and is described in the table below.
1312 * The first line indicates the side effect, and for field/array accesses the
1313 * second line indicates the type of the access (in the order of the
1314 * Primitive::Type enum).
1315 * The two numbered lines below indicate the bit position in the bitfield (read
1316 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001317 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001318 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1319 * +-------------+---------+---------+--------------+---------+---------+
1320 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1321 * | 3 |333333322|222222221| 1 |111111110|000000000|
1322 * | 7 |654321098|765432109| 8 |765432109|876543210|
1323 *
1324 * Note that, to ease the implementation, 'changes' bits are least significant
1325 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001326 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001327class SideEffects : public ValueObject {
1328 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001329 SideEffects() : flags_(0) {}
1330
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001331 static SideEffects None() {
1332 return SideEffects(0);
1333 }
1334
1335 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001336 return SideEffects(kAllChangeBits | kAllDependOnBits);
1337 }
1338
1339 static SideEffects AllChanges() {
1340 return SideEffects(kAllChangeBits);
1341 }
1342
1343 static SideEffects AllDependencies() {
1344 return SideEffects(kAllDependOnBits);
1345 }
1346
1347 static SideEffects AllExceptGCDependency() {
1348 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1349 }
1350
1351 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001352 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001353 }
1354
Aart Bik34c3ba92015-07-20 14:08:59 -07001355 static SideEffects AllWrites() {
1356 return SideEffects(kAllWrites);
1357 }
1358
1359 static SideEffects AllReads() {
1360 return SideEffects(kAllReads);
1361 }
1362
1363 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1364 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001365 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001366 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001367 }
1368
Aart Bik854a02b2015-07-14 16:07:00 -07001369 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1370 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001371 }
1372
Aart Bik34c3ba92015-07-20 14:08:59 -07001373 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1374 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001375 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001376 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001377 }
1378
1379 static SideEffects ArrayReadOfType(Primitive::Type type) {
1380 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1381 }
1382
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001383 static SideEffects CanTriggerGC() {
1384 return SideEffects(1ULL << kCanTriggerGCBit);
1385 }
1386
1387 static SideEffects DependsOnGC() {
1388 return SideEffects(1ULL << kDependsOnGCBit);
1389 }
1390
Aart Bik854a02b2015-07-14 16:07:00 -07001391 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001392 SideEffects Union(SideEffects other) const {
1393 return SideEffects(flags_ | other.flags_);
1394 }
1395
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001396 SideEffects Exclusion(SideEffects other) const {
1397 return SideEffects(flags_ & ~other.flags_);
1398 }
1399
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001400 void Add(SideEffects other) {
1401 flags_ |= other.flags_;
1402 }
1403
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001404 bool Includes(SideEffects other) const {
1405 return (other.flags_ & flags_) == other.flags_;
1406 }
1407
1408 bool HasSideEffects() const {
1409 return (flags_ & kAllChangeBits);
1410 }
1411
1412 bool HasDependencies() const {
1413 return (flags_ & kAllDependOnBits);
1414 }
1415
1416 // Returns true if there are no side effects or dependencies.
1417 bool DoesNothing() const {
1418 return flags_ == 0;
1419 }
1420
Aart Bik854a02b2015-07-14 16:07:00 -07001421 // Returns true if something is written.
1422 bool DoesAnyWrite() const {
1423 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001424 }
1425
Aart Bik854a02b2015-07-14 16:07:00 -07001426 // Returns true if something is read.
1427 bool DoesAnyRead() const {
1428 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001429 }
1430
Aart Bik854a02b2015-07-14 16:07:00 -07001431 // Returns true if potentially everything is written and read
1432 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001433 bool DoesAllReadWrite() const {
1434 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1435 }
1436
Aart Bik854a02b2015-07-14 16:07:00 -07001437 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001438 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001439 }
1440
1441 // Returns true if this may read something written by other.
1442 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001443 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1444 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001445 }
1446
1447 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001448 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001449 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001450 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001451 for (int s = kLastBit; s >= 0; s--) {
1452 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1453 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1454 // This is a bit for the GC side effect.
1455 if (current_bit_is_set) {
1456 flags += "GC";
1457 }
Aart Bik854a02b2015-07-14 16:07:00 -07001458 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001459 } else {
1460 // This is a bit for the array/field analysis.
1461 // The underscore character stands for the 'can trigger GC' bit.
1462 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1463 if (current_bit_is_set) {
1464 flags += kDebug[s];
1465 }
1466 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1467 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1468 flags += "|";
1469 }
1470 }
Aart Bik854a02b2015-07-14 16:07:00 -07001471 }
1472 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001473 }
1474
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001475 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001476
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001477 private:
1478 static constexpr int kFieldArrayAnalysisBits = 9;
1479
1480 static constexpr int kFieldWriteOffset = 0;
1481 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1482 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1483 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1484
1485 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1486
1487 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1488 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1489 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1490 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1491
1492 static constexpr int kLastBit = kDependsOnGCBit;
1493 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1494
1495 // Aliases.
1496
1497 static_assert(kChangeBits == kDependOnBits,
1498 "the 'change' bits should match the 'depend on' bits.");
1499
1500 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1501 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1502 static constexpr uint64_t kAllWrites =
1503 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1504 static constexpr uint64_t kAllReads =
1505 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001506
Aart Bik854a02b2015-07-14 16:07:00 -07001507 // Work around the fact that HIR aliases I/F and J/D.
1508 // TODO: remove this interceptor once HIR types are clean
1509 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1510 switch (type) {
1511 case Primitive::kPrimInt:
1512 case Primitive::kPrimFloat:
1513 return TypeFlag(Primitive::kPrimInt, offset) |
1514 TypeFlag(Primitive::kPrimFloat, offset);
1515 case Primitive::kPrimLong:
1516 case Primitive::kPrimDouble:
1517 return TypeFlag(Primitive::kPrimLong, offset) |
1518 TypeFlag(Primitive::kPrimDouble, offset);
1519 default:
1520 return TypeFlag(type, offset);
1521 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001522 }
1523
Aart Bik854a02b2015-07-14 16:07:00 -07001524 // Translates type to bit flag.
1525 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1526 CHECK_NE(type, Primitive::kPrimVoid);
1527 const uint64_t one = 1;
1528 const int shift = type; // 0-based consecutive enum
1529 DCHECK_LE(kFieldWriteOffset, shift);
1530 DCHECK_LT(shift, kArrayWriteOffset);
1531 return one << (type + offset);
1532 }
1533
1534 // Private constructor on direct flags value.
1535 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1536
1537 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001538};
1539
David Brazdiled596192015-01-23 10:39:45 +00001540// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001541class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001542 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001543 HEnvironment(ArenaAllocator* arena,
1544 size_t number_of_vregs,
1545 const DexFile& dex_file,
1546 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001547 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001548 InvokeType invoke_type,
1549 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001550 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1551 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001552 parent_(nullptr),
1553 dex_file_(dex_file),
1554 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001555 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001556 invoke_type_(invoke_type),
1557 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001558 }
1559
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001560 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001561 : HEnvironment(arena,
1562 to_copy.Size(),
1563 to_copy.GetDexFile(),
1564 to_copy.GetMethodIdx(),
1565 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001566 to_copy.GetInvokeType(),
1567 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001568
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001569 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001570 if (parent_ != nullptr) {
1571 parent_->SetAndCopyParentChain(allocator, parent);
1572 } else {
1573 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1574 parent_->CopyFrom(parent);
1575 if (parent->GetParent() != nullptr) {
1576 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1577 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001578 }
David Brazdiled596192015-01-23 10:39:45 +00001579 }
1580
Vladimir Marko71bf8092015-09-15 15:33:14 +01001581 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001582 void CopyFrom(HEnvironment* environment);
1583
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001584 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1585 // input to the loop phi instead. This is for inserting instructions that
1586 // require an environment (like HDeoptimization) in the loop pre-header.
1587 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001588
1589 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001590 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001591 }
1592
1593 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001594 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001595 }
1596
David Brazdil1abb4192015-02-17 18:33:36 +00001597 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001598
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001599 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001600
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001601 HEnvironment* GetParent() const { return parent_; }
1602
1603 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001604 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001605 }
1606
1607 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001608 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001609 }
1610
1611 uint32_t GetDexPc() const {
1612 return dex_pc_;
1613 }
1614
1615 uint32_t GetMethodIdx() const {
1616 return method_idx_;
1617 }
1618
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001619 InvokeType GetInvokeType() const {
1620 return invoke_type_;
1621 }
1622
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001623 const DexFile& GetDexFile() const {
1624 return dex_file_;
1625 }
1626
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001627 HInstruction* GetHolder() const {
1628 return holder_;
1629 }
1630
David Brazdiled596192015-01-23 10:39:45 +00001631 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001632 // Record instructions' use entries of this environment for constant-time removal.
1633 // It should only be called by HInstruction when a new environment use is added.
1634 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1635 DCHECK(env_use->GetUser() == this);
1636 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001637 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001638 }
David Brazdiled596192015-01-23 10:39:45 +00001639
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001640 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1641 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001642 HEnvironment* parent_;
1643 const DexFile& dex_file_;
1644 const uint32_t method_idx_;
1645 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001646 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001647
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001648 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001649 HInstruction* const holder_;
1650
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001651 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001652
1653 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1654};
1655
Calin Juravleacf735c2015-02-12 15:25:22 +00001656class ReferenceTypeInfo : ValueObject {
1657 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001658 typedef Handle<mirror::Class> TypeHandle;
1659
Calin Juravle2e768302015-07-28 14:41:11 +00001660 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1661 // The constructor will check that the type_handle is valid.
1662 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001663 }
1664
Calin Juravle2e768302015-07-28 14:41:11 +00001665 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1666
1667 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1668 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001669 }
1670
Calin Juravle2e768302015-07-28 14:41:11 +00001671 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1672 return IsValidHandle(type_handle_);
1673 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001674
Calin Juravleacf735c2015-02-12 15:25:22 +00001675 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001676
1677 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1678 DCHECK(IsValid());
1679 return GetTypeHandle()->IsObjectClass();
1680 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001681
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001682 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001683 DCHECK(IsValid());
1684 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001685 }
1686
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001687 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1688 DCHECK(IsValid());
1689 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1690 }
1691
Mathieu Chartier90443472015-07-16 20:32:27 -07001692 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001693 DCHECK(IsValid());
1694 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001695 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001696
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001697 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001698 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001699 return GetTypeHandle()->IsArrayClass();
1700 }
1701
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001702 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1703 DCHECK(IsValid());
1704 return GetTypeHandle()->IsPrimitiveArray();
1705 }
1706
1707 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1708 DCHECK(IsValid());
1709 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1710 }
1711
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001712 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001713 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001714 if (!IsExact()) return false;
1715 if (!IsArrayClass()) return false;
1716 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1717 }
1718
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001719 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1720 DCHECK(IsValid());
1721 if (!IsExact()) return false;
1722 if (!IsArrayClass()) return false;
1723 if (!rti.IsArrayClass()) return false;
1724 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1725 rti.GetTypeHandle()->GetComponentType());
1726 }
1727
Calin Juravleacf735c2015-02-12 15:25:22 +00001728 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1729
Mathieu Chartier90443472015-07-16 20:32:27 -07001730 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001731 DCHECK(IsValid());
1732 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001733 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1734 }
1735
1736 // Returns true if the type information provide the same amount of details.
1737 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001738 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001739 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001740 if (!IsValid() && !rti.IsValid()) {
1741 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001742 return true;
1743 }
Calin Juravle2e768302015-07-28 14:41:11 +00001744 if (!IsValid() || !rti.IsValid()) {
1745 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001746 return false;
1747 }
Calin Juravle2e768302015-07-28 14:41:11 +00001748 return IsExact() == rti.IsExact()
1749 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001750 }
1751
1752 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001753 ReferenceTypeInfo();
1754 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001755
Calin Juravleacf735c2015-02-12 15:25:22 +00001756 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001757 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001758 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001759 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001760 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001761};
1762
1763std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1764
Vladimir Markof9f64412015-09-02 14:05:49 +01001765class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001766 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001767 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001768 : previous_(nullptr),
1769 next_(nullptr),
1770 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001771 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001772 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001773 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001774 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001775 locations_(nullptr),
1776 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001777 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001778 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001779 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001780
Dave Allison20dfc792014-06-16 20:44:29 -07001781 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001782
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001783#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001784 enum InstructionKind {
1785 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1786 };
1787#undef DECLARE_KIND
1788
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001789 HInstruction* GetNext() const { return next_; }
1790 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001791
Calin Juravle77520bc2015-01-12 18:45:46 +00001792 HInstruction* GetNextDisregardingMoves() const;
1793 HInstruction* GetPreviousDisregardingMoves() const;
1794
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001795 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001796 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001797 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001798 bool IsInBlock() const { return block_ != nullptr; }
1799 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001800 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001801
Roland Levillain6b879dd2014-09-22 17:13:44 +01001802 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001803 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001804
1805 virtual void Accept(HGraphVisitor* visitor) = 0;
1806 virtual const char* DebugName() const = 0;
1807
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001808 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001809 void SetRawInputAt(size_t index, HInstruction* input) {
1810 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1811 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001812
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001813 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001814
1815 uint32_t GetDexPc() const { return dex_pc_; }
1816
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001817 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001818
Roland Levillaine161a2a2014-10-03 12:45:18 +01001819 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001820 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001821
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001822 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001823 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001824
Calin Juravle10e244f2015-01-26 18:54:32 +00001825 // Does not apply for all instructions, but having this at top level greatly
1826 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001827 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001828 virtual bool CanBeNull() const {
1829 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1830 return true;
1831 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001832
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001833 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001834 return false;
1835 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001836
Calin Juravle2e768302015-07-28 14:41:11 +00001837 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001838
Calin Juravle61d544b2015-02-23 16:46:57 +00001839 ReferenceTypeInfo GetReferenceTypeInfo() const {
1840 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1841 return reference_type_info_;
1842 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001843
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001844 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001845 DCHECK(user != nullptr);
1846 HUseListNode<HInstruction*>* use =
1847 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1848 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001849 }
1850
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001851 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001852 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001853 HUseListNode<HEnvironment*>* env_use =
1854 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1855 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001856 }
1857
David Brazdil1abb4192015-02-17 18:33:36 +00001858 void RemoveAsUserOfInput(size_t input) {
1859 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1860 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1861 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001862
David Brazdil1abb4192015-02-17 18:33:36 +00001863 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1864 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001865
David Brazdiled596192015-01-23 10:39:45 +00001866 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1867 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001868 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001869 bool HasOnlyOneNonEnvironmentUse() const {
1870 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1871 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001872
Roland Levillain6c82d402014-10-13 16:10:27 +01001873 // Does this instruction strictly dominate `other_instruction`?
1874 // Returns false if this instruction and `other_instruction` are the same.
1875 // Aborts if this instruction and `other_instruction` are both phis.
1876 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001877
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001878 int GetId() const { return id_; }
1879 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001880
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001881 int GetSsaIndex() const { return ssa_index_; }
1882 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1883 bool HasSsaIndex() const { return ssa_index_ != -1; }
1884
1885 bool HasEnvironment() const { return environment_ != nullptr; }
1886 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001887 // Set the `environment_` field. Raw because this method does not
1888 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001889 void SetRawEnvironment(HEnvironment* environment) {
1890 DCHECK(environment_ == nullptr);
1891 DCHECK_EQ(environment->GetHolder(), this);
1892 environment_ = environment;
1893 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001894
1895 // Set the environment of this instruction, copying it from `environment`. While
1896 // copying, the uses lists are being updated.
1897 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001898 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001899 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001900 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001901 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001902 if (environment->GetParent() != nullptr) {
1903 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1904 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001905 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001906
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001907 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1908 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001909 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001910 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001911 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001912 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001913 if (environment->GetParent() != nullptr) {
1914 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1915 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001916 }
1917
Nicolas Geoffray39468442014-09-02 15:17:15 +01001918 // Returns the number of entries in the environment. Typically, that is the
1919 // number of dex registers in a method. It could be more in case of inlining.
1920 size_t EnvironmentSize() const;
1921
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001922 LocationSummary* GetLocations() const { return locations_; }
1923 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001924
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001925 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001926 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001927
Alexandre Rames188d4312015-04-09 18:30:21 +01001928 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1929 // uses of this instruction by `other` are *not* updated.
1930 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1931 ReplaceWith(other);
1932 other->ReplaceInput(this, use_index);
1933 }
1934
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001935 // Move `this` instruction before `cursor`.
1936 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001937
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001938#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001939 bool Is##type() const { return (As##type() != nullptr); } \
1940 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001941 virtual H##type* As##type() { return nullptr; }
1942
1943 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1944#undef INSTRUCTION_TYPE_CHECK
1945
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001946 // Returns whether the instruction can be moved within the graph.
1947 virtual bool CanBeMoved() const { return false; }
1948
1949 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001950 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001951 return false;
1952 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001953
1954 // Returns whether any data encoded in the two instructions is equal.
1955 // This method does not look at the inputs. Both instructions must be
1956 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001957 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001958 return false;
1959 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001960
1961 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001962 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001963 // 2) Their inputs are identical.
1964 bool Equals(HInstruction* other) const;
1965
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001966 virtual InstructionKind GetKind() const = 0;
1967
1968 virtual size_t ComputeHashCode() const {
1969 size_t result = GetKind();
1970 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1971 result = (result * 31) + InputAt(i)->GetId();
1972 }
1973 return result;
1974 }
1975
1976 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001977 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001978
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001979 size_t GetLifetimePosition() const { return lifetime_position_; }
1980 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1981 LiveInterval* GetLiveInterval() const { return live_interval_; }
1982 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1983 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1984
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001985 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1986
1987 // Returns whether the code generation of the instruction will require to have access
1988 // to the current method. Such instructions are:
1989 // (1): Instructions that require an environment, as calling the runtime requires
1990 // to walk the stack and have the current method stored at a specific stack address.
1991 // (2): Object literals like classes and strings, that are loaded from the dex cache
1992 // fields of the current method.
1993 bool NeedsCurrentMethod() const {
1994 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1995 }
1996
Vladimir Markodc151b22015-10-15 18:02:30 +01001997 // Returns whether the code generation of the instruction will require to have access
1998 // to the dex cache of the current method's declaring class via the current method.
1999 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002000
Mark Mendellc4701932015-04-10 13:18:51 -04002001 // Does this instruction have any use in an environment before
2002 // control flow hits 'other'?
2003 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2004
2005 // Remove all references to environment uses of this instruction.
2006 // The caller must ensure that this is safe to do.
2007 void RemoveEnvironmentUsers();
2008
David Brazdil1abb4192015-02-17 18:33:36 +00002009 protected:
2010 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2011 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2012
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002013 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002014 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2015
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002016 HInstruction* previous_;
2017 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002018 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002019 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002020
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002021 // An instruction gets an id when it is added to the graph.
2022 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002023 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002024 int id_;
2025
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002026 // When doing liveness analysis, instructions that have uses get an SSA index.
2027 int ssa_index_;
2028
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002029 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002030 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002031
2032 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002033 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002034
Nicolas Geoffray39468442014-09-02 15:17:15 +01002035 // The environment associated with this instruction. Not null if the instruction
2036 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002037 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002038
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002039 // Set by the code generator.
2040 LocationSummary* locations_;
2041
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002042 // Set by the liveness analysis.
2043 LiveInterval* live_interval_;
2044
2045 // Set by the liveness analysis, this is the position in a linear
2046 // order of blocks where this instruction's live interval start.
2047 size_t lifetime_position_;
2048
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002049 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002050
Calin Juravleacf735c2015-02-12 15:25:22 +00002051 // TODO: for primitive types this should be marked as invalid.
2052 ReferenceTypeInfo reference_type_info_;
2053
David Brazdil1abb4192015-02-17 18:33:36 +00002054 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002055 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002056 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002057 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002058 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002059
2060 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2061};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002062std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002063
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002064class HInputIterator : public ValueObject {
2065 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002066 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002067
2068 bool Done() const { return index_ == instruction_->InputCount(); }
2069 HInstruction* Current() const { return instruction_->InputAt(index_); }
2070 void Advance() { index_++; }
2071
2072 private:
2073 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002074 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002075
2076 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2077};
2078
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002079class HInstructionIterator : public ValueObject {
2080 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002081 explicit HInstructionIterator(const HInstructionList& instructions)
2082 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002083 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002084 }
2085
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002086 bool Done() const { return instruction_ == nullptr; }
2087 HInstruction* Current() const { return instruction_; }
2088 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002089 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002090 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002091 }
2092
2093 private:
2094 HInstruction* instruction_;
2095 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002096
2097 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002098};
2099
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002100class HBackwardInstructionIterator : public ValueObject {
2101 public:
2102 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2103 : instruction_(instructions.last_instruction_) {
2104 next_ = Done() ? nullptr : instruction_->GetPrevious();
2105 }
2106
2107 bool Done() const { return instruction_ == nullptr; }
2108 HInstruction* Current() const { return instruction_; }
2109 void Advance() {
2110 instruction_ = next_;
2111 next_ = Done() ? nullptr : instruction_->GetPrevious();
2112 }
2113
2114 private:
2115 HInstruction* instruction_;
2116 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002117
2118 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002119};
2120
Vladimir Markof9f64412015-09-02 14:05:49 +01002121template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002122class HTemplateInstruction: public HInstruction {
2123 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002124 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002125 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002126 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002127
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002128 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002129
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002130 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002131 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2132 DCHECK_LT(i, N);
2133 return inputs_[i];
2134 }
David Brazdil1abb4192015-02-17 18:33:36 +00002135
2136 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002137 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002138 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002139 }
2140
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002141 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002142 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002143
2144 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002145};
2146
Vladimir Markof9f64412015-09-02 14:05:49 +01002147// HTemplateInstruction specialization for N=0.
2148template<>
2149class HTemplateInstruction<0>: public HInstruction {
2150 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002151 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002152 : HInstruction(side_effects, dex_pc) {}
2153
Vladimir Markof9f64412015-09-02 14:05:49 +01002154 virtual ~HTemplateInstruction() {}
2155
2156 size_t InputCount() const OVERRIDE { return 0; }
2157
2158 protected:
2159 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2160 LOG(FATAL) << "Unreachable";
2161 UNREACHABLE();
2162 }
2163
2164 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2165 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2166 LOG(FATAL) << "Unreachable";
2167 UNREACHABLE();
2168 }
2169
2170 private:
2171 friend class SsaBuilder;
2172};
2173
Dave Allison20dfc792014-06-16 20:44:29 -07002174template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002175class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002176 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002177 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002178 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002179 virtual ~HExpression() {}
2180
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002181 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002182
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002183 protected:
2184 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002185};
2186
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002187// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2188// instruction that branches to the exit block.
2189class HReturnVoid : public HTemplateInstruction<0> {
2190 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002191 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2192 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002193
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002194 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002195
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002196 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002197
2198 private:
2199 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2200};
2201
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002202// Represents dex's RETURN opcodes. A HReturn is a control flow
2203// instruction that branches to the exit block.
2204class HReturn : public HTemplateInstruction<1> {
2205 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002206 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2207 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002208 SetRawInputAt(0, value);
2209 }
2210
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002211 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002212
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002213 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002214
2215 private:
2216 DISALLOW_COPY_AND_ASSIGN(HReturn);
2217};
2218
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002219// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002220// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002221// exit block.
2222class HExit : public HTemplateInstruction<0> {
2223 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002224 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002225
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002226 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002227
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002228 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002229
2230 private:
2231 DISALLOW_COPY_AND_ASSIGN(HExit);
2232};
2233
2234// Jumps from one block to another.
2235class HGoto : public HTemplateInstruction<0> {
2236 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002237 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002238
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002239 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002240
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002241 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002242 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002243 }
2244
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002245 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002246
2247 private:
2248 DISALLOW_COPY_AND_ASSIGN(HGoto);
2249};
2250
Roland Levillain9867bc72015-08-05 10:21:34 +01002251class HConstant : public HExpression<0> {
2252 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002253 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2254 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002255
2256 bool CanBeMoved() const OVERRIDE { return true; }
2257
2258 virtual bool IsMinusOne() const { return false; }
2259 virtual bool IsZero() const { return false; }
2260 virtual bool IsOne() const { return false; }
2261
David Brazdil77a48ae2015-09-15 12:34:04 +00002262 virtual uint64_t GetValueAsUint64() const = 0;
2263
Roland Levillain9867bc72015-08-05 10:21:34 +01002264 DECLARE_INSTRUCTION(Constant);
2265
2266 private:
2267 DISALLOW_COPY_AND_ASSIGN(HConstant);
2268};
2269
2270class HNullConstant : public HConstant {
2271 public:
2272 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2273 return true;
2274 }
2275
David Brazdil77a48ae2015-09-15 12:34:04 +00002276 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2277
Roland Levillain9867bc72015-08-05 10:21:34 +01002278 size_t ComputeHashCode() const OVERRIDE { return 0; }
2279
2280 DECLARE_INSTRUCTION(NullConstant);
2281
2282 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002283 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002284
2285 friend class HGraph;
2286 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2287};
2288
2289// Constants of the type int. Those can be from Dex instructions, or
2290// synthesized (for example with the if-eqz instruction).
2291class HIntConstant : public HConstant {
2292 public:
2293 int32_t GetValue() const { return value_; }
2294
David Brazdil9f389d42015-10-01 14:32:56 +01002295 uint64_t GetValueAsUint64() const OVERRIDE {
2296 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2297 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002298
Roland Levillain9867bc72015-08-05 10:21:34 +01002299 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2300 DCHECK(other->IsIntConstant());
2301 return other->AsIntConstant()->value_ == value_;
2302 }
2303
2304 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2305
2306 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2307 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2308 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2309
2310 DECLARE_INSTRUCTION(IntConstant);
2311
2312 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002313 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2314 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2315 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2316 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002317
2318 const int32_t value_;
2319
2320 friend class HGraph;
2321 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2322 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2323 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2324};
2325
2326class HLongConstant : public HConstant {
2327 public:
2328 int64_t GetValue() const { return value_; }
2329
David Brazdil77a48ae2015-09-15 12:34:04 +00002330 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2331
Roland Levillain9867bc72015-08-05 10:21:34 +01002332 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2333 DCHECK(other->IsLongConstant());
2334 return other->AsLongConstant()->value_ == value_;
2335 }
2336
2337 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2338
2339 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2340 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2341 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2342
2343 DECLARE_INSTRUCTION(LongConstant);
2344
2345 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002346 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2347 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002348
2349 const int64_t value_;
2350
2351 friend class HGraph;
2352 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2353};
Dave Allison20dfc792014-06-16 20:44:29 -07002354
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002355// Conditional branch. A block ending with an HIf instruction must have
2356// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002357class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002358 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002359 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2360 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002361 SetRawInputAt(0, input);
2362 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002363
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002364 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002365
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002366 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002367 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002368 }
2369
2370 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002371 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002372 }
2373
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002374 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002375
2376 private:
2377 DISALLOW_COPY_AND_ASSIGN(HIf);
2378};
2379
David Brazdilfc6a86a2015-06-26 10:33:45 +00002380
2381// Abstract instruction which marks the beginning and/or end of a try block and
2382// links it to the respective exception handlers. Behaves the same as a Goto in
2383// non-exceptional control flow.
2384// Normal-flow successor is stored at index zero, exception handlers under
2385// higher indices in no particular order.
2386class HTryBoundary : public HTemplateInstruction<0> {
2387 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002388 enum BoundaryKind {
2389 kEntry,
2390 kExit,
2391 };
2392
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002393 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2394 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002395
2396 bool IsControlFlow() const OVERRIDE { return true; }
2397
2398 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002399 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002400
2401 // Returns whether `handler` is among its exception handlers (non-zero index
2402 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002403 bool HasExceptionHandler(const HBasicBlock& handler) const {
2404 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002405 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002406 }
2407
2408 // If not present already, adds `handler` to its block's list of exception
2409 // handlers.
2410 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002411 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002412 GetBlock()->AddSuccessor(handler);
2413 }
2414 }
2415
David Brazdil56e1acc2015-06-30 15:41:36 +01002416 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002417
David Brazdilffee3d32015-07-06 11:48:53 +01002418 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2419
David Brazdilfc6a86a2015-06-26 10:33:45 +00002420 DECLARE_INSTRUCTION(TryBoundary);
2421
2422 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002423 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002424
2425 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2426};
2427
David Brazdilffee3d32015-07-06 11:48:53 +01002428// Iterator over exception handlers of a given HTryBoundary, i.e. over
2429// exceptional successors of its basic block.
2430class HExceptionHandlerIterator : public ValueObject {
2431 public:
2432 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2433 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2434
Vladimir Marko60584552015-09-03 13:35:12 +00002435 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002436 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002437 size_t CurrentSuccessorIndex() const { return index_; }
2438 void Advance() { ++index_; }
2439
2440 private:
2441 const HBasicBlock& block_;
2442 size_t index_;
2443
2444 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2445};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002446
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002447// Deoptimize to interpreter, upon checking a condition.
2448class HDeoptimize : public HTemplateInstruction<1> {
2449 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002450 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2451 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002452 SetRawInputAt(0, cond);
2453 }
2454
2455 bool NeedsEnvironment() const OVERRIDE { return true; }
2456 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002457
2458 DECLARE_INSTRUCTION(Deoptimize);
2459
2460 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002461 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2462};
2463
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002464// Represents the ArtMethod that was passed as a first argument to
2465// the method. It is used by instructions that depend on it, like
2466// instructions that work with the dex cache.
2467class HCurrentMethod : public HExpression<0> {
2468 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002469 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2470 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002471
2472 DECLARE_INSTRUCTION(CurrentMethod);
2473
2474 private:
2475 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2476};
2477
Mark Mendellfe57faa2015-09-18 09:26:15 -04002478// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2479// have one successor for each entry in the switch table, and the final successor
2480// will be the block containing the next Dex opcode.
2481class HPackedSwitch : public HTemplateInstruction<1> {
2482 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002483 HPackedSwitch(int32_t start_value,
2484 uint32_t num_entries,
2485 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002486 uint32_t dex_pc = kNoDexPc)
2487 : HTemplateInstruction(SideEffects::None(), dex_pc),
2488 start_value_(start_value),
2489 num_entries_(num_entries) {
2490 SetRawInputAt(0, input);
2491 }
2492
2493 bool IsControlFlow() const OVERRIDE { return true; }
2494
2495 int32_t GetStartValue() const { return start_value_; }
2496
Vladimir Marko211c2112015-09-24 16:52:33 +01002497 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002498
2499 HBasicBlock* GetDefaultBlock() const {
2500 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002501 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002502 }
2503 DECLARE_INSTRUCTION(PackedSwitch);
2504
2505 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002506 const int32_t start_value_;
2507 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002508
2509 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2510};
2511
Roland Levillain88cb1752014-10-20 16:36:47 +01002512class HUnaryOperation : public HExpression<1> {
2513 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002514 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2515 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002516 SetRawInputAt(0, input);
2517 }
2518
2519 HInstruction* GetInput() const { return InputAt(0); }
2520 Primitive::Type GetResultType() const { return GetType(); }
2521
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002522 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002523 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002524 return true;
2525 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002526
Roland Levillain9240d6a2014-10-20 16:47:04 +01002527 // Try to statically evaluate `operation` and return a HConstant
2528 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002529 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002530 HConstant* TryStaticEvaluation() const;
2531
2532 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002533 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2534 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002535
Roland Levillain88cb1752014-10-20 16:36:47 +01002536 DECLARE_INSTRUCTION(UnaryOperation);
2537
2538 private:
2539 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2540};
2541
Dave Allison20dfc792014-06-16 20:44:29 -07002542class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002543 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002544 HBinaryOperation(Primitive::Type result_type,
2545 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002546 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002547 SideEffects side_effects = SideEffects::None(),
2548 uint32_t dex_pc = kNoDexPc)
2549 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002550 SetRawInputAt(0, left);
2551 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002552 }
2553
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002554 HInstruction* GetLeft() const { return InputAt(0); }
2555 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002556 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002557
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002558 virtual bool IsCommutative() const { return false; }
2559
2560 // Put constant on the right.
2561 // Returns whether order is changed.
2562 bool OrderInputsWithConstantOnTheRight() {
2563 HInstruction* left = InputAt(0);
2564 HInstruction* right = InputAt(1);
2565 if (left->IsConstant() && !right->IsConstant()) {
2566 ReplaceInput(right, 0);
2567 ReplaceInput(left, 1);
2568 return true;
2569 }
2570 return false;
2571 }
2572
2573 // Order inputs by instruction id, but favor constant on the right side.
2574 // This helps GVN for commutative ops.
2575 void OrderInputs() {
2576 DCHECK(IsCommutative());
2577 HInstruction* left = InputAt(0);
2578 HInstruction* right = InputAt(1);
2579 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2580 return;
2581 }
2582 if (OrderInputsWithConstantOnTheRight()) {
2583 return;
2584 }
2585 // Order according to instruction id.
2586 if (left->GetId() > right->GetId()) {
2587 ReplaceInput(right, 0);
2588 ReplaceInput(left, 1);
2589 }
2590 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002591
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002592 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002593 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002594 return true;
2595 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002596
Roland Levillain9240d6a2014-10-20 16:47:04 +01002597 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002598 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002599 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002600 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002601
2602 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002603 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2604 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2605 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2606 HLongConstant* y ATTRIBUTE_UNUSED) const {
2607 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2608 return nullptr;
2609 }
2610 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2611 HIntConstant* y ATTRIBUTE_UNUSED) const {
2612 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2613 return nullptr;
2614 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002615
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002616 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002617 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002618 HConstant* GetConstantRight() const;
2619
2620 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002621 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002622 HInstruction* GetLeastConstantLeft() const;
2623
Roland Levillainccc07a92014-09-16 14:48:16 +01002624 DECLARE_INSTRUCTION(BinaryOperation);
2625
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002626 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002627 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2628};
2629
Mark Mendellc4701932015-04-10 13:18:51 -04002630// The comparison bias applies for floating point operations and indicates how NaN
2631// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002632enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002633 kNoBias, // bias is not applicable (i.e. for long operation)
2634 kGtBias, // return 1 for NaN comparisons
2635 kLtBias, // return -1 for NaN comparisons
2636};
2637
Dave Allison20dfc792014-06-16 20:44:29 -07002638class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002639 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002640 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2641 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002642 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002643 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002644
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002645 bool NeedsMaterialization() const { return needs_materialization_; }
2646 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002647
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002648 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002649 // `instruction`, and disregard moves in between.
2650 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002651
Dave Allison20dfc792014-06-16 20:44:29 -07002652 DECLARE_INSTRUCTION(Condition);
2653
2654 virtual IfCondition GetCondition() const = 0;
2655
Mark Mendellc4701932015-04-10 13:18:51 -04002656 virtual IfCondition GetOppositeCondition() const = 0;
2657
Roland Levillain4fa13f62015-07-06 18:11:54 +01002658 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002659
2660 void SetBias(ComparisonBias bias) { bias_ = bias; }
2661
2662 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2663 return bias_ == other->AsCondition()->bias_;
2664 }
2665
Roland Levillain4fa13f62015-07-06 18:11:54 +01002666 bool IsFPConditionTrueIfNaN() const {
2667 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2668 IfCondition if_cond = GetCondition();
2669 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2670 }
2671
2672 bool IsFPConditionFalseIfNaN() const {
2673 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2674 IfCondition if_cond = GetCondition();
2675 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2676 }
2677
Dave Allison20dfc792014-06-16 20:44:29 -07002678 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002679 // For register allocation purposes, returns whether this instruction needs to be
2680 // materialized (that is, not just be in the processor flags).
2681 bool needs_materialization_;
2682
Mark Mendellc4701932015-04-10 13:18:51 -04002683 // Needed if we merge a HCompare into a HCondition.
2684 ComparisonBias bias_;
2685
Dave Allison20dfc792014-06-16 20:44:29 -07002686 DISALLOW_COPY_AND_ASSIGN(HCondition);
2687};
2688
2689// Instruction to check if two inputs are equal to each other.
2690class HEqual : public HCondition {
2691 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002692 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2693 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002694
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002695 bool IsCommutative() const OVERRIDE { return true; }
2696
Roland Levillain9867bc72015-08-05 10:21:34 +01002697 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002698 return GetBlock()->GetGraph()->GetIntConstant(
2699 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002700 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002701 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002702 return GetBlock()->GetGraph()->GetIntConstant(
2703 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002704 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002705
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002706 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002707
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002708 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002709 return kCondEQ;
2710 }
2711
Mark Mendellc4701932015-04-10 13:18:51 -04002712 IfCondition GetOppositeCondition() const OVERRIDE {
2713 return kCondNE;
2714 }
2715
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002716 private:
Aart Bike9f37602015-10-09 11:15:55 -07002717 template <typename T> bool Compute(T x, T y) const { return x == y; }
2718
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002719 DISALLOW_COPY_AND_ASSIGN(HEqual);
2720};
2721
Dave Allison20dfc792014-06-16 20:44:29 -07002722class HNotEqual : public HCondition {
2723 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002724 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2725 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002726
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002727 bool IsCommutative() const OVERRIDE { return true; }
2728
Roland Levillain9867bc72015-08-05 10:21:34 +01002729 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002730 return GetBlock()->GetGraph()->GetIntConstant(
2731 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002732 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002733 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002734 return GetBlock()->GetGraph()->GetIntConstant(
2735 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002736 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002737
Dave Allison20dfc792014-06-16 20:44:29 -07002738 DECLARE_INSTRUCTION(NotEqual);
2739
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002740 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002741 return kCondNE;
2742 }
2743
Mark Mendellc4701932015-04-10 13:18:51 -04002744 IfCondition GetOppositeCondition() const OVERRIDE {
2745 return kCondEQ;
2746 }
2747
Dave Allison20dfc792014-06-16 20:44:29 -07002748 private:
Aart Bike9f37602015-10-09 11:15:55 -07002749 template <typename T> bool Compute(T x, T y) const { return x != y; }
2750
Dave Allison20dfc792014-06-16 20:44:29 -07002751 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2752};
2753
2754class HLessThan : public HCondition {
2755 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002756 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2757 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002758
Roland Levillain9867bc72015-08-05 10:21:34 +01002759 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002760 return GetBlock()->GetGraph()->GetIntConstant(
2761 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002762 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002763 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002764 return GetBlock()->GetGraph()->GetIntConstant(
2765 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002766 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002767
Dave Allison20dfc792014-06-16 20:44:29 -07002768 DECLARE_INSTRUCTION(LessThan);
2769
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002770 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002771 return kCondLT;
2772 }
2773
Mark Mendellc4701932015-04-10 13:18:51 -04002774 IfCondition GetOppositeCondition() const OVERRIDE {
2775 return kCondGE;
2776 }
2777
Dave Allison20dfc792014-06-16 20:44:29 -07002778 private:
Aart Bike9f37602015-10-09 11:15:55 -07002779 template <typename T> bool Compute(T x, T y) const { return x < y; }
2780
Dave Allison20dfc792014-06-16 20:44:29 -07002781 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2782};
2783
2784class HLessThanOrEqual : public HCondition {
2785 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002786 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2787 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002788
Roland Levillain9867bc72015-08-05 10:21:34 +01002789 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002790 return GetBlock()->GetGraph()->GetIntConstant(
2791 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002792 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002793 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002794 return GetBlock()->GetGraph()->GetIntConstant(
2795 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002796 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002797
Dave Allison20dfc792014-06-16 20:44:29 -07002798 DECLARE_INSTRUCTION(LessThanOrEqual);
2799
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002800 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002801 return kCondLE;
2802 }
2803
Mark Mendellc4701932015-04-10 13:18:51 -04002804 IfCondition GetOppositeCondition() const OVERRIDE {
2805 return kCondGT;
2806 }
2807
Dave Allison20dfc792014-06-16 20:44:29 -07002808 private:
Aart Bike9f37602015-10-09 11:15:55 -07002809 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2810
Dave Allison20dfc792014-06-16 20:44:29 -07002811 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2812};
2813
2814class HGreaterThan : public HCondition {
2815 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002816 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2817 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002818
Roland Levillain9867bc72015-08-05 10:21:34 +01002819 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002820 return GetBlock()->GetGraph()->GetIntConstant(
2821 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002822 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002823 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002824 return GetBlock()->GetGraph()->GetIntConstant(
2825 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002826 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002827
Dave Allison20dfc792014-06-16 20:44:29 -07002828 DECLARE_INSTRUCTION(GreaterThan);
2829
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002830 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002831 return kCondGT;
2832 }
2833
Mark Mendellc4701932015-04-10 13:18:51 -04002834 IfCondition GetOppositeCondition() const OVERRIDE {
2835 return kCondLE;
2836 }
2837
Dave Allison20dfc792014-06-16 20:44:29 -07002838 private:
Aart Bike9f37602015-10-09 11:15:55 -07002839 template <typename T> bool Compute(T x, T y) const { return x > y; }
2840
Dave Allison20dfc792014-06-16 20:44:29 -07002841 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2842};
2843
2844class HGreaterThanOrEqual : public HCondition {
2845 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002846 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2847 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002848
Roland Levillain9867bc72015-08-05 10:21:34 +01002849 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002850 return GetBlock()->GetGraph()->GetIntConstant(
2851 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002852 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002853 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002854 return GetBlock()->GetGraph()->GetIntConstant(
2855 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002856 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002857
Dave Allison20dfc792014-06-16 20:44:29 -07002858 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2859
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002860 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002861 return kCondGE;
2862 }
2863
Mark Mendellc4701932015-04-10 13:18:51 -04002864 IfCondition GetOppositeCondition() const OVERRIDE {
2865 return kCondLT;
2866 }
2867
Dave Allison20dfc792014-06-16 20:44:29 -07002868 private:
Aart Bike9f37602015-10-09 11:15:55 -07002869 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2870
Dave Allison20dfc792014-06-16 20:44:29 -07002871 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2872};
2873
Aart Bike9f37602015-10-09 11:15:55 -07002874class HBelow : public HCondition {
2875 public:
2876 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2877 : HCondition(first, second, dex_pc) {}
2878
2879 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2880 return GetBlock()->GetGraph()->GetIntConstant(
2881 Compute(static_cast<uint32_t>(x->GetValue()),
2882 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2883 }
2884 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2885 return GetBlock()->GetGraph()->GetIntConstant(
2886 Compute(static_cast<uint64_t>(x->GetValue()),
2887 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2888 }
2889
2890 DECLARE_INSTRUCTION(Below);
2891
2892 IfCondition GetCondition() const OVERRIDE {
2893 return kCondB;
2894 }
2895
2896 IfCondition GetOppositeCondition() const OVERRIDE {
2897 return kCondAE;
2898 }
2899
2900 private:
2901 template <typename T> bool Compute(T x, T y) const { return x < y; }
2902
2903 DISALLOW_COPY_AND_ASSIGN(HBelow);
2904};
2905
2906class HBelowOrEqual : public HCondition {
2907 public:
2908 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2909 : HCondition(first, second, dex_pc) {}
2910
2911 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2912 return GetBlock()->GetGraph()->GetIntConstant(
2913 Compute(static_cast<uint32_t>(x->GetValue()),
2914 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2915 }
2916 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2917 return GetBlock()->GetGraph()->GetIntConstant(
2918 Compute(static_cast<uint64_t>(x->GetValue()),
2919 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2920 }
2921
2922 DECLARE_INSTRUCTION(BelowOrEqual);
2923
2924 IfCondition GetCondition() const OVERRIDE {
2925 return kCondBE;
2926 }
2927
2928 IfCondition GetOppositeCondition() const OVERRIDE {
2929 return kCondA;
2930 }
2931
2932 private:
2933 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2934
2935 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2936};
2937
2938class HAbove : public HCondition {
2939 public:
2940 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2941 : HCondition(first, second, dex_pc) {}
2942
2943 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2944 return GetBlock()->GetGraph()->GetIntConstant(
2945 Compute(static_cast<uint32_t>(x->GetValue()),
2946 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2947 }
2948 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2949 return GetBlock()->GetGraph()->GetIntConstant(
2950 Compute(static_cast<uint64_t>(x->GetValue()),
2951 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2952 }
2953
2954 DECLARE_INSTRUCTION(Above);
2955
2956 IfCondition GetCondition() const OVERRIDE {
2957 return kCondA;
2958 }
2959
2960 IfCondition GetOppositeCondition() const OVERRIDE {
2961 return kCondBE;
2962 }
2963
2964 private:
2965 template <typename T> bool Compute(T x, T y) const { return x > y; }
2966
2967 DISALLOW_COPY_AND_ASSIGN(HAbove);
2968};
2969
2970class HAboveOrEqual : public HCondition {
2971 public:
2972 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2973 : HCondition(first, second, dex_pc) {}
2974
2975 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2976 return GetBlock()->GetGraph()->GetIntConstant(
2977 Compute(static_cast<uint32_t>(x->GetValue()),
2978 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2979 }
2980 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2981 return GetBlock()->GetGraph()->GetIntConstant(
2982 Compute(static_cast<uint64_t>(x->GetValue()),
2983 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2984 }
2985
2986 DECLARE_INSTRUCTION(AboveOrEqual);
2987
2988 IfCondition GetCondition() const OVERRIDE {
2989 return kCondAE;
2990 }
2991
2992 IfCondition GetOppositeCondition() const OVERRIDE {
2993 return kCondB;
2994 }
2995
2996 private:
2997 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2998
2999 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3000};
Dave Allison20dfc792014-06-16 20:44:29 -07003001
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003002// Instruction to check how two inputs compare to each other.
3003// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3004class HCompare : public HBinaryOperation {
3005 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003006 HCompare(Primitive::Type type,
3007 HInstruction* first,
3008 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003009 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003010 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003011 : HBinaryOperation(Primitive::kPrimInt,
3012 first,
3013 second,
3014 SideEffectsForArchRuntimeCalls(type),
3015 dex_pc),
3016 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003017 DCHECK_EQ(type, first->GetType());
3018 DCHECK_EQ(type, second->GetType());
3019 }
3020
Roland Levillain9867bc72015-08-05 10:21:34 +01003021 template <typename T>
3022 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003023
Roland Levillain9867bc72015-08-05 10:21:34 +01003024 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003025 return GetBlock()->GetGraph()->GetIntConstant(
3026 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003027 }
3028 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003029 return GetBlock()->GetGraph()->GetIntConstant(
3030 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003031 }
3032
Calin Juravleddb7df22014-11-25 20:56:51 +00003033 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3034 return bias_ == other->AsCompare()->bias_;
3035 }
3036
Mark Mendellc4701932015-04-10 13:18:51 -04003037 ComparisonBias GetBias() const { return bias_; }
3038
Roland Levillain4fa13f62015-07-06 18:11:54 +01003039 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003040
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003041
3042 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3043 // MIPS64 uses a runtime call for FP comparisons.
3044 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3045 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003046
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003047 DECLARE_INSTRUCTION(Compare);
3048
3049 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003050 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003051
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003052 DISALLOW_COPY_AND_ASSIGN(HCompare);
3053};
3054
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003055// A local in the graph. Corresponds to a Dex register.
3056class HLocal : public HTemplateInstruction<0> {
3057 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003058 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003059 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003060
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003061 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003062
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003063 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003064
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003065 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003066 // The Dex register number.
3067 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003068
3069 DISALLOW_COPY_AND_ASSIGN(HLocal);
3070};
3071
3072// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003073class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003074 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003075 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3076 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003077 SetRawInputAt(0, local);
3078 }
3079
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003080 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3081
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003082 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003083
3084 private:
3085 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3086};
3087
3088// Store a value in a given local. This instruction has two inputs: the value
3089// and the local.
3090class HStoreLocal : public HTemplateInstruction<2> {
3091 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003092 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3093 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003094 SetRawInputAt(0, local);
3095 SetRawInputAt(1, value);
3096 }
3097
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003098 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3099
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003100 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003101
3102 private:
3103 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3104};
3105
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003106class HFloatConstant : public HConstant {
3107 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003108 float GetValue() const { return value_; }
3109
David Brazdil77a48ae2015-09-15 12:34:04 +00003110 uint64_t GetValueAsUint64() const OVERRIDE {
3111 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3112 }
3113
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003114 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003115 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003116 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003117 }
3118
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003119 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003120
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003121 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003122 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003123 }
3124 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003125 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003126 }
3127 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003128 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3129 }
3130 bool IsNaN() const {
3131 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003132 }
3133
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003134 DECLARE_INSTRUCTION(FloatConstant);
3135
3136 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003137 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3138 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3139 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3140 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003141
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003142 const float value_;
3143
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003144 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003145 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003146 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003147 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3148};
3149
3150class HDoubleConstant : public HConstant {
3151 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003152 double GetValue() const { return value_; }
3153
David Brazdil77a48ae2015-09-15 12:34:04 +00003154 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3155
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003156 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003157 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003158 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003159 }
3160
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003161 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003162
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003163 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003164 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003165 }
3166 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003167 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003168 }
3169 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003170 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3171 }
3172 bool IsNaN() const {
3173 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003174 }
3175
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003176 DECLARE_INSTRUCTION(DoubleConstant);
3177
3178 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003179 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3180 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3181 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3182 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003183
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003184 const double value_;
3185
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003186 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003187 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003188 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003189 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3190};
3191
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003192enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003193#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003194#include "intrinsics_list.h"
3195 kNone,
3196 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3197#undef INTRINSICS_LIST
3198#undef OPTIMIZING_INTRINSICS
3199};
3200std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3201
Agi Csaki05f20562015-08-19 14:58:14 -07003202enum IntrinsicNeedsEnvironmentOrCache {
3203 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3204 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003205};
3206
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003207class HInvoke : public HInstruction {
3208 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003209 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003210
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003211 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003212
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003213 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003214 SetRawInputAt(index, argument);
3215 }
3216
Roland Levillain3e3d7332015-04-28 11:00:54 +01003217 // Return the number of arguments. This number can be lower than
3218 // the number of inputs returned by InputCount(), as some invoke
3219 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3220 // inputs at the end of their list of inputs.
3221 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3222
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003223 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003224
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003225
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003226 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003227 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003228
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003229 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3230
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003231 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003232 return intrinsic_;
3233 }
3234
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003235 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003236
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003237 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003238 return GetEnvironment()->GetParent() != nullptr;
3239 }
3240
3241 bool CanThrow() const OVERRIDE { return true; }
3242
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003243 uint32_t* GetIntrinsicOptimizations() {
3244 return &intrinsic_optimizations_;
3245 }
3246
3247 const uint32_t* GetIntrinsicOptimizations() const {
3248 return &intrinsic_optimizations_;
3249 }
3250
3251 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3252
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003253 DECLARE_INSTRUCTION(Invoke);
3254
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003255 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003256 HInvoke(ArenaAllocator* arena,
3257 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003258 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003259 Primitive::Type return_type,
3260 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003261 uint32_t dex_method_index,
3262 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003263 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003264 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003265 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003266 inputs_(number_of_arguments + number_of_other_inputs,
3267 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003268 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003269 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003270 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003271 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003272 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003273 }
3274
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003275 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003276 return inputs_[index];
3277 }
3278
David Brazdil1abb4192015-02-17 18:33:36 +00003279 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003280 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003281 }
3282
Roland Levillain3e3d7332015-04-28 11:00:54 +01003283 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003284 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003285 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003286 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003287 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003288 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003289
3290 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3291 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003292
3293 private:
3294 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3295};
3296
Calin Juravle175dc732015-08-25 15:42:32 +01003297class HInvokeUnresolved : public HInvoke {
3298 public:
3299 HInvokeUnresolved(ArenaAllocator* arena,
3300 uint32_t number_of_arguments,
3301 Primitive::Type return_type,
3302 uint32_t dex_pc,
3303 uint32_t dex_method_index,
3304 InvokeType invoke_type)
3305 : HInvoke(arena,
3306 number_of_arguments,
3307 0u /* number_of_other_inputs */,
3308 return_type,
3309 dex_pc,
3310 dex_method_index,
3311 invoke_type) {
3312 }
3313
3314 DECLARE_INSTRUCTION(InvokeUnresolved);
3315
3316 private:
3317 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3318};
3319
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003320class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003321 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003322 // Requirements of this method call regarding the class
3323 // initialization (clinit) check of its declaring class.
3324 enum class ClinitCheckRequirement {
3325 kNone, // Class already initialized.
3326 kExplicit, // Static call having explicit clinit check as last input.
3327 kImplicit, // Static call implicitly requiring a clinit check.
3328 };
3329
Vladimir Marko58155012015-08-19 12:49:41 +00003330 // Determines how to load the target ArtMethod*.
3331 enum class MethodLoadKind {
3332 // Use a String init ArtMethod* loaded from Thread entrypoints.
3333 kStringInit,
3334
3335 // Use the method's own ArtMethod* loaded by the register allocator.
3336 kRecursive,
3337
3338 // Use ArtMethod* at a known address, embed the direct address in the code.
3339 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3340 kDirectAddress,
3341
3342 // Use ArtMethod* at an address that will be known at link time, embed the direct
3343 // address in the code. If the image is relocatable, emit .patch_oat entry.
3344 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3345 // the image relocatable or not.
3346 kDirectAddressWithFixup,
3347
3348 // Load from resoved methods array in the dex cache using a PC-relative load.
3349 // Used when we need to use the dex cache, for example for invoke-static that
3350 // may cause class initialization (the entry may point to a resolution method),
3351 // and we know that we can access the dex cache arrays using a PC-relative load.
3352 kDexCachePcRelative,
3353
3354 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3355 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3356 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3357 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3358 kDexCacheViaMethod,
3359 };
3360
3361 // Determines the location of the code pointer.
3362 enum class CodePtrLocation {
3363 // Recursive call, use local PC-relative call instruction.
3364 kCallSelf,
3365
3366 // Use PC-relative call instruction patched at link time.
3367 // Used for calls within an oat file, boot->boot or app->app.
3368 kCallPCRelative,
3369
3370 // Call to a known target address, embed the direct address in code.
3371 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3372 kCallDirect,
3373
3374 // Call to a target address that will be known at link time, embed the direct
3375 // address in code. If the image is relocatable, emit .patch_oat entry.
3376 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3377 // the image relocatable or not.
3378 kCallDirectWithFixup,
3379
3380 // Use code pointer from the ArtMethod*.
3381 // Used when we don't know the target code. This is also the last-resort-kind used when
3382 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3383 kCallArtMethod,
3384 };
3385
3386 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003387 MethodLoadKind method_load_kind;
3388 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003389 // The method load data holds
3390 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3391 // Note that there are multiple string init methods, each having its own offset.
3392 // - the method address for kDirectAddress
3393 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003394 uint64_t method_load_data;
3395 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003396 };
3397
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003398 HInvokeStaticOrDirect(ArenaAllocator* arena,
3399 uint32_t number_of_arguments,
3400 Primitive::Type return_type,
3401 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003402 uint32_t method_index,
3403 MethodReference target_method,
3404 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003405 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003406 InvokeType invoke_type,
3407 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003408 : HInvoke(arena,
3409 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003410 // There is potentially one extra argument for the HCurrentMethod node, and
3411 // potentially one other if the clinit check is explicit, and potentially
3412 // one other if the method is a string factory.
3413 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3414 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3415 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003416 return_type,
3417 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003418 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003419 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003420 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003421 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003422 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003423 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003424
Vladimir Markodc151b22015-10-15 18:02:30 +01003425 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003426 bool had_current_method_input = HasCurrentMethodInput();
3427 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3428
3429 // Using the current method is the default and once we find a better
3430 // method load kind, we should not go back to using the current method.
3431 DCHECK(had_current_method_input || !needs_current_method_input);
3432
3433 if (had_current_method_input && !needs_current_method_input) {
3434 DCHECK_EQ(InputAt(GetCurrentMethodInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3435 RemoveInputAt(GetCurrentMethodInputIndex());
3436 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003437 dispatch_info_ = dispatch_info;
3438 }
3439
Vladimir Markob554b5a2015-11-06 12:57:55 +00003440 void RemoveInputAt(size_t index);
3441
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003442 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003443 // We access the method via the dex cache so we can't do an implicit null check.
3444 // TODO: for intrinsics we can generate implicit null checks.
3445 return false;
3446 }
3447
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003448 bool CanBeNull() const OVERRIDE {
3449 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3450 }
3451
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003452 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003453 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3454 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3455 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003456 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003457 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003458 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003459 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003460 bool HasPcRelDexCache() const {
3461 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3462 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003463 bool HasCurrentMethodInput() const {
3464 // This function can be called only after the invoke has been fully initialized by the builder.
3465 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
3466 DCHECK(InputAt(GetCurrentMethodInputIndex())->IsCurrentMethod());
3467 return true;
3468 } else {
3469 DCHECK(InputCount() == GetCurrentMethodInputIndex() ||
3470 !InputAt(GetCurrentMethodInputIndex())->IsCurrentMethod());
3471 return false;
3472 }
3473 }
Vladimir Marko58155012015-08-19 12:49:41 +00003474 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3475 MethodReference GetTargetMethod() const { return target_method_; }
3476
3477 int32_t GetStringInitOffset() const {
3478 DCHECK(IsStringInit());
3479 return dispatch_info_.method_load_data;
3480 }
3481
3482 uint64_t GetMethodAddress() const {
3483 DCHECK(HasMethodAddress());
3484 return dispatch_info_.method_load_data;
3485 }
3486
3487 uint32_t GetDexCacheArrayOffset() const {
3488 DCHECK(HasPcRelDexCache());
3489 return dispatch_info_.method_load_data;
3490 }
3491
3492 uint64_t GetDirectCodePtr() const {
3493 DCHECK(HasDirectCodePtr());
3494 return dispatch_info_.direct_code_ptr;
3495 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003496
Calin Juravle68ad6492015-08-18 17:08:12 +01003497 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3498
Roland Levillain4c0eb422015-04-24 16:43:49 +01003499 // Is this instruction a call to a static method?
3500 bool IsStatic() const {
3501 return GetInvokeType() == kStatic;
3502 }
3503
Roland Levillain3e3d7332015-04-28 11:00:54 +01003504 // Remove the art::HLoadClass instruction set as last input by
3505 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3506 // the initial art::HClinitCheck instruction (only relevant for
3507 // static calls with explicit clinit check).
3508 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003509 DCHECK(IsStaticWithExplicitClinitCheck());
3510 size_t last_input_index = InputCount() - 1;
3511 HInstruction* last_input = InputAt(last_input_index);
3512 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003513 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003514 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003515 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003516 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3517 DCHECK(IsStaticWithImplicitClinitCheck());
3518 }
3519
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003520 bool IsStringFactoryFor(HFakeString* str) const {
3521 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003522 DCHECK(!HasCurrentMethodInput());
3523 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003524 return InputAt(InputCount() - 1)->AsFakeString() == str;
3525 }
3526
3527 void RemoveFakeStringArgumentAsLastInput() {
3528 DCHECK(IsStringInit());
3529 size_t last_input_index = InputCount() - 1;
3530 HInstruction* last_input = InputAt(last_input_index);
3531 DCHECK(last_input != nullptr);
3532 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3533 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003534 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003535 }
3536
Roland Levillain4c0eb422015-04-24 16:43:49 +01003537 // Is this a call to a static method whose declaring class has an
3538 // explicit intialization check in the graph?
3539 bool IsStaticWithExplicitClinitCheck() const {
3540 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3541 }
3542
3543 // Is this a call to a static method whose declaring class has an
3544 // implicit intialization check requirement?
3545 bool IsStaticWithImplicitClinitCheck() const {
3546 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3547 }
3548
Vladimir Markob554b5a2015-11-06 12:57:55 +00003549 // Does this method load kind need the current method as an input?
3550 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3551 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3552 }
3553
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003554 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003555
Roland Levillain4c0eb422015-04-24 16:43:49 +01003556 protected:
3557 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3558 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3559 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3560 HInstruction* input = input_record.GetInstruction();
3561 // `input` is the last input of a static invoke marked as having
3562 // an explicit clinit check. It must either be:
3563 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3564 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3565 DCHECK(input != nullptr);
3566 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3567 }
3568 return input_record;
3569 }
3570
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003571 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003572 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003573 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003574 // The target method may refer to different dex file or method index than the original
3575 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3576 // in derived class to increase visibility.
3577 MethodReference target_method_;
3578 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003579
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003580 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003581};
3582
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003583class HInvokeVirtual : public HInvoke {
3584 public:
3585 HInvokeVirtual(ArenaAllocator* arena,
3586 uint32_t number_of_arguments,
3587 Primitive::Type return_type,
3588 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003589 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003590 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003591 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003592 vtable_index_(vtable_index) {}
3593
Calin Juravle641547a2015-04-21 22:08:51 +01003594 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003595 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003596 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003597 }
3598
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003599 uint32_t GetVTableIndex() const { return vtable_index_; }
3600
3601 DECLARE_INSTRUCTION(InvokeVirtual);
3602
3603 private:
3604 const uint32_t vtable_index_;
3605
3606 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3607};
3608
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003609class HInvokeInterface : public HInvoke {
3610 public:
3611 HInvokeInterface(ArenaAllocator* arena,
3612 uint32_t number_of_arguments,
3613 Primitive::Type return_type,
3614 uint32_t dex_pc,
3615 uint32_t dex_method_index,
3616 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003617 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003618 imt_index_(imt_index) {}
3619
Calin Juravle641547a2015-04-21 22:08:51 +01003620 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003621 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003622 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003623 }
3624
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003625 uint32_t GetImtIndex() const { return imt_index_; }
3626 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3627
3628 DECLARE_INSTRUCTION(InvokeInterface);
3629
3630 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003631 const uint32_t imt_index_;
3632
3633 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3634};
3635
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003636class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003637 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003638 HNewInstance(HCurrentMethod* current_method,
3639 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003640 uint16_t type_index,
3641 const DexFile& dex_file,
3642 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003643 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003644 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003645 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003646 entrypoint_(entrypoint) {
3647 SetRawInputAt(0, current_method);
3648 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003649
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003650 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003651 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003652
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003653 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003654 bool NeedsEnvironment() const OVERRIDE { return true; }
Andreas Gampe55d02cf2015-10-29 02:59:50 +00003655 // It may throw when called on:
3656 // - interfaces
3657 // - abstract/innaccessible/unknown classes
3658 // TODO: optimize when possible.
3659 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003660
Calin Juravle10e244f2015-01-26 18:54:32 +00003661 bool CanBeNull() const OVERRIDE { return false; }
3662
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003663 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3664
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003665 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003666
3667 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003668 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003669 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003670 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003671
3672 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3673};
3674
Roland Levillain88cb1752014-10-20 16:36:47 +01003675class HNeg : public HUnaryOperation {
3676 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003677 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3678 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003679
Roland Levillain9867bc72015-08-05 10:21:34 +01003680 template <typename T> T Compute(T x) const { return -x; }
3681
3682 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003683 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003684 }
3685 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003686 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003687 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003688
Roland Levillain88cb1752014-10-20 16:36:47 +01003689 DECLARE_INSTRUCTION(Neg);
3690
3691 private:
3692 DISALLOW_COPY_AND_ASSIGN(HNeg);
3693};
3694
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003695class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003696 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003697 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003698 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003699 uint32_t dex_pc,
3700 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003701 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003702 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003703 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003704 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003705 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003706 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003707 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003708 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003709 }
3710
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003711 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003712 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003713
3714 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003715 bool NeedsEnvironment() const OVERRIDE { return true; }
3716
Mingyao Yang0c365e62015-03-31 15:09:29 -07003717 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3718 bool CanThrow() const OVERRIDE { return true; }
3719
Calin Juravle10e244f2015-01-26 18:54:32 +00003720 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003721
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003722 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3723
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003724 DECLARE_INSTRUCTION(NewArray);
3725
3726 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003727 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003728 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003729 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003730
3731 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3732};
3733
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003734class HAdd : public HBinaryOperation {
3735 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003736 HAdd(Primitive::Type result_type,
3737 HInstruction* left,
3738 HInstruction* right,
3739 uint32_t dex_pc = kNoDexPc)
3740 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003741
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003742 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003743
Roland Levillain9867bc72015-08-05 10:21:34 +01003744 template <typename T> T Compute(T x, T y) const { return x + y; }
3745
3746 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003747 return GetBlock()->GetGraph()->GetIntConstant(
3748 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003749 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003750 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003751 return GetBlock()->GetGraph()->GetLongConstant(
3752 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003753 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003754
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003755 DECLARE_INSTRUCTION(Add);
3756
3757 private:
3758 DISALLOW_COPY_AND_ASSIGN(HAdd);
3759};
3760
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003761class HSub : public HBinaryOperation {
3762 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003763 HSub(Primitive::Type result_type,
3764 HInstruction* left,
3765 HInstruction* right,
3766 uint32_t dex_pc = kNoDexPc)
3767 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003768
Roland Levillain9867bc72015-08-05 10:21:34 +01003769 template <typename T> T Compute(T x, T y) const { return x - y; }
3770
3771 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003772 return GetBlock()->GetGraph()->GetIntConstant(
3773 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003774 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003775 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003776 return GetBlock()->GetGraph()->GetLongConstant(
3777 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003778 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003779
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003780 DECLARE_INSTRUCTION(Sub);
3781
3782 private:
3783 DISALLOW_COPY_AND_ASSIGN(HSub);
3784};
3785
Calin Juravle34bacdf2014-10-07 20:23:36 +01003786class HMul : public HBinaryOperation {
3787 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003788 HMul(Primitive::Type result_type,
3789 HInstruction* left,
3790 HInstruction* right,
3791 uint32_t dex_pc = kNoDexPc)
3792 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003793
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003794 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003795
Roland Levillain9867bc72015-08-05 10:21:34 +01003796 template <typename T> T Compute(T x, T y) const { return x * y; }
3797
3798 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003799 return GetBlock()->GetGraph()->GetIntConstant(
3800 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003801 }
3802 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003803 return GetBlock()->GetGraph()->GetLongConstant(
3804 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003805 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003806
3807 DECLARE_INSTRUCTION(Mul);
3808
3809 private:
3810 DISALLOW_COPY_AND_ASSIGN(HMul);
3811};
3812
Calin Juravle7c4954d2014-10-28 16:57:40 +00003813class HDiv : public HBinaryOperation {
3814 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003815 HDiv(Primitive::Type result_type,
3816 HInstruction* left,
3817 HInstruction* right,
3818 uint32_t dex_pc)
3819 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003820
Roland Levillain9867bc72015-08-05 10:21:34 +01003821 template <typename T>
3822 T Compute(T x, T y) const {
3823 // Our graph structure ensures we never have 0 for `y` during
3824 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003825 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003826 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003827 return (y == -1) ? -x : x / y;
3828 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003829
Roland Levillain9867bc72015-08-05 10:21:34 +01003830 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003831 return GetBlock()->GetGraph()->GetIntConstant(
3832 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003833 }
3834 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003835 return GetBlock()->GetGraph()->GetLongConstant(
3836 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003837 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003838
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003839 static SideEffects SideEffectsForArchRuntimeCalls() {
3840 // The generated code can use a runtime call.
3841 return SideEffects::CanTriggerGC();
3842 }
3843
Calin Juravle7c4954d2014-10-28 16:57:40 +00003844 DECLARE_INSTRUCTION(Div);
3845
3846 private:
3847 DISALLOW_COPY_AND_ASSIGN(HDiv);
3848};
3849
Calin Juravlebacfec32014-11-14 15:54:36 +00003850class HRem : public HBinaryOperation {
3851 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003852 HRem(Primitive::Type result_type,
3853 HInstruction* left,
3854 HInstruction* right,
3855 uint32_t dex_pc)
3856 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003857
Roland Levillain9867bc72015-08-05 10:21:34 +01003858 template <typename T>
3859 T Compute(T x, T y) const {
3860 // Our graph structure ensures we never have 0 for `y` during
3861 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003862 DCHECK_NE(y, 0);
3863 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3864 return (y == -1) ? 0 : x % y;
3865 }
3866
Roland Levillain9867bc72015-08-05 10:21:34 +01003867 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003868 return GetBlock()->GetGraph()->GetIntConstant(
3869 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003870 }
3871 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003872 return GetBlock()->GetGraph()->GetLongConstant(
3873 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003874 }
3875
Calin Juravlebacfec32014-11-14 15:54:36 +00003876
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003877 static SideEffects SideEffectsForArchRuntimeCalls() {
3878 return SideEffects::CanTriggerGC();
3879 }
3880
Calin Juravlebacfec32014-11-14 15:54:36 +00003881 DECLARE_INSTRUCTION(Rem);
3882
3883 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003884 DISALLOW_COPY_AND_ASSIGN(HRem);
3885};
3886
Calin Juravled0d48522014-11-04 16:40:20 +00003887class HDivZeroCheck : public HExpression<1> {
3888 public:
3889 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003890 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003891 SetRawInputAt(0, value);
3892 }
3893
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003894 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3895
Calin Juravled0d48522014-11-04 16:40:20 +00003896 bool CanBeMoved() const OVERRIDE { return true; }
3897
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003898 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003899 return true;
3900 }
3901
3902 bool NeedsEnvironment() const OVERRIDE { return true; }
3903 bool CanThrow() const OVERRIDE { return true; }
3904
Calin Juravled0d48522014-11-04 16:40:20 +00003905 DECLARE_INSTRUCTION(DivZeroCheck);
3906
3907 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003908 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3909};
3910
Calin Juravle9aec02f2014-11-18 23:06:35 +00003911class HShl : public HBinaryOperation {
3912 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003913 HShl(Primitive::Type result_type,
3914 HInstruction* left,
3915 HInstruction* right,
3916 uint32_t dex_pc = kNoDexPc)
3917 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003918
Roland Levillain9867bc72015-08-05 10:21:34 +01003919 template <typename T, typename U, typename V>
3920 T Compute(T x, U y, V max_shift_value) const {
3921 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3922 "V is not the unsigned integer type corresponding to T");
3923 return x << (y & max_shift_value);
3924 }
3925
3926 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3927 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003928 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003929 }
3930 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3931 // case is handled as `x << static_cast<int>(y)`.
3932 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3933 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003934 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003935 }
3936 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3937 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003938 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003939 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003940
3941 DECLARE_INSTRUCTION(Shl);
3942
3943 private:
3944 DISALLOW_COPY_AND_ASSIGN(HShl);
3945};
3946
3947class HShr : public HBinaryOperation {
3948 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003949 HShr(Primitive::Type result_type,
3950 HInstruction* left,
3951 HInstruction* right,
3952 uint32_t dex_pc = kNoDexPc)
3953 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003954
Roland Levillain9867bc72015-08-05 10:21:34 +01003955 template <typename T, typename U, typename V>
3956 T Compute(T x, U y, V max_shift_value) const {
3957 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3958 "V is not the unsigned integer type corresponding to T");
3959 return x >> (y & max_shift_value);
3960 }
3961
3962 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3963 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003964 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003965 }
3966 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3967 // case is handled as `x >> static_cast<int>(y)`.
3968 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3969 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003970 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003971 }
3972 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3973 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003974 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003975 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003976
3977 DECLARE_INSTRUCTION(Shr);
3978
3979 private:
3980 DISALLOW_COPY_AND_ASSIGN(HShr);
3981};
3982
3983class HUShr : public HBinaryOperation {
3984 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003985 HUShr(Primitive::Type result_type,
3986 HInstruction* left,
3987 HInstruction* right,
3988 uint32_t dex_pc = kNoDexPc)
3989 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003990
Roland Levillain9867bc72015-08-05 10:21:34 +01003991 template <typename T, typename U, typename V>
3992 T Compute(T x, U y, V max_shift_value) const {
3993 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3994 "V is not the unsigned integer type corresponding to T");
3995 V ux = static_cast<V>(x);
3996 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003997 }
3998
Roland Levillain9867bc72015-08-05 10:21:34 +01003999 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4000 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004001 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004002 }
4003 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4004 // case is handled as `x >>> static_cast<int>(y)`.
4005 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4006 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004007 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004008 }
4009 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4010 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004011 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004012 }
4013
4014 DECLARE_INSTRUCTION(UShr);
4015
4016 private:
4017 DISALLOW_COPY_AND_ASSIGN(HUShr);
4018};
4019
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004020class HAnd : public HBinaryOperation {
4021 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004022 HAnd(Primitive::Type result_type,
4023 HInstruction* left,
4024 HInstruction* right,
4025 uint32_t dex_pc = kNoDexPc)
4026 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004027
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004028 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004029
Roland Levillain9867bc72015-08-05 10:21:34 +01004030 template <typename T, typename U>
4031 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4032
4033 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004034 return GetBlock()->GetGraph()->GetIntConstant(
4035 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004036 }
4037 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004038 return GetBlock()->GetGraph()->GetLongConstant(
4039 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004040 }
4041 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004042 return GetBlock()->GetGraph()->GetLongConstant(
4043 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004044 }
4045 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004046 return GetBlock()->GetGraph()->GetLongConstant(
4047 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004048 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004049
4050 DECLARE_INSTRUCTION(And);
4051
4052 private:
4053 DISALLOW_COPY_AND_ASSIGN(HAnd);
4054};
4055
4056class HOr : public HBinaryOperation {
4057 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004058 HOr(Primitive::Type result_type,
4059 HInstruction* left,
4060 HInstruction* right,
4061 uint32_t dex_pc = kNoDexPc)
4062 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004063
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004064 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004065
Roland Levillain9867bc72015-08-05 10:21:34 +01004066 template <typename T, typename U>
4067 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4068
4069 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004070 return GetBlock()->GetGraph()->GetIntConstant(
4071 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004072 }
4073 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004074 return GetBlock()->GetGraph()->GetLongConstant(
4075 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004076 }
4077 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004078 return GetBlock()->GetGraph()->GetLongConstant(
4079 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004080 }
4081 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004082 return GetBlock()->GetGraph()->GetLongConstant(
4083 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004084 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004085
4086 DECLARE_INSTRUCTION(Or);
4087
4088 private:
4089 DISALLOW_COPY_AND_ASSIGN(HOr);
4090};
4091
4092class HXor : public HBinaryOperation {
4093 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004094 HXor(Primitive::Type result_type,
4095 HInstruction* left,
4096 HInstruction* right,
4097 uint32_t dex_pc = kNoDexPc)
4098 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004099
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004100 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004101
Roland Levillain9867bc72015-08-05 10:21:34 +01004102 template <typename T, typename U>
4103 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4104
4105 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004106 return GetBlock()->GetGraph()->GetIntConstant(
4107 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004108 }
4109 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004110 return GetBlock()->GetGraph()->GetLongConstant(
4111 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004112 }
4113 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004114 return GetBlock()->GetGraph()->GetLongConstant(
4115 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004116 }
4117 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004118 return GetBlock()->GetGraph()->GetLongConstant(
4119 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004120 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004121
4122 DECLARE_INSTRUCTION(Xor);
4123
4124 private:
4125 DISALLOW_COPY_AND_ASSIGN(HXor);
4126};
4127
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004128// The value of a parameter in this method. Its location depends on
4129// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004130class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004131 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004132 HParameterValue(const DexFile& dex_file,
4133 uint16_t type_index,
4134 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004135 Primitive::Type parameter_type,
4136 bool is_this = false)
4137 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004138 dex_file_(dex_file),
4139 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004140 index_(index),
4141 is_this_(is_this),
4142 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004143
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004144 const DexFile& GetDexFile() const { return dex_file_; }
4145 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004146 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004147 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004148
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004149 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4150 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004151
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004152 DECLARE_INSTRUCTION(ParameterValue);
4153
4154 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004155 const DexFile& dex_file_;
4156 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004157 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004158 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004159 const uint8_t index_;
4160
Calin Juravle10e244f2015-01-26 18:54:32 +00004161 // Whether or not the parameter value corresponds to 'this' argument.
4162 const bool is_this_;
4163
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004164 bool can_be_null_;
4165
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004166 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4167};
4168
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004169class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004170 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004171 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4172 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004173
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004174 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004175 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004176 return true;
4177 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004178
Roland Levillain9867bc72015-08-05 10:21:34 +01004179 template <typename T> T Compute(T x) const { return ~x; }
4180
4181 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004182 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004183 }
4184 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004185 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004186 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004187
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004188 DECLARE_INSTRUCTION(Not);
4189
4190 private:
4191 DISALLOW_COPY_AND_ASSIGN(HNot);
4192};
4193
David Brazdil66d126e2015-04-03 16:02:44 +01004194class HBooleanNot : public HUnaryOperation {
4195 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004196 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4197 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004198
4199 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004200 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004201 return true;
4202 }
4203
Roland Levillain9867bc72015-08-05 10:21:34 +01004204 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004205 DCHECK(IsUint<1>(x));
4206 return !x;
4207 }
4208
Roland Levillain9867bc72015-08-05 10:21:34 +01004209 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004210 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004211 }
4212 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4213 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004214 UNREACHABLE();
4215 }
4216
4217 DECLARE_INSTRUCTION(BooleanNot);
4218
4219 private:
4220 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4221};
4222
Roland Levillaindff1f282014-11-05 14:15:05 +00004223class HTypeConversion : public HExpression<1> {
4224 public:
4225 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004226 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004227 : HExpression(result_type,
4228 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4229 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004230 SetRawInputAt(0, input);
4231 DCHECK_NE(input->GetType(), result_type);
4232 }
4233
4234 HInstruction* GetInput() const { return InputAt(0); }
4235 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4236 Primitive::Type GetResultType() const { return GetType(); }
4237
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004238 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004239 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004240
Roland Levillaindff1f282014-11-05 14:15:05 +00004241 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004242 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004243
Mark Mendelle82549b2015-05-06 10:55:34 -04004244 // Try to statically evaluate the conversion and return a HConstant
4245 // containing the result. If the input cannot be converted, return nullptr.
4246 HConstant* TryStaticEvaluation() const;
4247
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004248 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4249 Primitive::Type result_type) {
4250 // Some architectures may not require the 'GC' side effects, but at this point
4251 // in the compilation process we do not know what architecture we will
4252 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004253 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4254 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004255 return SideEffects::CanTriggerGC();
4256 }
4257 return SideEffects::None();
4258 }
4259
Roland Levillaindff1f282014-11-05 14:15:05 +00004260 DECLARE_INSTRUCTION(TypeConversion);
4261
4262 private:
4263 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4264};
4265
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004266static constexpr uint32_t kNoRegNumber = -1;
4267
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004268class HPhi : public HInstruction {
4269 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004270 HPhi(ArenaAllocator* arena,
4271 uint32_t reg_number,
4272 size_t number_of_inputs,
4273 Primitive::Type type,
4274 uint32_t dex_pc = kNoDexPc)
4275 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004276 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004277 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004278 type_(type),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004279 is_live_(false),
Calin Juravle10e244f2015-01-26 18:54:32 +00004280 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004281 }
4282
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004283 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4284 static Primitive::Type ToPhiType(Primitive::Type type) {
4285 switch (type) {
4286 case Primitive::kPrimBoolean:
4287 case Primitive::kPrimByte:
4288 case Primitive::kPrimShort:
4289 case Primitive::kPrimChar:
4290 return Primitive::kPrimInt;
4291 default:
4292 return type;
4293 }
4294 }
4295
David Brazdilffee3d32015-07-06 11:48:53 +01004296 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4297
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004298 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004299
4300 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004301 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004302
Calin Juravle10e244f2015-01-26 18:54:32 +00004303 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004304 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004305
Calin Juravle10e244f2015-01-26 18:54:32 +00004306 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4307 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4308
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004309 uint32_t GetRegNumber() const { return reg_number_; }
4310
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004311 void SetDead() { is_live_ = false; }
4312 void SetLive() { is_live_ = true; }
4313 bool IsDead() const { return !is_live_; }
4314 bool IsLive() const { return is_live_; }
4315
David Brazdil77a48ae2015-09-15 12:34:04 +00004316 bool IsVRegEquivalentOf(HInstruction* other) const {
4317 return other != nullptr
4318 && other->IsPhi()
4319 && other->AsPhi()->GetBlock() == GetBlock()
4320 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4321 }
4322
Calin Juravlea4f88312015-04-16 12:57:19 +01004323 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4324 // An equivalent phi is a phi having the same dex register and type.
4325 // It assumes that phis with the same dex register are adjacent.
4326 HPhi* GetNextEquivalentPhiWithSameType() {
4327 HInstruction* next = GetNext();
4328 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4329 if (next->GetType() == GetType()) {
4330 return next->AsPhi();
4331 }
4332 next = next->GetNext();
4333 }
4334 return nullptr;
4335 }
4336
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004337 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004338
David Brazdil1abb4192015-02-17 18:33:36 +00004339 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004340 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004341 return inputs_[index];
4342 }
David Brazdil1abb4192015-02-17 18:33:36 +00004343
4344 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004345 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004346 }
4347
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004348 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004349 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004350 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004351 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004352 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004353 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004354
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004355 DISALLOW_COPY_AND_ASSIGN(HPhi);
4356};
4357
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004358class HNullCheck : public HExpression<1> {
4359 public:
4360 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004361 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004362 SetRawInputAt(0, value);
4363 }
4364
Calin Juravle10e244f2015-01-26 18:54:32 +00004365 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004366 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004367 return true;
4368 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004369
Calin Juravle10e244f2015-01-26 18:54:32 +00004370 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004371
Calin Juravle10e244f2015-01-26 18:54:32 +00004372 bool CanThrow() const OVERRIDE { return true; }
4373
4374 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004375
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004376
4377 DECLARE_INSTRUCTION(NullCheck);
4378
4379 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004380 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4381};
4382
4383class FieldInfo : public ValueObject {
4384 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004385 FieldInfo(MemberOffset field_offset,
4386 Primitive::Type field_type,
4387 bool is_volatile,
4388 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004389 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004390 const DexFile& dex_file,
4391 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004392 : field_offset_(field_offset),
4393 field_type_(field_type),
4394 is_volatile_(is_volatile),
4395 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004396 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004397 dex_file_(dex_file),
4398 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004399
4400 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004401 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004402 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004403 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004404 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004405 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004406 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004407
4408 private:
4409 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004410 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004411 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004412 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004413 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004414 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004415 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004416};
4417
4418class HInstanceFieldGet : public HExpression<1> {
4419 public:
4420 HInstanceFieldGet(HInstruction* value,
4421 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004422 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004423 bool is_volatile,
4424 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004425 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004426 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004427 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004428 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004429 : HExpression(field_type,
4430 SideEffects::FieldReadOfType(field_type, is_volatile),
4431 dex_pc),
4432 field_info_(field_offset,
4433 field_type,
4434 is_volatile,
4435 field_idx,
4436 declaring_class_def_index,
4437 dex_file,
4438 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004439 SetRawInputAt(0, value);
4440 }
4441
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004442 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004443
4444 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4445 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4446 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004447 }
4448
Calin Juravle641547a2015-04-21 22:08:51 +01004449 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4450 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004451 }
4452
4453 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004454 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4455 }
4456
Calin Juravle52c48962014-12-16 17:02:57 +00004457 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004458 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004459 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004460 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004461
4462 DECLARE_INSTRUCTION(InstanceFieldGet);
4463
4464 private:
4465 const FieldInfo field_info_;
4466
4467 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4468};
4469
4470class HInstanceFieldSet : public HTemplateInstruction<2> {
4471 public:
4472 HInstanceFieldSet(HInstruction* object,
4473 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004474 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004475 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004476 bool is_volatile,
4477 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004478 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004479 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004480 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004481 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004482 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4483 dex_pc),
4484 field_info_(field_offset,
4485 field_type,
4486 is_volatile,
4487 field_idx,
4488 declaring_class_def_index,
4489 dex_file,
4490 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004491 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004492 SetRawInputAt(0, object);
4493 SetRawInputAt(1, value);
4494 }
4495
Calin Juravle641547a2015-04-21 22:08:51 +01004496 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4497 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004498 }
4499
Calin Juravle52c48962014-12-16 17:02:57 +00004500 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004501 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004502 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004503 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004504 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004505 bool GetValueCanBeNull() const { return value_can_be_null_; }
4506 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004507
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004508 DECLARE_INSTRUCTION(InstanceFieldSet);
4509
4510 private:
4511 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004512 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004513
4514 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4515};
4516
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004517class HArrayGet : public HExpression<2> {
4518 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004519 HArrayGet(HInstruction* array,
4520 HInstruction* index,
4521 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004522 uint32_t dex_pc,
4523 SideEffects additional_side_effects = SideEffects::None())
4524 : HExpression(type,
4525 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004526 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004527 SetRawInputAt(0, array);
4528 SetRawInputAt(1, index);
4529 }
4530
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004531 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004532 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004533 return true;
4534 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004535 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004536 // TODO: We can be smarter here.
4537 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4538 // which generates the implicit null check. There are cases when these can be removed
4539 // to produce better code. If we ever add optimizations to do so we should allow an
4540 // implicit check here (as long as the address falls in the first page).
4541 return false;
4542 }
4543
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004544 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004545
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004546 HInstruction* GetArray() const { return InputAt(0); }
4547 HInstruction* GetIndex() const { return InputAt(1); }
4548
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004549 DECLARE_INSTRUCTION(ArrayGet);
4550
4551 private:
4552 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4553};
4554
4555class HArraySet : public HTemplateInstruction<3> {
4556 public:
4557 HArraySet(HInstruction* array,
4558 HInstruction* index,
4559 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004560 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004561 uint32_t dex_pc,
4562 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004563 : HTemplateInstruction(
4564 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004565 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4566 additional_side_effects),
4567 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004568 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004569 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004570 value_can_be_null_(true),
4571 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004572 SetRawInputAt(0, array);
4573 SetRawInputAt(1, index);
4574 SetRawInputAt(2, value);
4575 }
4576
Calin Juravle77520bc2015-01-12 18:45:46 +00004577 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004578 // We currently always call a runtime method to catch array store
4579 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004580 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004581 }
4582
Mingyao Yang81014cb2015-06-02 03:16:27 -07004583 // Can throw ArrayStoreException.
4584 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4585
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004586 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004587 // TODO: Same as for ArrayGet.
4588 return false;
4589 }
4590
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004591 void ClearNeedsTypeCheck() {
4592 needs_type_check_ = false;
4593 }
4594
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004595 void ClearValueCanBeNull() {
4596 value_can_be_null_ = false;
4597 }
4598
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004599 void SetStaticTypeOfArrayIsObjectArray() {
4600 static_type_of_array_is_object_array_ = true;
4601 }
4602
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004603 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004604 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004605 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004606
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004607 HInstruction* GetArray() const { return InputAt(0); }
4608 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004609 HInstruction* GetValue() const { return InputAt(2); }
4610
4611 Primitive::Type GetComponentType() const {
4612 // The Dex format does not type floating point index operations. Since the
4613 // `expected_component_type_` is set during building and can therefore not
4614 // be correct, we also check what is the value type. If it is a floating
4615 // point type, we must use that type.
4616 Primitive::Type value_type = GetValue()->GetType();
4617 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4618 ? value_type
4619 : expected_component_type_;
4620 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004621
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004622 Primitive::Type GetRawExpectedComponentType() const {
4623 return expected_component_type_;
4624 }
4625
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004626 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4627 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4628 }
4629
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004630 DECLARE_INSTRUCTION(ArraySet);
4631
4632 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004633 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004634 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004635 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004636 // Cached information for the reference_type_info_ so that codegen
4637 // does not need to inspect the static type.
4638 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004639
4640 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4641};
4642
4643class HArrayLength : public HExpression<1> {
4644 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004645 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004646 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004647 // Note that arrays do not change length, so the instruction does not
4648 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004649 SetRawInputAt(0, array);
4650 }
4651
Calin Juravle77520bc2015-01-12 18:45:46 +00004652 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004653 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004654 return true;
4655 }
Calin Juravle641547a2015-04-21 22:08:51 +01004656 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4657 return obj == InputAt(0);
4658 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004659
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004660 DECLARE_INSTRUCTION(ArrayLength);
4661
4662 private:
4663 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4664};
4665
4666class HBoundsCheck : public HExpression<2> {
4667 public:
4668 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004669 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004670 DCHECK(index->GetType() == Primitive::kPrimInt);
4671 SetRawInputAt(0, index);
4672 SetRawInputAt(1, length);
4673 }
4674
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004675 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004676 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004677 return true;
4678 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004679
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004680 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004681
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004682 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004683
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004684 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004685
4686 DECLARE_INSTRUCTION(BoundsCheck);
4687
4688 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004689 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4690};
4691
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004692/**
4693 * Some DEX instructions are folded into multiple HInstructions that need
4694 * to stay live until the last HInstruction. This class
4695 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004696 * HInstruction stays live. `index` represents the stack location index of the
4697 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004698 */
4699class HTemporary : public HTemplateInstruction<0> {
4700 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004701 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4702 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004703
4704 size_t GetIndex() const { return index_; }
4705
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004706 Primitive::Type GetType() const OVERRIDE {
4707 // The previous instruction is the one that will be stored in the temporary location.
4708 DCHECK(GetPrevious() != nullptr);
4709 return GetPrevious()->GetType();
4710 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004711
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004712 DECLARE_INSTRUCTION(Temporary);
4713
4714 private:
4715 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004716 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4717};
4718
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004719class HSuspendCheck : public HTemplateInstruction<0> {
4720 public:
4721 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004722 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004723
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004724 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004725 return true;
4726 }
4727
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004728 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4729 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004730
4731 DECLARE_INSTRUCTION(SuspendCheck);
4732
4733 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004734 // Only used for code generation, in order to share the same slow path between back edges
4735 // of a same loop.
4736 SlowPathCode* slow_path_;
4737
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004738 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4739};
4740
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004741/**
4742 * Instruction to load a Class object.
4743 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004744class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004745 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004746 HLoadClass(HCurrentMethod* current_method,
4747 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004748 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004749 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004750 uint32_t dex_pc,
4751 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004752 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004753 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004754 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004755 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004756 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004757 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004758 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004759 // Referrers class should not need access check. We never inline unverified
4760 // methods so we can't possibly end up in this situation.
4761 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004762 SetRawInputAt(0, current_method);
4763 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004764
4765 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004766
4767 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004768 // Note that we don't need to test for generate_clinit_check_.
4769 // Whether or not we need to generate the clinit check is processed in
4770 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004771 return other->AsLoadClass()->type_index_ == type_index_ &&
4772 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004773 }
4774
4775 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4776
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004777 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004778 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004779 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004780
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004781 bool NeedsEnvironment() const OVERRIDE {
4782 // Will call runtime and load the class if the class is not loaded yet.
4783 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004784 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004785 }
4786
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004787 bool MustGenerateClinitCheck() const {
4788 return generate_clinit_check_;
4789 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004790 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004791 // The entrypoint the code generator is going to call does not do
4792 // clinit of the class.
4793 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004794 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004795 }
4796
4797 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004798 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4799 }
4800
4801 bool NeedsAccessCheck() const {
4802 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004803 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004804
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004805 bool CanThrow() const OVERRIDE {
4806 // May call runtime and and therefore can throw.
4807 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004808 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004809 }
4810
Calin Juravleacf735c2015-02-12 15:25:22 +00004811 ReferenceTypeInfo GetLoadedClassRTI() {
4812 return loaded_class_rti_;
4813 }
4814
4815 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4816 // Make sure we only set exact types (the loaded class should never be merged).
4817 DCHECK(rti.IsExact());
4818 loaded_class_rti_ = rti;
4819 }
4820
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004821 const DexFile& GetDexFile() { return dex_file_; }
4822
Vladimir Markodc151b22015-10-15 18:02:30 +01004823 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004824
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004825 static SideEffects SideEffectsForArchRuntimeCalls() {
4826 return SideEffects::CanTriggerGC();
4827 }
4828
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004829 DECLARE_INSTRUCTION(LoadClass);
4830
4831 private:
4832 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004833 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004834 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004835 // Whether this instruction must generate the initialization check.
4836 // Used for code generation.
4837 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004838 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004839
Calin Juravleacf735c2015-02-12 15:25:22 +00004840 ReferenceTypeInfo loaded_class_rti_;
4841
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004842 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4843};
4844
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004845class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004846 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004847 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004848 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4849 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004850 SetRawInputAt(0, current_method);
4851 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004852
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004853 bool CanBeMoved() const OVERRIDE { return true; }
4854
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004855 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4856 return other->AsLoadString()->string_index_ == string_index_;
4857 }
4858
4859 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4860
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004861 uint32_t GetStringIndex() const { return string_index_; }
4862
4863 // TODO: Can we deopt or debug when we resolve a string?
4864 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004865 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004866 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004867
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004868 static SideEffects SideEffectsForArchRuntimeCalls() {
4869 return SideEffects::CanTriggerGC();
4870 }
4871
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004872 DECLARE_INSTRUCTION(LoadString);
4873
4874 private:
4875 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004876
4877 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4878};
4879
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004880/**
4881 * Performs an initialization check on its Class object input.
4882 */
4883class HClinitCheck : public HExpression<1> {
4884 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004885 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004886 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004887 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004888 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4889 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004890 SetRawInputAt(0, constant);
4891 }
4892
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004893 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004894 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004895 return true;
4896 }
4897
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004898 bool NeedsEnvironment() const OVERRIDE {
4899 // May call runtime to initialize the class.
4900 return true;
4901 }
4902
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004903
4904 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4905
4906 DECLARE_INSTRUCTION(ClinitCheck);
4907
4908 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004909 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4910};
4911
4912class HStaticFieldGet : public HExpression<1> {
4913 public:
4914 HStaticFieldGet(HInstruction* cls,
4915 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004916 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004917 bool is_volatile,
4918 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004919 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004920 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004921 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004922 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004923 : HExpression(field_type,
4924 SideEffects::FieldReadOfType(field_type, is_volatile),
4925 dex_pc),
4926 field_info_(field_offset,
4927 field_type,
4928 is_volatile,
4929 field_idx,
4930 declaring_class_def_index,
4931 dex_file,
4932 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004933 SetRawInputAt(0, cls);
4934 }
4935
Calin Juravle52c48962014-12-16 17:02:57 +00004936
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004937 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004938
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004939 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004940 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4941 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004942 }
4943
4944 size_t ComputeHashCode() const OVERRIDE {
4945 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4946 }
4947
Calin Juravle52c48962014-12-16 17:02:57 +00004948 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004949 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4950 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004951 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004952
4953 DECLARE_INSTRUCTION(StaticFieldGet);
4954
4955 private:
4956 const FieldInfo field_info_;
4957
4958 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4959};
4960
4961class HStaticFieldSet : public HTemplateInstruction<2> {
4962 public:
4963 HStaticFieldSet(HInstruction* cls,
4964 HInstruction* value,
4965 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004966 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004967 bool is_volatile,
4968 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004969 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004970 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004971 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004972 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004973 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4974 dex_pc),
4975 field_info_(field_offset,
4976 field_type,
4977 is_volatile,
4978 field_idx,
4979 declaring_class_def_index,
4980 dex_file,
4981 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004982 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004983 SetRawInputAt(0, cls);
4984 SetRawInputAt(1, value);
4985 }
4986
Calin Juravle52c48962014-12-16 17:02:57 +00004987 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004988 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4989 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004990 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004991
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004992 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004993 bool GetValueCanBeNull() const { return value_can_be_null_; }
4994 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004995
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004996 DECLARE_INSTRUCTION(StaticFieldSet);
4997
4998 private:
4999 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005000 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005001
5002 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5003};
5004
Calin Juravlee460d1d2015-09-29 04:52:17 +01005005class HUnresolvedInstanceFieldGet : public HExpression<1> {
5006 public:
5007 HUnresolvedInstanceFieldGet(HInstruction* obj,
5008 Primitive::Type field_type,
5009 uint32_t field_index,
5010 uint32_t dex_pc)
5011 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5012 field_index_(field_index) {
5013 SetRawInputAt(0, obj);
5014 }
5015
5016 bool NeedsEnvironment() const OVERRIDE { return true; }
5017 bool CanThrow() const OVERRIDE { return true; }
5018
5019 Primitive::Type GetFieldType() const { return GetType(); }
5020 uint32_t GetFieldIndex() const { return field_index_; }
5021
5022 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5023
5024 private:
5025 const uint32_t field_index_;
5026
5027 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5028};
5029
5030class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5031 public:
5032 HUnresolvedInstanceFieldSet(HInstruction* obj,
5033 HInstruction* value,
5034 Primitive::Type field_type,
5035 uint32_t field_index,
5036 uint32_t dex_pc)
5037 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5038 field_type_(field_type),
5039 field_index_(field_index) {
5040 DCHECK_EQ(field_type, value->GetType());
5041 SetRawInputAt(0, obj);
5042 SetRawInputAt(1, value);
5043 }
5044
5045 bool NeedsEnvironment() const OVERRIDE { return true; }
5046 bool CanThrow() const OVERRIDE { return true; }
5047
5048 Primitive::Type GetFieldType() const { return field_type_; }
5049 uint32_t GetFieldIndex() const { return field_index_; }
5050
5051 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5052
5053 private:
5054 const Primitive::Type field_type_;
5055 const uint32_t field_index_;
5056
5057 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5058};
5059
5060class HUnresolvedStaticFieldGet : public HExpression<0> {
5061 public:
5062 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5063 uint32_t field_index,
5064 uint32_t dex_pc)
5065 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5066 field_index_(field_index) {
5067 }
5068
5069 bool NeedsEnvironment() const OVERRIDE { return true; }
5070 bool CanThrow() const OVERRIDE { return true; }
5071
5072 Primitive::Type GetFieldType() const { return GetType(); }
5073 uint32_t GetFieldIndex() const { return field_index_; }
5074
5075 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5076
5077 private:
5078 const uint32_t field_index_;
5079
5080 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5081};
5082
5083class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5084 public:
5085 HUnresolvedStaticFieldSet(HInstruction* value,
5086 Primitive::Type field_type,
5087 uint32_t field_index,
5088 uint32_t dex_pc)
5089 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5090 field_type_(field_type),
5091 field_index_(field_index) {
5092 DCHECK_EQ(field_type, value->GetType());
5093 SetRawInputAt(0, value);
5094 }
5095
5096 bool NeedsEnvironment() const OVERRIDE { return true; }
5097 bool CanThrow() const OVERRIDE { return true; }
5098
5099 Primitive::Type GetFieldType() const { return field_type_; }
5100 uint32_t GetFieldIndex() const { return field_index_; }
5101
5102 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5103
5104 private:
5105 const Primitive::Type field_type_;
5106 const uint32_t field_index_;
5107
5108 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5109};
5110
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005111// Implement the move-exception DEX instruction.
5112class HLoadException : public HExpression<0> {
5113 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005114 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5115 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005116
David Brazdilbbd733e2015-08-18 17:48:17 +01005117 bool CanBeNull() const OVERRIDE { return false; }
5118
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005119 DECLARE_INSTRUCTION(LoadException);
5120
5121 private:
5122 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5123};
5124
David Brazdilcb1c0552015-08-04 16:22:25 +01005125// Implicit part of move-exception which clears thread-local exception storage.
5126// Must not be removed because the runtime expects the TLS to get cleared.
5127class HClearException : public HTemplateInstruction<0> {
5128 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005129 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5130 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005131
5132 DECLARE_INSTRUCTION(ClearException);
5133
5134 private:
5135 DISALLOW_COPY_AND_ASSIGN(HClearException);
5136};
5137
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005138class HThrow : public HTemplateInstruction<1> {
5139 public:
5140 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005141 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005142 SetRawInputAt(0, exception);
5143 }
5144
5145 bool IsControlFlow() const OVERRIDE { return true; }
5146
5147 bool NeedsEnvironment() const OVERRIDE { return true; }
5148
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005149 bool CanThrow() const OVERRIDE { return true; }
5150
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005151
5152 DECLARE_INSTRUCTION(Throw);
5153
5154 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005155 DISALLOW_COPY_AND_ASSIGN(HThrow);
5156};
5157
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005158/**
5159 * Implementation strategies for the code generator of a HInstanceOf
5160 * or `HCheckCast`.
5161 */
5162enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005163 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005164 kExactCheck, // Can do a single class compare.
5165 kClassHierarchyCheck, // Can just walk the super class chain.
5166 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5167 kInterfaceCheck, // No optimization yet when checking against an interface.
5168 kArrayObjectCheck, // Can just check if the array is not primitive.
5169 kArrayCheck // No optimization yet when checking against a generic array.
5170};
5171
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005172class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005173 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005174 HInstanceOf(HInstruction* object,
5175 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005176 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005177 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005178 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005179 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005180 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005181 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005182 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005183 SetRawInputAt(0, object);
5184 SetRawInputAt(1, constant);
5185 }
5186
5187 bool CanBeMoved() const OVERRIDE { return true; }
5188
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005189 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005190 return true;
5191 }
5192
5193 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005194 return false;
5195 }
5196
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005197 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5198
5199 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005200
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005201 // Used only in code generation.
5202 bool MustDoNullCheck() const { return must_do_null_check_; }
5203 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5204
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005205 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5206 return (check_kind == TypeCheckKind::kExactCheck)
5207 ? SideEffects::None()
5208 // Mips currently does runtime calls for any other checks.
5209 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005210 }
5211
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005212 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005213
5214 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005215 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005216 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005217
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005218 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5219};
5220
Calin Juravleb1498f62015-02-16 13:13:29 +00005221class HBoundType : public HExpression<1> {
5222 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005223 // Constructs an HBoundType with the given upper_bound.
5224 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005225 HBoundType(HInstruction* input,
5226 ReferenceTypeInfo upper_bound,
5227 bool upper_can_be_null,
5228 uint32_t dex_pc = kNoDexPc)
5229 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005230 upper_bound_(upper_bound),
5231 upper_can_be_null_(upper_can_be_null),
5232 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005233 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005234 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005235 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005236 }
5237
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005238 // GetUpper* should only be used in reference type propagation.
5239 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5240 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005241
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005242 void SetCanBeNull(bool can_be_null) {
5243 DCHECK(upper_can_be_null_ || !can_be_null);
5244 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005245 }
5246
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005247 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5248
Calin Juravleb1498f62015-02-16 13:13:29 +00005249 DECLARE_INSTRUCTION(BoundType);
5250
5251 private:
5252 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005253 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5254 // It is used to bound the type in cases like:
5255 // if (x instanceof ClassX) {
5256 // // uper_bound_ will be ClassX
5257 // }
5258 const ReferenceTypeInfo upper_bound_;
5259 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5260 // is false then can_be_null_ cannot be true).
5261 const bool upper_can_be_null_;
5262 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005263
5264 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5265};
5266
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005267class HCheckCast : public HTemplateInstruction<2> {
5268 public:
5269 HCheckCast(HInstruction* object,
5270 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005271 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005272 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005273 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005274 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005275 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005276 SetRawInputAt(0, object);
5277 SetRawInputAt(1, constant);
5278 }
5279
5280 bool CanBeMoved() const OVERRIDE { return true; }
5281
5282 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5283 return true;
5284 }
5285
5286 bool NeedsEnvironment() const OVERRIDE {
5287 // Instruction may throw a CheckCastError.
5288 return true;
5289 }
5290
5291 bool CanThrow() const OVERRIDE { return true; }
5292
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005293 bool MustDoNullCheck() const { return must_do_null_check_; }
5294 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005295 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005296
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005297 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005298
5299 DECLARE_INSTRUCTION(CheckCast);
5300
5301 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005302 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005303 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005304
5305 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005306};
5307
Calin Juravle27df7582015-04-17 19:12:31 +01005308class HMemoryBarrier : public HTemplateInstruction<0> {
5309 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005310 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005311 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005312 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005313 barrier_kind_(barrier_kind) {}
5314
5315 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5316
5317 DECLARE_INSTRUCTION(MemoryBarrier);
5318
5319 private:
5320 const MemBarrierKind barrier_kind_;
5321
5322 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5323};
5324
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005325class HMonitorOperation : public HTemplateInstruction<1> {
5326 public:
5327 enum OperationKind {
5328 kEnter,
5329 kExit,
5330 };
5331
5332 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005333 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005334 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5335 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005336 SetRawInputAt(0, object);
5337 }
5338
5339 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005340 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5341
5342 bool CanThrow() const OVERRIDE {
5343 // Verifier guarantees that monitor-exit cannot throw.
5344 // This is important because it allows the HGraphBuilder to remove
5345 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5346 return IsEnter();
5347 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005348
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005349
5350 bool IsEnter() const { return kind_ == kEnter; }
5351
5352 DECLARE_INSTRUCTION(MonitorOperation);
5353
Calin Juravle52c48962014-12-16 17:02:57 +00005354 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005355 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005356
5357 private:
5358 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5359};
5360
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005361/**
5362 * A HInstruction used as a marker for the replacement of new + <init>
5363 * of a String to a call to a StringFactory. Only baseline will see
5364 * the node at code generation, where it will be be treated as null.
5365 * When compiling non-baseline, `HFakeString` instructions are being removed
5366 * in the instruction simplifier.
5367 */
5368class HFakeString : public HTemplateInstruction<0> {
5369 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005370 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5371 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005372
5373 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5374
5375 DECLARE_INSTRUCTION(FakeString);
5376
5377 private:
5378 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5379};
5380
Vladimir Markof9f64412015-09-02 14:05:49 +01005381class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005382 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005383 MoveOperands(Location source,
5384 Location destination,
5385 Primitive::Type type,
5386 HInstruction* instruction)
5387 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005388
5389 Location GetSource() const { return source_; }
5390 Location GetDestination() const { return destination_; }
5391
5392 void SetSource(Location value) { source_ = value; }
5393 void SetDestination(Location value) { destination_ = value; }
5394
5395 // The parallel move resolver marks moves as "in-progress" by clearing the
5396 // destination (but not the source).
5397 Location MarkPending() {
5398 DCHECK(!IsPending());
5399 Location dest = destination_;
5400 destination_ = Location::NoLocation();
5401 return dest;
5402 }
5403
5404 void ClearPending(Location dest) {
5405 DCHECK(IsPending());
5406 destination_ = dest;
5407 }
5408
5409 bool IsPending() const {
5410 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5411 return destination_.IsInvalid() && !source_.IsInvalid();
5412 }
5413
5414 // True if this blocks a move from the given location.
5415 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005416 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005417 }
5418
5419 // A move is redundant if it's been eliminated, if its source and
5420 // destination are the same, or if its destination is unneeded.
5421 bool IsRedundant() const {
5422 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5423 }
5424
5425 // We clear both operands to indicate move that's been eliminated.
5426 void Eliminate() {
5427 source_ = destination_ = Location::NoLocation();
5428 }
5429
5430 bool IsEliminated() const {
5431 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5432 return source_.IsInvalid();
5433 }
5434
Alexey Frunze4dda3372015-06-01 18:31:49 -07005435 Primitive::Type GetType() const { return type_; }
5436
Nicolas Geoffray90218252015-04-15 11:56:51 +01005437 bool Is64BitMove() const {
5438 return Primitive::Is64BitType(type_);
5439 }
5440
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005441 HInstruction* GetInstruction() const { return instruction_; }
5442
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005443 private:
5444 Location source_;
5445 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005446 // The type this move is for.
5447 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005448 // The instruction this move is assocatied with. Null when this move is
5449 // for moving an input in the expected locations of user (including a phi user).
5450 // This is only used in debug mode, to ensure we do not connect interval siblings
5451 // in the same parallel move.
5452 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005453};
5454
5455static constexpr size_t kDefaultNumberOfMoves = 4;
5456
5457class HParallelMove : public HTemplateInstruction<0> {
5458 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005459 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005460 : HTemplateInstruction(SideEffects::None(), dex_pc),
5461 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5462 moves_.reserve(kDefaultNumberOfMoves);
5463 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005464
Nicolas Geoffray90218252015-04-15 11:56:51 +01005465 void AddMove(Location source,
5466 Location destination,
5467 Primitive::Type type,
5468 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005469 DCHECK(source.IsValid());
5470 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005471 if (kIsDebugBuild) {
5472 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005473 for (const MoveOperands& move : moves_) {
5474 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005475 // Special case the situation where the move is for the spill slot
5476 // of the instruction.
5477 if ((GetPrevious() == instruction)
5478 || ((GetPrevious() == nullptr)
5479 && instruction->IsPhi()
5480 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005481 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005482 << "Doing parallel moves for the same instruction.";
5483 } else {
5484 DCHECK(false) << "Doing parallel moves for the same instruction.";
5485 }
5486 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005487 }
5488 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005489 for (const MoveOperands& move : moves_) {
5490 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005491 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005492 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005493 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005494 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005495 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005496 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005497 }
5498
Vladimir Marko225b6462015-09-28 12:17:40 +01005499 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005500 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005501 }
5502
Vladimir Marko225b6462015-09-28 12:17:40 +01005503 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005504
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005505 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005506
5507 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005508 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005509
5510 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5511};
5512
Mark Mendell0616ae02015-04-17 12:49:27 -04005513} // namespace art
5514
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005515#ifdef ART_ENABLE_CODEGEN_arm64
5516#include "nodes_arm64.h"
5517#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005518#ifdef ART_ENABLE_CODEGEN_x86
5519#include "nodes_x86.h"
5520#endif
5521
5522namespace art {
5523
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005524class HGraphVisitor : public ValueObject {
5525 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005526 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5527 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005528
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005529 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005530 virtual void VisitBasicBlock(HBasicBlock* block);
5531
Roland Levillain633021e2014-10-01 14:12:25 +01005532 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005533 void VisitInsertionOrder();
5534
Roland Levillain633021e2014-10-01 14:12:25 +01005535 // Visit the graph following dominator tree reverse post-order.
5536 void VisitReversePostOrder();
5537
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005538 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005539
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005540 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005541#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005542 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5543
5544 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5545
5546#undef DECLARE_VISIT_INSTRUCTION
5547
5548 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005549 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005550
5551 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5552};
5553
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005554class HGraphDelegateVisitor : public HGraphVisitor {
5555 public:
5556 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5557 virtual ~HGraphDelegateVisitor() {}
5558
5559 // Visit functions that delegate to to super class.
5560#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005561 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005562
5563 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5564
5565#undef DECLARE_VISIT_INSTRUCTION
5566
5567 private:
5568 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5569};
5570
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005571class HInsertionOrderIterator : public ValueObject {
5572 public:
5573 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5574
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005575 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005576 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005577 void Advance() { ++index_; }
5578
5579 private:
5580 const HGraph& graph_;
5581 size_t index_;
5582
5583 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5584};
5585
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005586class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005587 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005588 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5589 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005590 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005591 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005592
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005593 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5594 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005595 void Advance() { ++index_; }
5596
5597 private:
5598 const HGraph& graph_;
5599 size_t index_;
5600
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005601 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005602};
5603
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005604class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005605 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005606 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005607 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005608 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005609 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005610 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005611
5612 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005613 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005614 void Advance() { --index_; }
5615
5616 private:
5617 const HGraph& graph_;
5618 size_t index_;
5619
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005620 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005621};
5622
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005623class HLinearPostOrderIterator : public ValueObject {
5624 public:
5625 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005626 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005627
5628 bool Done() const { return index_ == 0; }
5629
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005630 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005631
5632 void Advance() {
5633 --index_;
5634 DCHECK_GE(index_, 0U);
5635 }
5636
5637 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005638 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005639 size_t index_;
5640
5641 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5642};
5643
5644class HLinearOrderIterator : public ValueObject {
5645 public:
5646 explicit HLinearOrderIterator(const HGraph& graph)
5647 : order_(graph.GetLinearOrder()), index_(0) {}
5648
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005649 bool Done() const { return index_ == order_.size(); }
5650 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005651 void Advance() { ++index_; }
5652
5653 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005654 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005655 size_t index_;
5656
5657 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5658};
5659
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005660// Iterator over the blocks that art part of the loop. Includes blocks part
5661// of an inner loop. The order in which the blocks are iterated is on their
5662// block id.
5663class HBlocksInLoopIterator : public ValueObject {
5664 public:
5665 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5666 : blocks_in_loop_(info.GetBlocks()),
5667 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5668 index_(0) {
5669 if (!blocks_in_loop_.IsBitSet(index_)) {
5670 Advance();
5671 }
5672 }
5673
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005674 bool Done() const { return index_ == blocks_.size(); }
5675 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005676 void Advance() {
5677 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005678 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005679 if (blocks_in_loop_.IsBitSet(index_)) {
5680 break;
5681 }
5682 }
5683 }
5684
5685 private:
5686 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005687 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005688 size_t index_;
5689
5690 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5691};
5692
Mingyao Yang3584bce2015-05-19 16:01:59 -07005693// Iterator over the blocks that art part of the loop. Includes blocks part
5694// of an inner loop. The order in which the blocks are iterated is reverse
5695// post order.
5696class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5697 public:
5698 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5699 : blocks_in_loop_(info.GetBlocks()),
5700 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5701 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005702 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005703 Advance();
5704 }
5705 }
5706
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005707 bool Done() const { return index_ == blocks_.size(); }
5708 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005709 void Advance() {
5710 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005711 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5712 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005713 break;
5714 }
5715 }
5716 }
5717
5718 private:
5719 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005720 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005721 size_t index_;
5722
5723 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5724};
5725
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005726inline int64_t Int64FromConstant(HConstant* constant) {
5727 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5728 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5729 : constant->AsLongConstant()->GetValue();
5730}
5731
Vladimir Marko58155012015-08-19 12:49:41 +00005732inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5733 // For the purposes of the compiler, the dex files must actually be the same object
5734 // if we want to safely treat them as the same. This is especially important for JIT
5735 // as custom class loaders can open the same underlying file (or memory) multiple
5736 // times and provide different class resolution but no two class loaders should ever
5737 // use the same DexFile object - doing so is an unsupported hack that can lead to
5738 // all sorts of weird failures.
5739 return &lhs == &rhs;
5740}
5741
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005742} // namespace art
5743
5744#endif // ART_COMPILER_OPTIMIZING_NODES_H_