blob: 17b70949af1c13cafaa91781565140503429befc [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 Geoffraybe9a92a2014-02-25 14:22:56 +0000354 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100355
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100356 const DexFile& GetDexFile() const {
357 return dex_file_;
358 }
359
360 uint32_t GetMethodIdx() const {
361 return method_idx_;
362 }
363
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100364 InvokeType GetInvokeType() const {
365 return invoke_type_;
366 }
367
Mark Mendellc4701932015-04-10 13:18:51 -0400368 InstructionSet GetInstructionSet() const {
369 return instruction_set_;
370 }
371
David Brazdil77a48ae2015-09-15 12:34:04 +0000372 bool HasTryCatch() const { return has_try_catch_; }
373 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100374
David Brazdil2d7352b2015-04-20 14:52:42 +0100375 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100376 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000377 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100378 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000379
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000380 template <class InstructionType, typename ValueType>
381 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600382 ArenaSafeMap<ValueType, InstructionType*>* cache,
383 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000384 // Try to find an existing constant of the given value.
385 InstructionType* constant = nullptr;
386 auto cached_constant = cache->find(value);
387 if (cached_constant != cache->end()) {
388 constant = cached_constant->second;
389 }
390
391 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100392 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000393 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600394 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000395 cache->Overwrite(value, constant);
396 InsertConstant(constant);
397 }
398 return constant;
399 }
400
David Brazdil8d5b8b22015-03-24 10:51:52 +0000401 void InsertConstant(HConstant* instruction);
402
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000403 // Cache a float constant into the graph. This method should only be
404 // called by the SsaBuilder when creating "equivalent" instructions.
405 void CacheFloatConstant(HFloatConstant* constant);
406
407 // See CacheFloatConstant comment.
408 void CacheDoubleConstant(HDoubleConstant* constant);
409
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000410 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000411
412 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100413 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000414
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100415 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100416 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000417
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100418 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100419 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100420
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000421 HBasicBlock* entry_block_;
422 HBasicBlock* exit_block_;
423
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100424 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100425 uint16_t maximum_number_of_out_vregs_;
426
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100427 // The number of virtual registers in this method. Contains the parameters.
428 uint16_t number_of_vregs_;
429
430 // The number of virtual registers used by parameters of this method.
431 uint16_t number_of_in_vregs_;
432
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000433 // Number of vreg size slots that the temporaries use (used in baseline compiler).
434 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100435
Mark Mendell1152c922015-04-24 17:06:35 -0400436 // Has bounds checks. We can totally skip BCE if it's false.
437 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800438
David Brazdil77a48ae2015-09-15 12:34:04 +0000439 // Flag whether there are any try/catch blocks in the graph. We will skip
440 // try/catch-related passes if false.
441 bool has_try_catch_;
442
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000443 // Indicates whether the graph should be compiled in a way that
444 // ensures full debuggability. If false, we can apply more
445 // aggressive optimizations that may limit the level of debugging.
446 const bool debuggable_;
447
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000448 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000449 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000450
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100451 // The dex file from which the method is from.
452 const DexFile& dex_file_;
453
454 // The method index in the dex file.
455 const uint32_t method_idx_;
456
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100457 // If inlined, this encodes how the callee is being invoked.
458 const InvokeType invoke_type_;
459
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100460 // Whether the graph has been transformed to SSA form. Only used
461 // in debug mode to ensure we are not using properties only valid
462 // for non-SSA form (like the number of temporaries).
463 bool in_ssa_form_;
464
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100465 const bool should_generate_constructor_barrier_;
466
Mathieu Chartiere401d142015-04-22 13:56:20 -0700467 const InstructionSet instruction_set_;
468
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000469 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000470 HNullConstant* cached_null_constant_;
471 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000472 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000473 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000474 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000475
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100476 HCurrentMethod* cached_current_method_;
477
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000478 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100479 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000480 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000481 DISALLOW_COPY_AND_ASSIGN(HGraph);
482};
483
Vladimir Markof9f64412015-09-02 14:05:49 +0100484class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000485 public:
486 HLoopInformation(HBasicBlock* header, HGraph* graph)
487 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100488 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100489 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100490 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100491 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
492 back_edges_.reserve(kDefaultNumberOfBackEdges);
493 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100494
495 HBasicBlock* GetHeader() const {
496 return header_;
497 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000498
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000499 void SetHeader(HBasicBlock* block) {
500 header_ = block;
501 }
502
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100503 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
504 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
505 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
506
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000507 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100508 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000509 }
510
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100511 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100512 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100513 }
514
David Brazdil46e2a392015-03-16 17:31:52 +0000515 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100516 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100517 }
518
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000519 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100520 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000521 }
522
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100523 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100524
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100525 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100526 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100527 }
528
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100529 // Returns the lifetime position of the back edge that has the
530 // greatest lifetime position.
531 size_t GetLifetimeEnd() const;
532
533 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100534 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100535 }
536
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100537 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100538 // that is the header dominates the back edge.
539 bool Populate();
540
David Brazdila4b8c212015-05-07 09:59:30 +0100541 // Reanalyzes the loop by removing loop info from its blocks and re-running
542 // Populate(). If there are no back edges left, the loop info is completely
543 // removed as well as its SuspendCheck instruction. It must be run on nested
544 // inner loops first.
545 void Update();
546
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100547 // Returns whether this loop information contains `block`.
548 // Note that this loop information *must* be populated before entering this function.
549 bool Contains(const HBasicBlock& block) const;
550
551 // Returns whether this loop information is an inner loop of `other`.
552 // Note that `other` *must* be populated before entering this function.
553 bool IsIn(const HLoopInformation& other) const;
554
Aart Bik73f1f3b2015-10-28 15:28:08 -0700555 // Returns true if instruction is not defined within this loop or any loop nested inside
556 // this loop. If must_dominate is set, only definitions that actually dominate the loop
557 // header can be invariant. Otherwise, any definition outside the loop, including
558 // definitions that appear after the loop, is invariant.
559 bool IsLoopInvariant(HInstruction* instruction, bool must_dominate) const;
560
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100561 const ArenaBitVector& GetBlocks() const { return blocks_; }
562
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000563 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000564 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000565
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000566 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100567 // Internal recursive implementation of `Populate`.
568 void PopulateRecursive(HBasicBlock* block);
569
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000570 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100571 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100572 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100573 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000574
575 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
576};
577
David Brazdilec16f792015-08-19 15:04:01 +0100578// Stores try/catch information for basic blocks.
579// Note that HGraph is constructed so that catch blocks cannot simultaneously
580// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100581class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100582 public:
583 // Try block information constructor.
584 explicit TryCatchInformation(const HTryBoundary& try_entry)
585 : try_entry_(&try_entry),
586 catch_dex_file_(nullptr),
587 catch_type_index_(DexFile::kDexNoIndex16) {
588 DCHECK(try_entry_ != nullptr);
589 }
590
591 // Catch block information constructor.
592 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
593 : try_entry_(nullptr),
594 catch_dex_file_(&dex_file),
595 catch_type_index_(catch_type_index) {}
596
597 bool IsTryBlock() const { return try_entry_ != nullptr; }
598
599 const HTryBoundary& GetTryEntry() const {
600 DCHECK(IsTryBlock());
601 return *try_entry_;
602 }
603
604 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
605
606 bool IsCatchAllTypeIndex() const {
607 DCHECK(IsCatchBlock());
608 return catch_type_index_ == DexFile::kDexNoIndex16;
609 }
610
611 uint16_t GetCatchTypeIndex() const {
612 DCHECK(IsCatchBlock());
613 return catch_type_index_;
614 }
615
616 const DexFile& GetCatchDexFile() const {
617 DCHECK(IsCatchBlock());
618 return *catch_dex_file_;
619 }
620
621 private:
622 // One of possibly several TryBoundary instructions entering the block's try.
623 // Only set for try blocks.
624 const HTryBoundary* try_entry_;
625
626 // Exception type information. Only set for catch blocks.
627 const DexFile* catch_dex_file_;
628 const uint16_t catch_type_index_;
629};
630
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100631static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100632static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100633
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000634// A block in a method. Contains the list of instructions represented
635// as a double linked list. Each block knows its predecessors and
636// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100637
Vladimir Markof9f64412015-09-02 14:05:49 +0100638class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000639 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600640 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000642 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
643 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000644 loop_information_(nullptr),
645 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000646 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100647 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100648 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100649 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000650 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000651 try_catch_information_(nullptr) {
652 predecessors_.reserve(kDefaultNumberOfPredecessors);
653 successors_.reserve(kDefaultNumberOfSuccessors);
654 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
655 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000656
Vladimir Marko60584552015-09-03 13:35:12 +0000657 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100658 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000659 }
660
Vladimir Marko60584552015-09-03 13:35:12 +0000661 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100662 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000663 }
664
Vladimir Marko60584552015-09-03 13:35:12 +0000665 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
666 return ContainsElement(successors_, block, start_from);
667 }
668
669 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100670 return dominated_blocks_;
671 }
672
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100673 bool IsEntryBlock() const {
674 return graph_->GetEntryBlock() == this;
675 }
676
677 bool IsExitBlock() const {
678 return graph_->GetExitBlock() == this;
679 }
680
David Brazdil46e2a392015-03-16 17:31:52 +0000681 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000682 bool IsSingleTryBoundary() const;
683
684 // Returns true if this block emits nothing but a jump.
685 bool IsSingleJump() const {
686 HLoopInformation* loop_info = GetLoopInformation();
687 return (IsSingleGoto() || IsSingleTryBoundary())
688 // Back edges generate a suspend check.
689 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
690 }
David Brazdil46e2a392015-03-16 17:31:52 +0000691
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000692 void AddBackEdge(HBasicBlock* back_edge) {
693 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000694 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000695 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100696 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000697 loop_information_->AddBackEdge(back_edge);
698 }
699
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000700 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000701 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000702
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100703 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000704 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600705 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000706
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000707 HBasicBlock* GetDominator() const { return dominator_; }
708 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000709 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
710
711 void RemoveDominatedBlock(HBasicBlock* block) {
712 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100713 }
Vladimir Marko60584552015-09-03 13:35:12 +0000714
715 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
716 ReplaceElement(dominated_blocks_, existing, new_block);
717 }
718
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100719 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000720
721 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100722 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000723 }
724
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100725 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
726 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100727 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100728 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100729 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
730 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000731
732 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000733 successors_.push_back(block);
734 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000735 }
736
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100737 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
738 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100739 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000740 new_block->predecessors_.push_back(this);
741 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000742 }
743
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000744 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
745 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000746 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000747 new_block->successors_.push_back(this);
748 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000749 }
750
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100751 // Insert `this` between `predecessor` and `successor. This method
752 // preserves the indicies, and will update the first edge found between
753 // `predecessor` and `successor`.
754 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
755 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100756 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000757 successor->predecessors_[predecessor_index] = this;
758 predecessor->successors_[successor_index] = this;
759 successors_.push_back(successor);
760 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100761 }
762
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100763 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000764 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100765 }
766
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000767 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000768 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000769 }
770
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100771 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000772 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100773 }
774
775 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000776 predecessors_.push_back(block);
777 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100778 }
779
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100780 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000781 DCHECK_EQ(predecessors_.size(), 2u);
782 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100783 }
784
David Brazdil769c9e52015-04-27 13:54:09 +0100785 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000786 DCHECK_EQ(successors_.size(), 2u);
787 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100788 }
789
David Brazdilfc6a86a2015-06-26 10:33:45 +0000790 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000791 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100792 }
793
David Brazdilfc6a86a2015-06-26 10:33:45 +0000794 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000795 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100796 }
797
David Brazdilfc6a86a2015-06-26 10:33:45 +0000798 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000799 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100800 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000801 }
802
803 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000804 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100805 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000806 }
807
808 // Returns whether the first occurrence of `predecessor` in the list of
809 // predecessors is at index `idx`.
810 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100811 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000812 return GetPredecessorIndexOf(predecessor) == idx;
813 }
814
David Brazdilffee3d32015-07-06 11:48:53 +0100815 // Returns the number of non-exceptional successors. SsaChecker ensures that
816 // these are stored at the beginning of the successor list.
817 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000818 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100819 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000820
David Brazdild7558da2015-09-22 13:04:14 +0100821 // Create a new block between this block and its predecessors. The new block
822 // is added to the graph, all predecessor edges are relinked to it and an edge
823 // is created to `this`. Returns the new empty block. Reverse post order or
824 // loop and try/catch information are not updated.
825 HBasicBlock* CreateImmediateDominator();
826
David Brazdilfc6a86a2015-06-26 10:33:45 +0000827 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100828 // created, latter block. Note that this method will add the block to the
829 // graph, create a Goto at the end of the former block and will create an edge
830 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100831 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000832 HBasicBlock* SplitBefore(HInstruction* cursor);
833
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000834 // Split the block into two blocks just after `cursor`. Returns the newly
835 // created block. Note that this method just updates raw block information,
836 // like predecessors, successors, dominators, and instruction list. It does not
837 // update the graph, reverse post order, loop information, nor make sure the
838 // blocks are consistent (for example ending with a control flow instruction).
839 HBasicBlock* SplitAfter(HInstruction* cursor);
840
David Brazdil9bc43612015-11-05 21:25:24 +0000841 // Split catch block into two blocks after the original move-exception bytecode
842 // instruction, or at the beginning if not present. Returns the newly created,
843 // latter block, or nullptr if such block could not be created (must be dead
844 // in that case). Note that this method just updates raw block information,
845 // like predecessors, successors, dominators, and instruction list. It does not
846 // update the graph, reverse post order, loop information, nor make sure the
847 // blocks are consistent (for example ending with a control flow instruction).
848 HBasicBlock* SplitCatchBlockAfterMoveException();
849
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000850 // Merge `other` at the end of `this`. Successors and dominated blocks of
851 // `other` are changed to be successors and dominated blocks of `this`. Note
852 // that this method does not update the graph, reverse post order, loop
853 // information, nor make sure the blocks are consistent (for example ending
854 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100855 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000856
857 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
858 // of `this` are moved to `other`.
859 // Note that this method does not update the graph, reverse post order, loop
860 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000861 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000862 void ReplaceWith(HBasicBlock* other);
863
David Brazdil2d7352b2015-04-20 14:52:42 +0100864 // Merge `other` at the end of `this`. This method updates loops, reverse post
865 // order, links to predecessors, successors, dominators and deletes the block
866 // from the graph. The two blocks must be successive, i.e. `this` the only
867 // predecessor of `other` and vice versa.
868 void MergeWith(HBasicBlock* other);
869
870 // Disconnects `this` from all its predecessors, successors and dominator,
871 // removes it from all loops it is included in and eventually from the graph.
872 // The block must not dominate any other block. Predecessors and successors
873 // are safely updated.
874 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000875
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000876 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100877 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100878 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100879 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100880 // Replace instruction `initial` with `replacement` within this block.
881 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
882 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100883 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100884 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000885 // RemoveInstruction and RemovePhi delete a given instruction from the respective
886 // instruction list. With 'ensure_safety' set to true, it verifies that the
887 // instruction is not in use and removes it from the use lists of its inputs.
888 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
889 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100890 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100891
892 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100893 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100894 }
895
Roland Levillain6b879dd2014-09-22 17:13:44 +0100896 bool IsLoopPreHeaderFirstPredecessor() const {
897 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100898 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100899 }
900
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100901 HLoopInformation* GetLoopInformation() const {
902 return loop_information_;
903 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000904
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000905 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100906 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000907 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100908 void SetInLoop(HLoopInformation* info) {
909 if (IsLoopHeader()) {
910 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100911 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100912 loop_information_ = info;
913 } else if (loop_information_->Contains(*info->GetHeader())) {
914 // Block is currently part of an outer loop. Make it part of this inner loop.
915 // Note that a non loop header having a loop information means this loop information
916 // has already been populated
917 loop_information_ = info;
918 } else {
919 // Block is part of an inner loop. Do not update the loop information.
920 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
921 // at this point, because this method is being called while populating `info`.
922 }
923 }
924
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000925 // Raw update of the loop information.
926 void SetLoopInformation(HLoopInformation* info) {
927 loop_information_ = info;
928 }
929
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100930 bool IsInLoop() const { return loop_information_ != nullptr; }
931
David Brazdilec16f792015-08-19 15:04:01 +0100932 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
933
934 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
935 try_catch_information_ = try_catch_information;
936 }
937
938 bool IsTryBlock() const {
939 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
940 }
941
942 bool IsCatchBlock() const {
943 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
944 }
David Brazdilffee3d32015-07-06 11:48:53 +0100945
946 // Returns the try entry that this block's successors should have. They will
947 // be in the same try, unless the block ends in a try boundary. In that case,
948 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100949 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100950
David Brazdild7558da2015-09-22 13:04:14 +0100951 bool HasThrowingInstructions() const;
952
David Brazdila4b8c212015-05-07 09:59:30 +0100953 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100954 bool Dominates(HBasicBlock* block) const;
955
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100956 size_t GetLifetimeStart() const { return lifetime_start_; }
957 size_t GetLifetimeEnd() const { return lifetime_end_; }
958
959 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
960 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
961
David Brazdil8d5b8b22015-03-24 10:51:52 +0000962 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000963 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100964 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000965 bool HasSinglePhi() const;
966
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000968 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000969 ArenaVector<HBasicBlock*> predecessors_;
970 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100971 HInstructionList instructions_;
972 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000973 HLoopInformation* loop_information_;
974 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000975 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100976 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100977 // The dex program counter of the first instruction of this block.
978 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100979 size_t lifetime_start_;
980 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100981 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100982
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000983 friend class HGraph;
984 friend class HInstruction;
985
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000986 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
987};
988
David Brazdilb2bd1c52015-03-25 11:17:37 +0000989// Iterates over the LoopInformation of all loops which contain 'block'
990// from the innermost to the outermost.
991class HLoopInformationOutwardIterator : public ValueObject {
992 public:
993 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
994 : current_(block.GetLoopInformation()) {}
995
996 bool Done() const { return current_ == nullptr; }
997
998 void Advance() {
999 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001000 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001001 }
1002
1003 HLoopInformation* Current() const {
1004 DCHECK(!Done());
1005 return current_;
1006 }
1007
1008 private:
1009 HLoopInformation* current_;
1010
1011 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1012};
1013
Alexandre Ramesef20f712015-06-09 10:29:30 +01001014#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001015 M(Above, Condition) \
1016 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001017 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001018 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001019 M(ArrayGet, Instruction) \
1020 M(ArrayLength, Instruction) \
1021 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001022 M(Below, Condition) \
1023 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001024 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001025 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001026 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001027 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001028 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001029 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001030 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001031 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001032 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001033 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001034 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001035 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001036 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001037 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001038 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001039 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001040 M(FloatConstant, Constant) \
1041 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001042 M(GreaterThan, Condition) \
1043 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001044 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001045 M(InstanceFieldGet, Instruction) \
1046 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001047 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001048 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001049 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001050 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001051 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001052 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001053 M(LessThan, Condition) \
1054 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001055 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001056 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001057 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001058 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001059 M(Local, Instruction) \
1060 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001061 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001062 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001063 M(Mul, BinaryOperation) \
1064 M(Neg, UnaryOperation) \
1065 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001066 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001067 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001068 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001069 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001070 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001071 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001072 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001073 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001074 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001075 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001076 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001077 M(Return, Instruction) \
1078 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001079 M(Shl, BinaryOperation) \
1080 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001081 M(StaticFieldGet, Instruction) \
1082 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001083 M(UnresolvedInstanceFieldGet, Instruction) \
1084 M(UnresolvedInstanceFieldSet, Instruction) \
1085 M(UnresolvedStaticFieldGet, Instruction) \
1086 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001087 M(StoreLocal, Instruction) \
1088 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001089 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001090 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001091 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001092 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001093 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001094 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001095 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001096
Alexandre Ramesef20f712015-06-09 10:29:30 +01001097#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1098
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001099#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001100#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001101#else
1102#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1103 M(Arm64IntermediateAddress, Instruction)
1104#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001105
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001106#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1107
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001108#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1109
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001110#ifndef ART_ENABLE_CODEGEN_x86
1111#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1112#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001113#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1114 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001115 M(X86LoadFromConstantTable, Instruction) \
1116 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001117#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001118
1119#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1120
1121#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1122 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1123 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1124 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001125 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001126 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001127 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1128 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1129
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001130#define FOR_EACH_INSTRUCTION(M) \
1131 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1132 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001133 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001134 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001135 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001136
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001137#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001138FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1139#undef FORWARD_DECLARATION
1140
Roland Levillainccc07a92014-09-16 14:48:16 +01001141#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001142 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1143 const char* DebugName() const OVERRIDE { return #type; } \
1144 const H##type* As##type() const OVERRIDE { return this; } \
1145 H##type* As##type() OVERRIDE { return this; } \
1146 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001147 return other->Is##type(); \
1148 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001149 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001150
David Brazdiled596192015-01-23 10:39:45 +00001151template <typename T> class HUseList;
1152
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001153template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001154class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001155 public:
David Brazdiled596192015-01-23 10:39:45 +00001156 HUseListNode* GetPrevious() const { return prev_; }
1157 HUseListNode* GetNext() const { return next_; }
1158 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001159 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001160 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001161
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001162 private:
David Brazdiled596192015-01-23 10:39:45 +00001163 HUseListNode(T user, size_t index)
1164 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1165
1166 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001167 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001168 HUseListNode<T>* prev_;
1169 HUseListNode<T>* next_;
1170
1171 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001172
1173 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1174};
1175
David Brazdiled596192015-01-23 10:39:45 +00001176template <typename T>
1177class HUseList : public ValueObject {
1178 public:
1179 HUseList() : first_(nullptr) {}
1180
1181 void Clear() {
1182 first_ = nullptr;
1183 }
1184
1185 // Adds a new entry at the beginning of the use list and returns
1186 // the newly created node.
1187 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001188 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001189 if (IsEmpty()) {
1190 first_ = new_node;
1191 } else {
1192 first_->prev_ = new_node;
1193 new_node->next_ = first_;
1194 first_ = new_node;
1195 }
1196 return new_node;
1197 }
1198
1199 HUseListNode<T>* GetFirst() const {
1200 return first_;
1201 }
1202
1203 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001204 DCHECK(node != nullptr);
1205 DCHECK(Contains(node));
1206
David Brazdiled596192015-01-23 10:39:45 +00001207 if (node->prev_ != nullptr) {
1208 node->prev_->next_ = node->next_;
1209 }
1210 if (node->next_ != nullptr) {
1211 node->next_->prev_ = node->prev_;
1212 }
1213 if (node == first_) {
1214 first_ = node->next_;
1215 }
1216 }
1217
David Brazdil1abb4192015-02-17 18:33:36 +00001218 bool Contains(const HUseListNode<T>* node) const {
1219 if (node == nullptr) {
1220 return false;
1221 }
1222 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1223 if (current == node) {
1224 return true;
1225 }
1226 }
1227 return false;
1228 }
1229
David Brazdiled596192015-01-23 10:39:45 +00001230 bool IsEmpty() const {
1231 return first_ == nullptr;
1232 }
1233
1234 bool HasOnlyOneUse() const {
1235 return first_ != nullptr && first_->next_ == nullptr;
1236 }
1237
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001238 size_t SizeSlow() const {
1239 size_t count = 0;
1240 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1241 ++count;
1242 }
1243 return count;
1244 }
1245
David Brazdiled596192015-01-23 10:39:45 +00001246 private:
1247 HUseListNode<T>* first_;
1248};
1249
1250template<typename T>
1251class HUseIterator : public ValueObject {
1252 public:
1253 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1254
1255 bool Done() const { return current_ == nullptr; }
1256
1257 void Advance() {
1258 DCHECK(!Done());
1259 current_ = current_->GetNext();
1260 }
1261
1262 HUseListNode<T>* Current() const {
1263 DCHECK(!Done());
1264 return current_;
1265 }
1266
1267 private:
1268 HUseListNode<T>* current_;
1269
1270 friend class HValue;
1271};
1272
David Brazdil1abb4192015-02-17 18:33:36 +00001273// This class is used by HEnvironment and HInstruction classes to record the
1274// instructions they use and pointers to the corresponding HUseListNodes kept
1275// by the used instructions.
1276template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001277class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001278 public:
1279 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1280 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1281
1282 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1283 : instruction_(old_record.instruction_), use_node_(use_node) {
1284 DCHECK(instruction_ != nullptr);
1285 DCHECK(use_node_ != nullptr);
1286 DCHECK(old_record.use_node_ == nullptr);
1287 }
1288
1289 HInstruction* GetInstruction() const { return instruction_; }
1290 HUseListNode<T>* GetUseNode() const { return use_node_; }
1291
1292 private:
1293 // Instruction used by the user.
1294 HInstruction* instruction_;
1295
1296 // Corresponding entry in the use list kept by 'instruction_'.
1297 HUseListNode<T>* use_node_;
1298};
1299
Aart Bik854a02b2015-07-14 16:07:00 -07001300/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001301 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001302 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001303 * For write/read dependences on fields/arrays, the dependence analysis uses
1304 * type disambiguation (e.g. a float field write cannot modify the value of an
1305 * integer field read) and the access type (e.g. a reference array write cannot
1306 * modify the value of a reference field read [although it may modify the
1307 * reference fetch prior to reading the field, which is represented by its own
1308 * write/read dependence]). The analysis makes conservative points-to
1309 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1310 * the same, and any reference read depends on any reference read without
1311 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001312 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001313 * The internal representation uses 38-bit and is described in the table below.
1314 * The first line indicates the side effect, and for field/array accesses the
1315 * second line indicates the type of the access (in the order of the
1316 * Primitive::Type enum).
1317 * The two numbered lines below indicate the bit position in the bitfield (read
1318 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001319 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001320 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1321 * +-------------+---------+---------+--------------+---------+---------+
1322 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1323 * | 3 |333333322|222222221| 1 |111111110|000000000|
1324 * | 7 |654321098|765432109| 8 |765432109|876543210|
1325 *
1326 * Note that, to ease the implementation, 'changes' bits are least significant
1327 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001328 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001329class SideEffects : public ValueObject {
1330 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001331 SideEffects() : flags_(0) {}
1332
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001333 static SideEffects None() {
1334 return SideEffects(0);
1335 }
1336
1337 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001338 return SideEffects(kAllChangeBits | kAllDependOnBits);
1339 }
1340
1341 static SideEffects AllChanges() {
1342 return SideEffects(kAllChangeBits);
1343 }
1344
1345 static SideEffects AllDependencies() {
1346 return SideEffects(kAllDependOnBits);
1347 }
1348
1349 static SideEffects AllExceptGCDependency() {
1350 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1351 }
1352
1353 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001354 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001355 }
1356
Aart Bik34c3ba92015-07-20 14:08:59 -07001357 static SideEffects AllWrites() {
1358 return SideEffects(kAllWrites);
1359 }
1360
1361 static SideEffects AllReads() {
1362 return SideEffects(kAllReads);
1363 }
1364
1365 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1366 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001367 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001368 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001369 }
1370
Aart Bik854a02b2015-07-14 16:07:00 -07001371 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1372 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001373 }
1374
Aart Bik34c3ba92015-07-20 14:08:59 -07001375 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1376 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001377 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001378 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001379 }
1380
1381 static SideEffects ArrayReadOfType(Primitive::Type type) {
1382 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1383 }
1384
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001385 static SideEffects CanTriggerGC() {
1386 return SideEffects(1ULL << kCanTriggerGCBit);
1387 }
1388
1389 static SideEffects DependsOnGC() {
1390 return SideEffects(1ULL << kDependsOnGCBit);
1391 }
1392
Aart Bik854a02b2015-07-14 16:07:00 -07001393 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001394 SideEffects Union(SideEffects other) const {
1395 return SideEffects(flags_ | other.flags_);
1396 }
1397
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001398 SideEffects Exclusion(SideEffects other) const {
1399 return SideEffects(flags_ & ~other.flags_);
1400 }
1401
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001402 void Add(SideEffects other) {
1403 flags_ |= other.flags_;
1404 }
1405
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001406 bool Includes(SideEffects other) const {
1407 return (other.flags_ & flags_) == other.flags_;
1408 }
1409
1410 bool HasSideEffects() const {
1411 return (flags_ & kAllChangeBits);
1412 }
1413
1414 bool HasDependencies() const {
1415 return (flags_ & kAllDependOnBits);
1416 }
1417
1418 // Returns true if there are no side effects or dependencies.
1419 bool DoesNothing() const {
1420 return flags_ == 0;
1421 }
1422
Aart Bik854a02b2015-07-14 16:07:00 -07001423 // Returns true if something is written.
1424 bool DoesAnyWrite() const {
1425 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001426 }
1427
Aart Bik854a02b2015-07-14 16:07:00 -07001428 // Returns true if something is read.
1429 bool DoesAnyRead() const {
1430 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001431 }
1432
Aart Bik854a02b2015-07-14 16:07:00 -07001433 // Returns true if potentially everything is written and read
1434 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001435 bool DoesAllReadWrite() const {
1436 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1437 }
1438
Aart Bik854a02b2015-07-14 16:07:00 -07001439 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001440 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001441 }
1442
1443 // Returns true if this may read something written by other.
1444 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001445 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1446 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001447 }
1448
1449 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001450 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001451 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001452 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001453 for (int s = kLastBit; s >= 0; s--) {
1454 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1455 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1456 // This is a bit for the GC side effect.
1457 if (current_bit_is_set) {
1458 flags += "GC";
1459 }
Aart Bik854a02b2015-07-14 16:07:00 -07001460 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001461 } else {
1462 // This is a bit for the array/field analysis.
1463 // The underscore character stands for the 'can trigger GC' bit.
1464 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1465 if (current_bit_is_set) {
1466 flags += kDebug[s];
1467 }
1468 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1469 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1470 flags += "|";
1471 }
1472 }
Aart Bik854a02b2015-07-14 16:07:00 -07001473 }
1474 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001475 }
1476
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001477 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001478
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001479 private:
1480 static constexpr int kFieldArrayAnalysisBits = 9;
1481
1482 static constexpr int kFieldWriteOffset = 0;
1483 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1484 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1485 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1486
1487 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1488
1489 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1490 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1491 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1492 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1493
1494 static constexpr int kLastBit = kDependsOnGCBit;
1495 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1496
1497 // Aliases.
1498
1499 static_assert(kChangeBits == kDependOnBits,
1500 "the 'change' bits should match the 'depend on' bits.");
1501
1502 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1503 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1504 static constexpr uint64_t kAllWrites =
1505 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1506 static constexpr uint64_t kAllReads =
1507 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001508
Aart Bik854a02b2015-07-14 16:07:00 -07001509 // Work around the fact that HIR aliases I/F and J/D.
1510 // TODO: remove this interceptor once HIR types are clean
1511 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1512 switch (type) {
1513 case Primitive::kPrimInt:
1514 case Primitive::kPrimFloat:
1515 return TypeFlag(Primitive::kPrimInt, offset) |
1516 TypeFlag(Primitive::kPrimFloat, offset);
1517 case Primitive::kPrimLong:
1518 case Primitive::kPrimDouble:
1519 return TypeFlag(Primitive::kPrimLong, offset) |
1520 TypeFlag(Primitive::kPrimDouble, offset);
1521 default:
1522 return TypeFlag(type, offset);
1523 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001524 }
1525
Aart Bik854a02b2015-07-14 16:07:00 -07001526 // Translates type to bit flag.
1527 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1528 CHECK_NE(type, Primitive::kPrimVoid);
1529 const uint64_t one = 1;
1530 const int shift = type; // 0-based consecutive enum
1531 DCHECK_LE(kFieldWriteOffset, shift);
1532 DCHECK_LT(shift, kArrayWriteOffset);
1533 return one << (type + offset);
1534 }
1535
1536 // Private constructor on direct flags value.
1537 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1538
1539 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001540};
1541
David Brazdiled596192015-01-23 10:39:45 +00001542// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001543class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001544 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001545 HEnvironment(ArenaAllocator* arena,
1546 size_t number_of_vregs,
1547 const DexFile& dex_file,
1548 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001549 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001550 InvokeType invoke_type,
1551 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001552 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1553 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001554 parent_(nullptr),
1555 dex_file_(dex_file),
1556 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001557 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001558 invoke_type_(invoke_type),
1559 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001560 }
1561
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001562 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001563 : HEnvironment(arena,
1564 to_copy.Size(),
1565 to_copy.GetDexFile(),
1566 to_copy.GetMethodIdx(),
1567 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001568 to_copy.GetInvokeType(),
1569 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001570
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001571 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001572 if (parent_ != nullptr) {
1573 parent_->SetAndCopyParentChain(allocator, parent);
1574 } else {
1575 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1576 parent_->CopyFrom(parent);
1577 if (parent->GetParent() != nullptr) {
1578 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1579 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001580 }
David Brazdiled596192015-01-23 10:39:45 +00001581 }
1582
Vladimir Marko71bf8092015-09-15 15:33:14 +01001583 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001584 void CopyFrom(HEnvironment* environment);
1585
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001586 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1587 // input to the loop phi instead. This is for inserting instructions that
1588 // require an environment (like HDeoptimization) in the loop pre-header.
1589 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001590
1591 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001592 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001593 }
1594
1595 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001596 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001597 }
1598
David Brazdil1abb4192015-02-17 18:33:36 +00001599 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001600
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001601 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001602
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001603 HEnvironment* GetParent() const { return parent_; }
1604
1605 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001606 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001607 }
1608
1609 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001610 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001611 }
1612
1613 uint32_t GetDexPc() const {
1614 return dex_pc_;
1615 }
1616
1617 uint32_t GetMethodIdx() const {
1618 return method_idx_;
1619 }
1620
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001621 InvokeType GetInvokeType() const {
1622 return invoke_type_;
1623 }
1624
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001625 const DexFile& GetDexFile() const {
1626 return dex_file_;
1627 }
1628
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001629 HInstruction* GetHolder() const {
1630 return holder_;
1631 }
1632
David Brazdiled596192015-01-23 10:39:45 +00001633 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001634 // Record instructions' use entries of this environment for constant-time removal.
1635 // It should only be called by HInstruction when a new environment use is added.
1636 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1637 DCHECK(env_use->GetUser() == this);
1638 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001639 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001640 }
David Brazdiled596192015-01-23 10:39:45 +00001641
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001642 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1643 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001644 HEnvironment* parent_;
1645 const DexFile& dex_file_;
1646 const uint32_t method_idx_;
1647 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001648 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001649
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001650 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001651 HInstruction* const holder_;
1652
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001653 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001654
1655 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1656};
1657
Calin Juravleacf735c2015-02-12 15:25:22 +00001658class ReferenceTypeInfo : ValueObject {
1659 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001660 typedef Handle<mirror::Class> TypeHandle;
1661
Calin Juravle2e768302015-07-28 14:41:11 +00001662 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1663 // The constructor will check that the type_handle is valid.
1664 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001665 }
1666
Calin Juravle2e768302015-07-28 14:41:11 +00001667 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1668
1669 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1670 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001671 }
1672
Calin Juravle2e768302015-07-28 14:41:11 +00001673 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1674 return IsValidHandle(type_handle_);
1675 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001676
Calin Juravleacf735c2015-02-12 15:25:22 +00001677 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001678
1679 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1680 DCHECK(IsValid());
1681 return GetTypeHandle()->IsObjectClass();
1682 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001683
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001684 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001685 DCHECK(IsValid());
1686 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001687 }
1688
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001689 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1690 DCHECK(IsValid());
1691 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1692 }
1693
Mathieu Chartier90443472015-07-16 20:32:27 -07001694 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001695 DCHECK(IsValid());
1696 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001697 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001698
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001699 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001700 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001701 return GetTypeHandle()->IsArrayClass();
1702 }
1703
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001704 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1705 DCHECK(IsValid());
1706 return GetTypeHandle()->IsPrimitiveArray();
1707 }
1708
1709 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1710 DCHECK(IsValid());
1711 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1712 }
1713
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001714 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001715 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001716 if (!IsExact()) return false;
1717 if (!IsArrayClass()) return false;
1718 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1719 }
1720
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001721 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1722 DCHECK(IsValid());
1723 if (!IsExact()) return false;
1724 if (!IsArrayClass()) return false;
1725 if (!rti.IsArrayClass()) return false;
1726 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1727 rti.GetTypeHandle()->GetComponentType());
1728 }
1729
Calin Juravleacf735c2015-02-12 15:25:22 +00001730 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1731
Mathieu Chartier90443472015-07-16 20:32:27 -07001732 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001733 DCHECK(IsValid());
1734 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001735 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1736 }
1737
1738 // Returns true if the type information provide the same amount of details.
1739 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001740 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001741 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001742 if (!IsValid() && !rti.IsValid()) {
1743 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001744 return true;
1745 }
Calin Juravle2e768302015-07-28 14:41:11 +00001746 if (!IsValid() || !rti.IsValid()) {
1747 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001748 return false;
1749 }
Calin Juravle2e768302015-07-28 14:41:11 +00001750 return IsExact() == rti.IsExact()
1751 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001752 }
1753
1754 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001755 ReferenceTypeInfo();
1756 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001757
Calin Juravleacf735c2015-02-12 15:25:22 +00001758 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001759 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001760 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001761 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001762 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001763};
1764
1765std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1766
Vladimir Markof9f64412015-09-02 14:05:49 +01001767class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001768 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001769 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001770 : previous_(nullptr),
1771 next_(nullptr),
1772 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001773 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001774 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001775 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001776 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001777 locations_(nullptr),
1778 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001779 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001780 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001781 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001782
Dave Allison20dfc792014-06-16 20:44:29 -07001783 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001784
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001785#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001786 enum InstructionKind {
1787 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1788 };
1789#undef DECLARE_KIND
1790
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001791 HInstruction* GetNext() const { return next_; }
1792 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001793
Calin Juravle77520bc2015-01-12 18:45:46 +00001794 HInstruction* GetNextDisregardingMoves() const;
1795 HInstruction* GetPreviousDisregardingMoves() const;
1796
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001797 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001798 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001799 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001800 bool IsInBlock() const { return block_ != nullptr; }
1801 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001802 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001803
Roland Levillain6b879dd2014-09-22 17:13:44 +01001804 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001805 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001806
1807 virtual void Accept(HGraphVisitor* visitor) = 0;
1808 virtual const char* DebugName() const = 0;
1809
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001810 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001811 void SetRawInputAt(size_t index, HInstruction* input) {
1812 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1813 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001814
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001815 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001816
1817 uint32_t GetDexPc() const { return dex_pc_; }
1818
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001819 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001820
Roland Levillaine161a2a2014-10-03 12:45:18 +01001821 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001822 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001823
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001824 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001825 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001826
Calin Juravle10e244f2015-01-26 18:54:32 +00001827 // Does not apply for all instructions, but having this at top level greatly
1828 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001829 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001830 virtual bool CanBeNull() const {
1831 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1832 return true;
1833 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001834
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001835 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001836 return false;
1837 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001838
Calin Juravle2e768302015-07-28 14:41:11 +00001839 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001840
Calin Juravle61d544b2015-02-23 16:46:57 +00001841 ReferenceTypeInfo GetReferenceTypeInfo() const {
1842 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1843 return reference_type_info_;
1844 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001845
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001846 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001847 DCHECK(user != nullptr);
1848 HUseListNode<HInstruction*>* use =
1849 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1850 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001851 }
1852
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001853 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001854 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001855 HUseListNode<HEnvironment*>* env_use =
1856 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1857 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001858 }
1859
David Brazdil1abb4192015-02-17 18:33:36 +00001860 void RemoveAsUserOfInput(size_t input) {
1861 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1862 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1863 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001864
David Brazdil1abb4192015-02-17 18:33:36 +00001865 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1866 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001867
David Brazdiled596192015-01-23 10:39:45 +00001868 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1869 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001870 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001871 bool HasOnlyOneNonEnvironmentUse() const {
1872 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1873 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001874
Roland Levillain6c82d402014-10-13 16:10:27 +01001875 // Does this instruction strictly dominate `other_instruction`?
1876 // Returns false if this instruction and `other_instruction` are the same.
1877 // Aborts if this instruction and `other_instruction` are both phis.
1878 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001879
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001880 int GetId() const { return id_; }
1881 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001882
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001883 int GetSsaIndex() const { return ssa_index_; }
1884 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1885 bool HasSsaIndex() const { return ssa_index_ != -1; }
1886
1887 bool HasEnvironment() const { return environment_ != nullptr; }
1888 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001889 // Set the `environment_` field. Raw because this method does not
1890 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001891 void SetRawEnvironment(HEnvironment* environment) {
1892 DCHECK(environment_ == nullptr);
1893 DCHECK_EQ(environment->GetHolder(), this);
1894 environment_ = environment;
1895 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001896
1897 // Set the environment of this instruction, copying it from `environment`. While
1898 // copying, the uses lists are being updated.
1899 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001900 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001901 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001902 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001903 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001904 if (environment->GetParent() != nullptr) {
1905 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1906 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001907 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001908
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001909 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1910 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001911 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001912 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001913 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001914 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001915 if (environment->GetParent() != nullptr) {
1916 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1917 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001918 }
1919
Nicolas Geoffray39468442014-09-02 15:17:15 +01001920 // Returns the number of entries in the environment. Typically, that is the
1921 // number of dex registers in a method. It could be more in case of inlining.
1922 size_t EnvironmentSize() const;
1923
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001924 LocationSummary* GetLocations() const { return locations_; }
1925 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001926
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001927 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001928 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001929
Alexandre Rames188d4312015-04-09 18:30:21 +01001930 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1931 // uses of this instruction by `other` are *not* updated.
1932 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1933 ReplaceWith(other);
1934 other->ReplaceInput(this, use_index);
1935 }
1936
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001937 // Move `this` instruction before `cursor`.
1938 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001939
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001940#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001941 bool Is##type() const { return (As##type() != nullptr); } \
1942 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001943 virtual H##type* As##type() { return nullptr; }
1944
1945 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1946#undef INSTRUCTION_TYPE_CHECK
1947
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001948 // Returns whether the instruction can be moved within the graph.
1949 virtual bool CanBeMoved() const { return false; }
1950
1951 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001952 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001953 return false;
1954 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001955
1956 // Returns whether any data encoded in the two instructions is equal.
1957 // This method does not look at the inputs. Both instructions must be
1958 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001959 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001960 return false;
1961 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001962
1963 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001964 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001965 // 2) Their inputs are identical.
1966 bool Equals(HInstruction* other) const;
1967
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001968 virtual InstructionKind GetKind() const = 0;
1969
1970 virtual size_t ComputeHashCode() const {
1971 size_t result = GetKind();
1972 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1973 result = (result * 31) + InputAt(i)->GetId();
1974 }
1975 return result;
1976 }
1977
1978 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001979 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001980
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001981 size_t GetLifetimePosition() const { return lifetime_position_; }
1982 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1983 LiveInterval* GetLiveInterval() const { return live_interval_; }
1984 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1985 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1986
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001987 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1988
1989 // Returns whether the code generation of the instruction will require to have access
1990 // to the current method. Such instructions are:
1991 // (1): Instructions that require an environment, as calling the runtime requires
1992 // to walk the stack and have the current method stored at a specific stack address.
1993 // (2): Object literals like classes and strings, that are loaded from the dex cache
1994 // fields of the current method.
1995 bool NeedsCurrentMethod() const {
1996 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1997 }
1998
Vladimir Markodc151b22015-10-15 18:02:30 +01001999 // Returns whether the code generation of the instruction will require to have access
2000 // to the dex cache of the current method's declaring class via the current method.
2001 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002002
Mark Mendellc4701932015-04-10 13:18:51 -04002003 // Does this instruction have any use in an environment before
2004 // control flow hits 'other'?
2005 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2006
2007 // Remove all references to environment uses of this instruction.
2008 // The caller must ensure that this is safe to do.
2009 void RemoveEnvironmentUsers();
2010
David Brazdil1abb4192015-02-17 18:33:36 +00002011 protected:
2012 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2013 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2014
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002015 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002016 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2017
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002018 HInstruction* previous_;
2019 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002020 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002021 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002022
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002023 // An instruction gets an id when it is added to the graph.
2024 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002025 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002026 int id_;
2027
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002028 // When doing liveness analysis, instructions that have uses get an SSA index.
2029 int ssa_index_;
2030
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002031 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002032 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002033
2034 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002035 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002036
Nicolas Geoffray39468442014-09-02 15:17:15 +01002037 // The environment associated with this instruction. Not null if the instruction
2038 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002039 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002040
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002041 // Set by the code generator.
2042 LocationSummary* locations_;
2043
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002044 // Set by the liveness analysis.
2045 LiveInterval* live_interval_;
2046
2047 // Set by the liveness analysis, this is the position in a linear
2048 // order of blocks where this instruction's live interval start.
2049 size_t lifetime_position_;
2050
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002051 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002052
Calin Juravleacf735c2015-02-12 15:25:22 +00002053 // TODO: for primitive types this should be marked as invalid.
2054 ReferenceTypeInfo reference_type_info_;
2055
David Brazdil1abb4192015-02-17 18:33:36 +00002056 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002057 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002058 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002059 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002060 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002061
2062 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2063};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002064std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002065
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002066class HInputIterator : public ValueObject {
2067 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002068 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002069
2070 bool Done() const { return index_ == instruction_->InputCount(); }
2071 HInstruction* Current() const { return instruction_->InputAt(index_); }
2072 void Advance() { index_++; }
2073
2074 private:
2075 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002076 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002077
2078 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2079};
2080
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002081class HInstructionIterator : public ValueObject {
2082 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002083 explicit HInstructionIterator(const HInstructionList& instructions)
2084 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002085 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002086 }
2087
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002088 bool Done() const { return instruction_ == nullptr; }
2089 HInstruction* Current() const { return instruction_; }
2090 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002091 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002092 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002093 }
2094
2095 private:
2096 HInstruction* instruction_;
2097 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002098
2099 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002100};
2101
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002102class HBackwardInstructionIterator : public ValueObject {
2103 public:
2104 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2105 : instruction_(instructions.last_instruction_) {
2106 next_ = Done() ? nullptr : instruction_->GetPrevious();
2107 }
2108
2109 bool Done() const { return instruction_ == nullptr; }
2110 HInstruction* Current() const { return instruction_; }
2111 void Advance() {
2112 instruction_ = next_;
2113 next_ = Done() ? nullptr : instruction_->GetPrevious();
2114 }
2115
2116 private:
2117 HInstruction* instruction_;
2118 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002119
2120 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002121};
2122
Vladimir Markof9f64412015-09-02 14:05:49 +01002123template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002124class HTemplateInstruction: public HInstruction {
2125 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002126 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002127 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002128 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002129
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002130 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002131
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002132 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002133 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2134 DCHECK_LT(i, N);
2135 return inputs_[i];
2136 }
David Brazdil1abb4192015-02-17 18:33:36 +00002137
2138 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002139 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002140 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002141 }
2142
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002143 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002144 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002145
2146 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002147};
2148
Vladimir Markof9f64412015-09-02 14:05:49 +01002149// HTemplateInstruction specialization for N=0.
2150template<>
2151class HTemplateInstruction<0>: public HInstruction {
2152 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002153 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002154 : HInstruction(side_effects, dex_pc) {}
2155
Vladimir Markof9f64412015-09-02 14:05:49 +01002156 virtual ~HTemplateInstruction() {}
2157
2158 size_t InputCount() const OVERRIDE { return 0; }
2159
2160 protected:
2161 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2162 LOG(FATAL) << "Unreachable";
2163 UNREACHABLE();
2164 }
2165
2166 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2167 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2168 LOG(FATAL) << "Unreachable";
2169 UNREACHABLE();
2170 }
2171
2172 private:
2173 friend class SsaBuilder;
2174};
2175
Dave Allison20dfc792014-06-16 20:44:29 -07002176template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002177class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002178 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002179 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002180 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002181 virtual ~HExpression() {}
2182
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002183 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002184
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002185 protected:
2186 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002187};
2188
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002189// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2190// instruction that branches to the exit block.
2191class HReturnVoid : public HTemplateInstruction<0> {
2192 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002193 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2194 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002195
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002196 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002197
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002198 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002199
2200 private:
2201 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2202};
2203
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002204// Represents dex's RETURN opcodes. A HReturn is a control flow
2205// instruction that branches to the exit block.
2206class HReturn : public HTemplateInstruction<1> {
2207 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002208 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2209 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002210 SetRawInputAt(0, value);
2211 }
2212
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002213 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002214
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002215 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002216
2217 private:
2218 DISALLOW_COPY_AND_ASSIGN(HReturn);
2219};
2220
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002221// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002222// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002223// exit block.
2224class HExit : public HTemplateInstruction<0> {
2225 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002226 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002227
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002228 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002229
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002230 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002231
2232 private:
2233 DISALLOW_COPY_AND_ASSIGN(HExit);
2234};
2235
2236// Jumps from one block to another.
2237class HGoto : public HTemplateInstruction<0> {
2238 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002239 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002240
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002241 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002242
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002243 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002244 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002245 }
2246
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002247 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002248
2249 private:
2250 DISALLOW_COPY_AND_ASSIGN(HGoto);
2251};
2252
Roland Levillain9867bc72015-08-05 10:21:34 +01002253class HConstant : public HExpression<0> {
2254 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002255 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2256 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002257
2258 bool CanBeMoved() const OVERRIDE { return true; }
2259
2260 virtual bool IsMinusOne() const { return false; }
2261 virtual bool IsZero() const { return false; }
2262 virtual bool IsOne() const { return false; }
2263
David Brazdil77a48ae2015-09-15 12:34:04 +00002264 virtual uint64_t GetValueAsUint64() const = 0;
2265
Roland Levillain9867bc72015-08-05 10:21:34 +01002266 DECLARE_INSTRUCTION(Constant);
2267
2268 private:
2269 DISALLOW_COPY_AND_ASSIGN(HConstant);
2270};
2271
2272class HNullConstant : public HConstant {
2273 public:
2274 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2275 return true;
2276 }
2277
David Brazdil77a48ae2015-09-15 12:34:04 +00002278 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2279
Roland Levillain9867bc72015-08-05 10:21:34 +01002280 size_t ComputeHashCode() const OVERRIDE { return 0; }
2281
2282 DECLARE_INSTRUCTION(NullConstant);
2283
2284 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002285 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002286
2287 friend class HGraph;
2288 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2289};
2290
2291// Constants of the type int. Those can be from Dex instructions, or
2292// synthesized (for example with the if-eqz instruction).
2293class HIntConstant : public HConstant {
2294 public:
2295 int32_t GetValue() const { return value_; }
2296
David Brazdil9f389d42015-10-01 14:32:56 +01002297 uint64_t GetValueAsUint64() const OVERRIDE {
2298 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2299 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002300
Roland Levillain9867bc72015-08-05 10:21:34 +01002301 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2302 DCHECK(other->IsIntConstant());
2303 return other->AsIntConstant()->value_ == value_;
2304 }
2305
2306 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2307
2308 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2309 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2310 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2311
2312 DECLARE_INSTRUCTION(IntConstant);
2313
2314 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002315 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2316 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2317 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2318 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002319
2320 const int32_t value_;
2321
2322 friend class HGraph;
2323 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2324 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2325 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2326};
2327
2328class HLongConstant : public HConstant {
2329 public:
2330 int64_t GetValue() const { return value_; }
2331
David Brazdil77a48ae2015-09-15 12:34:04 +00002332 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2333
Roland Levillain9867bc72015-08-05 10:21:34 +01002334 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2335 DCHECK(other->IsLongConstant());
2336 return other->AsLongConstant()->value_ == value_;
2337 }
2338
2339 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2340
2341 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2342 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2343 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2344
2345 DECLARE_INSTRUCTION(LongConstant);
2346
2347 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002348 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2349 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002350
2351 const int64_t value_;
2352
2353 friend class HGraph;
2354 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2355};
Dave Allison20dfc792014-06-16 20:44:29 -07002356
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002357// Conditional branch. A block ending with an HIf instruction must have
2358// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002359class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002360 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002361 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2362 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002363 SetRawInputAt(0, input);
2364 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002365
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002366 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002367
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002368 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002369 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002370 }
2371
2372 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002373 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002374 }
2375
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002376 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002377
2378 private:
2379 DISALLOW_COPY_AND_ASSIGN(HIf);
2380};
2381
David Brazdilfc6a86a2015-06-26 10:33:45 +00002382
2383// Abstract instruction which marks the beginning and/or end of a try block and
2384// links it to the respective exception handlers. Behaves the same as a Goto in
2385// non-exceptional control flow.
2386// Normal-flow successor is stored at index zero, exception handlers under
2387// higher indices in no particular order.
2388class HTryBoundary : public HTemplateInstruction<0> {
2389 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002390 enum BoundaryKind {
2391 kEntry,
2392 kExit,
2393 };
2394
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002395 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2396 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002397
2398 bool IsControlFlow() const OVERRIDE { return true; }
2399
2400 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002401 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002402
2403 // Returns whether `handler` is among its exception handlers (non-zero index
2404 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002405 bool HasExceptionHandler(const HBasicBlock& handler) const {
2406 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002407 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002408 }
2409
2410 // If not present already, adds `handler` to its block's list of exception
2411 // handlers.
2412 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002413 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002414 GetBlock()->AddSuccessor(handler);
2415 }
2416 }
2417
David Brazdil56e1acc2015-06-30 15:41:36 +01002418 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002419
David Brazdilffee3d32015-07-06 11:48:53 +01002420 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2421
David Brazdilfc6a86a2015-06-26 10:33:45 +00002422 DECLARE_INSTRUCTION(TryBoundary);
2423
2424 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002425 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002426
2427 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2428};
2429
David Brazdilffee3d32015-07-06 11:48:53 +01002430// Iterator over exception handlers of a given HTryBoundary, i.e. over
2431// exceptional successors of its basic block.
2432class HExceptionHandlerIterator : public ValueObject {
2433 public:
2434 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2435 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2436
Vladimir Marko60584552015-09-03 13:35:12 +00002437 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002438 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002439 size_t CurrentSuccessorIndex() const { return index_; }
2440 void Advance() { ++index_; }
2441
2442 private:
2443 const HBasicBlock& block_;
2444 size_t index_;
2445
2446 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2447};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002448
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002449// Deoptimize to interpreter, upon checking a condition.
2450class HDeoptimize : public HTemplateInstruction<1> {
2451 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002452 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2453 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002454 SetRawInputAt(0, cond);
2455 }
2456
2457 bool NeedsEnvironment() const OVERRIDE { return true; }
2458 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002459
2460 DECLARE_INSTRUCTION(Deoptimize);
2461
2462 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002463 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2464};
2465
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002466// Represents the ArtMethod that was passed as a first argument to
2467// the method. It is used by instructions that depend on it, like
2468// instructions that work with the dex cache.
2469class HCurrentMethod : public HExpression<0> {
2470 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002471 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2472 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002473
2474 DECLARE_INSTRUCTION(CurrentMethod);
2475
2476 private:
2477 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2478};
2479
Mark Mendellfe57faa2015-09-18 09:26:15 -04002480// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2481// have one successor for each entry in the switch table, and the final successor
2482// will be the block containing the next Dex opcode.
2483class HPackedSwitch : public HTemplateInstruction<1> {
2484 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002485 HPackedSwitch(int32_t start_value,
2486 uint32_t num_entries,
2487 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002488 uint32_t dex_pc = kNoDexPc)
2489 : HTemplateInstruction(SideEffects::None(), dex_pc),
2490 start_value_(start_value),
2491 num_entries_(num_entries) {
2492 SetRawInputAt(0, input);
2493 }
2494
2495 bool IsControlFlow() const OVERRIDE { return true; }
2496
2497 int32_t GetStartValue() const { return start_value_; }
2498
Vladimir Marko211c2112015-09-24 16:52:33 +01002499 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002500
2501 HBasicBlock* GetDefaultBlock() const {
2502 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002503 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002504 }
2505 DECLARE_INSTRUCTION(PackedSwitch);
2506
2507 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002508 const int32_t start_value_;
2509 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002510
2511 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2512};
2513
Roland Levillain88cb1752014-10-20 16:36:47 +01002514class HUnaryOperation : public HExpression<1> {
2515 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002516 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2517 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002518 SetRawInputAt(0, input);
2519 }
2520
2521 HInstruction* GetInput() const { return InputAt(0); }
2522 Primitive::Type GetResultType() const { return GetType(); }
2523
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002524 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002525 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002526 return true;
2527 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002528
Roland Levillain9240d6a2014-10-20 16:47:04 +01002529 // Try to statically evaluate `operation` and return a HConstant
2530 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002531 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002532 HConstant* TryStaticEvaluation() const;
2533
2534 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002535 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2536 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002537
Roland Levillain88cb1752014-10-20 16:36:47 +01002538 DECLARE_INSTRUCTION(UnaryOperation);
2539
2540 private:
2541 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2542};
2543
Dave Allison20dfc792014-06-16 20:44:29 -07002544class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002545 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002546 HBinaryOperation(Primitive::Type result_type,
2547 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002548 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002549 SideEffects side_effects = SideEffects::None(),
2550 uint32_t dex_pc = kNoDexPc)
2551 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002552 SetRawInputAt(0, left);
2553 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002554 }
2555
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002556 HInstruction* GetLeft() const { return InputAt(0); }
2557 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002558 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002559
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002560 virtual bool IsCommutative() const { return false; }
2561
2562 // Put constant on the right.
2563 // Returns whether order is changed.
2564 bool OrderInputsWithConstantOnTheRight() {
2565 HInstruction* left = InputAt(0);
2566 HInstruction* right = InputAt(1);
2567 if (left->IsConstant() && !right->IsConstant()) {
2568 ReplaceInput(right, 0);
2569 ReplaceInput(left, 1);
2570 return true;
2571 }
2572 return false;
2573 }
2574
2575 // Order inputs by instruction id, but favor constant on the right side.
2576 // This helps GVN for commutative ops.
2577 void OrderInputs() {
2578 DCHECK(IsCommutative());
2579 HInstruction* left = InputAt(0);
2580 HInstruction* right = InputAt(1);
2581 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2582 return;
2583 }
2584 if (OrderInputsWithConstantOnTheRight()) {
2585 return;
2586 }
2587 // Order according to instruction id.
2588 if (left->GetId() > right->GetId()) {
2589 ReplaceInput(right, 0);
2590 ReplaceInput(left, 1);
2591 }
2592 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002593
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002594 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002595 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002596 return true;
2597 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002598
Roland Levillain9240d6a2014-10-20 16:47:04 +01002599 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002600 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002601 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002602 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002603
2604 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002605 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2606 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2607 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2608 HLongConstant* y ATTRIBUTE_UNUSED) const {
2609 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2610 return nullptr;
2611 }
2612 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2613 HIntConstant* y ATTRIBUTE_UNUSED) const {
2614 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2615 return nullptr;
2616 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002617
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002618 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002619 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002620 HConstant* GetConstantRight() const;
2621
2622 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002623 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002624 HInstruction* GetLeastConstantLeft() const;
2625
Roland Levillainccc07a92014-09-16 14:48:16 +01002626 DECLARE_INSTRUCTION(BinaryOperation);
2627
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002628 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002629 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2630};
2631
Mark Mendellc4701932015-04-10 13:18:51 -04002632// The comparison bias applies for floating point operations and indicates how NaN
2633// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002634enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002635 kNoBias, // bias is not applicable (i.e. for long operation)
2636 kGtBias, // return 1 for NaN comparisons
2637 kLtBias, // return -1 for NaN comparisons
2638};
2639
Dave Allison20dfc792014-06-16 20:44:29 -07002640class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002641 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002642 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2643 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002644 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002645 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002646
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002647 bool NeedsMaterialization() const { return needs_materialization_; }
2648 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002649
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002650 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002651 // `instruction`, and disregard moves in between.
2652 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002653
Dave Allison20dfc792014-06-16 20:44:29 -07002654 DECLARE_INSTRUCTION(Condition);
2655
2656 virtual IfCondition GetCondition() const = 0;
2657
Mark Mendellc4701932015-04-10 13:18:51 -04002658 virtual IfCondition GetOppositeCondition() const = 0;
2659
Roland Levillain4fa13f62015-07-06 18:11:54 +01002660 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002661
2662 void SetBias(ComparisonBias bias) { bias_ = bias; }
2663
2664 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2665 return bias_ == other->AsCondition()->bias_;
2666 }
2667
Roland Levillain4fa13f62015-07-06 18:11:54 +01002668 bool IsFPConditionTrueIfNaN() const {
2669 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2670 IfCondition if_cond = GetCondition();
2671 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2672 }
2673
2674 bool IsFPConditionFalseIfNaN() const {
2675 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2676 IfCondition if_cond = GetCondition();
2677 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2678 }
2679
Dave Allison20dfc792014-06-16 20:44:29 -07002680 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002681 // For register allocation purposes, returns whether this instruction needs to be
2682 // materialized (that is, not just be in the processor flags).
2683 bool needs_materialization_;
2684
Mark Mendellc4701932015-04-10 13:18:51 -04002685 // Needed if we merge a HCompare into a HCondition.
2686 ComparisonBias bias_;
2687
Dave Allison20dfc792014-06-16 20:44:29 -07002688 DISALLOW_COPY_AND_ASSIGN(HCondition);
2689};
2690
2691// Instruction to check if two inputs are equal to each other.
2692class HEqual : public HCondition {
2693 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002694 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2695 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002696
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002697 bool IsCommutative() const OVERRIDE { return true; }
2698
Roland Levillain9867bc72015-08-05 10:21:34 +01002699 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002700 return GetBlock()->GetGraph()->GetIntConstant(
2701 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002702 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002703 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002704 return GetBlock()->GetGraph()->GetIntConstant(
2705 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002706 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002707
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002708 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002709
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002710 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002711 return kCondEQ;
2712 }
2713
Mark Mendellc4701932015-04-10 13:18:51 -04002714 IfCondition GetOppositeCondition() const OVERRIDE {
2715 return kCondNE;
2716 }
2717
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002718 private:
Aart Bike9f37602015-10-09 11:15:55 -07002719 template <typename T> bool Compute(T x, T y) const { return x == y; }
2720
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002721 DISALLOW_COPY_AND_ASSIGN(HEqual);
2722};
2723
Dave Allison20dfc792014-06-16 20:44:29 -07002724class HNotEqual : public HCondition {
2725 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002726 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2727 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002728
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002729 bool IsCommutative() const OVERRIDE { return true; }
2730
Roland Levillain9867bc72015-08-05 10:21:34 +01002731 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002732 return GetBlock()->GetGraph()->GetIntConstant(
2733 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002734 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002735 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002736 return GetBlock()->GetGraph()->GetIntConstant(
2737 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002738 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002739
Dave Allison20dfc792014-06-16 20:44:29 -07002740 DECLARE_INSTRUCTION(NotEqual);
2741
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002742 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002743 return kCondNE;
2744 }
2745
Mark Mendellc4701932015-04-10 13:18:51 -04002746 IfCondition GetOppositeCondition() const OVERRIDE {
2747 return kCondEQ;
2748 }
2749
Dave Allison20dfc792014-06-16 20:44:29 -07002750 private:
Aart Bike9f37602015-10-09 11:15:55 -07002751 template <typename T> bool Compute(T x, T y) const { return x != y; }
2752
Dave Allison20dfc792014-06-16 20:44:29 -07002753 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2754};
2755
2756class HLessThan : public HCondition {
2757 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002758 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2759 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002760
Roland Levillain9867bc72015-08-05 10:21:34 +01002761 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002762 return GetBlock()->GetGraph()->GetIntConstant(
2763 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002764 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002765 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002766 return GetBlock()->GetGraph()->GetIntConstant(
2767 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002768 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002769
Dave Allison20dfc792014-06-16 20:44:29 -07002770 DECLARE_INSTRUCTION(LessThan);
2771
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002772 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002773 return kCondLT;
2774 }
2775
Mark Mendellc4701932015-04-10 13:18:51 -04002776 IfCondition GetOppositeCondition() const OVERRIDE {
2777 return kCondGE;
2778 }
2779
Dave Allison20dfc792014-06-16 20:44:29 -07002780 private:
Aart Bike9f37602015-10-09 11:15:55 -07002781 template <typename T> bool Compute(T x, T y) const { return x < y; }
2782
Dave Allison20dfc792014-06-16 20:44:29 -07002783 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2784};
2785
2786class HLessThanOrEqual : public HCondition {
2787 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002788 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2789 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002790
Roland Levillain9867bc72015-08-05 10:21:34 +01002791 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002792 return GetBlock()->GetGraph()->GetIntConstant(
2793 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002794 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002795 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002796 return GetBlock()->GetGraph()->GetIntConstant(
2797 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002798 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002799
Dave Allison20dfc792014-06-16 20:44:29 -07002800 DECLARE_INSTRUCTION(LessThanOrEqual);
2801
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002802 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002803 return kCondLE;
2804 }
2805
Mark Mendellc4701932015-04-10 13:18:51 -04002806 IfCondition GetOppositeCondition() const OVERRIDE {
2807 return kCondGT;
2808 }
2809
Dave Allison20dfc792014-06-16 20:44:29 -07002810 private:
Aart Bike9f37602015-10-09 11:15:55 -07002811 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2812
Dave Allison20dfc792014-06-16 20:44:29 -07002813 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2814};
2815
2816class HGreaterThan : public HCondition {
2817 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002818 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2819 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002820
Roland Levillain9867bc72015-08-05 10:21:34 +01002821 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002822 return GetBlock()->GetGraph()->GetIntConstant(
2823 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002824 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002825 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002826 return GetBlock()->GetGraph()->GetIntConstant(
2827 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002828 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002829
Dave Allison20dfc792014-06-16 20:44:29 -07002830 DECLARE_INSTRUCTION(GreaterThan);
2831
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002832 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002833 return kCondGT;
2834 }
2835
Mark Mendellc4701932015-04-10 13:18:51 -04002836 IfCondition GetOppositeCondition() const OVERRIDE {
2837 return kCondLE;
2838 }
2839
Dave Allison20dfc792014-06-16 20:44:29 -07002840 private:
Aart Bike9f37602015-10-09 11:15:55 -07002841 template <typename T> bool Compute(T x, T y) const { return x > y; }
2842
Dave Allison20dfc792014-06-16 20:44:29 -07002843 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2844};
2845
2846class HGreaterThanOrEqual : public HCondition {
2847 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002848 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2849 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002850
Roland Levillain9867bc72015-08-05 10:21:34 +01002851 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002852 return GetBlock()->GetGraph()->GetIntConstant(
2853 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002854 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002855 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002856 return GetBlock()->GetGraph()->GetIntConstant(
2857 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002858 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002859
Dave Allison20dfc792014-06-16 20:44:29 -07002860 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2861
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002862 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002863 return kCondGE;
2864 }
2865
Mark Mendellc4701932015-04-10 13:18:51 -04002866 IfCondition GetOppositeCondition() const OVERRIDE {
2867 return kCondLT;
2868 }
2869
Dave Allison20dfc792014-06-16 20:44:29 -07002870 private:
Aart Bike9f37602015-10-09 11:15:55 -07002871 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2872
Dave Allison20dfc792014-06-16 20:44:29 -07002873 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2874};
2875
Aart Bike9f37602015-10-09 11:15:55 -07002876class HBelow : public HCondition {
2877 public:
2878 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2879 : HCondition(first, second, dex_pc) {}
2880
2881 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2882 return GetBlock()->GetGraph()->GetIntConstant(
2883 Compute(static_cast<uint32_t>(x->GetValue()),
2884 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2885 }
2886 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2887 return GetBlock()->GetGraph()->GetIntConstant(
2888 Compute(static_cast<uint64_t>(x->GetValue()),
2889 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2890 }
2891
2892 DECLARE_INSTRUCTION(Below);
2893
2894 IfCondition GetCondition() const OVERRIDE {
2895 return kCondB;
2896 }
2897
2898 IfCondition GetOppositeCondition() const OVERRIDE {
2899 return kCondAE;
2900 }
2901
2902 private:
2903 template <typename T> bool Compute(T x, T y) const { return x < y; }
2904
2905 DISALLOW_COPY_AND_ASSIGN(HBelow);
2906};
2907
2908class HBelowOrEqual : public HCondition {
2909 public:
2910 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2911 : HCondition(first, second, dex_pc) {}
2912
2913 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2914 return GetBlock()->GetGraph()->GetIntConstant(
2915 Compute(static_cast<uint32_t>(x->GetValue()),
2916 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2917 }
2918 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2919 return GetBlock()->GetGraph()->GetIntConstant(
2920 Compute(static_cast<uint64_t>(x->GetValue()),
2921 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2922 }
2923
2924 DECLARE_INSTRUCTION(BelowOrEqual);
2925
2926 IfCondition GetCondition() const OVERRIDE {
2927 return kCondBE;
2928 }
2929
2930 IfCondition GetOppositeCondition() const OVERRIDE {
2931 return kCondA;
2932 }
2933
2934 private:
2935 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2936
2937 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2938};
2939
2940class HAbove : public HCondition {
2941 public:
2942 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2943 : HCondition(first, second, dex_pc) {}
2944
2945 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2946 return GetBlock()->GetGraph()->GetIntConstant(
2947 Compute(static_cast<uint32_t>(x->GetValue()),
2948 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2949 }
2950 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2951 return GetBlock()->GetGraph()->GetIntConstant(
2952 Compute(static_cast<uint64_t>(x->GetValue()),
2953 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2954 }
2955
2956 DECLARE_INSTRUCTION(Above);
2957
2958 IfCondition GetCondition() const OVERRIDE {
2959 return kCondA;
2960 }
2961
2962 IfCondition GetOppositeCondition() const OVERRIDE {
2963 return kCondBE;
2964 }
2965
2966 private:
2967 template <typename T> bool Compute(T x, T y) const { return x > y; }
2968
2969 DISALLOW_COPY_AND_ASSIGN(HAbove);
2970};
2971
2972class HAboveOrEqual : public HCondition {
2973 public:
2974 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2975 : HCondition(first, second, dex_pc) {}
2976
2977 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2978 return GetBlock()->GetGraph()->GetIntConstant(
2979 Compute(static_cast<uint32_t>(x->GetValue()),
2980 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2981 }
2982 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2983 return GetBlock()->GetGraph()->GetIntConstant(
2984 Compute(static_cast<uint64_t>(x->GetValue()),
2985 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2986 }
2987
2988 DECLARE_INSTRUCTION(AboveOrEqual);
2989
2990 IfCondition GetCondition() const OVERRIDE {
2991 return kCondAE;
2992 }
2993
2994 IfCondition GetOppositeCondition() const OVERRIDE {
2995 return kCondB;
2996 }
2997
2998 private:
2999 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3000
3001 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3002};
Dave Allison20dfc792014-06-16 20:44:29 -07003003
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003004// Instruction to check how two inputs compare to each other.
3005// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3006class HCompare : public HBinaryOperation {
3007 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003008 HCompare(Primitive::Type type,
3009 HInstruction* first,
3010 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003011 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003012 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003013 : HBinaryOperation(Primitive::kPrimInt,
3014 first,
3015 second,
3016 SideEffectsForArchRuntimeCalls(type),
3017 dex_pc),
3018 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003019 DCHECK_EQ(type, first->GetType());
3020 DCHECK_EQ(type, second->GetType());
3021 }
3022
Roland Levillain9867bc72015-08-05 10:21:34 +01003023 template <typename T>
3024 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003025
Roland Levillain9867bc72015-08-05 10:21:34 +01003026 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003027 return GetBlock()->GetGraph()->GetIntConstant(
3028 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003029 }
3030 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003031 return GetBlock()->GetGraph()->GetIntConstant(
3032 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003033 }
3034
Calin Juravleddb7df22014-11-25 20:56:51 +00003035 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3036 return bias_ == other->AsCompare()->bias_;
3037 }
3038
Mark Mendellc4701932015-04-10 13:18:51 -04003039 ComparisonBias GetBias() const { return bias_; }
3040
Roland Levillain4fa13f62015-07-06 18:11:54 +01003041 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003042
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003043
3044 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3045 // MIPS64 uses a runtime call for FP comparisons.
3046 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3047 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003048
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003049 DECLARE_INSTRUCTION(Compare);
3050
3051 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003052 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003053
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003054 DISALLOW_COPY_AND_ASSIGN(HCompare);
3055};
3056
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003057// A local in the graph. Corresponds to a Dex register.
3058class HLocal : public HTemplateInstruction<0> {
3059 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003060 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003061 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003062
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003063 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003064
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003065 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003066
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003067 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003068 // The Dex register number.
3069 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003070
3071 DISALLOW_COPY_AND_ASSIGN(HLocal);
3072};
3073
3074// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003075class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003076 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003077 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3078 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003079 SetRawInputAt(0, local);
3080 }
3081
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003082 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3083
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003084 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003085
3086 private:
3087 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3088};
3089
3090// Store a value in a given local. This instruction has two inputs: the value
3091// and the local.
3092class HStoreLocal : public HTemplateInstruction<2> {
3093 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003094 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3095 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003096 SetRawInputAt(0, local);
3097 SetRawInputAt(1, value);
3098 }
3099
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003100 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3101
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003102 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003103
3104 private:
3105 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3106};
3107
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003108class HFloatConstant : public HConstant {
3109 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003110 float GetValue() const { return value_; }
3111
David Brazdil77a48ae2015-09-15 12:34:04 +00003112 uint64_t GetValueAsUint64() const OVERRIDE {
3113 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3114 }
3115
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003116 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003117 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003118 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003119 }
3120
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003121 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003122
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003123 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003124 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003125 }
3126 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003127 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003128 }
3129 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003130 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3131 }
3132 bool IsNaN() const {
3133 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003134 }
3135
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003136 DECLARE_INSTRUCTION(FloatConstant);
3137
3138 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003139 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3140 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3141 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3142 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003143
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003144 const float value_;
3145
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003146 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003147 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003148 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003149 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3150};
3151
3152class HDoubleConstant : public HConstant {
3153 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003154 double GetValue() const { return value_; }
3155
David Brazdil77a48ae2015-09-15 12:34:04 +00003156 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3157
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003158 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003159 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003160 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003161 }
3162
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003163 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003164
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003165 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003166 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003167 }
3168 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003169 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003170 }
3171 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003172 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3173 }
3174 bool IsNaN() const {
3175 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003176 }
3177
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003178 DECLARE_INSTRUCTION(DoubleConstant);
3179
3180 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003181 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3182 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3183 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3184 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003185
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003186 const double value_;
3187
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003188 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003189 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003190 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003191 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3192};
3193
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003194enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003195#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003196#include "intrinsics_list.h"
3197 kNone,
3198 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3199#undef INTRINSICS_LIST
3200#undef OPTIMIZING_INTRINSICS
3201};
3202std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3203
Agi Csaki05f20562015-08-19 14:58:14 -07003204enum IntrinsicNeedsEnvironmentOrCache {
3205 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3206 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003207};
3208
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003209class HInvoke : public HInstruction {
3210 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003211 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003212
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003213 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003214
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003215 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003216 SetRawInputAt(index, argument);
3217 }
3218
Roland Levillain3e3d7332015-04-28 11:00:54 +01003219 // Return the number of arguments. This number can be lower than
3220 // the number of inputs returned by InputCount(), as some invoke
3221 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3222 // inputs at the end of their list of inputs.
3223 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3224
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003225 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003226
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003227
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003228 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003229 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003230
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003231 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3232
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003233 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003234 return intrinsic_;
3235 }
3236
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003237 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003238
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003239 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003240 return GetEnvironment()->GetParent() != nullptr;
3241 }
3242
3243 bool CanThrow() const OVERRIDE { return true; }
3244
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003245 uint32_t* GetIntrinsicOptimizations() {
3246 return &intrinsic_optimizations_;
3247 }
3248
3249 const uint32_t* GetIntrinsicOptimizations() const {
3250 return &intrinsic_optimizations_;
3251 }
3252
3253 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3254
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003255 DECLARE_INSTRUCTION(Invoke);
3256
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003257 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003258 HInvoke(ArenaAllocator* arena,
3259 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003260 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003261 Primitive::Type return_type,
3262 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003263 uint32_t dex_method_index,
3264 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003265 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003266 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003267 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003268 inputs_(number_of_arguments + number_of_other_inputs,
3269 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003270 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003271 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003272 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003273 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003274 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003275 }
3276
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003277 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003278 return inputs_[index];
3279 }
3280
David Brazdil1abb4192015-02-17 18:33:36 +00003281 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003282 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003283 }
3284
Roland Levillain3e3d7332015-04-28 11:00:54 +01003285 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003286 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003287 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003288 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003289 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003290 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003291
3292 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3293 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003294
3295 private:
3296 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3297};
3298
Calin Juravle175dc732015-08-25 15:42:32 +01003299class HInvokeUnresolved : public HInvoke {
3300 public:
3301 HInvokeUnresolved(ArenaAllocator* arena,
3302 uint32_t number_of_arguments,
3303 Primitive::Type return_type,
3304 uint32_t dex_pc,
3305 uint32_t dex_method_index,
3306 InvokeType invoke_type)
3307 : HInvoke(arena,
3308 number_of_arguments,
3309 0u /* number_of_other_inputs */,
3310 return_type,
3311 dex_pc,
3312 dex_method_index,
3313 invoke_type) {
3314 }
3315
3316 DECLARE_INSTRUCTION(InvokeUnresolved);
3317
3318 private:
3319 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3320};
3321
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003322class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003323 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003324 // Requirements of this method call regarding the class
3325 // initialization (clinit) check of its declaring class.
3326 enum class ClinitCheckRequirement {
3327 kNone, // Class already initialized.
3328 kExplicit, // Static call having explicit clinit check as last input.
3329 kImplicit, // Static call implicitly requiring a clinit check.
3330 };
3331
Vladimir Marko58155012015-08-19 12:49:41 +00003332 // Determines how to load the target ArtMethod*.
3333 enum class MethodLoadKind {
3334 // Use a String init ArtMethod* loaded from Thread entrypoints.
3335 kStringInit,
3336
3337 // Use the method's own ArtMethod* loaded by the register allocator.
3338 kRecursive,
3339
3340 // Use ArtMethod* at a known address, embed the direct address in the code.
3341 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3342 kDirectAddress,
3343
3344 // Use ArtMethod* at an address that will be known at link time, embed the direct
3345 // address in the code. If the image is relocatable, emit .patch_oat entry.
3346 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3347 // the image relocatable or not.
3348 kDirectAddressWithFixup,
3349
3350 // Load from resoved methods array in the dex cache using a PC-relative load.
3351 // Used when we need to use the dex cache, for example for invoke-static that
3352 // may cause class initialization (the entry may point to a resolution method),
3353 // and we know that we can access the dex cache arrays using a PC-relative load.
3354 kDexCachePcRelative,
3355
3356 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3357 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3358 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3359 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3360 kDexCacheViaMethod,
3361 };
3362
3363 // Determines the location of the code pointer.
3364 enum class CodePtrLocation {
3365 // Recursive call, use local PC-relative call instruction.
3366 kCallSelf,
3367
3368 // Use PC-relative call instruction patched at link time.
3369 // Used for calls within an oat file, boot->boot or app->app.
3370 kCallPCRelative,
3371
3372 // Call to a known target address, embed the direct address in code.
3373 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3374 kCallDirect,
3375
3376 // Call to a target address that will be known at link time, embed the direct
3377 // address in code. If the image is relocatable, emit .patch_oat entry.
3378 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3379 // the image relocatable or not.
3380 kCallDirectWithFixup,
3381
3382 // Use code pointer from the ArtMethod*.
3383 // Used when we don't know the target code. This is also the last-resort-kind used when
3384 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3385 kCallArtMethod,
3386 };
3387
3388 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003389 MethodLoadKind method_load_kind;
3390 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003391 // The method load data holds
3392 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3393 // Note that there are multiple string init methods, each having its own offset.
3394 // - the method address for kDirectAddress
3395 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003396 uint64_t method_load_data;
3397 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003398 };
3399
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003400 HInvokeStaticOrDirect(ArenaAllocator* arena,
3401 uint32_t number_of_arguments,
3402 Primitive::Type return_type,
3403 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003404 uint32_t method_index,
3405 MethodReference target_method,
3406 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003407 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003408 InvokeType invoke_type,
3409 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003410 : HInvoke(arena,
3411 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003412 // There is potentially one extra argument for the HCurrentMethod node, and
3413 // potentially one other if the clinit check is explicit, and potentially
3414 // one other if the method is a string factory.
3415 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3416 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3417 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003418 return_type,
3419 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003420 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003421 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003422 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003423 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003424 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003425 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003426
Vladimir Markodc151b22015-10-15 18:02:30 +01003427 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003428 bool had_current_method_input = HasCurrentMethodInput();
3429 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3430
3431 // Using the current method is the default and once we find a better
3432 // method load kind, we should not go back to using the current method.
3433 DCHECK(had_current_method_input || !needs_current_method_input);
3434
3435 if (had_current_method_input && !needs_current_method_input) {
3436 DCHECK_EQ(InputAt(GetCurrentMethodInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3437 RemoveInputAt(GetCurrentMethodInputIndex());
3438 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003439 dispatch_info_ = dispatch_info;
3440 }
3441
Vladimir Markob554b5a2015-11-06 12:57:55 +00003442 void RemoveInputAt(size_t index);
3443
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003444 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003445 // We access the method via the dex cache so we can't do an implicit null check.
3446 // TODO: for intrinsics we can generate implicit null checks.
3447 return false;
3448 }
3449
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003450 bool CanBeNull() const OVERRIDE {
3451 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3452 }
3453
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003454 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003455 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3456 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3457 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003458 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003459 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003460 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003461 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003462 bool HasPcRelDexCache() const {
3463 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3464 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003465 bool HasCurrentMethodInput() const {
3466 // This function can be called only after the invoke has been fully initialized by the builder.
3467 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
3468 DCHECK(InputAt(GetCurrentMethodInputIndex())->IsCurrentMethod());
3469 return true;
3470 } else {
3471 DCHECK(InputCount() == GetCurrentMethodInputIndex() ||
3472 !InputAt(GetCurrentMethodInputIndex())->IsCurrentMethod());
3473 return false;
3474 }
3475 }
Vladimir Marko58155012015-08-19 12:49:41 +00003476 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3477 MethodReference GetTargetMethod() const { return target_method_; }
3478
3479 int32_t GetStringInitOffset() const {
3480 DCHECK(IsStringInit());
3481 return dispatch_info_.method_load_data;
3482 }
3483
3484 uint64_t GetMethodAddress() const {
3485 DCHECK(HasMethodAddress());
3486 return dispatch_info_.method_load_data;
3487 }
3488
3489 uint32_t GetDexCacheArrayOffset() const {
3490 DCHECK(HasPcRelDexCache());
3491 return dispatch_info_.method_load_data;
3492 }
3493
3494 uint64_t GetDirectCodePtr() const {
3495 DCHECK(HasDirectCodePtr());
3496 return dispatch_info_.direct_code_ptr;
3497 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003498
Calin Juravle68ad6492015-08-18 17:08:12 +01003499 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3500
Roland Levillain4c0eb422015-04-24 16:43:49 +01003501 // Is this instruction a call to a static method?
3502 bool IsStatic() const {
3503 return GetInvokeType() == kStatic;
3504 }
3505
Roland Levillain3e3d7332015-04-28 11:00:54 +01003506 // Remove the art::HLoadClass instruction set as last input by
3507 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3508 // the initial art::HClinitCheck instruction (only relevant for
3509 // static calls with explicit clinit check).
3510 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003511 DCHECK(IsStaticWithExplicitClinitCheck());
3512 size_t last_input_index = InputCount() - 1;
3513 HInstruction* last_input = InputAt(last_input_index);
3514 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003515 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003516 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003517 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003518 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3519 DCHECK(IsStaticWithImplicitClinitCheck());
3520 }
3521
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003522 bool IsStringFactoryFor(HFakeString* str) const {
3523 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003524 DCHECK(!HasCurrentMethodInput());
3525 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003526 return InputAt(InputCount() - 1)->AsFakeString() == str;
3527 }
3528
3529 void RemoveFakeStringArgumentAsLastInput() {
3530 DCHECK(IsStringInit());
3531 size_t last_input_index = InputCount() - 1;
3532 HInstruction* last_input = InputAt(last_input_index);
3533 DCHECK(last_input != nullptr);
3534 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3535 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003536 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003537 }
3538
Roland Levillain4c0eb422015-04-24 16:43:49 +01003539 // Is this a call to a static method whose declaring class has an
3540 // explicit intialization check in the graph?
3541 bool IsStaticWithExplicitClinitCheck() const {
3542 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3543 }
3544
3545 // Is this a call to a static method whose declaring class has an
3546 // implicit intialization check requirement?
3547 bool IsStaticWithImplicitClinitCheck() const {
3548 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3549 }
3550
Vladimir Markob554b5a2015-11-06 12:57:55 +00003551 // Does this method load kind need the current method as an input?
3552 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3553 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3554 }
3555
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003556 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003557
Roland Levillain4c0eb422015-04-24 16:43:49 +01003558 protected:
3559 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3560 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3561 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3562 HInstruction* input = input_record.GetInstruction();
3563 // `input` is the last input of a static invoke marked as having
3564 // an explicit clinit check. It must either be:
3565 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3566 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3567 DCHECK(input != nullptr);
3568 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3569 }
3570 return input_record;
3571 }
3572
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003573 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003574 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003575 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003576 // The target method may refer to different dex file or method index than the original
3577 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3578 // in derived class to increase visibility.
3579 MethodReference target_method_;
3580 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003581
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003582 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003583};
3584
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003585class HInvokeVirtual : public HInvoke {
3586 public:
3587 HInvokeVirtual(ArenaAllocator* arena,
3588 uint32_t number_of_arguments,
3589 Primitive::Type return_type,
3590 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003591 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003592 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003593 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003594 vtable_index_(vtable_index) {}
3595
Calin Juravle641547a2015-04-21 22:08:51 +01003596 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003597 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003598 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003599 }
3600
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003601 uint32_t GetVTableIndex() const { return vtable_index_; }
3602
3603 DECLARE_INSTRUCTION(InvokeVirtual);
3604
3605 private:
3606 const uint32_t vtable_index_;
3607
3608 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3609};
3610
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003611class HInvokeInterface : public HInvoke {
3612 public:
3613 HInvokeInterface(ArenaAllocator* arena,
3614 uint32_t number_of_arguments,
3615 Primitive::Type return_type,
3616 uint32_t dex_pc,
3617 uint32_t dex_method_index,
3618 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003619 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003620 imt_index_(imt_index) {}
3621
Calin Juravle641547a2015-04-21 22:08:51 +01003622 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003623 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003624 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003625 }
3626
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003627 uint32_t GetImtIndex() const { return imt_index_; }
3628 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3629
3630 DECLARE_INSTRUCTION(InvokeInterface);
3631
3632 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003633 const uint32_t imt_index_;
3634
3635 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3636};
3637
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003638class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003639 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003640 HNewInstance(HCurrentMethod* current_method,
3641 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003642 uint16_t type_index,
3643 const DexFile& dex_file,
3644 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003645 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003646 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003647 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003648 entrypoint_(entrypoint) {
3649 SetRawInputAt(0, current_method);
3650 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003651
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003652 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003653 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003654
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003655 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003656 bool NeedsEnvironment() const OVERRIDE { return true; }
Andreas Gampe55d02cf2015-10-29 02:59:50 +00003657 // It may throw when called on:
3658 // - interfaces
3659 // - abstract/innaccessible/unknown classes
3660 // TODO: optimize when possible.
3661 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003662
Calin Juravle10e244f2015-01-26 18:54:32 +00003663 bool CanBeNull() const OVERRIDE { return false; }
3664
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003665 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3666
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003667 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003668
3669 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003670 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003671 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003672 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003673
3674 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3675};
3676
Roland Levillain88cb1752014-10-20 16:36:47 +01003677class HNeg : public HUnaryOperation {
3678 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003679 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3680 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003681
Roland Levillain9867bc72015-08-05 10:21:34 +01003682 template <typename T> T Compute(T x) const { return -x; }
3683
3684 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003685 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003686 }
3687 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003688 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003689 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003690
Roland Levillain88cb1752014-10-20 16:36:47 +01003691 DECLARE_INSTRUCTION(Neg);
3692
3693 private:
3694 DISALLOW_COPY_AND_ASSIGN(HNeg);
3695};
3696
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003697class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003698 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003699 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003700 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003701 uint32_t dex_pc,
3702 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003703 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003704 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003705 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003706 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003707 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003708 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003709 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003710 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003711 }
3712
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003713 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003714 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003715
3716 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003717 bool NeedsEnvironment() const OVERRIDE { return true; }
3718
Mingyao Yang0c365e62015-03-31 15:09:29 -07003719 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3720 bool CanThrow() const OVERRIDE { return true; }
3721
Calin Juravle10e244f2015-01-26 18:54:32 +00003722 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003723
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003724 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3725
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003726 DECLARE_INSTRUCTION(NewArray);
3727
3728 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003729 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003730 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003731 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003732
3733 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3734};
3735
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003736class HAdd : public HBinaryOperation {
3737 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003738 HAdd(Primitive::Type result_type,
3739 HInstruction* left,
3740 HInstruction* right,
3741 uint32_t dex_pc = kNoDexPc)
3742 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003743
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003744 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003745
Roland Levillain9867bc72015-08-05 10:21:34 +01003746 template <typename T> T Compute(T x, T y) const { return x + y; }
3747
3748 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003749 return GetBlock()->GetGraph()->GetIntConstant(
3750 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003751 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003752 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003753 return GetBlock()->GetGraph()->GetLongConstant(
3754 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003755 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003756
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003757 DECLARE_INSTRUCTION(Add);
3758
3759 private:
3760 DISALLOW_COPY_AND_ASSIGN(HAdd);
3761};
3762
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003763class HSub : public HBinaryOperation {
3764 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003765 HSub(Primitive::Type result_type,
3766 HInstruction* left,
3767 HInstruction* right,
3768 uint32_t dex_pc = kNoDexPc)
3769 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003770
Roland Levillain9867bc72015-08-05 10:21:34 +01003771 template <typename T> T Compute(T x, T y) const { return x - y; }
3772
3773 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003774 return GetBlock()->GetGraph()->GetIntConstant(
3775 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003776 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003777 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003778 return GetBlock()->GetGraph()->GetLongConstant(
3779 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003780 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003781
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003782 DECLARE_INSTRUCTION(Sub);
3783
3784 private:
3785 DISALLOW_COPY_AND_ASSIGN(HSub);
3786};
3787
Calin Juravle34bacdf2014-10-07 20:23:36 +01003788class HMul : public HBinaryOperation {
3789 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003790 HMul(Primitive::Type result_type,
3791 HInstruction* left,
3792 HInstruction* right,
3793 uint32_t dex_pc = kNoDexPc)
3794 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003795
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003796 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003797
Roland Levillain9867bc72015-08-05 10:21:34 +01003798 template <typename T> T Compute(T x, T y) const { return x * y; }
3799
3800 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003801 return GetBlock()->GetGraph()->GetIntConstant(
3802 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003803 }
3804 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003805 return GetBlock()->GetGraph()->GetLongConstant(
3806 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003807 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003808
3809 DECLARE_INSTRUCTION(Mul);
3810
3811 private:
3812 DISALLOW_COPY_AND_ASSIGN(HMul);
3813};
3814
Calin Juravle7c4954d2014-10-28 16:57:40 +00003815class HDiv : public HBinaryOperation {
3816 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003817 HDiv(Primitive::Type result_type,
3818 HInstruction* left,
3819 HInstruction* right,
3820 uint32_t dex_pc)
3821 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003822
Roland Levillain9867bc72015-08-05 10:21:34 +01003823 template <typename T>
3824 T Compute(T x, T y) const {
3825 // Our graph structure ensures we never have 0 for `y` during
3826 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003827 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003828 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003829 return (y == -1) ? -x : x / y;
3830 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003831
Roland Levillain9867bc72015-08-05 10:21:34 +01003832 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003833 return GetBlock()->GetGraph()->GetIntConstant(
3834 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003835 }
3836 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003837 return GetBlock()->GetGraph()->GetLongConstant(
3838 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003839 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003840
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003841 static SideEffects SideEffectsForArchRuntimeCalls() {
3842 // The generated code can use a runtime call.
3843 return SideEffects::CanTriggerGC();
3844 }
3845
Calin Juravle7c4954d2014-10-28 16:57:40 +00003846 DECLARE_INSTRUCTION(Div);
3847
3848 private:
3849 DISALLOW_COPY_AND_ASSIGN(HDiv);
3850};
3851
Calin Juravlebacfec32014-11-14 15:54:36 +00003852class HRem : public HBinaryOperation {
3853 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003854 HRem(Primitive::Type result_type,
3855 HInstruction* left,
3856 HInstruction* right,
3857 uint32_t dex_pc)
3858 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003859
Roland Levillain9867bc72015-08-05 10:21:34 +01003860 template <typename T>
3861 T Compute(T x, T y) const {
3862 // Our graph structure ensures we never have 0 for `y` during
3863 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003864 DCHECK_NE(y, 0);
3865 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3866 return (y == -1) ? 0 : x % y;
3867 }
3868
Roland Levillain9867bc72015-08-05 10:21:34 +01003869 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003870 return GetBlock()->GetGraph()->GetIntConstant(
3871 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003872 }
3873 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003874 return GetBlock()->GetGraph()->GetLongConstant(
3875 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003876 }
3877
Calin Juravlebacfec32014-11-14 15:54:36 +00003878
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003879 static SideEffects SideEffectsForArchRuntimeCalls() {
3880 return SideEffects::CanTriggerGC();
3881 }
3882
Calin Juravlebacfec32014-11-14 15:54:36 +00003883 DECLARE_INSTRUCTION(Rem);
3884
3885 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003886 DISALLOW_COPY_AND_ASSIGN(HRem);
3887};
3888
Calin Juravled0d48522014-11-04 16:40:20 +00003889class HDivZeroCheck : public HExpression<1> {
3890 public:
3891 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003892 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003893 SetRawInputAt(0, value);
3894 }
3895
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003896 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3897
Calin Juravled0d48522014-11-04 16:40:20 +00003898 bool CanBeMoved() const OVERRIDE { return true; }
3899
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003900 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003901 return true;
3902 }
3903
3904 bool NeedsEnvironment() const OVERRIDE { return true; }
3905 bool CanThrow() const OVERRIDE { return true; }
3906
Calin Juravled0d48522014-11-04 16:40:20 +00003907 DECLARE_INSTRUCTION(DivZeroCheck);
3908
3909 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003910 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3911};
3912
Calin Juravle9aec02f2014-11-18 23:06:35 +00003913class HShl : public HBinaryOperation {
3914 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003915 HShl(Primitive::Type result_type,
3916 HInstruction* left,
3917 HInstruction* right,
3918 uint32_t dex_pc = kNoDexPc)
3919 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003920
Roland Levillain9867bc72015-08-05 10:21:34 +01003921 template <typename T, typename U, typename V>
3922 T Compute(T x, U y, V max_shift_value) const {
3923 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3924 "V is not the unsigned integer type corresponding to T");
3925 return x << (y & max_shift_value);
3926 }
3927
3928 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3929 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003930 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003931 }
3932 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3933 // case is handled as `x << static_cast<int>(y)`.
3934 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3935 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003936 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003937 }
3938 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3939 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003940 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003941 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003942
3943 DECLARE_INSTRUCTION(Shl);
3944
3945 private:
3946 DISALLOW_COPY_AND_ASSIGN(HShl);
3947};
3948
3949class HShr : public HBinaryOperation {
3950 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003951 HShr(Primitive::Type result_type,
3952 HInstruction* left,
3953 HInstruction* right,
3954 uint32_t dex_pc = kNoDexPc)
3955 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003956
Roland Levillain9867bc72015-08-05 10:21:34 +01003957 template <typename T, typename U, typename V>
3958 T Compute(T x, U y, V max_shift_value) const {
3959 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3960 "V is not the unsigned integer type corresponding to T");
3961 return x >> (y & max_shift_value);
3962 }
3963
3964 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3965 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003966 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003967 }
3968 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3969 // case is handled as `x >> static_cast<int>(y)`.
3970 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3971 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003972 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003973 }
3974 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3975 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003976 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003977 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003978
3979 DECLARE_INSTRUCTION(Shr);
3980
3981 private:
3982 DISALLOW_COPY_AND_ASSIGN(HShr);
3983};
3984
3985class HUShr : public HBinaryOperation {
3986 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003987 HUShr(Primitive::Type result_type,
3988 HInstruction* left,
3989 HInstruction* right,
3990 uint32_t dex_pc = kNoDexPc)
3991 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003992
Roland Levillain9867bc72015-08-05 10:21:34 +01003993 template <typename T, typename U, typename V>
3994 T Compute(T x, U y, V max_shift_value) const {
3995 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3996 "V is not the unsigned integer type corresponding to T");
3997 V ux = static_cast<V>(x);
3998 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003999 }
4000
Roland Levillain9867bc72015-08-05 10:21:34 +01004001 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4002 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004003 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004004 }
4005 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4006 // case is handled as `x >>> static_cast<int>(y)`.
4007 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4008 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004009 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004010 }
4011 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4012 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004013 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004014 }
4015
4016 DECLARE_INSTRUCTION(UShr);
4017
4018 private:
4019 DISALLOW_COPY_AND_ASSIGN(HUShr);
4020};
4021
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004022class HAnd : public HBinaryOperation {
4023 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004024 HAnd(Primitive::Type result_type,
4025 HInstruction* left,
4026 HInstruction* right,
4027 uint32_t dex_pc = kNoDexPc)
4028 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004029
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004030 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004031
Roland Levillain9867bc72015-08-05 10:21:34 +01004032 template <typename T, typename U>
4033 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4034
4035 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004036 return GetBlock()->GetGraph()->GetIntConstant(
4037 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004038 }
4039 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004040 return GetBlock()->GetGraph()->GetLongConstant(
4041 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004042 }
4043 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004044 return GetBlock()->GetGraph()->GetLongConstant(
4045 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004046 }
4047 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004048 return GetBlock()->GetGraph()->GetLongConstant(
4049 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004050 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004051
4052 DECLARE_INSTRUCTION(And);
4053
4054 private:
4055 DISALLOW_COPY_AND_ASSIGN(HAnd);
4056};
4057
4058class HOr : public HBinaryOperation {
4059 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004060 HOr(Primitive::Type result_type,
4061 HInstruction* left,
4062 HInstruction* right,
4063 uint32_t dex_pc = kNoDexPc)
4064 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004065
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004066 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004067
Roland Levillain9867bc72015-08-05 10:21:34 +01004068 template <typename T, typename U>
4069 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4070
4071 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004072 return GetBlock()->GetGraph()->GetIntConstant(
4073 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004074 }
4075 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004076 return GetBlock()->GetGraph()->GetLongConstant(
4077 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004078 }
4079 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004080 return GetBlock()->GetGraph()->GetLongConstant(
4081 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004082 }
4083 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004084 return GetBlock()->GetGraph()->GetLongConstant(
4085 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004086 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004087
4088 DECLARE_INSTRUCTION(Or);
4089
4090 private:
4091 DISALLOW_COPY_AND_ASSIGN(HOr);
4092};
4093
4094class HXor : public HBinaryOperation {
4095 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004096 HXor(Primitive::Type result_type,
4097 HInstruction* left,
4098 HInstruction* right,
4099 uint32_t dex_pc = kNoDexPc)
4100 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004101
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004102 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004103
Roland Levillain9867bc72015-08-05 10:21:34 +01004104 template <typename T, typename U>
4105 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4106
4107 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004108 return GetBlock()->GetGraph()->GetIntConstant(
4109 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004110 }
4111 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004112 return GetBlock()->GetGraph()->GetLongConstant(
4113 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004114 }
4115 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004116 return GetBlock()->GetGraph()->GetLongConstant(
4117 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004118 }
4119 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004120 return GetBlock()->GetGraph()->GetLongConstant(
4121 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004122 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004123
4124 DECLARE_INSTRUCTION(Xor);
4125
4126 private:
4127 DISALLOW_COPY_AND_ASSIGN(HXor);
4128};
4129
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004130// The value of a parameter in this method. Its location depends on
4131// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004132class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004133 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004134 HParameterValue(const DexFile& dex_file,
4135 uint16_t type_index,
4136 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004137 Primitive::Type parameter_type,
4138 bool is_this = false)
4139 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004140 dex_file_(dex_file),
4141 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004142 index_(index),
4143 is_this_(is_this),
4144 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004145
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004146 const DexFile& GetDexFile() const { return dex_file_; }
4147 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004148 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004149 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004150
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004151 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4152 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004153
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004154 DECLARE_INSTRUCTION(ParameterValue);
4155
4156 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004157 const DexFile& dex_file_;
4158 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004159 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004160 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004161 const uint8_t index_;
4162
Calin Juravle10e244f2015-01-26 18:54:32 +00004163 // Whether or not the parameter value corresponds to 'this' argument.
4164 const bool is_this_;
4165
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004166 bool can_be_null_;
4167
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004168 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4169};
4170
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004171class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004172 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004173 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4174 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004175
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004176 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004177 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004178 return true;
4179 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004180
Roland Levillain9867bc72015-08-05 10:21:34 +01004181 template <typename T> T Compute(T x) const { return ~x; }
4182
4183 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004184 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004185 }
4186 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004187 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004188 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004189
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004190 DECLARE_INSTRUCTION(Not);
4191
4192 private:
4193 DISALLOW_COPY_AND_ASSIGN(HNot);
4194};
4195
David Brazdil66d126e2015-04-03 16:02:44 +01004196class HBooleanNot : public HUnaryOperation {
4197 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004198 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4199 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004200
4201 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004202 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004203 return true;
4204 }
4205
Roland Levillain9867bc72015-08-05 10:21:34 +01004206 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004207 DCHECK(IsUint<1>(x));
4208 return !x;
4209 }
4210
Roland Levillain9867bc72015-08-05 10:21:34 +01004211 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004212 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004213 }
4214 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4215 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004216 UNREACHABLE();
4217 }
4218
4219 DECLARE_INSTRUCTION(BooleanNot);
4220
4221 private:
4222 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4223};
4224
Roland Levillaindff1f282014-11-05 14:15:05 +00004225class HTypeConversion : public HExpression<1> {
4226 public:
4227 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004228 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004229 : HExpression(result_type,
4230 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4231 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004232 SetRawInputAt(0, input);
4233 DCHECK_NE(input->GetType(), result_type);
4234 }
4235
4236 HInstruction* GetInput() const { return InputAt(0); }
4237 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4238 Primitive::Type GetResultType() const { return GetType(); }
4239
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004240 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004241 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004242
Roland Levillaindff1f282014-11-05 14:15:05 +00004243 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004244 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004245
Mark Mendelle82549b2015-05-06 10:55:34 -04004246 // Try to statically evaluate the conversion and return a HConstant
4247 // containing the result. If the input cannot be converted, return nullptr.
4248 HConstant* TryStaticEvaluation() const;
4249
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004250 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4251 Primitive::Type result_type) {
4252 // Some architectures may not require the 'GC' side effects, but at this point
4253 // in the compilation process we do not know what architecture we will
4254 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004255 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4256 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004257 return SideEffects::CanTriggerGC();
4258 }
4259 return SideEffects::None();
4260 }
4261
Roland Levillaindff1f282014-11-05 14:15:05 +00004262 DECLARE_INSTRUCTION(TypeConversion);
4263
4264 private:
4265 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4266};
4267
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004268static constexpr uint32_t kNoRegNumber = -1;
4269
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004270class HPhi : public HInstruction {
4271 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004272 HPhi(ArenaAllocator* arena,
4273 uint32_t reg_number,
4274 size_t number_of_inputs,
4275 Primitive::Type type,
4276 uint32_t dex_pc = kNoDexPc)
4277 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004278 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004279 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004280 type_(type),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004281 is_live_(false),
Calin Juravle10e244f2015-01-26 18:54:32 +00004282 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004283 }
4284
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004285 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4286 static Primitive::Type ToPhiType(Primitive::Type type) {
4287 switch (type) {
4288 case Primitive::kPrimBoolean:
4289 case Primitive::kPrimByte:
4290 case Primitive::kPrimShort:
4291 case Primitive::kPrimChar:
4292 return Primitive::kPrimInt;
4293 default:
4294 return type;
4295 }
4296 }
4297
David Brazdilffee3d32015-07-06 11:48:53 +01004298 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4299
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004300 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004301
4302 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004303 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004304
Calin Juravle10e244f2015-01-26 18:54:32 +00004305 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004306 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004307
Calin Juravle10e244f2015-01-26 18:54:32 +00004308 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4309 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4310
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004311 uint32_t GetRegNumber() const { return reg_number_; }
4312
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004313 void SetDead() { is_live_ = false; }
4314 void SetLive() { is_live_ = true; }
4315 bool IsDead() const { return !is_live_; }
4316 bool IsLive() const { return is_live_; }
4317
David Brazdil77a48ae2015-09-15 12:34:04 +00004318 bool IsVRegEquivalentOf(HInstruction* other) const {
4319 return other != nullptr
4320 && other->IsPhi()
4321 && other->AsPhi()->GetBlock() == GetBlock()
4322 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4323 }
4324
Calin Juravlea4f88312015-04-16 12:57:19 +01004325 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4326 // An equivalent phi is a phi having the same dex register and type.
4327 // It assumes that phis with the same dex register are adjacent.
4328 HPhi* GetNextEquivalentPhiWithSameType() {
4329 HInstruction* next = GetNext();
4330 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4331 if (next->GetType() == GetType()) {
4332 return next->AsPhi();
4333 }
4334 next = next->GetNext();
4335 }
4336 return nullptr;
4337 }
4338
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004339 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004340
David Brazdil1abb4192015-02-17 18:33:36 +00004341 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004342 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004343 return inputs_[index];
4344 }
David Brazdil1abb4192015-02-17 18:33:36 +00004345
4346 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004347 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004348 }
4349
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004350 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004351 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004352 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004353 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004354 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004355 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004356
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004357 DISALLOW_COPY_AND_ASSIGN(HPhi);
4358};
4359
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004360class HNullCheck : public HExpression<1> {
4361 public:
4362 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004363 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004364 SetRawInputAt(0, value);
4365 }
4366
Calin Juravle10e244f2015-01-26 18:54:32 +00004367 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004368 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004369 return true;
4370 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004371
Calin Juravle10e244f2015-01-26 18:54:32 +00004372 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004373
Calin Juravle10e244f2015-01-26 18:54:32 +00004374 bool CanThrow() const OVERRIDE { return true; }
4375
4376 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004377
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004378
4379 DECLARE_INSTRUCTION(NullCheck);
4380
4381 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004382 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4383};
4384
4385class FieldInfo : public ValueObject {
4386 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004387 FieldInfo(MemberOffset field_offset,
4388 Primitive::Type field_type,
4389 bool is_volatile,
4390 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004391 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004392 const DexFile& dex_file,
4393 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004394 : field_offset_(field_offset),
4395 field_type_(field_type),
4396 is_volatile_(is_volatile),
4397 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004398 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004399 dex_file_(dex_file),
4400 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004401
4402 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004403 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004404 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004405 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004406 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004407 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004408 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004409
4410 private:
4411 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004412 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004413 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004414 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004415 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004416 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004417 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004418};
4419
4420class HInstanceFieldGet : public HExpression<1> {
4421 public:
4422 HInstanceFieldGet(HInstruction* value,
4423 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004424 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004425 bool is_volatile,
4426 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004427 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004428 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004429 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004430 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004431 : HExpression(field_type,
4432 SideEffects::FieldReadOfType(field_type, is_volatile),
4433 dex_pc),
4434 field_info_(field_offset,
4435 field_type,
4436 is_volatile,
4437 field_idx,
4438 declaring_class_def_index,
4439 dex_file,
4440 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004441 SetRawInputAt(0, value);
4442 }
4443
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004444 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004445
4446 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4447 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4448 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004449 }
4450
Calin Juravle641547a2015-04-21 22:08:51 +01004451 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4452 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004453 }
4454
4455 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004456 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4457 }
4458
Calin Juravle52c48962014-12-16 17:02:57 +00004459 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004460 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004461 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004462 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004463
4464 DECLARE_INSTRUCTION(InstanceFieldGet);
4465
4466 private:
4467 const FieldInfo field_info_;
4468
4469 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4470};
4471
4472class HInstanceFieldSet : public HTemplateInstruction<2> {
4473 public:
4474 HInstanceFieldSet(HInstruction* object,
4475 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004476 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004477 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004478 bool is_volatile,
4479 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004480 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004481 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004482 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004483 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004484 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4485 dex_pc),
4486 field_info_(field_offset,
4487 field_type,
4488 is_volatile,
4489 field_idx,
4490 declaring_class_def_index,
4491 dex_file,
4492 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004493 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004494 SetRawInputAt(0, object);
4495 SetRawInputAt(1, value);
4496 }
4497
Calin Juravle641547a2015-04-21 22:08:51 +01004498 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4499 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004500 }
4501
Calin Juravle52c48962014-12-16 17:02:57 +00004502 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004503 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004504 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004505 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004506 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004507 bool GetValueCanBeNull() const { return value_can_be_null_; }
4508 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004509
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004510 DECLARE_INSTRUCTION(InstanceFieldSet);
4511
4512 private:
4513 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004514 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004515
4516 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4517};
4518
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004519class HArrayGet : public HExpression<2> {
4520 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004521 HArrayGet(HInstruction* array,
4522 HInstruction* index,
4523 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004524 uint32_t dex_pc,
4525 SideEffects additional_side_effects = SideEffects::None())
4526 : HExpression(type,
4527 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004528 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004529 SetRawInputAt(0, array);
4530 SetRawInputAt(1, index);
4531 }
4532
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004533 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004534 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004535 return true;
4536 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004537 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004538 // TODO: We can be smarter here.
4539 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4540 // which generates the implicit null check. There are cases when these can be removed
4541 // to produce better code. If we ever add optimizations to do so we should allow an
4542 // implicit check here (as long as the address falls in the first page).
4543 return false;
4544 }
4545
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004546 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004547
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004548 HInstruction* GetArray() const { return InputAt(0); }
4549 HInstruction* GetIndex() const { return InputAt(1); }
4550
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004551 DECLARE_INSTRUCTION(ArrayGet);
4552
4553 private:
4554 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4555};
4556
4557class HArraySet : public HTemplateInstruction<3> {
4558 public:
4559 HArraySet(HInstruction* array,
4560 HInstruction* index,
4561 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004562 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004563 uint32_t dex_pc,
4564 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004565 : HTemplateInstruction(
4566 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004567 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4568 additional_side_effects),
4569 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004570 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004571 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004572 value_can_be_null_(true),
4573 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004574 SetRawInputAt(0, array);
4575 SetRawInputAt(1, index);
4576 SetRawInputAt(2, value);
4577 }
4578
Calin Juravle77520bc2015-01-12 18:45:46 +00004579 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004580 // We currently always call a runtime method to catch array store
4581 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004582 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004583 }
4584
Mingyao Yang81014cb2015-06-02 03:16:27 -07004585 // Can throw ArrayStoreException.
4586 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4587
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004588 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004589 // TODO: Same as for ArrayGet.
4590 return false;
4591 }
4592
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004593 void ClearNeedsTypeCheck() {
4594 needs_type_check_ = false;
4595 }
4596
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004597 void ClearValueCanBeNull() {
4598 value_can_be_null_ = false;
4599 }
4600
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004601 void SetStaticTypeOfArrayIsObjectArray() {
4602 static_type_of_array_is_object_array_ = true;
4603 }
4604
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004605 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004606 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004607 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004608
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004609 HInstruction* GetArray() const { return InputAt(0); }
4610 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004611 HInstruction* GetValue() const { return InputAt(2); }
4612
4613 Primitive::Type GetComponentType() const {
4614 // The Dex format does not type floating point index operations. Since the
4615 // `expected_component_type_` is set during building and can therefore not
4616 // be correct, we also check what is the value type. If it is a floating
4617 // point type, we must use that type.
4618 Primitive::Type value_type = GetValue()->GetType();
4619 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4620 ? value_type
4621 : expected_component_type_;
4622 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004623
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004624 Primitive::Type GetRawExpectedComponentType() const {
4625 return expected_component_type_;
4626 }
4627
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004628 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4629 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4630 }
4631
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004632 DECLARE_INSTRUCTION(ArraySet);
4633
4634 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004635 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004636 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004637 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004638 // Cached information for the reference_type_info_ so that codegen
4639 // does not need to inspect the static type.
4640 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004641
4642 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4643};
4644
4645class HArrayLength : public HExpression<1> {
4646 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004647 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004648 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004649 // Note that arrays do not change length, so the instruction does not
4650 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004651 SetRawInputAt(0, array);
4652 }
4653
Calin Juravle77520bc2015-01-12 18:45:46 +00004654 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004655 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004656 return true;
4657 }
Calin Juravle641547a2015-04-21 22:08:51 +01004658 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4659 return obj == InputAt(0);
4660 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004661
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004662 DECLARE_INSTRUCTION(ArrayLength);
4663
4664 private:
4665 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4666};
4667
4668class HBoundsCheck : public HExpression<2> {
4669 public:
4670 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004671 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004672 DCHECK(index->GetType() == Primitive::kPrimInt);
4673 SetRawInputAt(0, index);
4674 SetRawInputAt(1, length);
4675 }
4676
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004677 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004678 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004679 return true;
4680 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004681
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004682 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004683
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004684 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004685
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004686 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004687
4688 DECLARE_INSTRUCTION(BoundsCheck);
4689
4690 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004691 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4692};
4693
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004694/**
4695 * Some DEX instructions are folded into multiple HInstructions that need
4696 * to stay live until the last HInstruction. This class
4697 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004698 * HInstruction stays live. `index` represents the stack location index of the
4699 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004700 */
4701class HTemporary : public HTemplateInstruction<0> {
4702 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004703 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4704 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004705
4706 size_t GetIndex() const { return index_; }
4707
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004708 Primitive::Type GetType() const OVERRIDE {
4709 // The previous instruction is the one that will be stored in the temporary location.
4710 DCHECK(GetPrevious() != nullptr);
4711 return GetPrevious()->GetType();
4712 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004713
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004714 DECLARE_INSTRUCTION(Temporary);
4715
4716 private:
4717 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004718 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4719};
4720
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004721class HSuspendCheck : public HTemplateInstruction<0> {
4722 public:
4723 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004724 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004725
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004726 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004727 return true;
4728 }
4729
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004730 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4731 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004732
4733 DECLARE_INSTRUCTION(SuspendCheck);
4734
4735 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004736 // Only used for code generation, in order to share the same slow path between back edges
4737 // of a same loop.
4738 SlowPathCode* slow_path_;
4739
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004740 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4741};
4742
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004743/**
4744 * Instruction to load a Class object.
4745 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004746class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004747 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004748 HLoadClass(HCurrentMethod* current_method,
4749 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004750 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004751 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004752 uint32_t dex_pc,
4753 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004754 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004755 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004756 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004757 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004758 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004759 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004760 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004761 // Referrers class should not need access check. We never inline unverified
4762 // methods so we can't possibly end up in this situation.
4763 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004764 SetRawInputAt(0, current_method);
4765 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004766
4767 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004768
4769 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004770 // Note that we don't need to test for generate_clinit_check_.
4771 // Whether or not we need to generate the clinit check is processed in
4772 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004773 return other->AsLoadClass()->type_index_ == type_index_ &&
4774 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004775 }
4776
4777 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4778
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004779 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004780 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004781 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004782
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004783 bool NeedsEnvironment() const OVERRIDE {
4784 // Will call runtime and load the class if the class is not loaded yet.
4785 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004786 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004787 }
4788
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004789 bool MustGenerateClinitCheck() const {
4790 return generate_clinit_check_;
4791 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004792 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004793 // The entrypoint the code generator is going to call does not do
4794 // clinit of the class.
4795 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004796 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004797 }
4798
4799 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004800 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4801 }
4802
4803 bool NeedsAccessCheck() const {
4804 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004805 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004806
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004807 bool CanThrow() const OVERRIDE {
4808 // May call runtime and and therefore can throw.
4809 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004810 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004811 }
4812
Calin Juravleacf735c2015-02-12 15:25:22 +00004813 ReferenceTypeInfo GetLoadedClassRTI() {
4814 return loaded_class_rti_;
4815 }
4816
4817 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4818 // Make sure we only set exact types (the loaded class should never be merged).
4819 DCHECK(rti.IsExact());
4820 loaded_class_rti_ = rti;
4821 }
4822
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004823 const DexFile& GetDexFile() { return dex_file_; }
4824
Vladimir Markodc151b22015-10-15 18:02:30 +01004825 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004826
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004827 static SideEffects SideEffectsForArchRuntimeCalls() {
4828 return SideEffects::CanTriggerGC();
4829 }
4830
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004831 DECLARE_INSTRUCTION(LoadClass);
4832
4833 private:
4834 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004835 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004836 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004837 // Whether this instruction must generate the initialization check.
4838 // Used for code generation.
4839 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004840 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004841
Calin Juravleacf735c2015-02-12 15:25:22 +00004842 ReferenceTypeInfo loaded_class_rti_;
4843
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004844 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4845};
4846
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004847class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004848 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004849 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004850 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4851 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004852 SetRawInputAt(0, current_method);
4853 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004854
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004855 bool CanBeMoved() const OVERRIDE { return true; }
4856
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004857 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4858 return other->AsLoadString()->string_index_ == string_index_;
4859 }
4860
4861 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4862
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004863 uint32_t GetStringIndex() const { return string_index_; }
4864
4865 // TODO: Can we deopt or debug when we resolve a string?
4866 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004867 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004868 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004869
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004870 static SideEffects SideEffectsForArchRuntimeCalls() {
4871 return SideEffects::CanTriggerGC();
4872 }
4873
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004874 DECLARE_INSTRUCTION(LoadString);
4875
4876 private:
4877 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004878
4879 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4880};
4881
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004882/**
4883 * Performs an initialization check on its Class object input.
4884 */
4885class HClinitCheck : public HExpression<1> {
4886 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004887 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004888 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004889 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004890 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4891 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004892 SetRawInputAt(0, constant);
4893 }
4894
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004895 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004896 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004897 return true;
4898 }
4899
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004900 bool NeedsEnvironment() const OVERRIDE {
4901 // May call runtime to initialize the class.
4902 return true;
4903 }
4904
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004905
4906 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4907
4908 DECLARE_INSTRUCTION(ClinitCheck);
4909
4910 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004911 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4912};
4913
4914class HStaticFieldGet : public HExpression<1> {
4915 public:
4916 HStaticFieldGet(HInstruction* cls,
4917 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004918 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004919 bool is_volatile,
4920 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004921 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004922 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004923 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004924 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004925 : HExpression(field_type,
4926 SideEffects::FieldReadOfType(field_type, is_volatile),
4927 dex_pc),
4928 field_info_(field_offset,
4929 field_type,
4930 is_volatile,
4931 field_idx,
4932 declaring_class_def_index,
4933 dex_file,
4934 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004935 SetRawInputAt(0, cls);
4936 }
4937
Calin Juravle52c48962014-12-16 17:02:57 +00004938
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004939 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004940
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004941 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004942 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4943 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004944 }
4945
4946 size_t ComputeHashCode() const OVERRIDE {
4947 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4948 }
4949
Calin Juravle52c48962014-12-16 17:02:57 +00004950 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004951 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4952 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004953 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004954
4955 DECLARE_INSTRUCTION(StaticFieldGet);
4956
4957 private:
4958 const FieldInfo field_info_;
4959
4960 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4961};
4962
4963class HStaticFieldSet : public HTemplateInstruction<2> {
4964 public:
4965 HStaticFieldSet(HInstruction* cls,
4966 HInstruction* value,
4967 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004968 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004969 bool is_volatile,
4970 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004971 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004972 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004973 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004974 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004975 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4976 dex_pc),
4977 field_info_(field_offset,
4978 field_type,
4979 is_volatile,
4980 field_idx,
4981 declaring_class_def_index,
4982 dex_file,
4983 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004984 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004985 SetRawInputAt(0, cls);
4986 SetRawInputAt(1, value);
4987 }
4988
Calin Juravle52c48962014-12-16 17:02:57 +00004989 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004990 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4991 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004992 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004993
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004994 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004995 bool GetValueCanBeNull() const { return value_can_be_null_; }
4996 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004997
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004998 DECLARE_INSTRUCTION(StaticFieldSet);
4999
5000 private:
5001 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005002 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005003
5004 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5005};
5006
Calin Juravlee460d1d2015-09-29 04:52:17 +01005007class HUnresolvedInstanceFieldGet : public HExpression<1> {
5008 public:
5009 HUnresolvedInstanceFieldGet(HInstruction* obj,
5010 Primitive::Type field_type,
5011 uint32_t field_index,
5012 uint32_t dex_pc)
5013 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5014 field_index_(field_index) {
5015 SetRawInputAt(0, obj);
5016 }
5017
5018 bool NeedsEnvironment() const OVERRIDE { return true; }
5019 bool CanThrow() const OVERRIDE { return true; }
5020
5021 Primitive::Type GetFieldType() const { return GetType(); }
5022 uint32_t GetFieldIndex() const { return field_index_; }
5023
5024 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5025
5026 private:
5027 const uint32_t field_index_;
5028
5029 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5030};
5031
5032class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5033 public:
5034 HUnresolvedInstanceFieldSet(HInstruction* obj,
5035 HInstruction* value,
5036 Primitive::Type field_type,
5037 uint32_t field_index,
5038 uint32_t dex_pc)
5039 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5040 field_type_(field_type),
5041 field_index_(field_index) {
5042 DCHECK_EQ(field_type, value->GetType());
5043 SetRawInputAt(0, obj);
5044 SetRawInputAt(1, value);
5045 }
5046
5047 bool NeedsEnvironment() const OVERRIDE { return true; }
5048 bool CanThrow() const OVERRIDE { return true; }
5049
5050 Primitive::Type GetFieldType() const { return field_type_; }
5051 uint32_t GetFieldIndex() const { return field_index_; }
5052
5053 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5054
5055 private:
5056 const Primitive::Type field_type_;
5057 const uint32_t field_index_;
5058
5059 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5060};
5061
5062class HUnresolvedStaticFieldGet : public HExpression<0> {
5063 public:
5064 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5065 uint32_t field_index,
5066 uint32_t dex_pc)
5067 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5068 field_index_(field_index) {
5069 }
5070
5071 bool NeedsEnvironment() const OVERRIDE { return true; }
5072 bool CanThrow() const OVERRIDE { return true; }
5073
5074 Primitive::Type GetFieldType() const { return GetType(); }
5075 uint32_t GetFieldIndex() const { return field_index_; }
5076
5077 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5078
5079 private:
5080 const uint32_t field_index_;
5081
5082 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5083};
5084
5085class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5086 public:
5087 HUnresolvedStaticFieldSet(HInstruction* value,
5088 Primitive::Type field_type,
5089 uint32_t field_index,
5090 uint32_t dex_pc)
5091 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5092 field_type_(field_type),
5093 field_index_(field_index) {
5094 DCHECK_EQ(field_type, value->GetType());
5095 SetRawInputAt(0, value);
5096 }
5097
5098 bool NeedsEnvironment() const OVERRIDE { return true; }
5099 bool CanThrow() const OVERRIDE { return true; }
5100
5101 Primitive::Type GetFieldType() const { return field_type_; }
5102 uint32_t GetFieldIndex() const { return field_index_; }
5103
5104 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5105
5106 private:
5107 const Primitive::Type field_type_;
5108 const uint32_t field_index_;
5109
5110 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5111};
5112
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005113// Implement the move-exception DEX instruction.
5114class HLoadException : public HExpression<0> {
5115 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005116 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5117 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005118
David Brazdilbbd733e2015-08-18 17:48:17 +01005119 bool CanBeNull() const OVERRIDE { return false; }
5120
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005121 DECLARE_INSTRUCTION(LoadException);
5122
5123 private:
5124 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5125};
5126
David Brazdilcb1c0552015-08-04 16:22:25 +01005127// Implicit part of move-exception which clears thread-local exception storage.
5128// Must not be removed because the runtime expects the TLS to get cleared.
5129class HClearException : public HTemplateInstruction<0> {
5130 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005131 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5132 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005133
5134 DECLARE_INSTRUCTION(ClearException);
5135
5136 private:
5137 DISALLOW_COPY_AND_ASSIGN(HClearException);
5138};
5139
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005140class HThrow : public HTemplateInstruction<1> {
5141 public:
5142 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005143 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005144 SetRawInputAt(0, exception);
5145 }
5146
5147 bool IsControlFlow() const OVERRIDE { return true; }
5148
5149 bool NeedsEnvironment() const OVERRIDE { return true; }
5150
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005151 bool CanThrow() const OVERRIDE { return true; }
5152
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005153
5154 DECLARE_INSTRUCTION(Throw);
5155
5156 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005157 DISALLOW_COPY_AND_ASSIGN(HThrow);
5158};
5159
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005160/**
5161 * Implementation strategies for the code generator of a HInstanceOf
5162 * or `HCheckCast`.
5163 */
5164enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005165 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005166 kExactCheck, // Can do a single class compare.
5167 kClassHierarchyCheck, // Can just walk the super class chain.
5168 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5169 kInterfaceCheck, // No optimization yet when checking against an interface.
5170 kArrayObjectCheck, // Can just check if the array is not primitive.
5171 kArrayCheck // No optimization yet when checking against a generic array.
5172};
5173
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005174class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005175 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005176 HInstanceOf(HInstruction* object,
5177 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005178 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005179 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005180 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005181 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005182 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005183 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005184 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005185 SetRawInputAt(0, object);
5186 SetRawInputAt(1, constant);
5187 }
5188
5189 bool CanBeMoved() const OVERRIDE { return true; }
5190
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005191 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005192 return true;
5193 }
5194
5195 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005196 return false;
5197 }
5198
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005199 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5200
5201 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005202
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005203 // Used only in code generation.
5204 bool MustDoNullCheck() const { return must_do_null_check_; }
5205 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5206
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005207 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5208 return (check_kind == TypeCheckKind::kExactCheck)
5209 ? SideEffects::None()
5210 // Mips currently does runtime calls for any other checks.
5211 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005212 }
5213
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005214 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005215
5216 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005217 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005218 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005219
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005220 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5221};
5222
Calin Juravleb1498f62015-02-16 13:13:29 +00005223class HBoundType : public HExpression<1> {
5224 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005225 // Constructs an HBoundType with the given upper_bound.
5226 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005227 HBoundType(HInstruction* input,
5228 ReferenceTypeInfo upper_bound,
5229 bool upper_can_be_null,
5230 uint32_t dex_pc = kNoDexPc)
5231 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005232 upper_bound_(upper_bound),
5233 upper_can_be_null_(upper_can_be_null),
5234 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005235 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005236 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005237 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005238 }
5239
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005240 // GetUpper* should only be used in reference type propagation.
5241 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5242 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005243
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005244 void SetCanBeNull(bool can_be_null) {
5245 DCHECK(upper_can_be_null_ || !can_be_null);
5246 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005247 }
5248
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005249 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5250
Calin Juravleb1498f62015-02-16 13:13:29 +00005251 DECLARE_INSTRUCTION(BoundType);
5252
5253 private:
5254 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005255 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5256 // It is used to bound the type in cases like:
5257 // if (x instanceof ClassX) {
5258 // // uper_bound_ will be ClassX
5259 // }
5260 const ReferenceTypeInfo upper_bound_;
5261 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5262 // is false then can_be_null_ cannot be true).
5263 const bool upper_can_be_null_;
5264 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005265
5266 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5267};
5268
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005269class HCheckCast : public HTemplateInstruction<2> {
5270 public:
5271 HCheckCast(HInstruction* object,
5272 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005273 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005274 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005275 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005276 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005277 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005278 SetRawInputAt(0, object);
5279 SetRawInputAt(1, constant);
5280 }
5281
5282 bool CanBeMoved() const OVERRIDE { return true; }
5283
5284 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5285 return true;
5286 }
5287
5288 bool NeedsEnvironment() const OVERRIDE {
5289 // Instruction may throw a CheckCastError.
5290 return true;
5291 }
5292
5293 bool CanThrow() const OVERRIDE { return true; }
5294
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005295 bool MustDoNullCheck() const { return must_do_null_check_; }
5296 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005297 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005298
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005299 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005300
5301 DECLARE_INSTRUCTION(CheckCast);
5302
5303 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005304 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005305 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005306
5307 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005308};
5309
Calin Juravle27df7582015-04-17 19:12:31 +01005310class HMemoryBarrier : public HTemplateInstruction<0> {
5311 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005312 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005313 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005314 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005315 barrier_kind_(barrier_kind) {}
5316
5317 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5318
5319 DECLARE_INSTRUCTION(MemoryBarrier);
5320
5321 private:
5322 const MemBarrierKind barrier_kind_;
5323
5324 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5325};
5326
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005327class HMonitorOperation : public HTemplateInstruction<1> {
5328 public:
5329 enum OperationKind {
5330 kEnter,
5331 kExit,
5332 };
5333
5334 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005335 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005336 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5337 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005338 SetRawInputAt(0, object);
5339 }
5340
5341 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005342 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5343
5344 bool CanThrow() const OVERRIDE {
5345 // Verifier guarantees that monitor-exit cannot throw.
5346 // This is important because it allows the HGraphBuilder to remove
5347 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5348 return IsEnter();
5349 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005350
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005351
5352 bool IsEnter() const { return kind_ == kEnter; }
5353
5354 DECLARE_INSTRUCTION(MonitorOperation);
5355
Calin Juravle52c48962014-12-16 17:02:57 +00005356 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005357 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005358
5359 private:
5360 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5361};
5362
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005363/**
5364 * A HInstruction used as a marker for the replacement of new + <init>
5365 * of a String to a call to a StringFactory. Only baseline will see
5366 * the node at code generation, where it will be be treated as null.
5367 * When compiling non-baseline, `HFakeString` instructions are being removed
5368 * in the instruction simplifier.
5369 */
5370class HFakeString : public HTemplateInstruction<0> {
5371 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005372 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5373 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005374
5375 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5376
5377 DECLARE_INSTRUCTION(FakeString);
5378
5379 private:
5380 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5381};
5382
Vladimir Markof9f64412015-09-02 14:05:49 +01005383class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005384 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005385 MoveOperands(Location source,
5386 Location destination,
5387 Primitive::Type type,
5388 HInstruction* instruction)
5389 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005390
5391 Location GetSource() const { return source_; }
5392 Location GetDestination() const { return destination_; }
5393
5394 void SetSource(Location value) { source_ = value; }
5395 void SetDestination(Location value) { destination_ = value; }
5396
5397 // The parallel move resolver marks moves as "in-progress" by clearing the
5398 // destination (but not the source).
5399 Location MarkPending() {
5400 DCHECK(!IsPending());
5401 Location dest = destination_;
5402 destination_ = Location::NoLocation();
5403 return dest;
5404 }
5405
5406 void ClearPending(Location dest) {
5407 DCHECK(IsPending());
5408 destination_ = dest;
5409 }
5410
5411 bool IsPending() const {
5412 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5413 return destination_.IsInvalid() && !source_.IsInvalid();
5414 }
5415
5416 // True if this blocks a move from the given location.
5417 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005418 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005419 }
5420
5421 // A move is redundant if it's been eliminated, if its source and
5422 // destination are the same, or if its destination is unneeded.
5423 bool IsRedundant() const {
5424 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5425 }
5426
5427 // We clear both operands to indicate move that's been eliminated.
5428 void Eliminate() {
5429 source_ = destination_ = Location::NoLocation();
5430 }
5431
5432 bool IsEliminated() const {
5433 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5434 return source_.IsInvalid();
5435 }
5436
Alexey Frunze4dda3372015-06-01 18:31:49 -07005437 Primitive::Type GetType() const { return type_; }
5438
Nicolas Geoffray90218252015-04-15 11:56:51 +01005439 bool Is64BitMove() const {
5440 return Primitive::Is64BitType(type_);
5441 }
5442
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005443 HInstruction* GetInstruction() const { return instruction_; }
5444
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005445 private:
5446 Location source_;
5447 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005448 // The type this move is for.
5449 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005450 // The instruction this move is assocatied with. Null when this move is
5451 // for moving an input in the expected locations of user (including a phi user).
5452 // This is only used in debug mode, to ensure we do not connect interval siblings
5453 // in the same parallel move.
5454 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005455};
5456
5457static constexpr size_t kDefaultNumberOfMoves = 4;
5458
5459class HParallelMove : public HTemplateInstruction<0> {
5460 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005461 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005462 : HTemplateInstruction(SideEffects::None(), dex_pc),
5463 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5464 moves_.reserve(kDefaultNumberOfMoves);
5465 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005466
Nicolas Geoffray90218252015-04-15 11:56:51 +01005467 void AddMove(Location source,
5468 Location destination,
5469 Primitive::Type type,
5470 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005471 DCHECK(source.IsValid());
5472 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005473 if (kIsDebugBuild) {
5474 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005475 for (const MoveOperands& move : moves_) {
5476 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005477 // Special case the situation where the move is for the spill slot
5478 // of the instruction.
5479 if ((GetPrevious() == instruction)
5480 || ((GetPrevious() == nullptr)
5481 && instruction->IsPhi()
5482 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005483 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005484 << "Doing parallel moves for the same instruction.";
5485 } else {
5486 DCHECK(false) << "Doing parallel moves for the same instruction.";
5487 }
5488 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005489 }
5490 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005491 for (const MoveOperands& move : moves_) {
5492 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005493 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005494 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005495 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005496 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005497 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005498 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005499 }
5500
Vladimir Marko225b6462015-09-28 12:17:40 +01005501 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005502 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005503 }
5504
Vladimir Marko225b6462015-09-28 12:17:40 +01005505 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005506
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005507 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005508
5509 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005510 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005511
5512 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5513};
5514
Mark Mendell0616ae02015-04-17 12:49:27 -04005515} // namespace art
5516
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005517#ifdef ART_ENABLE_CODEGEN_arm64
5518#include "nodes_arm64.h"
5519#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005520#ifdef ART_ENABLE_CODEGEN_x86
5521#include "nodes_x86.h"
5522#endif
5523
5524namespace art {
5525
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005526class HGraphVisitor : public ValueObject {
5527 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005528 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5529 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005530
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005531 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005532 virtual void VisitBasicBlock(HBasicBlock* block);
5533
Roland Levillain633021e2014-10-01 14:12:25 +01005534 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005535 void VisitInsertionOrder();
5536
Roland Levillain633021e2014-10-01 14:12:25 +01005537 // Visit the graph following dominator tree reverse post-order.
5538 void VisitReversePostOrder();
5539
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005540 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005541
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005542 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005543#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005544 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5545
5546 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5547
5548#undef DECLARE_VISIT_INSTRUCTION
5549
5550 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005551 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005552
5553 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5554};
5555
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005556class HGraphDelegateVisitor : public HGraphVisitor {
5557 public:
5558 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5559 virtual ~HGraphDelegateVisitor() {}
5560
5561 // Visit functions that delegate to to super class.
5562#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005563 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005564
5565 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5566
5567#undef DECLARE_VISIT_INSTRUCTION
5568
5569 private:
5570 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5571};
5572
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005573class HInsertionOrderIterator : public ValueObject {
5574 public:
5575 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5576
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005577 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005578 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005579 void Advance() { ++index_; }
5580
5581 private:
5582 const HGraph& graph_;
5583 size_t index_;
5584
5585 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5586};
5587
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005588class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005589 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005590 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5591 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005592 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005593 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005594
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005595 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5596 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005597 void Advance() { ++index_; }
5598
5599 private:
5600 const HGraph& graph_;
5601 size_t index_;
5602
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005603 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005604};
5605
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005606class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005607 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005608 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005609 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005610 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005611 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005612 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005613
5614 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005615 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005616 void Advance() { --index_; }
5617
5618 private:
5619 const HGraph& graph_;
5620 size_t index_;
5621
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005622 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005623};
5624
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005625class HLinearPostOrderIterator : public ValueObject {
5626 public:
5627 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005628 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005629
5630 bool Done() const { return index_ == 0; }
5631
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005632 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005633
5634 void Advance() {
5635 --index_;
5636 DCHECK_GE(index_, 0U);
5637 }
5638
5639 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005640 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005641 size_t index_;
5642
5643 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5644};
5645
5646class HLinearOrderIterator : public ValueObject {
5647 public:
5648 explicit HLinearOrderIterator(const HGraph& graph)
5649 : order_(graph.GetLinearOrder()), index_(0) {}
5650
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005651 bool Done() const { return index_ == order_.size(); }
5652 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005653 void Advance() { ++index_; }
5654
5655 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005656 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005657 size_t index_;
5658
5659 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5660};
5661
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005662// Iterator over the blocks that art part of the loop. Includes blocks part
5663// of an inner loop. The order in which the blocks are iterated is on their
5664// block id.
5665class HBlocksInLoopIterator : public ValueObject {
5666 public:
5667 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5668 : blocks_in_loop_(info.GetBlocks()),
5669 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5670 index_(0) {
5671 if (!blocks_in_loop_.IsBitSet(index_)) {
5672 Advance();
5673 }
5674 }
5675
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005676 bool Done() const { return index_ == blocks_.size(); }
5677 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005678 void Advance() {
5679 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005680 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005681 if (blocks_in_loop_.IsBitSet(index_)) {
5682 break;
5683 }
5684 }
5685 }
5686
5687 private:
5688 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005689 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005690 size_t index_;
5691
5692 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5693};
5694
Mingyao Yang3584bce2015-05-19 16:01:59 -07005695// Iterator over the blocks that art part of the loop. Includes blocks part
5696// of an inner loop. The order in which the blocks are iterated is reverse
5697// post order.
5698class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5699 public:
5700 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5701 : blocks_in_loop_(info.GetBlocks()),
5702 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5703 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005704 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005705 Advance();
5706 }
5707 }
5708
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005709 bool Done() const { return index_ == blocks_.size(); }
5710 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005711 void Advance() {
5712 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005713 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5714 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005715 break;
5716 }
5717 }
5718 }
5719
5720 private:
5721 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005722 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005723 size_t index_;
5724
5725 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5726};
5727
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005728inline int64_t Int64FromConstant(HConstant* constant) {
5729 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5730 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5731 : constant->AsLongConstant()->GetValue();
5732}
5733
Vladimir Marko58155012015-08-19 12:49:41 +00005734inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5735 // For the purposes of the compiler, the dex files must actually be the same object
5736 // if we want to safely treat them as the same. This is especially important for JIT
5737 // as custom class loaders can open the same underlying file (or memory) multiple
5738 // times and provide different class resolution but no two class loaders should ever
5739 // use the same DexFile object - doing so is an unsupported hack that can lead to
5740 // all sorts of weird failures.
5741 return &lhs == &rhs;
5742}
5743
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005744} // namespace art
5745
5746#endif // ART_COMPILER_OPTIMIZING_NODES_H_