blob: ba140afc6befcfae6091267630839583f2cfea16 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39namespace art {
40
David Brazdil1abb4192015-02-17 18:33:36 +000041class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000044class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010045class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010046class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Calin Juravle9aec02f2014-11-18 23:06:35 +000074static constexpr uint32_t kMaxIntShiftValue = 0x1f;
75static constexpr uint64_t kMaxLongShiftValue = 0x3f;
76
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010077static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070078static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010080static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
81
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060082static constexpr uint32_t kNoDexPc = -1;
83
Dave Allison20dfc792014-06-16 20:44:29 -070084enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070085 // All types.
86 kCondEQ, // ==
87 kCondNE, // !=
88 // Signed integers and floating-point numbers.
89 kCondLT, // <
90 kCondLE, // <=
91 kCondGT, // >
92 kCondGE, // >=
93 // Unsigned integers.
94 kCondB, // <
95 kCondBE, // <=
96 kCondA, // >
97 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070098};
99
Vladimir Markof9f64412015-09-02 14:05:49 +0100100class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100101 public:
102 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
103
104 void AddInstruction(HInstruction* instruction);
105 void RemoveInstruction(HInstruction* instruction);
106
David Brazdilc3d743f2015-04-22 13:40:50 +0100107 // Insert `instruction` before/after an existing instruction `cursor`.
108 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
109 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
110
Roland Levillain6b469232014-09-25 10:10:38 +0100111 // Return true if this list contains `instruction`.
112 bool Contains(HInstruction* instruction) const;
113
Roland Levillainccc07a92014-09-16 14:48:16 +0100114 // Return true if `instruction1` is found before `instruction2` in
115 // this instruction list and false otherwise. Abort if none
116 // of these instructions is found.
117 bool FoundBefore(const HInstruction* instruction1,
118 const HInstruction* instruction2) const;
119
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000120 bool IsEmpty() const { return first_instruction_ == nullptr; }
121 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
122
123 // Update the block of all instructions to be `block`.
124 void SetBlockOfInstructions(HBasicBlock* block) const;
125
126 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
127 void Add(const HInstructionList& instruction_list);
128
David Brazdil2d7352b2015-04-20 14:52:42 +0100129 // Return the number of instructions in the list. This is an expensive operation.
130 size_t CountSize() const;
131
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100132 private:
133 HInstruction* first_instruction_;
134 HInstruction* last_instruction_;
135
136 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000137 friend class HGraph;
138 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100139 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100140 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100141
142 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
143};
144
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000145// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100146class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000147 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100148 HGraph(ArenaAllocator* arena,
149 const DexFile& dex_file,
150 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100151 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700152 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100153 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100154 bool debuggable = false,
155 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000156 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100157 blocks_(arena->Adapter(kArenaAllocBlockList)),
158 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
159 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700160 entry_block_(nullptr),
161 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100162 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100163 number_of_vregs_(0),
164 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000165 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400166 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000167 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000168 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000169 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100170 dex_file_(dex_file),
171 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100172 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100173 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100174 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700175 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000176 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100177 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
178 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
179 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
180 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
181 cached_current_method_(nullptr) {
182 blocks_.reserve(kDefaultNumberOfBlocks);
183 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000184
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000185 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100186 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
187
David Brazdil69ba7b72015-06-23 18:27:30 +0100188 bool IsInSsaForm() const { return in_ssa_form_; }
189
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000190 HBasicBlock* GetEntryBlock() const { return entry_block_; }
191 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100192 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000193
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000194 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
195 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000196
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000197 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 // Try building the SSA form of this graph, with dominance computation and loop
200 // recognition. Returns whether it was successful in doing all these steps.
201 bool TryBuildingSsa() {
202 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000203 // The SSA builder requires loops to all be natural. Specifically, the dead phi
204 // elimination phase checks the consistency of the graph when doing a post-order
205 // visit for eliminating dead phis: a dead phi can only have loop header phi
206 // users remaining when being visited.
207 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100208 // Precompute per-block try membership before entering the SSA builder,
209 // which needs the information to build catch block phis from values of
210 // locals at throwing instructions inside try blocks.
211 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100213 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000214 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215 }
216
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100217 void ComputeDominanceInformation();
218 void ClearDominanceInformation();
219
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000220 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000221 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100222 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100223 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000224
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000225 // Analyze all natural loops in this graph. Returns false if one
226 // loop is not natural, that is the header does not dominate the
227 // back edge.
228 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100229
David Brazdilffee3d32015-07-06 11:48:53 +0100230 // Iterate over blocks to compute try block membership. Needs reverse post
231 // order and loop information.
232 void ComputeTryBlockInformation();
233
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000234 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000235 // Returns the instruction used to replace the invoke expression or null if the
236 // invoke is for a void method.
237 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000238
Mingyao Yang3584bce2015-05-19 16:01:59 -0700239 // Need to add a couple of blocks to test if the loop body is entered and
240 // put deoptimization instructions, etc.
241 void TransformLoopHeaderForBCE(HBasicBlock* header);
242
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000243 // Removes `block` from the graph. Assumes `block` has been disconnected from
244 // other blocks and has no instructions or phis.
245 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000246
David Brazdilfc6a86a2015-06-26 10:33:45 +0000247 // Splits the edge between `block` and `successor` while preserving the
248 // indices in the predecessor/successor lists. If there are multiple edges
249 // between the blocks, the lowest indices are used.
250 // Returns the new block which is empty and has the same dex pc as `successor`.
251 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
252
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100253 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
254 void SimplifyLoop(HBasicBlock* header);
255
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000256 int32_t GetNextInstructionId() {
257 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000258 return current_instruction_id_++;
259 }
260
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000261 int32_t GetCurrentInstructionId() const {
262 return current_instruction_id_;
263 }
264
265 void SetCurrentInstructionId(int32_t id) {
266 current_instruction_id_ = id;
267 }
268
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100269 uint16_t GetMaximumNumberOfOutVRegs() const {
270 return maximum_number_of_out_vregs_;
271 }
272
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000273 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
274 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100275 }
276
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100277 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
278 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
279 }
280
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000281 void UpdateTemporariesVRegSlots(size_t slots) {
282 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100283 }
284
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000285 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100286 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000287 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100288 }
289
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100290 void SetNumberOfVRegs(uint16_t number_of_vregs) {
291 number_of_vregs_ = number_of_vregs;
292 }
293
294 uint16_t GetNumberOfVRegs() const {
295 return number_of_vregs_;
296 }
297
298 void SetNumberOfInVRegs(uint16_t value) {
299 number_of_in_vregs_ = value;
300 }
301
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100302 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100303 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100304 return number_of_vregs_ - number_of_in_vregs_;
305 }
306
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100307 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100308 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100309 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100310
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100311 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100312 return linear_order_;
313 }
314
Mark Mendell1152c922015-04-24 17:06:35 -0400315 bool HasBoundsChecks() const {
316 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800317 }
318
Mark Mendell1152c922015-04-24 17:06:35 -0400319 void SetHasBoundsChecks(bool value) {
320 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800321 }
322
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100323 bool ShouldGenerateConstructorBarrier() const {
324 return should_generate_constructor_barrier_;
325 }
326
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000327 bool IsDebuggable() const { return debuggable_; }
328
David Brazdil8d5b8b22015-03-24 10:51:52 +0000329 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000330 // already, it is created and inserted into the graph. This method is only for
331 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600332 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000333
334 // TODO: This is problematic for the consistency of reference type propagation
335 // because it can be created anytime after the pass and thus it will be left
336 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600337 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000338
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600339 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
340 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000341 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600342 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
343 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000344 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600345 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
346 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000347 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600348 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
349 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000350 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000351
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100352 HCurrentMethod* GetCurrentMethod();
353
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100354 const DexFile& GetDexFile() const {
355 return dex_file_;
356 }
357
358 uint32_t GetMethodIdx() const {
359 return method_idx_;
360 }
361
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100362 InvokeType GetInvokeType() const {
363 return invoke_type_;
364 }
365
Mark Mendellc4701932015-04-10 13:18:51 -0400366 InstructionSet GetInstructionSet() const {
367 return instruction_set_;
368 }
369
David Brazdil77a48ae2015-09-15 12:34:04 +0000370 bool HasTryCatch() const { return has_try_catch_; }
371 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100372
David Brazdil2d7352b2015-04-20 14:52:42 +0100373 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100374 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000375 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100376 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000377
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000378 template <class InstructionType, typename ValueType>
379 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600380 ArenaSafeMap<ValueType, InstructionType*>* cache,
381 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000382 // Try to find an existing constant of the given value.
383 InstructionType* constant = nullptr;
384 auto cached_constant = cache->find(value);
385 if (cached_constant != cache->end()) {
386 constant = cached_constant->second;
387 }
388
389 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100390 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000391 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600392 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000393 cache->Overwrite(value, constant);
394 InsertConstant(constant);
395 }
396 return constant;
397 }
398
David Brazdil8d5b8b22015-03-24 10:51:52 +0000399 void InsertConstant(HConstant* instruction);
400
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000401 // Cache a float constant into the graph. This method should only be
402 // called by the SsaBuilder when creating "equivalent" instructions.
403 void CacheFloatConstant(HFloatConstant* constant);
404
405 // See CacheFloatConstant comment.
406 void CacheDoubleConstant(HDoubleConstant* constant);
407
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000408 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000409
410 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100411 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000412
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100413 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100414 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000415
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100416 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100417 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100418
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000419 HBasicBlock* entry_block_;
420 HBasicBlock* exit_block_;
421
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100422 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100423 uint16_t maximum_number_of_out_vregs_;
424
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100425 // The number of virtual registers in this method. Contains the parameters.
426 uint16_t number_of_vregs_;
427
428 // The number of virtual registers used by parameters of this method.
429 uint16_t number_of_in_vregs_;
430
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000431 // Number of vreg size slots that the temporaries use (used in baseline compiler).
432 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100433
Mark Mendell1152c922015-04-24 17:06:35 -0400434 // Has bounds checks. We can totally skip BCE if it's false.
435 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800436
David Brazdil77a48ae2015-09-15 12:34:04 +0000437 // Flag whether there are any try/catch blocks in the graph. We will skip
438 // try/catch-related passes if false.
439 bool has_try_catch_;
440
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000441 // Indicates whether the graph should be compiled in a way that
442 // ensures full debuggability. If false, we can apply more
443 // aggressive optimizations that may limit the level of debugging.
444 const bool debuggable_;
445
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000446 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000447 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000448
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100449 // The dex file from which the method is from.
450 const DexFile& dex_file_;
451
452 // The method index in the dex file.
453 const uint32_t method_idx_;
454
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100455 // If inlined, this encodes how the callee is being invoked.
456 const InvokeType invoke_type_;
457
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100458 // Whether the graph has been transformed to SSA form. Only used
459 // in debug mode to ensure we are not using properties only valid
460 // for non-SSA form (like the number of temporaries).
461 bool in_ssa_form_;
462
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100463 const bool should_generate_constructor_barrier_;
464
Mathieu Chartiere401d142015-04-22 13:56:20 -0700465 const InstructionSet instruction_set_;
466
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000467 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000468 HNullConstant* cached_null_constant_;
469 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000470 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000471 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000472 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000473
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100474 HCurrentMethod* cached_current_method_;
475
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000476 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100477 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000478 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000479 DISALLOW_COPY_AND_ASSIGN(HGraph);
480};
481
Vladimir Markof9f64412015-09-02 14:05:49 +0100482class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000483 public:
484 HLoopInformation(HBasicBlock* header, HGraph* graph)
485 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100486 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100487 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100488 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100489 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
490 back_edges_.reserve(kDefaultNumberOfBackEdges);
491 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100492
493 HBasicBlock* GetHeader() const {
494 return header_;
495 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000496
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000497 void SetHeader(HBasicBlock* block) {
498 header_ = block;
499 }
500
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100501 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
502 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
503 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
504
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000505 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100506 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000507 }
508
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100509 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100510 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100511 }
512
David Brazdil46e2a392015-03-16 17:31:52 +0000513 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100514 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100515 }
516
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000517 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100518 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000519 }
520
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100521 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100522
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100523 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100524 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100525 }
526
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100527 // Returns the lifetime position of the back edge that has the
528 // greatest lifetime position.
529 size_t GetLifetimeEnd() const;
530
531 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100532 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100533 }
534
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100535 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100536 // that is the header dominates the back edge.
537 bool Populate();
538
David Brazdila4b8c212015-05-07 09:59:30 +0100539 // Reanalyzes the loop by removing loop info from its blocks and re-running
540 // Populate(). If there are no back edges left, the loop info is completely
541 // removed as well as its SuspendCheck instruction. It must be run on nested
542 // inner loops first.
543 void Update();
544
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100545 // Returns whether this loop information contains `block`.
546 // Note that this loop information *must* be populated before entering this function.
547 bool Contains(const HBasicBlock& block) const;
548
549 // Returns whether this loop information is an inner loop of `other`.
550 // Note that `other` *must* be populated before entering this function.
551 bool IsIn(const HLoopInformation& other) const;
552
Aart Bik73f1f3b2015-10-28 15:28:08 -0700553 // Returns true if instruction is not defined within this loop or any loop nested inside
554 // this loop. If must_dominate is set, only definitions that actually dominate the loop
555 // header can be invariant. Otherwise, any definition outside the loop, including
556 // definitions that appear after the loop, is invariant.
557 bool IsLoopInvariant(HInstruction* instruction, bool must_dominate) const;
558
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100559 const ArenaBitVector& GetBlocks() const { return blocks_; }
560
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000561 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000562 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000563
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000564 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100565 // Internal recursive implementation of `Populate`.
566 void PopulateRecursive(HBasicBlock* block);
567
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000568 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100569 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100570 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100571 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000572
573 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
574};
575
David Brazdilec16f792015-08-19 15:04:01 +0100576// Stores try/catch information for basic blocks.
577// Note that HGraph is constructed so that catch blocks cannot simultaneously
578// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100579class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100580 public:
581 // Try block information constructor.
582 explicit TryCatchInformation(const HTryBoundary& try_entry)
583 : try_entry_(&try_entry),
584 catch_dex_file_(nullptr),
585 catch_type_index_(DexFile::kDexNoIndex16) {
586 DCHECK(try_entry_ != nullptr);
587 }
588
589 // Catch block information constructor.
590 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
591 : try_entry_(nullptr),
592 catch_dex_file_(&dex_file),
593 catch_type_index_(catch_type_index) {}
594
595 bool IsTryBlock() const { return try_entry_ != nullptr; }
596
597 const HTryBoundary& GetTryEntry() const {
598 DCHECK(IsTryBlock());
599 return *try_entry_;
600 }
601
602 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
603
604 bool IsCatchAllTypeIndex() const {
605 DCHECK(IsCatchBlock());
606 return catch_type_index_ == DexFile::kDexNoIndex16;
607 }
608
609 uint16_t GetCatchTypeIndex() const {
610 DCHECK(IsCatchBlock());
611 return catch_type_index_;
612 }
613
614 const DexFile& GetCatchDexFile() const {
615 DCHECK(IsCatchBlock());
616 return *catch_dex_file_;
617 }
618
619 private:
620 // One of possibly several TryBoundary instructions entering the block's try.
621 // Only set for try blocks.
622 const HTryBoundary* try_entry_;
623
624 // Exception type information. Only set for catch blocks.
625 const DexFile* catch_dex_file_;
626 const uint16_t catch_type_index_;
627};
628
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100629static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100630static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100631
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632// A block in a method. Contains the list of instructions represented
633// as a double linked list. Each block knows its predecessors and
634// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100635
Vladimir Markof9f64412015-09-02 14:05:49 +0100636class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000637 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600638 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000639 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000640 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
641 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000642 loop_information_(nullptr),
643 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000644 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100645 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100646 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100647 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000648 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000649 try_catch_information_(nullptr) {
650 predecessors_.reserve(kDefaultNumberOfPredecessors);
651 successors_.reserve(kDefaultNumberOfSuccessors);
652 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
653 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000654
Vladimir Marko60584552015-09-03 13:35:12 +0000655 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100656 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000657 }
658
Vladimir Marko60584552015-09-03 13:35:12 +0000659 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100660 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000661 }
662
Vladimir Marko60584552015-09-03 13:35:12 +0000663 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
664 return ContainsElement(successors_, block, start_from);
665 }
666
667 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100668 return dominated_blocks_;
669 }
670
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100671 bool IsEntryBlock() const {
672 return graph_->GetEntryBlock() == this;
673 }
674
675 bool IsExitBlock() const {
676 return graph_->GetExitBlock() == this;
677 }
678
David Brazdil46e2a392015-03-16 17:31:52 +0000679 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000680 bool IsSingleTryBoundary() const;
681
682 // Returns true if this block emits nothing but a jump.
683 bool IsSingleJump() const {
684 HLoopInformation* loop_info = GetLoopInformation();
685 return (IsSingleGoto() || IsSingleTryBoundary())
686 // Back edges generate a suspend check.
687 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
688 }
David Brazdil46e2a392015-03-16 17:31:52 +0000689
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000690 void AddBackEdge(HBasicBlock* back_edge) {
691 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000692 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000693 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100694 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000695 loop_information_->AddBackEdge(back_edge);
696 }
697
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000698 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000699 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000700
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100701 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000702 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600703 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000704
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000705 HBasicBlock* GetDominator() const { return dominator_; }
706 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000707 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
708
709 void RemoveDominatedBlock(HBasicBlock* block) {
710 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100711 }
Vladimir Marko60584552015-09-03 13:35:12 +0000712
713 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
714 ReplaceElement(dominated_blocks_, existing, new_block);
715 }
716
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100717 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000718
719 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100720 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000721 }
722
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100723 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
724 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100725 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100726 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100727 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
728 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000729
730 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000731 successors_.push_back(block);
732 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000733 }
734
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100735 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
736 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100737 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000738 new_block->predecessors_.push_back(this);
739 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000740 }
741
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000742 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
743 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000744 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000745 new_block->successors_.push_back(this);
746 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000747 }
748
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100749 // Insert `this` between `predecessor` and `successor. This method
750 // preserves the indicies, and will update the first edge found between
751 // `predecessor` and `successor`.
752 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
753 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100754 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000755 successor->predecessors_[predecessor_index] = this;
756 predecessor->successors_[successor_index] = this;
757 successors_.push_back(successor);
758 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100759 }
760
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100761 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000762 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100763 }
764
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000765 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000766 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000767 }
768
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100769 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000770 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100771 }
772
773 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000774 predecessors_.push_back(block);
775 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100776 }
777
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100778 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000779 DCHECK_EQ(predecessors_.size(), 2u);
780 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100781 }
782
David Brazdil769c9e52015-04-27 13:54:09 +0100783 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000784 DCHECK_EQ(successors_.size(), 2u);
785 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100786 }
787
David Brazdilfc6a86a2015-06-26 10:33:45 +0000788 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000789 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100790 }
791
David Brazdilfc6a86a2015-06-26 10:33:45 +0000792 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000793 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100794 }
795
David Brazdilfc6a86a2015-06-26 10:33:45 +0000796 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000797 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100798 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000799 }
800
801 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000802 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100803 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000804 }
805
806 // Returns whether the first occurrence of `predecessor` in the list of
807 // predecessors is at index `idx`.
808 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100809 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000810 return GetPredecessorIndexOf(predecessor) == idx;
811 }
812
David Brazdilffee3d32015-07-06 11:48:53 +0100813 // Returns the number of non-exceptional successors. SsaChecker ensures that
814 // these are stored at the beginning of the successor list.
815 size_t NumberOfNormalSuccessors() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000816 return EndsWithTryBoundary() ? 1 : GetSuccessors().size();
David Brazdilffee3d32015-07-06 11:48:53 +0100817 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000818
David Brazdild7558da2015-09-22 13:04:14 +0100819 // Create a new block between this block and its predecessors. The new block
820 // is added to the graph, all predecessor edges are relinked to it and an edge
821 // is created to `this`. Returns the new empty block. Reverse post order or
822 // loop and try/catch information are not updated.
823 HBasicBlock* CreateImmediateDominator();
824
David Brazdilfc6a86a2015-06-26 10:33:45 +0000825 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100826 // created, latter block. Note that this method will add the block to the
827 // graph, create a Goto at the end of the former block and will create an edge
828 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100829 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000830 HBasicBlock* SplitBefore(HInstruction* cursor);
831
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000832 // Split the block into two blocks just after `cursor`. Returns the newly
833 // created block. Note that this method just updates raw block information,
834 // like predecessors, successors, dominators, and instruction list. It does not
835 // update the graph, reverse post order, loop information, nor make sure the
836 // blocks are consistent (for example ending with a control flow instruction).
837 HBasicBlock* SplitAfter(HInstruction* cursor);
838
David Brazdil9bc43612015-11-05 21:25:24 +0000839 // Split catch block into two blocks after the original move-exception bytecode
840 // instruction, or at the beginning if not present. Returns the newly created,
841 // latter block, or nullptr if such block could not be created (must be dead
842 // in that case). Note that this method just updates raw block information,
843 // like predecessors, successors, dominators, and instruction list. It does not
844 // update the graph, reverse post order, loop information, nor make sure the
845 // blocks are consistent (for example ending with a control flow instruction).
846 HBasicBlock* SplitCatchBlockAfterMoveException();
847
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000848 // Merge `other` at the end of `this`. Successors and dominated blocks of
849 // `other` are changed to be successors and dominated blocks of `this`. Note
850 // that this method does not update the graph, reverse post order, loop
851 // information, nor make sure the blocks are consistent (for example ending
852 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100853 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000854
855 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
856 // of `this` are moved to `other`.
857 // Note that this method does not update the graph, reverse post order, loop
858 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000859 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000860 void ReplaceWith(HBasicBlock* other);
861
David Brazdil2d7352b2015-04-20 14:52:42 +0100862 // Merge `other` at the end of `this`. This method updates loops, reverse post
863 // order, links to predecessors, successors, dominators and deletes the block
864 // from the graph. The two blocks must be successive, i.e. `this` the only
865 // predecessor of `other` and vice versa.
866 void MergeWith(HBasicBlock* other);
867
868 // Disconnects `this` from all its predecessors, successors and dominator,
869 // removes it from all loops it is included in and eventually from the graph.
870 // The block must not dominate any other block. Predecessors and successors
871 // are safely updated.
872 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000873
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000874 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100875 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100876 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100877 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100878 // Replace instruction `initial` with `replacement` within this block.
879 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
880 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100881 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100882 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000883 // RemoveInstruction and RemovePhi delete a given instruction from the respective
884 // instruction list. With 'ensure_safety' set to true, it verifies that the
885 // instruction is not in use and removes it from the use lists of its inputs.
886 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
887 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100888 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100889
890 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100891 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100892 }
893
Roland Levillain6b879dd2014-09-22 17:13:44 +0100894 bool IsLoopPreHeaderFirstPredecessor() const {
895 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100896 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100897 }
898
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100899 HLoopInformation* GetLoopInformation() const {
900 return loop_information_;
901 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000902
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000903 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100904 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000905 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100906 void SetInLoop(HLoopInformation* info) {
907 if (IsLoopHeader()) {
908 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100909 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100910 loop_information_ = info;
911 } else if (loop_information_->Contains(*info->GetHeader())) {
912 // Block is currently part of an outer loop. Make it part of this inner loop.
913 // Note that a non loop header having a loop information means this loop information
914 // has already been populated
915 loop_information_ = info;
916 } else {
917 // Block is part of an inner loop. Do not update the loop information.
918 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
919 // at this point, because this method is being called while populating `info`.
920 }
921 }
922
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000923 // Raw update of the loop information.
924 void SetLoopInformation(HLoopInformation* info) {
925 loop_information_ = info;
926 }
927
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100928 bool IsInLoop() const { return loop_information_ != nullptr; }
929
David Brazdilec16f792015-08-19 15:04:01 +0100930 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
931
932 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
933 try_catch_information_ = try_catch_information;
934 }
935
936 bool IsTryBlock() const {
937 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
938 }
939
940 bool IsCatchBlock() const {
941 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
942 }
David Brazdilffee3d32015-07-06 11:48:53 +0100943
944 // Returns the try entry that this block's successors should have. They will
945 // be in the same try, unless the block ends in a try boundary. In that case,
946 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100947 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100948
David Brazdild7558da2015-09-22 13:04:14 +0100949 bool HasThrowingInstructions() const;
950
David Brazdila4b8c212015-05-07 09:59:30 +0100951 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100952 bool Dominates(HBasicBlock* block) const;
953
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100954 size_t GetLifetimeStart() const { return lifetime_start_; }
955 size_t GetLifetimeEnd() const { return lifetime_end_; }
956
957 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
958 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
959
David Brazdil8d5b8b22015-03-24 10:51:52 +0000960 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000961 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100962 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000963 bool HasSinglePhi() const;
964
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000965 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000966 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000967 ArenaVector<HBasicBlock*> predecessors_;
968 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100969 HInstructionList instructions_;
970 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000971 HLoopInformation* loop_information_;
972 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000973 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100974 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100975 // The dex program counter of the first instruction of this block.
976 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100977 size_t lifetime_start_;
978 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100979 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100980
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000981 friend class HGraph;
982 friend class HInstruction;
983
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000984 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
985};
986
David Brazdilb2bd1c52015-03-25 11:17:37 +0000987// Iterates over the LoopInformation of all loops which contain 'block'
988// from the innermost to the outermost.
989class HLoopInformationOutwardIterator : public ValueObject {
990 public:
991 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
992 : current_(block.GetLoopInformation()) {}
993
994 bool Done() const { return current_ == nullptr; }
995
996 void Advance() {
997 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100998 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000999 }
1000
1001 HLoopInformation* Current() const {
1002 DCHECK(!Done());
1003 return current_;
1004 }
1005
1006 private:
1007 HLoopInformation* current_;
1008
1009 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1010};
1011
Alexandre Ramesef20f712015-06-09 10:29:30 +01001012#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001013 M(Above, Condition) \
1014 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001015 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001016 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001017 M(ArrayGet, Instruction) \
1018 M(ArrayLength, Instruction) \
1019 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001020 M(Below, Condition) \
1021 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001022 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001023 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001024 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001025 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001026 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001027 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001028 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001029 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001030 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001031 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001032 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001033 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001034 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001035 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001036 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001037 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001038 M(FloatConstant, Constant) \
1039 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001040 M(GreaterThan, Condition) \
1041 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001042 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001043 M(InstanceFieldGet, Instruction) \
1044 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001045 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001046 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001047 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001048 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001049 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001050 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001051 M(LessThan, Condition) \
1052 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001053 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001054 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001055 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001056 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001057 M(Local, Instruction) \
1058 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001059 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001060 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001061 M(Mul, BinaryOperation) \
1062 M(Neg, UnaryOperation) \
1063 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001064 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001065 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001066 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001067 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001068 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001069 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001070 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001071 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001072 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001073 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001074 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001075 M(Return, Instruction) \
1076 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001077 M(Shl, BinaryOperation) \
1078 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001079 M(StaticFieldGet, Instruction) \
1080 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001081 M(UnresolvedInstanceFieldGet, Instruction) \
1082 M(UnresolvedInstanceFieldSet, Instruction) \
1083 M(UnresolvedStaticFieldGet, Instruction) \
1084 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001085 M(StoreLocal, Instruction) \
1086 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001087 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001088 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001089 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001090 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001091 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001092 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001093 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001094
Alexandre Ramesef20f712015-06-09 10:29:30 +01001095#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1096
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001097#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001098#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001099#else
1100#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1101 M(Arm64IntermediateAddress, Instruction)
1102#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001103
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001104#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1105
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001106#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1107
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001108#ifndef ART_ENABLE_CODEGEN_x86
1109#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1110#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001111#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1112 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001113 M(X86LoadFromConstantTable, Instruction) \
1114 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001115#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001116
1117#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1118
1119#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1120 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1121 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1122 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001123 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001124 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001125 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1126 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1127
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001128#define FOR_EACH_INSTRUCTION(M) \
1129 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1130 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001131 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001132 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001133 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001134
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001135#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001136FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1137#undef FORWARD_DECLARATION
1138
Roland Levillainccc07a92014-09-16 14:48:16 +01001139#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001140 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1141 const char* DebugName() const OVERRIDE { return #type; } \
1142 const H##type* As##type() const OVERRIDE { return this; } \
1143 H##type* As##type() OVERRIDE { return this; } \
1144 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001145 return other->Is##type(); \
1146 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001147 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001148
David Brazdiled596192015-01-23 10:39:45 +00001149template <typename T> class HUseList;
1150
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001151template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001152class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001153 public:
David Brazdiled596192015-01-23 10:39:45 +00001154 HUseListNode* GetPrevious() const { return prev_; }
1155 HUseListNode* GetNext() const { return next_; }
1156 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001157 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001158 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001159
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001160 private:
David Brazdiled596192015-01-23 10:39:45 +00001161 HUseListNode(T user, size_t index)
1162 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1163
1164 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001165 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001166 HUseListNode<T>* prev_;
1167 HUseListNode<T>* next_;
1168
1169 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001170
1171 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1172};
1173
David Brazdiled596192015-01-23 10:39:45 +00001174template <typename T>
1175class HUseList : public ValueObject {
1176 public:
1177 HUseList() : first_(nullptr) {}
1178
1179 void Clear() {
1180 first_ = nullptr;
1181 }
1182
1183 // Adds a new entry at the beginning of the use list and returns
1184 // the newly created node.
1185 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001186 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001187 if (IsEmpty()) {
1188 first_ = new_node;
1189 } else {
1190 first_->prev_ = new_node;
1191 new_node->next_ = first_;
1192 first_ = new_node;
1193 }
1194 return new_node;
1195 }
1196
1197 HUseListNode<T>* GetFirst() const {
1198 return first_;
1199 }
1200
1201 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001202 DCHECK(node != nullptr);
1203 DCHECK(Contains(node));
1204
David Brazdiled596192015-01-23 10:39:45 +00001205 if (node->prev_ != nullptr) {
1206 node->prev_->next_ = node->next_;
1207 }
1208 if (node->next_ != nullptr) {
1209 node->next_->prev_ = node->prev_;
1210 }
1211 if (node == first_) {
1212 first_ = node->next_;
1213 }
1214 }
1215
David Brazdil1abb4192015-02-17 18:33:36 +00001216 bool Contains(const HUseListNode<T>* node) const {
1217 if (node == nullptr) {
1218 return false;
1219 }
1220 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1221 if (current == node) {
1222 return true;
1223 }
1224 }
1225 return false;
1226 }
1227
David Brazdiled596192015-01-23 10:39:45 +00001228 bool IsEmpty() const {
1229 return first_ == nullptr;
1230 }
1231
1232 bool HasOnlyOneUse() const {
1233 return first_ != nullptr && first_->next_ == nullptr;
1234 }
1235
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001236 size_t SizeSlow() const {
1237 size_t count = 0;
1238 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1239 ++count;
1240 }
1241 return count;
1242 }
1243
David Brazdiled596192015-01-23 10:39:45 +00001244 private:
1245 HUseListNode<T>* first_;
1246};
1247
1248template<typename T>
1249class HUseIterator : public ValueObject {
1250 public:
1251 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1252
1253 bool Done() const { return current_ == nullptr; }
1254
1255 void Advance() {
1256 DCHECK(!Done());
1257 current_ = current_->GetNext();
1258 }
1259
1260 HUseListNode<T>* Current() const {
1261 DCHECK(!Done());
1262 return current_;
1263 }
1264
1265 private:
1266 HUseListNode<T>* current_;
1267
1268 friend class HValue;
1269};
1270
David Brazdil1abb4192015-02-17 18:33:36 +00001271// This class is used by HEnvironment and HInstruction classes to record the
1272// instructions they use and pointers to the corresponding HUseListNodes kept
1273// by the used instructions.
1274template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001275class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001276 public:
1277 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1278 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1279
1280 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1281 : instruction_(old_record.instruction_), use_node_(use_node) {
1282 DCHECK(instruction_ != nullptr);
1283 DCHECK(use_node_ != nullptr);
1284 DCHECK(old_record.use_node_ == nullptr);
1285 }
1286
1287 HInstruction* GetInstruction() const { return instruction_; }
1288 HUseListNode<T>* GetUseNode() const { return use_node_; }
1289
1290 private:
1291 // Instruction used by the user.
1292 HInstruction* instruction_;
1293
1294 // Corresponding entry in the use list kept by 'instruction_'.
1295 HUseListNode<T>* use_node_;
1296};
1297
Aart Bik854a02b2015-07-14 16:07:00 -07001298/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001299 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001300 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001301 * For write/read dependences on fields/arrays, the dependence analysis uses
1302 * type disambiguation (e.g. a float field write cannot modify the value of an
1303 * integer field read) and the access type (e.g. a reference array write cannot
1304 * modify the value of a reference field read [although it may modify the
1305 * reference fetch prior to reading the field, which is represented by its own
1306 * write/read dependence]). The analysis makes conservative points-to
1307 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1308 * the same, and any reference read depends on any reference read without
1309 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001310 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001311 * The internal representation uses 38-bit and is described in the table below.
1312 * The first line indicates the side effect, and for field/array accesses the
1313 * second line indicates the type of the access (in the order of the
1314 * Primitive::Type enum).
1315 * The two numbered lines below indicate the bit position in the bitfield (read
1316 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001317 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001318 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1319 * +-------------+---------+---------+--------------+---------+---------+
1320 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1321 * | 3 |333333322|222222221| 1 |111111110|000000000|
1322 * | 7 |654321098|765432109| 8 |765432109|876543210|
1323 *
1324 * Note that, to ease the implementation, 'changes' bits are least significant
1325 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001326 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001327class SideEffects : public ValueObject {
1328 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001329 SideEffects() : flags_(0) {}
1330
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001331 static SideEffects None() {
1332 return SideEffects(0);
1333 }
1334
1335 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001336 return SideEffects(kAllChangeBits | kAllDependOnBits);
1337 }
1338
1339 static SideEffects AllChanges() {
1340 return SideEffects(kAllChangeBits);
1341 }
1342
1343 static SideEffects AllDependencies() {
1344 return SideEffects(kAllDependOnBits);
1345 }
1346
1347 static SideEffects AllExceptGCDependency() {
1348 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1349 }
1350
1351 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001352 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001353 }
1354
Aart Bik34c3ba92015-07-20 14:08:59 -07001355 static SideEffects AllWrites() {
1356 return SideEffects(kAllWrites);
1357 }
1358
1359 static SideEffects AllReads() {
1360 return SideEffects(kAllReads);
1361 }
1362
1363 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1364 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001365 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001366 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001367 }
1368
Aart Bik854a02b2015-07-14 16:07:00 -07001369 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1370 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001371 }
1372
Aart Bik34c3ba92015-07-20 14:08:59 -07001373 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1374 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001375 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001376 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001377 }
1378
1379 static SideEffects ArrayReadOfType(Primitive::Type type) {
1380 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1381 }
1382
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001383 static SideEffects CanTriggerGC() {
1384 return SideEffects(1ULL << kCanTriggerGCBit);
1385 }
1386
1387 static SideEffects DependsOnGC() {
1388 return SideEffects(1ULL << kDependsOnGCBit);
1389 }
1390
Aart Bik854a02b2015-07-14 16:07:00 -07001391 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001392 SideEffects Union(SideEffects other) const {
1393 return SideEffects(flags_ | other.flags_);
1394 }
1395
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001396 SideEffects Exclusion(SideEffects other) const {
1397 return SideEffects(flags_ & ~other.flags_);
1398 }
1399
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001400 void Add(SideEffects other) {
1401 flags_ |= other.flags_;
1402 }
1403
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001404 bool Includes(SideEffects other) const {
1405 return (other.flags_ & flags_) == other.flags_;
1406 }
1407
1408 bool HasSideEffects() const {
1409 return (flags_ & kAllChangeBits);
1410 }
1411
1412 bool HasDependencies() const {
1413 return (flags_ & kAllDependOnBits);
1414 }
1415
1416 // Returns true if there are no side effects or dependencies.
1417 bool DoesNothing() const {
1418 return flags_ == 0;
1419 }
1420
Aart Bik854a02b2015-07-14 16:07:00 -07001421 // Returns true if something is written.
1422 bool DoesAnyWrite() const {
1423 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001424 }
1425
Aart Bik854a02b2015-07-14 16:07:00 -07001426 // Returns true if something is read.
1427 bool DoesAnyRead() const {
1428 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001429 }
1430
Aart Bik854a02b2015-07-14 16:07:00 -07001431 // Returns true if potentially everything is written and read
1432 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001433 bool DoesAllReadWrite() const {
1434 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1435 }
1436
Aart Bik854a02b2015-07-14 16:07:00 -07001437 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001438 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001439 }
1440
1441 // Returns true if this may read something written by other.
1442 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001443 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1444 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001445 }
1446
1447 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001448 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001449 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001450 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001451 for (int s = kLastBit; s >= 0; s--) {
1452 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1453 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1454 // This is a bit for the GC side effect.
1455 if (current_bit_is_set) {
1456 flags += "GC";
1457 }
Aart Bik854a02b2015-07-14 16:07:00 -07001458 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001459 } else {
1460 // This is a bit for the array/field analysis.
1461 // The underscore character stands for the 'can trigger GC' bit.
1462 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1463 if (current_bit_is_set) {
1464 flags += kDebug[s];
1465 }
1466 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1467 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1468 flags += "|";
1469 }
1470 }
Aart Bik854a02b2015-07-14 16:07:00 -07001471 }
1472 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001473 }
1474
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001475 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001476
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001477 private:
1478 static constexpr int kFieldArrayAnalysisBits = 9;
1479
1480 static constexpr int kFieldWriteOffset = 0;
1481 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1482 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1483 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1484
1485 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1486
1487 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1488 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1489 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1490 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1491
1492 static constexpr int kLastBit = kDependsOnGCBit;
1493 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1494
1495 // Aliases.
1496
1497 static_assert(kChangeBits == kDependOnBits,
1498 "the 'change' bits should match the 'depend on' bits.");
1499
1500 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1501 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1502 static constexpr uint64_t kAllWrites =
1503 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1504 static constexpr uint64_t kAllReads =
1505 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001506
Aart Bik854a02b2015-07-14 16:07:00 -07001507 // Work around the fact that HIR aliases I/F and J/D.
1508 // TODO: remove this interceptor once HIR types are clean
1509 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1510 switch (type) {
1511 case Primitive::kPrimInt:
1512 case Primitive::kPrimFloat:
1513 return TypeFlag(Primitive::kPrimInt, offset) |
1514 TypeFlag(Primitive::kPrimFloat, offset);
1515 case Primitive::kPrimLong:
1516 case Primitive::kPrimDouble:
1517 return TypeFlag(Primitive::kPrimLong, offset) |
1518 TypeFlag(Primitive::kPrimDouble, offset);
1519 default:
1520 return TypeFlag(type, offset);
1521 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001522 }
1523
Aart Bik854a02b2015-07-14 16:07:00 -07001524 // Translates type to bit flag.
1525 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1526 CHECK_NE(type, Primitive::kPrimVoid);
1527 const uint64_t one = 1;
1528 const int shift = type; // 0-based consecutive enum
1529 DCHECK_LE(kFieldWriteOffset, shift);
1530 DCHECK_LT(shift, kArrayWriteOffset);
1531 return one << (type + offset);
1532 }
1533
1534 // Private constructor on direct flags value.
1535 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1536
1537 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001538};
1539
David Brazdiled596192015-01-23 10:39:45 +00001540// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001541class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001542 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001543 HEnvironment(ArenaAllocator* arena,
1544 size_t number_of_vregs,
1545 const DexFile& dex_file,
1546 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001547 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001548 InvokeType invoke_type,
1549 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001550 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1551 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001552 parent_(nullptr),
1553 dex_file_(dex_file),
1554 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001555 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001556 invoke_type_(invoke_type),
1557 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001558 }
1559
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001560 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001561 : HEnvironment(arena,
1562 to_copy.Size(),
1563 to_copy.GetDexFile(),
1564 to_copy.GetMethodIdx(),
1565 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001566 to_copy.GetInvokeType(),
1567 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001568
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001569 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001570 if (parent_ != nullptr) {
1571 parent_->SetAndCopyParentChain(allocator, parent);
1572 } else {
1573 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1574 parent_->CopyFrom(parent);
1575 if (parent->GetParent() != nullptr) {
1576 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1577 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001578 }
David Brazdiled596192015-01-23 10:39:45 +00001579 }
1580
Vladimir Marko71bf8092015-09-15 15:33:14 +01001581 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001582 void CopyFrom(HEnvironment* environment);
1583
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001584 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1585 // input to the loop phi instead. This is for inserting instructions that
1586 // require an environment (like HDeoptimization) in the loop pre-header.
1587 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001588
1589 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001590 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001591 }
1592
1593 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001594 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001595 }
1596
David Brazdil1abb4192015-02-17 18:33:36 +00001597 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001598
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001599 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001600
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001601 HEnvironment* GetParent() const { return parent_; }
1602
1603 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001604 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001605 }
1606
1607 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001608 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001609 }
1610
1611 uint32_t GetDexPc() const {
1612 return dex_pc_;
1613 }
1614
1615 uint32_t GetMethodIdx() const {
1616 return method_idx_;
1617 }
1618
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001619 InvokeType GetInvokeType() const {
1620 return invoke_type_;
1621 }
1622
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001623 const DexFile& GetDexFile() const {
1624 return dex_file_;
1625 }
1626
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001627 HInstruction* GetHolder() const {
1628 return holder_;
1629 }
1630
David Brazdiled596192015-01-23 10:39:45 +00001631 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001632 // Record instructions' use entries of this environment for constant-time removal.
1633 // It should only be called by HInstruction when a new environment use is added.
1634 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1635 DCHECK(env_use->GetUser() == this);
1636 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001637 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001638 }
David Brazdiled596192015-01-23 10:39:45 +00001639
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001640 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1641 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001642 HEnvironment* parent_;
1643 const DexFile& dex_file_;
1644 const uint32_t method_idx_;
1645 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001646 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001647
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001648 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001649 HInstruction* const holder_;
1650
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001651 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001652
1653 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1654};
1655
Calin Juravleacf735c2015-02-12 15:25:22 +00001656class ReferenceTypeInfo : ValueObject {
1657 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001658 typedef Handle<mirror::Class> TypeHandle;
1659
Calin Juravle2e768302015-07-28 14:41:11 +00001660 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1661 // The constructor will check that the type_handle is valid.
1662 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001663 }
1664
Calin Juravle2e768302015-07-28 14:41:11 +00001665 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1666
1667 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1668 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001669 }
1670
Calin Juravle2e768302015-07-28 14:41:11 +00001671 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1672 return IsValidHandle(type_handle_);
1673 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001674
Calin Juravleacf735c2015-02-12 15:25:22 +00001675 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001676
1677 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1678 DCHECK(IsValid());
1679 return GetTypeHandle()->IsObjectClass();
1680 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001681
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001682 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001683 DCHECK(IsValid());
1684 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001685 }
1686
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001687 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1688 DCHECK(IsValid());
1689 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1690 }
1691
Mathieu Chartier90443472015-07-16 20:32:27 -07001692 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001693 DCHECK(IsValid());
1694 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001695 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001696
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001697 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001698 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001699 return GetTypeHandle()->IsArrayClass();
1700 }
1701
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001702 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1703 DCHECK(IsValid());
1704 return GetTypeHandle()->IsPrimitiveArray();
1705 }
1706
1707 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1708 DCHECK(IsValid());
1709 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1710 }
1711
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001712 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001713 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001714 if (!IsExact()) return false;
1715 if (!IsArrayClass()) return false;
1716 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1717 }
1718
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001719 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1720 DCHECK(IsValid());
1721 if (!IsExact()) return false;
1722 if (!IsArrayClass()) return false;
1723 if (!rti.IsArrayClass()) return false;
1724 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1725 rti.GetTypeHandle()->GetComponentType());
1726 }
1727
Calin Juravleacf735c2015-02-12 15:25:22 +00001728 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1729
Mathieu Chartier90443472015-07-16 20:32:27 -07001730 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001731 DCHECK(IsValid());
1732 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001733 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1734 }
1735
1736 // Returns true if the type information provide the same amount of details.
1737 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001738 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001739 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001740 if (!IsValid() && !rti.IsValid()) {
1741 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001742 return true;
1743 }
Calin Juravle2e768302015-07-28 14:41:11 +00001744 if (!IsValid() || !rti.IsValid()) {
1745 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001746 return false;
1747 }
Calin Juravle2e768302015-07-28 14:41:11 +00001748 return IsExact() == rti.IsExact()
1749 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001750 }
1751
1752 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001753 ReferenceTypeInfo();
1754 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001755
Calin Juravleacf735c2015-02-12 15:25:22 +00001756 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001757 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001758 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001759 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001760 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001761};
1762
1763std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1764
Vladimir Markof9f64412015-09-02 14:05:49 +01001765class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001766 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001767 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001768 : previous_(nullptr),
1769 next_(nullptr),
1770 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001771 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001772 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001773 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001774 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001775 locations_(nullptr),
1776 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001777 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001778 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001779 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001780
Dave Allison20dfc792014-06-16 20:44:29 -07001781 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001782
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001783#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001784 enum InstructionKind {
1785 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1786 };
1787#undef DECLARE_KIND
1788
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001789 HInstruction* GetNext() const { return next_; }
1790 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001791
Calin Juravle77520bc2015-01-12 18:45:46 +00001792 HInstruction* GetNextDisregardingMoves() const;
1793 HInstruction* GetPreviousDisregardingMoves() const;
1794
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001795 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001796 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001797 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001798 bool IsInBlock() const { return block_ != nullptr; }
1799 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001800 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001801
Roland Levillain6b879dd2014-09-22 17:13:44 +01001802 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001803 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001804
1805 virtual void Accept(HGraphVisitor* visitor) = 0;
1806 virtual const char* DebugName() const = 0;
1807
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001808 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001809 void SetRawInputAt(size_t index, HInstruction* input) {
1810 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1811 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001812
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001813 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001814
1815 uint32_t GetDexPc() const { return dex_pc_; }
1816
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001817 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001818
Roland Levillaine161a2a2014-10-03 12:45:18 +01001819 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001820 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001821
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001822 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001823 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001824
Calin Juravle10e244f2015-01-26 18:54:32 +00001825 // Does not apply for all instructions, but having this at top level greatly
1826 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001827 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001828 virtual bool CanBeNull() const {
1829 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1830 return true;
1831 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001832
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001833 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001834 return false;
1835 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001836
Calin Juravle2e768302015-07-28 14:41:11 +00001837 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001838
Calin Juravle61d544b2015-02-23 16:46:57 +00001839 ReferenceTypeInfo GetReferenceTypeInfo() const {
1840 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1841 return reference_type_info_;
1842 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001843
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001844 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001845 DCHECK(user != nullptr);
1846 HUseListNode<HInstruction*>* use =
1847 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1848 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001849 }
1850
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001851 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001852 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001853 HUseListNode<HEnvironment*>* env_use =
1854 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1855 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001856 }
1857
David Brazdil1abb4192015-02-17 18:33:36 +00001858 void RemoveAsUserOfInput(size_t input) {
1859 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1860 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1861 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001862
David Brazdil1abb4192015-02-17 18:33:36 +00001863 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1864 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001865
David Brazdiled596192015-01-23 10:39:45 +00001866 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1867 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001868 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001869 bool HasOnlyOneNonEnvironmentUse() const {
1870 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1871 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001872
Roland Levillain6c82d402014-10-13 16:10:27 +01001873 // Does this instruction strictly dominate `other_instruction`?
1874 // Returns false if this instruction and `other_instruction` are the same.
1875 // Aborts if this instruction and `other_instruction` are both phis.
1876 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001877
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001878 int GetId() const { return id_; }
1879 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001880
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001881 int GetSsaIndex() const { return ssa_index_; }
1882 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1883 bool HasSsaIndex() const { return ssa_index_ != -1; }
1884
1885 bool HasEnvironment() const { return environment_ != nullptr; }
1886 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001887 // Set the `environment_` field. Raw because this method does not
1888 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001889 void SetRawEnvironment(HEnvironment* environment) {
1890 DCHECK(environment_ == nullptr);
1891 DCHECK_EQ(environment->GetHolder(), this);
1892 environment_ = environment;
1893 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001894
1895 // Set the environment of this instruction, copying it from `environment`. While
1896 // copying, the uses lists are being updated.
1897 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001898 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001899 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001900 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001901 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001902 if (environment->GetParent() != nullptr) {
1903 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1904 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001905 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001906
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001907 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1908 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001909 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001910 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001911 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001912 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001913 if (environment->GetParent() != nullptr) {
1914 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1915 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001916 }
1917
Nicolas Geoffray39468442014-09-02 15:17:15 +01001918 // Returns the number of entries in the environment. Typically, that is the
1919 // number of dex registers in a method. It could be more in case of inlining.
1920 size_t EnvironmentSize() const;
1921
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001922 LocationSummary* GetLocations() const { return locations_; }
1923 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001924
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001925 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001926 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001927
Alexandre Rames188d4312015-04-09 18:30:21 +01001928 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1929 // uses of this instruction by `other` are *not* updated.
1930 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1931 ReplaceWith(other);
1932 other->ReplaceInput(this, use_index);
1933 }
1934
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001935 // Move `this` instruction before `cursor`.
1936 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001937
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001938#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001939 bool Is##type() const { return (As##type() != nullptr); } \
1940 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001941 virtual H##type* As##type() { return nullptr; }
1942
1943 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1944#undef INSTRUCTION_TYPE_CHECK
1945
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001946 // Returns whether the instruction can be moved within the graph.
1947 virtual bool CanBeMoved() const { return false; }
1948
1949 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001950 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001951 return false;
1952 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001953
1954 // Returns whether any data encoded in the two instructions is equal.
1955 // This method does not look at the inputs. Both instructions must be
1956 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001957 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001958 return false;
1959 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001960
1961 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001962 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001963 // 2) Their inputs are identical.
1964 bool Equals(HInstruction* other) const;
1965
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001966 virtual InstructionKind GetKind() const = 0;
1967
1968 virtual size_t ComputeHashCode() const {
1969 size_t result = GetKind();
1970 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1971 result = (result * 31) + InputAt(i)->GetId();
1972 }
1973 return result;
1974 }
1975
1976 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001977 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001978
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001979 size_t GetLifetimePosition() const { return lifetime_position_; }
1980 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1981 LiveInterval* GetLiveInterval() const { return live_interval_; }
1982 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1983 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1984
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001985 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1986
1987 // Returns whether the code generation of the instruction will require to have access
1988 // to the current method. Such instructions are:
1989 // (1): Instructions that require an environment, as calling the runtime requires
1990 // to walk the stack and have the current method stored at a specific stack address.
1991 // (2): Object literals like classes and strings, that are loaded from the dex cache
1992 // fields of the current method.
1993 bool NeedsCurrentMethod() const {
1994 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1995 }
1996
Vladimir Markodc151b22015-10-15 18:02:30 +01001997 // Returns whether the code generation of the instruction will require to have access
1998 // to the dex cache of the current method's declaring class via the current method.
1999 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002000
Mark Mendellc4701932015-04-10 13:18:51 -04002001 // Does this instruction have any use in an environment before
2002 // control flow hits 'other'?
2003 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2004
2005 // Remove all references to environment uses of this instruction.
2006 // The caller must ensure that this is safe to do.
2007 void RemoveEnvironmentUsers();
2008
David Brazdil1abb4192015-02-17 18:33:36 +00002009 protected:
2010 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2011 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2012
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002013 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002014 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2015
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002016 HInstruction* previous_;
2017 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002018 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002019 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002020
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002021 // An instruction gets an id when it is added to the graph.
2022 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002023 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002024 int id_;
2025
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002026 // When doing liveness analysis, instructions that have uses get an SSA index.
2027 int ssa_index_;
2028
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002029 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002030 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002031
2032 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002033 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002034
Nicolas Geoffray39468442014-09-02 15:17:15 +01002035 // The environment associated with this instruction. Not null if the instruction
2036 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002037 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002038
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002039 // Set by the code generator.
2040 LocationSummary* locations_;
2041
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002042 // Set by the liveness analysis.
2043 LiveInterval* live_interval_;
2044
2045 // Set by the liveness analysis, this is the position in a linear
2046 // order of blocks where this instruction's live interval start.
2047 size_t lifetime_position_;
2048
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002049 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002050
Calin Juravleacf735c2015-02-12 15:25:22 +00002051 // TODO: for primitive types this should be marked as invalid.
2052 ReferenceTypeInfo reference_type_info_;
2053
David Brazdil1abb4192015-02-17 18:33:36 +00002054 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002055 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002056 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002057 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002058 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002059
2060 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2061};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002062std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002063
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002064class HInputIterator : public ValueObject {
2065 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002066 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002067
2068 bool Done() const { return index_ == instruction_->InputCount(); }
2069 HInstruction* Current() const { return instruction_->InputAt(index_); }
2070 void Advance() { index_++; }
2071
2072 private:
2073 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002074 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002075
2076 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2077};
2078
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002079class HInstructionIterator : public ValueObject {
2080 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002081 explicit HInstructionIterator(const HInstructionList& instructions)
2082 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002083 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002084 }
2085
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002086 bool Done() const { return instruction_ == nullptr; }
2087 HInstruction* Current() const { return instruction_; }
2088 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002089 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002090 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002091 }
2092
2093 private:
2094 HInstruction* instruction_;
2095 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002096
2097 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002098};
2099
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002100class HBackwardInstructionIterator : public ValueObject {
2101 public:
2102 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2103 : instruction_(instructions.last_instruction_) {
2104 next_ = Done() ? nullptr : instruction_->GetPrevious();
2105 }
2106
2107 bool Done() const { return instruction_ == nullptr; }
2108 HInstruction* Current() const { return instruction_; }
2109 void Advance() {
2110 instruction_ = next_;
2111 next_ = Done() ? nullptr : instruction_->GetPrevious();
2112 }
2113
2114 private:
2115 HInstruction* instruction_;
2116 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002117
2118 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002119};
2120
Vladimir Markof9f64412015-09-02 14:05:49 +01002121template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002122class HTemplateInstruction: public HInstruction {
2123 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002124 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002125 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002126 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002127
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002128 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002129
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002130 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002131 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2132 DCHECK_LT(i, N);
2133 return inputs_[i];
2134 }
David Brazdil1abb4192015-02-17 18:33:36 +00002135
2136 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002137 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002138 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002139 }
2140
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002141 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002142 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002143
2144 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002145};
2146
Vladimir Markof9f64412015-09-02 14:05:49 +01002147// HTemplateInstruction specialization for N=0.
2148template<>
2149class HTemplateInstruction<0>: public HInstruction {
2150 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002151 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002152 : HInstruction(side_effects, dex_pc) {}
2153
Vladimir Markof9f64412015-09-02 14:05:49 +01002154 virtual ~HTemplateInstruction() {}
2155
2156 size_t InputCount() const OVERRIDE { return 0; }
2157
2158 protected:
2159 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2160 LOG(FATAL) << "Unreachable";
2161 UNREACHABLE();
2162 }
2163
2164 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2165 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2166 LOG(FATAL) << "Unreachable";
2167 UNREACHABLE();
2168 }
2169
2170 private:
2171 friend class SsaBuilder;
2172};
2173
Dave Allison20dfc792014-06-16 20:44:29 -07002174template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002175class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002176 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002177 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002178 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002179 virtual ~HExpression() {}
2180
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002181 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002182
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002183 protected:
2184 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002185};
2186
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002187// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2188// instruction that branches to the exit block.
2189class HReturnVoid : public HTemplateInstruction<0> {
2190 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002191 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2192 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002193
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002194 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002195
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002196 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002197
2198 private:
2199 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2200};
2201
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002202// Represents dex's RETURN opcodes. A HReturn is a control flow
2203// instruction that branches to the exit block.
2204class HReturn : public HTemplateInstruction<1> {
2205 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002206 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2207 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002208 SetRawInputAt(0, value);
2209 }
2210
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002211 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002212
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002213 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002214
2215 private:
2216 DISALLOW_COPY_AND_ASSIGN(HReturn);
2217};
2218
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002219// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002220// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002221// exit block.
2222class HExit : public HTemplateInstruction<0> {
2223 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002224 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002225
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002226 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002227
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002228 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002229
2230 private:
2231 DISALLOW_COPY_AND_ASSIGN(HExit);
2232};
2233
2234// Jumps from one block to another.
2235class HGoto : public HTemplateInstruction<0> {
2236 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002237 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002238
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002239 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002240
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002241 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002242 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002243 }
2244
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002245 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002246
2247 private:
2248 DISALLOW_COPY_AND_ASSIGN(HGoto);
2249};
2250
Roland Levillain9867bc72015-08-05 10:21:34 +01002251class HConstant : public HExpression<0> {
2252 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002253 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2254 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002255
2256 bool CanBeMoved() const OVERRIDE { return true; }
2257
2258 virtual bool IsMinusOne() const { return false; }
2259 virtual bool IsZero() const { return false; }
2260 virtual bool IsOne() const { return false; }
2261
David Brazdil77a48ae2015-09-15 12:34:04 +00002262 virtual uint64_t GetValueAsUint64() const = 0;
2263
Roland Levillain9867bc72015-08-05 10:21:34 +01002264 DECLARE_INSTRUCTION(Constant);
2265
2266 private:
2267 DISALLOW_COPY_AND_ASSIGN(HConstant);
2268};
2269
2270class HNullConstant : public HConstant {
2271 public:
2272 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2273 return true;
2274 }
2275
David Brazdil77a48ae2015-09-15 12:34:04 +00002276 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2277
Roland Levillain9867bc72015-08-05 10:21:34 +01002278 size_t ComputeHashCode() const OVERRIDE { return 0; }
2279
2280 DECLARE_INSTRUCTION(NullConstant);
2281
2282 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002283 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002284
2285 friend class HGraph;
2286 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2287};
2288
2289// Constants of the type int. Those can be from Dex instructions, or
2290// synthesized (for example with the if-eqz instruction).
2291class HIntConstant : public HConstant {
2292 public:
2293 int32_t GetValue() const { return value_; }
2294
David Brazdil9f389d42015-10-01 14:32:56 +01002295 uint64_t GetValueAsUint64() const OVERRIDE {
2296 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2297 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002298
Roland Levillain9867bc72015-08-05 10:21:34 +01002299 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2300 DCHECK(other->IsIntConstant());
2301 return other->AsIntConstant()->value_ == value_;
2302 }
2303
2304 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2305
2306 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2307 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2308 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2309
2310 DECLARE_INSTRUCTION(IntConstant);
2311
2312 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002313 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2314 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2315 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2316 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002317
2318 const int32_t value_;
2319
2320 friend class HGraph;
2321 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2322 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2323 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2324};
2325
2326class HLongConstant : public HConstant {
2327 public:
2328 int64_t GetValue() const { return value_; }
2329
David Brazdil77a48ae2015-09-15 12:34:04 +00002330 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2331
Roland Levillain9867bc72015-08-05 10:21:34 +01002332 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2333 DCHECK(other->IsLongConstant());
2334 return other->AsLongConstant()->value_ == value_;
2335 }
2336
2337 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2338
2339 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2340 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2341 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2342
2343 DECLARE_INSTRUCTION(LongConstant);
2344
2345 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002346 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2347 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002348
2349 const int64_t value_;
2350
2351 friend class HGraph;
2352 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2353};
Dave Allison20dfc792014-06-16 20:44:29 -07002354
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002355// Conditional branch. A block ending with an HIf instruction must have
2356// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002357class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002358 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002359 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2360 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002361 SetRawInputAt(0, input);
2362 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002363
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002364 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002365
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002366 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002367 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002368 }
2369
2370 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002371 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002372 }
2373
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002374 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002375
2376 private:
2377 DISALLOW_COPY_AND_ASSIGN(HIf);
2378};
2379
David Brazdilfc6a86a2015-06-26 10:33:45 +00002380
2381// Abstract instruction which marks the beginning and/or end of a try block and
2382// links it to the respective exception handlers. Behaves the same as a Goto in
2383// non-exceptional control flow.
2384// Normal-flow successor is stored at index zero, exception handlers under
2385// higher indices in no particular order.
2386class HTryBoundary : public HTemplateInstruction<0> {
2387 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002388 enum BoundaryKind {
2389 kEntry,
2390 kExit,
2391 };
2392
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002393 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2394 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002395
2396 bool IsControlFlow() const OVERRIDE { return true; }
2397
2398 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002399 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002400
2401 // Returns whether `handler` is among its exception handlers (non-zero index
2402 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002403 bool HasExceptionHandler(const HBasicBlock& handler) const {
2404 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002405 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002406 }
2407
2408 // If not present already, adds `handler` to its block's list of exception
2409 // handlers.
2410 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002411 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002412 GetBlock()->AddSuccessor(handler);
2413 }
2414 }
2415
David Brazdil56e1acc2015-06-30 15:41:36 +01002416 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002417
David Brazdilffee3d32015-07-06 11:48:53 +01002418 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2419
David Brazdilfc6a86a2015-06-26 10:33:45 +00002420 DECLARE_INSTRUCTION(TryBoundary);
2421
2422 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002423 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002424
2425 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2426};
2427
David Brazdilffee3d32015-07-06 11:48:53 +01002428// Iterator over exception handlers of a given HTryBoundary, i.e. over
2429// exceptional successors of its basic block.
2430class HExceptionHandlerIterator : public ValueObject {
2431 public:
2432 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2433 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2434
Vladimir Marko60584552015-09-03 13:35:12 +00002435 bool Done() const { return index_ == block_.GetSuccessors().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01002436 HBasicBlock* Current() const { return block_.GetSuccessors()[index_]; }
David Brazdilffee3d32015-07-06 11:48:53 +01002437 size_t CurrentSuccessorIndex() const { return index_; }
2438 void Advance() { ++index_; }
2439
2440 private:
2441 const HBasicBlock& block_;
2442 size_t index_;
2443
2444 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2445};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002446
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002447// Deoptimize to interpreter, upon checking a condition.
2448class HDeoptimize : public HTemplateInstruction<1> {
2449 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002450 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2451 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002452 SetRawInputAt(0, cond);
2453 }
2454
2455 bool NeedsEnvironment() const OVERRIDE { return true; }
2456 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002457
2458 DECLARE_INSTRUCTION(Deoptimize);
2459
2460 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002461 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2462};
2463
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002464// Represents the ArtMethod that was passed as a first argument to
2465// the method. It is used by instructions that depend on it, like
2466// instructions that work with the dex cache.
2467class HCurrentMethod : public HExpression<0> {
2468 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002469 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2470 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002471
2472 DECLARE_INSTRUCTION(CurrentMethod);
2473
2474 private:
2475 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2476};
2477
Mark Mendellfe57faa2015-09-18 09:26:15 -04002478// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2479// have one successor for each entry in the switch table, and the final successor
2480// will be the block containing the next Dex opcode.
2481class HPackedSwitch : public HTemplateInstruction<1> {
2482 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002483 HPackedSwitch(int32_t start_value,
2484 uint32_t num_entries,
2485 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002486 uint32_t dex_pc = kNoDexPc)
2487 : HTemplateInstruction(SideEffects::None(), dex_pc),
2488 start_value_(start_value),
2489 num_entries_(num_entries) {
2490 SetRawInputAt(0, input);
2491 }
2492
2493 bool IsControlFlow() const OVERRIDE { return true; }
2494
2495 int32_t GetStartValue() const { return start_value_; }
2496
Vladimir Marko211c2112015-09-24 16:52:33 +01002497 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002498
2499 HBasicBlock* GetDefaultBlock() const {
2500 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002501 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002502 }
2503 DECLARE_INSTRUCTION(PackedSwitch);
2504
2505 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002506 const int32_t start_value_;
2507 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002508
2509 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2510};
2511
Roland Levillain88cb1752014-10-20 16:36:47 +01002512class HUnaryOperation : public HExpression<1> {
2513 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002514 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2515 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002516 SetRawInputAt(0, input);
2517 }
2518
2519 HInstruction* GetInput() const { return InputAt(0); }
2520 Primitive::Type GetResultType() const { return GetType(); }
2521
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002522 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002523 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002524 return true;
2525 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002526
Roland Levillain9240d6a2014-10-20 16:47:04 +01002527 // Try to statically evaluate `operation` and return a HConstant
2528 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002529 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002530 HConstant* TryStaticEvaluation() const;
2531
2532 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002533 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2534 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002535
Roland Levillain88cb1752014-10-20 16:36:47 +01002536 DECLARE_INSTRUCTION(UnaryOperation);
2537
2538 private:
2539 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2540};
2541
Dave Allison20dfc792014-06-16 20:44:29 -07002542class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002543 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002544 HBinaryOperation(Primitive::Type result_type,
2545 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002546 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002547 SideEffects side_effects = SideEffects::None(),
2548 uint32_t dex_pc = kNoDexPc)
2549 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002550 SetRawInputAt(0, left);
2551 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002552 }
2553
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002554 HInstruction* GetLeft() const { return InputAt(0); }
2555 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002556 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002557
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002558 virtual bool IsCommutative() const { return false; }
2559
2560 // Put constant on the right.
2561 // Returns whether order is changed.
2562 bool OrderInputsWithConstantOnTheRight() {
2563 HInstruction* left = InputAt(0);
2564 HInstruction* right = InputAt(1);
2565 if (left->IsConstant() && !right->IsConstant()) {
2566 ReplaceInput(right, 0);
2567 ReplaceInput(left, 1);
2568 return true;
2569 }
2570 return false;
2571 }
2572
2573 // Order inputs by instruction id, but favor constant on the right side.
2574 // This helps GVN for commutative ops.
2575 void OrderInputs() {
2576 DCHECK(IsCommutative());
2577 HInstruction* left = InputAt(0);
2578 HInstruction* right = InputAt(1);
2579 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2580 return;
2581 }
2582 if (OrderInputsWithConstantOnTheRight()) {
2583 return;
2584 }
2585 // Order according to instruction id.
2586 if (left->GetId() > right->GetId()) {
2587 ReplaceInput(right, 0);
2588 ReplaceInput(left, 1);
2589 }
2590 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002591
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002592 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002593 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002594 return true;
2595 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002596
Roland Levillain9240d6a2014-10-20 16:47:04 +01002597 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002598 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002599 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002600 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002601
2602 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002603 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2604 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2605 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2606 HLongConstant* y ATTRIBUTE_UNUSED) const {
2607 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2608 return nullptr;
2609 }
2610 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2611 HIntConstant* y ATTRIBUTE_UNUSED) const {
2612 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2613 return nullptr;
2614 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002615 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2616 HNullConstant* y ATTRIBUTE_UNUSED) const {
2617 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2618 return nullptr;
2619 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002620
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002621 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002622 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002623 HConstant* GetConstantRight() const;
2624
2625 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002626 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002627 HInstruction* GetLeastConstantLeft() const;
2628
Roland Levillainccc07a92014-09-16 14:48:16 +01002629 DECLARE_INSTRUCTION(BinaryOperation);
2630
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002631 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002632 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2633};
2634
Mark Mendellc4701932015-04-10 13:18:51 -04002635// The comparison bias applies for floating point operations and indicates how NaN
2636// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002637enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002638 kNoBias, // bias is not applicable (i.e. for long operation)
2639 kGtBias, // return 1 for NaN comparisons
2640 kLtBias, // return -1 for NaN comparisons
2641};
2642
Dave Allison20dfc792014-06-16 20:44:29 -07002643class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002644 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002645 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2646 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002647 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002648 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002649
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002650 bool NeedsMaterialization() const { return needs_materialization_; }
2651 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002652
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002653 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002654 // `instruction`, and disregard moves in between.
2655 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002656
Dave Allison20dfc792014-06-16 20:44:29 -07002657 DECLARE_INSTRUCTION(Condition);
2658
2659 virtual IfCondition GetCondition() const = 0;
2660
Mark Mendellc4701932015-04-10 13:18:51 -04002661 virtual IfCondition GetOppositeCondition() const = 0;
2662
Roland Levillain4fa13f62015-07-06 18:11:54 +01002663 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002664
2665 void SetBias(ComparisonBias bias) { bias_ = bias; }
2666
2667 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2668 return bias_ == other->AsCondition()->bias_;
2669 }
2670
Roland Levillain4fa13f62015-07-06 18:11:54 +01002671 bool IsFPConditionTrueIfNaN() const {
2672 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2673 IfCondition if_cond = GetCondition();
2674 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2675 }
2676
2677 bool IsFPConditionFalseIfNaN() const {
2678 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2679 IfCondition if_cond = GetCondition();
2680 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2681 }
2682
Dave Allison20dfc792014-06-16 20:44:29 -07002683 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002684 // For register allocation purposes, returns whether this instruction needs to be
2685 // materialized (that is, not just be in the processor flags).
2686 bool needs_materialization_;
2687
Mark Mendellc4701932015-04-10 13:18:51 -04002688 // Needed if we merge a HCompare into a HCondition.
2689 ComparisonBias bias_;
2690
Dave Allison20dfc792014-06-16 20:44:29 -07002691 DISALLOW_COPY_AND_ASSIGN(HCondition);
2692};
2693
2694// Instruction to check if two inputs are equal to each other.
2695class HEqual : public HCondition {
2696 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002697 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2698 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002699
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002700 bool IsCommutative() const OVERRIDE { return true; }
2701
Roland Levillain9867bc72015-08-05 10:21:34 +01002702 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002703 return GetBlock()->GetGraph()->GetIntConstant(
2704 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002705 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002706 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002707 return GetBlock()->GetGraph()->GetIntConstant(
2708 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002709 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002710 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2711 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
2712 return GetBlock()->GetGraph()->GetConstant(GetType(), 1);
2713 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002714
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002715 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002716
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002717 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002718 return kCondEQ;
2719 }
2720
Mark Mendellc4701932015-04-10 13:18:51 -04002721 IfCondition GetOppositeCondition() const OVERRIDE {
2722 return kCondNE;
2723 }
2724
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002725 private:
Aart Bike9f37602015-10-09 11:15:55 -07002726 template <typename T> bool Compute(T x, T y) const { return x == y; }
2727
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002728 DISALLOW_COPY_AND_ASSIGN(HEqual);
2729};
2730
Dave Allison20dfc792014-06-16 20:44:29 -07002731class HNotEqual : public HCondition {
2732 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002733 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2734 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002735
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002736 bool IsCommutative() const OVERRIDE { return true; }
2737
Roland Levillain9867bc72015-08-05 10:21:34 +01002738 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002739 return GetBlock()->GetGraph()->GetIntConstant(
2740 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002741 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002742 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002743 return GetBlock()->GetGraph()->GetIntConstant(
2744 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002745 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002746 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2747 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
2748 return GetBlock()->GetGraph()->GetConstant(GetType(), 0);
2749 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002750
Dave Allison20dfc792014-06-16 20:44:29 -07002751 DECLARE_INSTRUCTION(NotEqual);
2752
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002753 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002754 return kCondNE;
2755 }
2756
Mark Mendellc4701932015-04-10 13:18:51 -04002757 IfCondition GetOppositeCondition() const OVERRIDE {
2758 return kCondEQ;
2759 }
2760
Dave Allison20dfc792014-06-16 20:44:29 -07002761 private:
Aart Bike9f37602015-10-09 11:15:55 -07002762 template <typename T> bool Compute(T x, T y) const { return x != y; }
2763
Dave Allison20dfc792014-06-16 20:44:29 -07002764 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2765};
2766
2767class HLessThan : public HCondition {
2768 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002769 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2770 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002771
Roland Levillain9867bc72015-08-05 10:21:34 +01002772 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002773 return GetBlock()->GetGraph()->GetIntConstant(
2774 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002775 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002776 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002777 return GetBlock()->GetGraph()->GetIntConstant(
2778 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002779 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002780
Dave Allison20dfc792014-06-16 20:44:29 -07002781 DECLARE_INSTRUCTION(LessThan);
2782
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002783 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002784 return kCondLT;
2785 }
2786
Mark Mendellc4701932015-04-10 13:18:51 -04002787 IfCondition GetOppositeCondition() const OVERRIDE {
2788 return kCondGE;
2789 }
2790
Dave Allison20dfc792014-06-16 20:44:29 -07002791 private:
Aart Bike9f37602015-10-09 11:15:55 -07002792 template <typename T> bool Compute(T x, T y) const { return x < y; }
2793
Dave Allison20dfc792014-06-16 20:44:29 -07002794 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2795};
2796
2797class HLessThanOrEqual : public HCondition {
2798 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002799 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2800 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002801
Roland Levillain9867bc72015-08-05 10:21:34 +01002802 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002803 return GetBlock()->GetGraph()->GetIntConstant(
2804 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002805 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002806 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002807 return GetBlock()->GetGraph()->GetIntConstant(
2808 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002809 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002810
Dave Allison20dfc792014-06-16 20:44:29 -07002811 DECLARE_INSTRUCTION(LessThanOrEqual);
2812
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002813 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002814 return kCondLE;
2815 }
2816
Mark Mendellc4701932015-04-10 13:18:51 -04002817 IfCondition GetOppositeCondition() const OVERRIDE {
2818 return kCondGT;
2819 }
2820
Dave Allison20dfc792014-06-16 20:44:29 -07002821 private:
Aart Bike9f37602015-10-09 11:15:55 -07002822 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2823
Dave Allison20dfc792014-06-16 20:44:29 -07002824 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2825};
2826
2827class HGreaterThan : public HCondition {
2828 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002829 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2830 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002831
Roland Levillain9867bc72015-08-05 10:21:34 +01002832 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002833 return GetBlock()->GetGraph()->GetIntConstant(
2834 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002835 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002836 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002837 return GetBlock()->GetGraph()->GetIntConstant(
2838 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002839 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002840
Dave Allison20dfc792014-06-16 20:44:29 -07002841 DECLARE_INSTRUCTION(GreaterThan);
2842
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002843 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002844 return kCondGT;
2845 }
2846
Mark Mendellc4701932015-04-10 13:18:51 -04002847 IfCondition GetOppositeCondition() const OVERRIDE {
2848 return kCondLE;
2849 }
2850
Dave Allison20dfc792014-06-16 20:44:29 -07002851 private:
Aart Bike9f37602015-10-09 11:15:55 -07002852 template <typename T> bool Compute(T x, T y) const { return x > y; }
2853
Dave Allison20dfc792014-06-16 20:44:29 -07002854 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2855};
2856
2857class HGreaterThanOrEqual : public HCondition {
2858 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002859 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2860 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002861
Roland Levillain9867bc72015-08-05 10:21:34 +01002862 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002863 return GetBlock()->GetGraph()->GetIntConstant(
2864 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002865 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002866 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002867 return GetBlock()->GetGraph()->GetIntConstant(
2868 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002869 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002870
Dave Allison20dfc792014-06-16 20:44:29 -07002871 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2872
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002873 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002874 return kCondGE;
2875 }
2876
Mark Mendellc4701932015-04-10 13:18:51 -04002877 IfCondition GetOppositeCondition() const OVERRIDE {
2878 return kCondLT;
2879 }
2880
Dave Allison20dfc792014-06-16 20:44:29 -07002881 private:
Aart Bike9f37602015-10-09 11:15:55 -07002882 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2883
Dave Allison20dfc792014-06-16 20:44:29 -07002884 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2885};
2886
Aart Bike9f37602015-10-09 11:15:55 -07002887class HBelow : public HCondition {
2888 public:
2889 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2890 : HCondition(first, second, dex_pc) {}
2891
2892 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2893 return GetBlock()->GetGraph()->GetIntConstant(
2894 Compute(static_cast<uint32_t>(x->GetValue()),
2895 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2896 }
2897 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2898 return GetBlock()->GetGraph()->GetIntConstant(
2899 Compute(static_cast<uint64_t>(x->GetValue()),
2900 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2901 }
2902
2903 DECLARE_INSTRUCTION(Below);
2904
2905 IfCondition GetCondition() const OVERRIDE {
2906 return kCondB;
2907 }
2908
2909 IfCondition GetOppositeCondition() const OVERRIDE {
2910 return kCondAE;
2911 }
2912
2913 private:
2914 template <typename T> bool Compute(T x, T y) const { return x < y; }
2915
2916 DISALLOW_COPY_AND_ASSIGN(HBelow);
2917};
2918
2919class HBelowOrEqual : public HCondition {
2920 public:
2921 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2922 : HCondition(first, second, dex_pc) {}
2923
2924 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2925 return GetBlock()->GetGraph()->GetIntConstant(
2926 Compute(static_cast<uint32_t>(x->GetValue()),
2927 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2928 }
2929 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2930 return GetBlock()->GetGraph()->GetIntConstant(
2931 Compute(static_cast<uint64_t>(x->GetValue()),
2932 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2933 }
2934
2935 DECLARE_INSTRUCTION(BelowOrEqual);
2936
2937 IfCondition GetCondition() const OVERRIDE {
2938 return kCondBE;
2939 }
2940
2941 IfCondition GetOppositeCondition() const OVERRIDE {
2942 return kCondA;
2943 }
2944
2945 private:
2946 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2947
2948 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2949};
2950
2951class HAbove : public HCondition {
2952 public:
2953 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2954 : HCondition(first, second, dex_pc) {}
2955
2956 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2957 return GetBlock()->GetGraph()->GetIntConstant(
2958 Compute(static_cast<uint32_t>(x->GetValue()),
2959 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2960 }
2961 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2962 return GetBlock()->GetGraph()->GetIntConstant(
2963 Compute(static_cast<uint64_t>(x->GetValue()),
2964 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2965 }
2966
2967 DECLARE_INSTRUCTION(Above);
2968
2969 IfCondition GetCondition() const OVERRIDE {
2970 return kCondA;
2971 }
2972
2973 IfCondition GetOppositeCondition() const OVERRIDE {
2974 return kCondBE;
2975 }
2976
2977 private:
2978 template <typename T> bool Compute(T x, T y) const { return x > y; }
2979
2980 DISALLOW_COPY_AND_ASSIGN(HAbove);
2981};
2982
2983class HAboveOrEqual : public HCondition {
2984 public:
2985 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2986 : HCondition(first, second, dex_pc) {}
2987
2988 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2989 return GetBlock()->GetGraph()->GetIntConstant(
2990 Compute(static_cast<uint32_t>(x->GetValue()),
2991 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2992 }
2993 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2994 return GetBlock()->GetGraph()->GetIntConstant(
2995 Compute(static_cast<uint64_t>(x->GetValue()),
2996 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2997 }
2998
2999 DECLARE_INSTRUCTION(AboveOrEqual);
3000
3001 IfCondition GetCondition() const OVERRIDE {
3002 return kCondAE;
3003 }
3004
3005 IfCondition GetOppositeCondition() const OVERRIDE {
3006 return kCondB;
3007 }
3008
3009 private:
3010 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3011
3012 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3013};
Dave Allison20dfc792014-06-16 20:44:29 -07003014
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003015// Instruction to check how two inputs compare to each other.
3016// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3017class HCompare : public HBinaryOperation {
3018 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003019 HCompare(Primitive::Type type,
3020 HInstruction* first,
3021 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003022 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003023 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003024 : HBinaryOperation(Primitive::kPrimInt,
3025 first,
3026 second,
3027 SideEffectsForArchRuntimeCalls(type),
3028 dex_pc),
3029 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003030 DCHECK_EQ(type, first->GetType());
3031 DCHECK_EQ(type, second->GetType());
3032 }
3033
Roland Levillain9867bc72015-08-05 10:21:34 +01003034 template <typename T>
3035 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003036
Roland Levillain9867bc72015-08-05 10:21:34 +01003037 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003038 return GetBlock()->GetGraph()->GetIntConstant(
3039 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003040 }
3041 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003042 return GetBlock()->GetGraph()->GetIntConstant(
3043 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003044 }
3045
Calin Juravleddb7df22014-11-25 20:56:51 +00003046 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3047 return bias_ == other->AsCompare()->bias_;
3048 }
3049
Mark Mendellc4701932015-04-10 13:18:51 -04003050 ComparisonBias GetBias() const { return bias_; }
3051
Roland Levillain4fa13f62015-07-06 18:11:54 +01003052 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003053
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003054
3055 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3056 // MIPS64 uses a runtime call for FP comparisons.
3057 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3058 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003059
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003060 DECLARE_INSTRUCTION(Compare);
3061
3062 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003063 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003064
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003065 DISALLOW_COPY_AND_ASSIGN(HCompare);
3066};
3067
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003068// A local in the graph. Corresponds to a Dex register.
3069class HLocal : public HTemplateInstruction<0> {
3070 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003071 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003072 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003073
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003074 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003075
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003076 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003077
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003078 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003079 // The Dex register number.
3080 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003081
3082 DISALLOW_COPY_AND_ASSIGN(HLocal);
3083};
3084
3085// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003086class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003087 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003088 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3089 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003090 SetRawInputAt(0, local);
3091 }
3092
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003093 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3094
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003095 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003096
3097 private:
3098 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3099};
3100
3101// Store a value in a given local. This instruction has two inputs: the value
3102// and the local.
3103class HStoreLocal : public HTemplateInstruction<2> {
3104 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003105 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3106 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003107 SetRawInputAt(0, local);
3108 SetRawInputAt(1, value);
3109 }
3110
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003111 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3112
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003113 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003114
3115 private:
3116 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3117};
3118
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003119class HFloatConstant : public HConstant {
3120 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003121 float GetValue() const { return value_; }
3122
David Brazdil77a48ae2015-09-15 12:34:04 +00003123 uint64_t GetValueAsUint64() const OVERRIDE {
3124 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3125 }
3126
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003127 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003128 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003129 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003130 }
3131
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003132 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003133
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003134 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003135 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003136 }
3137 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003138 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003139 }
3140 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003141 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3142 }
3143 bool IsNaN() const {
3144 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003145 }
3146
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003147 DECLARE_INSTRUCTION(FloatConstant);
3148
3149 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003150 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3151 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3152 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3153 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003154
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003155 const float value_;
3156
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003157 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003158 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003159 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003160 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3161};
3162
3163class HDoubleConstant : public HConstant {
3164 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003165 double GetValue() const { return value_; }
3166
David Brazdil77a48ae2015-09-15 12:34:04 +00003167 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3168
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003169 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003170 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003171 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003172 }
3173
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003174 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003175
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003176 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003177 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003178 }
3179 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003180 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003181 }
3182 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003183 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3184 }
3185 bool IsNaN() const {
3186 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003187 }
3188
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003189 DECLARE_INSTRUCTION(DoubleConstant);
3190
3191 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003192 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3193 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3194 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3195 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003196
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003197 const double value_;
3198
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003199 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003200 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003201 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003202 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3203};
3204
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003205enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003206#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003207#include "intrinsics_list.h"
3208 kNone,
3209 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3210#undef INTRINSICS_LIST
3211#undef OPTIMIZING_INTRINSICS
3212};
3213std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3214
Agi Csaki05f20562015-08-19 14:58:14 -07003215enum IntrinsicNeedsEnvironmentOrCache {
3216 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3217 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003218};
3219
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003220class HInvoke : public HInstruction {
3221 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003222 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003223
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003224 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003225
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003226 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003227 SetRawInputAt(index, argument);
3228 }
3229
Roland Levillain3e3d7332015-04-28 11:00:54 +01003230 // Return the number of arguments. This number can be lower than
3231 // the number of inputs returned by InputCount(), as some invoke
3232 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3233 // inputs at the end of their list of inputs.
3234 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3235
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003236 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003237
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003238
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003239 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003240 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003241
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003242 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3243
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003244 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003245 return intrinsic_;
3246 }
3247
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003248 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003249
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003250 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003251 return GetEnvironment()->GetParent() != nullptr;
3252 }
3253
3254 bool CanThrow() const OVERRIDE { return true; }
3255
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003256 uint32_t* GetIntrinsicOptimizations() {
3257 return &intrinsic_optimizations_;
3258 }
3259
3260 const uint32_t* GetIntrinsicOptimizations() const {
3261 return &intrinsic_optimizations_;
3262 }
3263
3264 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3265
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003266 DECLARE_INSTRUCTION(Invoke);
3267
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003268 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003269 HInvoke(ArenaAllocator* arena,
3270 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003271 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003272 Primitive::Type return_type,
3273 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003274 uint32_t dex_method_index,
3275 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003276 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003277 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003278 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003279 inputs_(number_of_arguments + number_of_other_inputs,
3280 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003281 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003282 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003283 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003284 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003285 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003286 }
3287
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003288 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003289 return inputs_[index];
3290 }
3291
David Brazdil1abb4192015-02-17 18:33:36 +00003292 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003293 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003294 }
3295
Roland Levillain3e3d7332015-04-28 11:00:54 +01003296 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003297 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003298 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003299 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003300 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003301 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003302
3303 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3304 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003305
3306 private:
3307 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3308};
3309
Calin Juravle175dc732015-08-25 15:42:32 +01003310class HInvokeUnresolved : public HInvoke {
3311 public:
3312 HInvokeUnresolved(ArenaAllocator* arena,
3313 uint32_t number_of_arguments,
3314 Primitive::Type return_type,
3315 uint32_t dex_pc,
3316 uint32_t dex_method_index,
3317 InvokeType invoke_type)
3318 : HInvoke(arena,
3319 number_of_arguments,
3320 0u /* number_of_other_inputs */,
3321 return_type,
3322 dex_pc,
3323 dex_method_index,
3324 invoke_type) {
3325 }
3326
3327 DECLARE_INSTRUCTION(InvokeUnresolved);
3328
3329 private:
3330 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3331};
3332
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003333class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003334 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003335 // Requirements of this method call regarding the class
3336 // initialization (clinit) check of its declaring class.
3337 enum class ClinitCheckRequirement {
3338 kNone, // Class already initialized.
3339 kExplicit, // Static call having explicit clinit check as last input.
3340 kImplicit, // Static call implicitly requiring a clinit check.
3341 };
3342
Vladimir Marko58155012015-08-19 12:49:41 +00003343 // Determines how to load the target ArtMethod*.
3344 enum class MethodLoadKind {
3345 // Use a String init ArtMethod* loaded from Thread entrypoints.
3346 kStringInit,
3347
3348 // Use the method's own ArtMethod* loaded by the register allocator.
3349 kRecursive,
3350
3351 // Use ArtMethod* at a known address, embed the direct address in the code.
3352 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3353 kDirectAddress,
3354
3355 // Use ArtMethod* at an address that will be known at link time, embed the direct
3356 // address in the code. If the image is relocatable, emit .patch_oat entry.
3357 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3358 // the image relocatable or not.
3359 kDirectAddressWithFixup,
3360
3361 // Load from resoved methods array in the dex cache using a PC-relative load.
3362 // Used when we need to use the dex cache, for example for invoke-static that
3363 // may cause class initialization (the entry may point to a resolution method),
3364 // and we know that we can access the dex cache arrays using a PC-relative load.
3365 kDexCachePcRelative,
3366
3367 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3368 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3369 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3370 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3371 kDexCacheViaMethod,
3372 };
3373
3374 // Determines the location of the code pointer.
3375 enum class CodePtrLocation {
3376 // Recursive call, use local PC-relative call instruction.
3377 kCallSelf,
3378
3379 // Use PC-relative call instruction patched at link time.
3380 // Used for calls within an oat file, boot->boot or app->app.
3381 kCallPCRelative,
3382
3383 // Call to a known target address, embed the direct address in code.
3384 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3385 kCallDirect,
3386
3387 // Call to a target address that will be known at link time, embed the direct
3388 // address in code. If the image is relocatable, emit .patch_oat entry.
3389 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3390 // the image relocatable or not.
3391 kCallDirectWithFixup,
3392
3393 // Use code pointer from the ArtMethod*.
3394 // Used when we don't know the target code. This is also the last-resort-kind used when
3395 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3396 kCallArtMethod,
3397 };
3398
3399 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003400 MethodLoadKind method_load_kind;
3401 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003402 // The method load data holds
3403 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3404 // Note that there are multiple string init methods, each having its own offset.
3405 // - the method address for kDirectAddress
3406 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003407 uint64_t method_load_data;
3408 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003409 };
3410
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003411 HInvokeStaticOrDirect(ArenaAllocator* arena,
3412 uint32_t number_of_arguments,
3413 Primitive::Type return_type,
3414 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003415 uint32_t method_index,
3416 MethodReference target_method,
3417 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003418 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003419 InvokeType invoke_type,
3420 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003421 : HInvoke(arena,
3422 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003423 // There is potentially one extra argument for the HCurrentMethod node, and
3424 // potentially one other if the clinit check is explicit, and potentially
3425 // one other if the method is a string factory.
3426 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3427 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3428 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003429 return_type,
3430 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003431 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003432 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003433 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003434 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003435 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003436 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003437
Vladimir Markodc151b22015-10-15 18:02:30 +01003438 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003439 bool had_current_method_input = HasCurrentMethodInput();
3440 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3441
3442 // Using the current method is the default and once we find a better
3443 // method load kind, we should not go back to using the current method.
3444 DCHECK(had_current_method_input || !needs_current_method_input);
3445
3446 if (had_current_method_input && !needs_current_method_input) {
3447 DCHECK_EQ(InputAt(GetCurrentMethodInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3448 RemoveInputAt(GetCurrentMethodInputIndex());
3449 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003450 dispatch_info_ = dispatch_info;
3451 }
3452
Vladimir Markob554b5a2015-11-06 12:57:55 +00003453 void RemoveInputAt(size_t index);
3454
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003455 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003456 // We access the method via the dex cache so we can't do an implicit null check.
3457 // TODO: for intrinsics we can generate implicit null checks.
3458 return false;
3459 }
3460
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003461 bool CanBeNull() const OVERRIDE {
3462 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3463 }
3464
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003465 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003466 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3467 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3468 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003469 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003470 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003471 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Vladimir Marko58155012015-08-19 12:49:41 +00003472 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003473 bool HasPcRelDexCache() const {
3474 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3475 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003476 bool HasCurrentMethodInput() const {
3477 // This function can be called only after the invoke has been fully initialized by the builder.
3478 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
3479 DCHECK(InputAt(GetCurrentMethodInputIndex())->IsCurrentMethod());
3480 return true;
3481 } else {
3482 DCHECK(InputCount() == GetCurrentMethodInputIndex() ||
3483 !InputAt(GetCurrentMethodInputIndex())->IsCurrentMethod());
3484 return false;
3485 }
3486 }
Vladimir Marko58155012015-08-19 12:49:41 +00003487 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3488 MethodReference GetTargetMethod() const { return target_method_; }
3489
3490 int32_t GetStringInitOffset() const {
3491 DCHECK(IsStringInit());
3492 return dispatch_info_.method_load_data;
3493 }
3494
3495 uint64_t GetMethodAddress() const {
3496 DCHECK(HasMethodAddress());
3497 return dispatch_info_.method_load_data;
3498 }
3499
3500 uint32_t GetDexCacheArrayOffset() const {
3501 DCHECK(HasPcRelDexCache());
3502 return dispatch_info_.method_load_data;
3503 }
3504
3505 uint64_t GetDirectCodePtr() const {
3506 DCHECK(HasDirectCodePtr());
3507 return dispatch_info_.direct_code_ptr;
3508 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003509
Calin Juravle68ad6492015-08-18 17:08:12 +01003510 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3511
Roland Levillain4c0eb422015-04-24 16:43:49 +01003512 // Is this instruction a call to a static method?
3513 bool IsStatic() const {
3514 return GetInvokeType() == kStatic;
3515 }
3516
Roland Levillain3e3d7332015-04-28 11:00:54 +01003517 // Remove the art::HLoadClass instruction set as last input by
3518 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
3519 // the initial art::HClinitCheck instruction (only relevant for
3520 // static calls with explicit clinit check).
3521 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003522 DCHECK(IsStaticWithExplicitClinitCheck());
3523 size_t last_input_index = InputCount() - 1;
3524 HInstruction* last_input = InputAt(last_input_index);
3525 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01003526 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003527 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003528 inputs_.pop_back();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003529 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
3530 DCHECK(IsStaticWithImplicitClinitCheck());
3531 }
3532
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003533 bool IsStringFactoryFor(HFakeString* str) const {
3534 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003535 DCHECK(!HasCurrentMethodInput());
3536 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003537 return InputAt(InputCount() - 1)->AsFakeString() == str;
3538 }
3539
3540 void RemoveFakeStringArgumentAsLastInput() {
3541 DCHECK(IsStringInit());
3542 size_t last_input_index = InputCount() - 1;
3543 HInstruction* last_input = InputAt(last_input_index);
3544 DCHECK(last_input != nullptr);
3545 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3546 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003547 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003548 }
3549
Roland Levillain4c0eb422015-04-24 16:43:49 +01003550 // Is this a call to a static method whose declaring class has an
3551 // explicit intialization check in the graph?
3552 bool IsStaticWithExplicitClinitCheck() const {
3553 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3554 }
3555
3556 // Is this a call to a static method whose declaring class has an
3557 // implicit intialization check requirement?
3558 bool IsStaticWithImplicitClinitCheck() const {
3559 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3560 }
3561
Vladimir Markob554b5a2015-11-06 12:57:55 +00003562 // Does this method load kind need the current method as an input?
3563 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3564 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3565 }
3566
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003567 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003568
Roland Levillain4c0eb422015-04-24 16:43:49 +01003569 protected:
3570 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3571 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3572 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3573 HInstruction* input = input_record.GetInstruction();
3574 // `input` is the last input of a static invoke marked as having
3575 // an explicit clinit check. It must either be:
3576 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3577 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3578 DCHECK(input != nullptr);
3579 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3580 }
3581 return input_record;
3582 }
3583
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003584 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003585 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003586 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003587 // The target method may refer to different dex file or method index than the original
3588 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3589 // in derived class to increase visibility.
3590 MethodReference target_method_;
3591 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003592
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003593 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003594};
3595
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003596class HInvokeVirtual : public HInvoke {
3597 public:
3598 HInvokeVirtual(ArenaAllocator* arena,
3599 uint32_t number_of_arguments,
3600 Primitive::Type return_type,
3601 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003602 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003603 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003604 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003605 vtable_index_(vtable_index) {}
3606
Calin Juravle641547a2015-04-21 22:08:51 +01003607 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003608 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003609 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003610 }
3611
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003612 uint32_t GetVTableIndex() const { return vtable_index_; }
3613
3614 DECLARE_INSTRUCTION(InvokeVirtual);
3615
3616 private:
3617 const uint32_t vtable_index_;
3618
3619 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3620};
3621
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003622class HInvokeInterface : public HInvoke {
3623 public:
3624 HInvokeInterface(ArenaAllocator* arena,
3625 uint32_t number_of_arguments,
3626 Primitive::Type return_type,
3627 uint32_t dex_pc,
3628 uint32_t dex_method_index,
3629 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003630 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003631 imt_index_(imt_index) {}
3632
Calin Juravle641547a2015-04-21 22:08:51 +01003633 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003634 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003635 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003636 }
3637
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003638 uint32_t GetImtIndex() const { return imt_index_; }
3639 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3640
3641 DECLARE_INSTRUCTION(InvokeInterface);
3642
3643 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003644 const uint32_t imt_index_;
3645
3646 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3647};
3648
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003649class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003650 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003651 HNewInstance(HCurrentMethod* current_method,
3652 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003653 uint16_t type_index,
3654 const DexFile& dex_file,
3655 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003656 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003657 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003658 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003659 entrypoint_(entrypoint) {
3660 SetRawInputAt(0, current_method);
3661 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003662
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003663 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003664 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003665
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003666 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003667 bool NeedsEnvironment() const OVERRIDE { return true; }
Andreas Gampe55d02cf2015-10-29 02:59:50 +00003668 // It may throw when called on:
3669 // - interfaces
3670 // - abstract/innaccessible/unknown classes
3671 // TODO: optimize when possible.
3672 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003673
Calin Juravle10e244f2015-01-26 18:54:32 +00003674 bool CanBeNull() const OVERRIDE { return false; }
3675
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003676 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3677
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003678 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003679
3680 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003681 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003682 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003683 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003684
3685 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3686};
3687
Roland Levillain88cb1752014-10-20 16:36:47 +01003688class HNeg : public HUnaryOperation {
3689 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003690 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3691 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003692
Roland Levillain9867bc72015-08-05 10:21:34 +01003693 template <typename T> T Compute(T x) const { return -x; }
3694
3695 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003696 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003697 }
3698 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003699 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003700 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003701
Roland Levillain88cb1752014-10-20 16:36:47 +01003702 DECLARE_INSTRUCTION(Neg);
3703
3704 private:
3705 DISALLOW_COPY_AND_ASSIGN(HNeg);
3706};
3707
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003708class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003709 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003710 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003711 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003712 uint32_t dex_pc,
3713 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003714 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003715 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003716 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003717 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003718 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003719 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003720 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003721 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003722 }
3723
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003724 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003725 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003726
3727 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003728 bool NeedsEnvironment() const OVERRIDE { return true; }
3729
Mingyao Yang0c365e62015-03-31 15:09:29 -07003730 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3731 bool CanThrow() const OVERRIDE { return true; }
3732
Calin Juravle10e244f2015-01-26 18:54:32 +00003733 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003734
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003735 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3736
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003737 DECLARE_INSTRUCTION(NewArray);
3738
3739 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003740 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003741 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003742 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003743
3744 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3745};
3746
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003747class HAdd : public HBinaryOperation {
3748 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003749 HAdd(Primitive::Type result_type,
3750 HInstruction* left,
3751 HInstruction* right,
3752 uint32_t dex_pc = kNoDexPc)
3753 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003754
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003755 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003756
Roland Levillain9867bc72015-08-05 10:21:34 +01003757 template <typename T> T Compute(T x, T y) const { return x + y; }
3758
3759 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003760 return GetBlock()->GetGraph()->GetIntConstant(
3761 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003762 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003763 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003764 return GetBlock()->GetGraph()->GetLongConstant(
3765 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003766 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003767
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003768 DECLARE_INSTRUCTION(Add);
3769
3770 private:
3771 DISALLOW_COPY_AND_ASSIGN(HAdd);
3772};
3773
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003774class HSub : public HBinaryOperation {
3775 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003776 HSub(Primitive::Type result_type,
3777 HInstruction* left,
3778 HInstruction* right,
3779 uint32_t dex_pc = kNoDexPc)
3780 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003781
Roland Levillain9867bc72015-08-05 10:21:34 +01003782 template <typename T> T Compute(T x, T y) const { return x - y; }
3783
3784 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003785 return GetBlock()->GetGraph()->GetIntConstant(
3786 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003787 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003788 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003789 return GetBlock()->GetGraph()->GetLongConstant(
3790 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003791 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003792
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003793 DECLARE_INSTRUCTION(Sub);
3794
3795 private:
3796 DISALLOW_COPY_AND_ASSIGN(HSub);
3797};
3798
Calin Juravle34bacdf2014-10-07 20:23:36 +01003799class HMul : public HBinaryOperation {
3800 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003801 HMul(Primitive::Type result_type,
3802 HInstruction* left,
3803 HInstruction* right,
3804 uint32_t dex_pc = kNoDexPc)
3805 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003806
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003807 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003808
Roland Levillain9867bc72015-08-05 10:21:34 +01003809 template <typename T> T Compute(T x, T y) const { return x * y; }
3810
3811 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003812 return GetBlock()->GetGraph()->GetIntConstant(
3813 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003814 }
3815 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003816 return GetBlock()->GetGraph()->GetLongConstant(
3817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003818 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003819
3820 DECLARE_INSTRUCTION(Mul);
3821
3822 private:
3823 DISALLOW_COPY_AND_ASSIGN(HMul);
3824};
3825
Calin Juravle7c4954d2014-10-28 16:57:40 +00003826class HDiv : public HBinaryOperation {
3827 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003828 HDiv(Primitive::Type result_type,
3829 HInstruction* left,
3830 HInstruction* right,
3831 uint32_t dex_pc)
3832 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003833
Roland Levillain9867bc72015-08-05 10:21:34 +01003834 template <typename T>
3835 T Compute(T x, T y) const {
3836 // Our graph structure ensures we never have 0 for `y` during
3837 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003838 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003839 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003840 return (y == -1) ? -x : x / y;
3841 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003842
Roland Levillain9867bc72015-08-05 10:21:34 +01003843 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003844 return GetBlock()->GetGraph()->GetIntConstant(
3845 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003846 }
3847 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003848 return GetBlock()->GetGraph()->GetLongConstant(
3849 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003850 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003851
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003852 static SideEffects SideEffectsForArchRuntimeCalls() {
3853 // The generated code can use a runtime call.
3854 return SideEffects::CanTriggerGC();
3855 }
3856
Calin Juravle7c4954d2014-10-28 16:57:40 +00003857 DECLARE_INSTRUCTION(Div);
3858
3859 private:
3860 DISALLOW_COPY_AND_ASSIGN(HDiv);
3861};
3862
Calin Juravlebacfec32014-11-14 15:54:36 +00003863class HRem : public HBinaryOperation {
3864 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003865 HRem(Primitive::Type result_type,
3866 HInstruction* left,
3867 HInstruction* right,
3868 uint32_t dex_pc)
3869 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003870
Roland Levillain9867bc72015-08-05 10:21:34 +01003871 template <typename T>
3872 T Compute(T x, T y) const {
3873 // Our graph structure ensures we never have 0 for `y` during
3874 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003875 DCHECK_NE(y, 0);
3876 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3877 return (y == -1) ? 0 : x % y;
3878 }
3879
Roland Levillain9867bc72015-08-05 10:21:34 +01003880 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003881 return GetBlock()->GetGraph()->GetIntConstant(
3882 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003883 }
3884 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003885 return GetBlock()->GetGraph()->GetLongConstant(
3886 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003887 }
3888
Calin Juravlebacfec32014-11-14 15:54:36 +00003889
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003890 static SideEffects SideEffectsForArchRuntimeCalls() {
3891 return SideEffects::CanTriggerGC();
3892 }
3893
Calin Juravlebacfec32014-11-14 15:54:36 +00003894 DECLARE_INSTRUCTION(Rem);
3895
3896 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003897 DISALLOW_COPY_AND_ASSIGN(HRem);
3898};
3899
Calin Juravled0d48522014-11-04 16:40:20 +00003900class HDivZeroCheck : public HExpression<1> {
3901 public:
3902 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003903 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003904 SetRawInputAt(0, value);
3905 }
3906
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003907 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3908
Calin Juravled0d48522014-11-04 16:40:20 +00003909 bool CanBeMoved() const OVERRIDE { return true; }
3910
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003911 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003912 return true;
3913 }
3914
3915 bool NeedsEnvironment() const OVERRIDE { return true; }
3916 bool CanThrow() const OVERRIDE { return true; }
3917
Calin Juravled0d48522014-11-04 16:40:20 +00003918 DECLARE_INSTRUCTION(DivZeroCheck);
3919
3920 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003921 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3922};
3923
Calin Juravle9aec02f2014-11-18 23:06:35 +00003924class HShl : public HBinaryOperation {
3925 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003926 HShl(Primitive::Type result_type,
3927 HInstruction* left,
3928 HInstruction* right,
3929 uint32_t dex_pc = kNoDexPc)
3930 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003931
Roland Levillain9867bc72015-08-05 10:21:34 +01003932 template <typename T, typename U, typename V>
3933 T Compute(T x, U y, V max_shift_value) const {
3934 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3935 "V is not the unsigned integer type corresponding to T");
3936 return x << (y & max_shift_value);
3937 }
3938
3939 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3940 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003941 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003942 }
3943 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3944 // case is handled as `x << static_cast<int>(y)`.
3945 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3946 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003947 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003948 }
3949 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3950 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003951 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003952 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003953
3954 DECLARE_INSTRUCTION(Shl);
3955
3956 private:
3957 DISALLOW_COPY_AND_ASSIGN(HShl);
3958};
3959
3960class HShr : public HBinaryOperation {
3961 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003962 HShr(Primitive::Type result_type,
3963 HInstruction* left,
3964 HInstruction* right,
3965 uint32_t dex_pc = kNoDexPc)
3966 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003967
Roland Levillain9867bc72015-08-05 10:21:34 +01003968 template <typename T, typename U, typename V>
3969 T Compute(T x, U y, V max_shift_value) const {
3970 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3971 "V is not the unsigned integer type corresponding to T");
3972 return x >> (y & max_shift_value);
3973 }
3974
3975 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3976 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003977 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003978 }
3979 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3980 // case is handled as `x >> static_cast<int>(y)`.
3981 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3982 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003983 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003984 }
3985 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3986 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003987 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003988 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003989
3990 DECLARE_INSTRUCTION(Shr);
3991
3992 private:
3993 DISALLOW_COPY_AND_ASSIGN(HShr);
3994};
3995
3996class HUShr : public HBinaryOperation {
3997 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003998 HUShr(Primitive::Type result_type,
3999 HInstruction* left,
4000 HInstruction* right,
4001 uint32_t dex_pc = kNoDexPc)
4002 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004003
Roland Levillain9867bc72015-08-05 10:21:34 +01004004 template <typename T, typename U, typename V>
4005 T Compute(T x, U y, V max_shift_value) const {
4006 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4007 "V is not the unsigned integer type corresponding to T");
4008 V ux = static_cast<V>(x);
4009 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004010 }
4011
Roland Levillain9867bc72015-08-05 10:21:34 +01004012 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4013 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004014 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004015 }
4016 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4017 // case is handled as `x >>> static_cast<int>(y)`.
4018 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4019 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004020 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004021 }
4022 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4023 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004024 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004025 }
4026
4027 DECLARE_INSTRUCTION(UShr);
4028
4029 private:
4030 DISALLOW_COPY_AND_ASSIGN(HUShr);
4031};
4032
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004033class HAnd : public HBinaryOperation {
4034 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004035 HAnd(Primitive::Type result_type,
4036 HInstruction* left,
4037 HInstruction* right,
4038 uint32_t dex_pc = kNoDexPc)
4039 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004040
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004041 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004042
Roland Levillain9867bc72015-08-05 10:21:34 +01004043 template <typename T, typename U>
4044 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4045
4046 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004047 return GetBlock()->GetGraph()->GetIntConstant(
4048 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004049 }
4050 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004051 return GetBlock()->GetGraph()->GetLongConstant(
4052 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004053 }
4054 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004055 return GetBlock()->GetGraph()->GetLongConstant(
4056 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004057 }
4058 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004059 return GetBlock()->GetGraph()->GetLongConstant(
4060 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004061 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004062
4063 DECLARE_INSTRUCTION(And);
4064
4065 private:
4066 DISALLOW_COPY_AND_ASSIGN(HAnd);
4067};
4068
4069class HOr : public HBinaryOperation {
4070 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004071 HOr(Primitive::Type result_type,
4072 HInstruction* left,
4073 HInstruction* right,
4074 uint32_t dex_pc = kNoDexPc)
4075 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004076
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004077 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004078
Roland Levillain9867bc72015-08-05 10:21:34 +01004079 template <typename T, typename U>
4080 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4081
4082 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004083 return GetBlock()->GetGraph()->GetIntConstant(
4084 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004085 }
4086 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004087 return GetBlock()->GetGraph()->GetLongConstant(
4088 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004089 }
4090 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004091 return GetBlock()->GetGraph()->GetLongConstant(
4092 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004093 }
4094 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004095 return GetBlock()->GetGraph()->GetLongConstant(
4096 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004097 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004098
4099 DECLARE_INSTRUCTION(Or);
4100
4101 private:
4102 DISALLOW_COPY_AND_ASSIGN(HOr);
4103};
4104
4105class HXor : public HBinaryOperation {
4106 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004107 HXor(Primitive::Type result_type,
4108 HInstruction* left,
4109 HInstruction* right,
4110 uint32_t dex_pc = kNoDexPc)
4111 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004112
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004113 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004114
Roland Levillain9867bc72015-08-05 10:21:34 +01004115 template <typename T, typename U>
4116 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4117
4118 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004119 return GetBlock()->GetGraph()->GetIntConstant(
4120 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004121 }
4122 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004123 return GetBlock()->GetGraph()->GetLongConstant(
4124 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004125 }
4126 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004127 return GetBlock()->GetGraph()->GetLongConstant(
4128 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004129 }
4130 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004131 return GetBlock()->GetGraph()->GetLongConstant(
4132 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004133 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004134
4135 DECLARE_INSTRUCTION(Xor);
4136
4137 private:
4138 DISALLOW_COPY_AND_ASSIGN(HXor);
4139};
4140
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004141// The value of a parameter in this method. Its location depends on
4142// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004143class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004144 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004145 HParameterValue(const DexFile& dex_file,
4146 uint16_t type_index,
4147 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004148 Primitive::Type parameter_type,
4149 bool is_this = false)
4150 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004151 dex_file_(dex_file),
4152 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004153 index_(index),
4154 is_this_(is_this),
4155 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004156
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004157 const DexFile& GetDexFile() const { return dex_file_; }
4158 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004159 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004160 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004161
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004162 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4163 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004164
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004165 DECLARE_INSTRUCTION(ParameterValue);
4166
4167 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004168 const DexFile& dex_file_;
4169 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004170 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004171 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004172 const uint8_t index_;
4173
Calin Juravle10e244f2015-01-26 18:54:32 +00004174 // Whether or not the parameter value corresponds to 'this' argument.
4175 const bool is_this_;
4176
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004177 bool can_be_null_;
4178
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004179 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4180};
4181
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004182class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004183 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004184 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4185 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004186
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004187 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004188 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004189 return true;
4190 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004191
Roland Levillain9867bc72015-08-05 10:21:34 +01004192 template <typename T> T Compute(T x) const { return ~x; }
4193
4194 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004195 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004196 }
4197 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004198 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004199 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004200
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004201 DECLARE_INSTRUCTION(Not);
4202
4203 private:
4204 DISALLOW_COPY_AND_ASSIGN(HNot);
4205};
4206
David Brazdil66d126e2015-04-03 16:02:44 +01004207class HBooleanNot : public HUnaryOperation {
4208 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004209 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4210 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004211
4212 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004213 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004214 return true;
4215 }
4216
Roland Levillain9867bc72015-08-05 10:21:34 +01004217 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004218 DCHECK(IsUint<1>(x));
4219 return !x;
4220 }
4221
Roland Levillain9867bc72015-08-05 10:21:34 +01004222 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004223 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004224 }
4225 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4226 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004227 UNREACHABLE();
4228 }
4229
4230 DECLARE_INSTRUCTION(BooleanNot);
4231
4232 private:
4233 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4234};
4235
Roland Levillaindff1f282014-11-05 14:15:05 +00004236class HTypeConversion : public HExpression<1> {
4237 public:
4238 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004239 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004240 : HExpression(result_type,
4241 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4242 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004243 SetRawInputAt(0, input);
4244 DCHECK_NE(input->GetType(), result_type);
4245 }
4246
4247 HInstruction* GetInput() const { return InputAt(0); }
4248 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4249 Primitive::Type GetResultType() const { return GetType(); }
4250
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004251 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004252 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004253
Roland Levillaindff1f282014-11-05 14:15:05 +00004254 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004255 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004256
Mark Mendelle82549b2015-05-06 10:55:34 -04004257 // Try to statically evaluate the conversion and return a HConstant
4258 // containing the result. If the input cannot be converted, return nullptr.
4259 HConstant* TryStaticEvaluation() const;
4260
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004261 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4262 Primitive::Type result_type) {
4263 // Some architectures may not require the 'GC' side effects, but at this point
4264 // in the compilation process we do not know what architecture we will
4265 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004266 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4267 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004268 return SideEffects::CanTriggerGC();
4269 }
4270 return SideEffects::None();
4271 }
4272
Roland Levillaindff1f282014-11-05 14:15:05 +00004273 DECLARE_INSTRUCTION(TypeConversion);
4274
4275 private:
4276 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4277};
4278
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004279static constexpr uint32_t kNoRegNumber = -1;
4280
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004281class HPhi : public HInstruction {
4282 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004283 HPhi(ArenaAllocator* arena,
4284 uint32_t reg_number,
4285 size_t number_of_inputs,
4286 Primitive::Type type,
4287 uint32_t dex_pc = kNoDexPc)
4288 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004289 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004290 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004291 type_(type),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004292 is_live_(false),
Calin Juravle10e244f2015-01-26 18:54:32 +00004293 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004294 }
4295
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004296 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4297 static Primitive::Type ToPhiType(Primitive::Type type) {
4298 switch (type) {
4299 case Primitive::kPrimBoolean:
4300 case Primitive::kPrimByte:
4301 case Primitive::kPrimShort:
4302 case Primitive::kPrimChar:
4303 return Primitive::kPrimInt;
4304 default:
4305 return type;
4306 }
4307 }
4308
David Brazdilffee3d32015-07-06 11:48:53 +01004309 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4310
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004311 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004312
4313 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004314 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004315
Calin Juravle10e244f2015-01-26 18:54:32 +00004316 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004317 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004318
Calin Juravle10e244f2015-01-26 18:54:32 +00004319 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4320 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4321
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004322 uint32_t GetRegNumber() const { return reg_number_; }
4323
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004324 void SetDead() { is_live_ = false; }
4325 void SetLive() { is_live_ = true; }
4326 bool IsDead() const { return !is_live_; }
4327 bool IsLive() const { return is_live_; }
4328
David Brazdil77a48ae2015-09-15 12:34:04 +00004329 bool IsVRegEquivalentOf(HInstruction* other) const {
4330 return other != nullptr
4331 && other->IsPhi()
4332 && other->AsPhi()->GetBlock() == GetBlock()
4333 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4334 }
4335
Calin Juravlea4f88312015-04-16 12:57:19 +01004336 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4337 // An equivalent phi is a phi having the same dex register and type.
4338 // It assumes that phis with the same dex register are adjacent.
4339 HPhi* GetNextEquivalentPhiWithSameType() {
4340 HInstruction* next = GetNext();
4341 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4342 if (next->GetType() == GetType()) {
4343 return next->AsPhi();
4344 }
4345 next = next->GetNext();
4346 }
4347 return nullptr;
4348 }
4349
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004350 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004351
David Brazdil1abb4192015-02-17 18:33:36 +00004352 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004353 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004354 return inputs_[index];
4355 }
David Brazdil1abb4192015-02-17 18:33:36 +00004356
4357 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004358 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004359 }
4360
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004361 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004362 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004363 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004364 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004365 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004366 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004367
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004368 DISALLOW_COPY_AND_ASSIGN(HPhi);
4369};
4370
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004371class HNullCheck : public HExpression<1> {
4372 public:
4373 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004374 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004375 SetRawInputAt(0, value);
4376 }
4377
Calin Juravle10e244f2015-01-26 18:54:32 +00004378 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004379 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004380 return true;
4381 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004382
Calin Juravle10e244f2015-01-26 18:54:32 +00004383 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004384
Calin Juravle10e244f2015-01-26 18:54:32 +00004385 bool CanThrow() const OVERRIDE { return true; }
4386
4387 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004388
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004389
4390 DECLARE_INSTRUCTION(NullCheck);
4391
4392 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004393 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4394};
4395
4396class FieldInfo : public ValueObject {
4397 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004398 FieldInfo(MemberOffset field_offset,
4399 Primitive::Type field_type,
4400 bool is_volatile,
4401 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004402 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004403 const DexFile& dex_file,
4404 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004405 : field_offset_(field_offset),
4406 field_type_(field_type),
4407 is_volatile_(is_volatile),
4408 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004409 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004410 dex_file_(dex_file),
4411 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004412
4413 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004414 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004415 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004416 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004417 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004418 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004419 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004420
4421 private:
4422 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004423 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004424 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004425 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004426 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004427 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004428 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004429};
4430
4431class HInstanceFieldGet : public HExpression<1> {
4432 public:
4433 HInstanceFieldGet(HInstruction* value,
4434 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004435 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004436 bool is_volatile,
4437 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004438 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004439 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004440 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004441 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004442 : HExpression(field_type,
4443 SideEffects::FieldReadOfType(field_type, is_volatile),
4444 dex_pc),
4445 field_info_(field_offset,
4446 field_type,
4447 is_volatile,
4448 field_idx,
4449 declaring_class_def_index,
4450 dex_file,
4451 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004452 SetRawInputAt(0, value);
4453 }
4454
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004455 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004456
4457 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4458 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4459 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004460 }
4461
Calin Juravle641547a2015-04-21 22:08:51 +01004462 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4463 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004464 }
4465
4466 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004467 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4468 }
4469
Calin Juravle52c48962014-12-16 17:02:57 +00004470 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004471 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004472 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004473 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004474
4475 DECLARE_INSTRUCTION(InstanceFieldGet);
4476
4477 private:
4478 const FieldInfo field_info_;
4479
4480 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4481};
4482
4483class HInstanceFieldSet : public HTemplateInstruction<2> {
4484 public:
4485 HInstanceFieldSet(HInstruction* object,
4486 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004487 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004488 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004489 bool is_volatile,
4490 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004491 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004492 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004493 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004494 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004495 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4496 dex_pc),
4497 field_info_(field_offset,
4498 field_type,
4499 is_volatile,
4500 field_idx,
4501 declaring_class_def_index,
4502 dex_file,
4503 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004504 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004505 SetRawInputAt(0, object);
4506 SetRawInputAt(1, value);
4507 }
4508
Calin Juravle641547a2015-04-21 22:08:51 +01004509 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4510 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004511 }
4512
Calin Juravle52c48962014-12-16 17:02:57 +00004513 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004514 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004515 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004516 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004517 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004518 bool GetValueCanBeNull() const { return value_can_be_null_; }
4519 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004520
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004521 DECLARE_INSTRUCTION(InstanceFieldSet);
4522
4523 private:
4524 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004525 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004526
4527 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4528};
4529
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004530class HArrayGet : public HExpression<2> {
4531 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004532 HArrayGet(HInstruction* array,
4533 HInstruction* index,
4534 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004535 uint32_t dex_pc,
4536 SideEffects additional_side_effects = SideEffects::None())
4537 : HExpression(type,
4538 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004539 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004540 SetRawInputAt(0, array);
4541 SetRawInputAt(1, index);
4542 }
4543
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004544 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004545 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004546 return true;
4547 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004548 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004549 // TODO: We can be smarter here.
4550 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4551 // which generates the implicit null check. There are cases when these can be removed
4552 // to produce better code. If we ever add optimizations to do so we should allow an
4553 // implicit check here (as long as the address falls in the first page).
4554 return false;
4555 }
4556
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004557 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004558
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004559 HInstruction* GetArray() const { return InputAt(0); }
4560 HInstruction* GetIndex() const { return InputAt(1); }
4561
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004562 DECLARE_INSTRUCTION(ArrayGet);
4563
4564 private:
4565 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4566};
4567
4568class HArraySet : public HTemplateInstruction<3> {
4569 public:
4570 HArraySet(HInstruction* array,
4571 HInstruction* index,
4572 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004573 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004574 uint32_t dex_pc,
4575 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004576 : HTemplateInstruction(
4577 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004578 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4579 additional_side_effects),
4580 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004581 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004582 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004583 value_can_be_null_(true),
4584 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004585 SetRawInputAt(0, array);
4586 SetRawInputAt(1, index);
4587 SetRawInputAt(2, value);
4588 }
4589
Calin Juravle77520bc2015-01-12 18:45:46 +00004590 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004591 // We currently always call a runtime method to catch array store
4592 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004593 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004594 }
4595
Mingyao Yang81014cb2015-06-02 03:16:27 -07004596 // Can throw ArrayStoreException.
4597 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4598
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004599 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004600 // TODO: Same as for ArrayGet.
4601 return false;
4602 }
4603
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004604 void ClearNeedsTypeCheck() {
4605 needs_type_check_ = false;
4606 }
4607
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004608 void ClearValueCanBeNull() {
4609 value_can_be_null_ = false;
4610 }
4611
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004612 void SetStaticTypeOfArrayIsObjectArray() {
4613 static_type_of_array_is_object_array_ = true;
4614 }
4615
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004616 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004617 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004618 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004619
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004620 HInstruction* GetArray() const { return InputAt(0); }
4621 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004622 HInstruction* GetValue() const { return InputAt(2); }
4623
4624 Primitive::Type GetComponentType() const {
4625 // The Dex format does not type floating point index operations. Since the
4626 // `expected_component_type_` is set during building and can therefore not
4627 // be correct, we also check what is the value type. If it is a floating
4628 // point type, we must use that type.
4629 Primitive::Type value_type = GetValue()->GetType();
4630 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4631 ? value_type
4632 : expected_component_type_;
4633 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004634
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004635 Primitive::Type GetRawExpectedComponentType() const {
4636 return expected_component_type_;
4637 }
4638
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004639 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4640 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4641 }
4642
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004643 DECLARE_INSTRUCTION(ArraySet);
4644
4645 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004646 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004647 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004648 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004649 // Cached information for the reference_type_info_ so that codegen
4650 // does not need to inspect the static type.
4651 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004652
4653 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4654};
4655
4656class HArrayLength : public HExpression<1> {
4657 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004658 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004659 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004660 // Note that arrays do not change length, so the instruction does not
4661 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004662 SetRawInputAt(0, array);
4663 }
4664
Calin Juravle77520bc2015-01-12 18:45:46 +00004665 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004666 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004667 return true;
4668 }
Calin Juravle641547a2015-04-21 22:08:51 +01004669 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4670 return obj == InputAt(0);
4671 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004672
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004673 DECLARE_INSTRUCTION(ArrayLength);
4674
4675 private:
4676 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4677};
4678
4679class HBoundsCheck : public HExpression<2> {
4680 public:
4681 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004682 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004683 DCHECK(index->GetType() == Primitive::kPrimInt);
4684 SetRawInputAt(0, index);
4685 SetRawInputAt(1, length);
4686 }
4687
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004688 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004689 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004690 return true;
4691 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004692
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004693 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004694
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004695 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004696
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004697 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004698
4699 DECLARE_INSTRUCTION(BoundsCheck);
4700
4701 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004702 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4703};
4704
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004705/**
4706 * Some DEX instructions are folded into multiple HInstructions that need
4707 * to stay live until the last HInstruction. This class
4708 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004709 * HInstruction stays live. `index` represents the stack location index of the
4710 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004711 */
4712class HTemporary : public HTemplateInstruction<0> {
4713 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004714 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4715 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004716
4717 size_t GetIndex() const { return index_; }
4718
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004719 Primitive::Type GetType() const OVERRIDE {
4720 // The previous instruction is the one that will be stored in the temporary location.
4721 DCHECK(GetPrevious() != nullptr);
4722 return GetPrevious()->GetType();
4723 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004724
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004725 DECLARE_INSTRUCTION(Temporary);
4726
4727 private:
4728 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004729 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4730};
4731
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004732class HSuspendCheck : public HTemplateInstruction<0> {
4733 public:
4734 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004735 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004736
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004737 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004738 return true;
4739 }
4740
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004741 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4742 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004743
4744 DECLARE_INSTRUCTION(SuspendCheck);
4745
4746 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004747 // Only used for code generation, in order to share the same slow path between back edges
4748 // of a same loop.
4749 SlowPathCode* slow_path_;
4750
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004751 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4752};
4753
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004754/**
4755 * Instruction to load a Class object.
4756 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004757class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004758 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004759 HLoadClass(HCurrentMethod* current_method,
4760 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004761 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004762 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004763 uint32_t dex_pc,
4764 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004765 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004766 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004767 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004768 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004769 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004770 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004771 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004772 // Referrers class should not need access check. We never inline unverified
4773 // methods so we can't possibly end up in this situation.
4774 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004775 SetRawInputAt(0, current_method);
4776 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004777
4778 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004779
4780 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004781 // Note that we don't need to test for generate_clinit_check_.
4782 // Whether or not we need to generate the clinit check is processed in
4783 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004784 return other->AsLoadClass()->type_index_ == type_index_ &&
4785 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004786 }
4787
4788 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4789
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004790 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004791 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004792 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004793
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004794 bool NeedsEnvironment() const OVERRIDE {
4795 // Will call runtime and load the class if the class is not loaded yet.
4796 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004797 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004798 }
4799
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004800 bool MustGenerateClinitCheck() const {
4801 return generate_clinit_check_;
4802 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004803 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004804 // The entrypoint the code generator is going to call does not do
4805 // clinit of the class.
4806 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004807 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004808 }
4809
4810 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004811 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4812 }
4813
4814 bool NeedsAccessCheck() const {
4815 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004816 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004817
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004818 bool CanThrow() const OVERRIDE {
4819 // May call runtime and and therefore can throw.
4820 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004821 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004822 }
4823
Calin Juravleacf735c2015-02-12 15:25:22 +00004824 ReferenceTypeInfo GetLoadedClassRTI() {
4825 return loaded_class_rti_;
4826 }
4827
4828 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4829 // Make sure we only set exact types (the loaded class should never be merged).
4830 DCHECK(rti.IsExact());
4831 loaded_class_rti_ = rti;
4832 }
4833
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004834 const DexFile& GetDexFile() { return dex_file_; }
4835
Vladimir Markodc151b22015-10-15 18:02:30 +01004836 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004837
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004838 static SideEffects SideEffectsForArchRuntimeCalls() {
4839 return SideEffects::CanTriggerGC();
4840 }
4841
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004842 DECLARE_INSTRUCTION(LoadClass);
4843
4844 private:
4845 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004846 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004847 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004848 // Whether this instruction must generate the initialization check.
4849 // Used for code generation.
4850 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004851 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004852
Calin Juravleacf735c2015-02-12 15:25:22 +00004853 ReferenceTypeInfo loaded_class_rti_;
4854
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004855 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4856};
4857
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004858class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004859 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004860 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004861 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4862 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004863 SetRawInputAt(0, current_method);
4864 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004865
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004866 bool CanBeMoved() const OVERRIDE { return true; }
4867
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004868 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4869 return other->AsLoadString()->string_index_ == string_index_;
4870 }
4871
4872 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4873
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004874 uint32_t GetStringIndex() const { return string_index_; }
4875
4876 // TODO: Can we deopt or debug when we resolve a string?
4877 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004878 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004879 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004880
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004881 static SideEffects SideEffectsForArchRuntimeCalls() {
4882 return SideEffects::CanTriggerGC();
4883 }
4884
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004885 DECLARE_INSTRUCTION(LoadString);
4886
4887 private:
4888 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004889
4890 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4891};
4892
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004893/**
4894 * Performs an initialization check on its Class object input.
4895 */
4896class HClinitCheck : public HExpression<1> {
4897 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004898 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004899 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004900 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004901 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4902 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004903 SetRawInputAt(0, constant);
4904 }
4905
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004906 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004907 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004908 return true;
4909 }
4910
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004911 bool NeedsEnvironment() const OVERRIDE {
4912 // May call runtime to initialize the class.
4913 return true;
4914 }
4915
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004916
4917 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4918
4919 DECLARE_INSTRUCTION(ClinitCheck);
4920
4921 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004922 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4923};
4924
4925class HStaticFieldGet : public HExpression<1> {
4926 public:
4927 HStaticFieldGet(HInstruction* cls,
4928 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004929 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004930 bool is_volatile,
4931 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004932 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004933 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004934 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004935 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004936 : HExpression(field_type,
4937 SideEffects::FieldReadOfType(field_type, is_volatile),
4938 dex_pc),
4939 field_info_(field_offset,
4940 field_type,
4941 is_volatile,
4942 field_idx,
4943 declaring_class_def_index,
4944 dex_file,
4945 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004946 SetRawInputAt(0, cls);
4947 }
4948
Calin Juravle52c48962014-12-16 17:02:57 +00004949
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004950 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004951
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004952 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004953 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4954 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004955 }
4956
4957 size_t ComputeHashCode() const OVERRIDE {
4958 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4959 }
4960
Calin Juravle52c48962014-12-16 17:02:57 +00004961 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004962 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4963 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004964 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004965
4966 DECLARE_INSTRUCTION(StaticFieldGet);
4967
4968 private:
4969 const FieldInfo field_info_;
4970
4971 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4972};
4973
4974class HStaticFieldSet : public HTemplateInstruction<2> {
4975 public:
4976 HStaticFieldSet(HInstruction* cls,
4977 HInstruction* value,
4978 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004979 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004980 bool is_volatile,
4981 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004982 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004983 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004984 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004985 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004986 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4987 dex_pc),
4988 field_info_(field_offset,
4989 field_type,
4990 is_volatile,
4991 field_idx,
4992 declaring_class_def_index,
4993 dex_file,
4994 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004995 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004996 SetRawInputAt(0, cls);
4997 SetRawInputAt(1, value);
4998 }
4999
Calin Juravle52c48962014-12-16 17:02:57 +00005000 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005001 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5002 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005003 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005004
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005005 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005006 bool GetValueCanBeNull() const { return value_can_be_null_; }
5007 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005008
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005009 DECLARE_INSTRUCTION(StaticFieldSet);
5010
5011 private:
5012 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005013 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005014
5015 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5016};
5017
Calin Juravlee460d1d2015-09-29 04:52:17 +01005018class HUnresolvedInstanceFieldGet : public HExpression<1> {
5019 public:
5020 HUnresolvedInstanceFieldGet(HInstruction* obj,
5021 Primitive::Type field_type,
5022 uint32_t field_index,
5023 uint32_t dex_pc)
5024 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5025 field_index_(field_index) {
5026 SetRawInputAt(0, obj);
5027 }
5028
5029 bool NeedsEnvironment() const OVERRIDE { return true; }
5030 bool CanThrow() const OVERRIDE { return true; }
5031
5032 Primitive::Type GetFieldType() const { return GetType(); }
5033 uint32_t GetFieldIndex() const { return field_index_; }
5034
5035 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5036
5037 private:
5038 const uint32_t field_index_;
5039
5040 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5041};
5042
5043class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5044 public:
5045 HUnresolvedInstanceFieldSet(HInstruction* obj,
5046 HInstruction* value,
5047 Primitive::Type field_type,
5048 uint32_t field_index,
5049 uint32_t dex_pc)
5050 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5051 field_type_(field_type),
5052 field_index_(field_index) {
5053 DCHECK_EQ(field_type, value->GetType());
5054 SetRawInputAt(0, obj);
5055 SetRawInputAt(1, value);
5056 }
5057
5058 bool NeedsEnvironment() const OVERRIDE { return true; }
5059 bool CanThrow() const OVERRIDE { return true; }
5060
5061 Primitive::Type GetFieldType() const { return field_type_; }
5062 uint32_t GetFieldIndex() const { return field_index_; }
5063
5064 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5065
5066 private:
5067 const Primitive::Type field_type_;
5068 const uint32_t field_index_;
5069
5070 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5071};
5072
5073class HUnresolvedStaticFieldGet : public HExpression<0> {
5074 public:
5075 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5076 uint32_t field_index,
5077 uint32_t dex_pc)
5078 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5079 field_index_(field_index) {
5080 }
5081
5082 bool NeedsEnvironment() const OVERRIDE { return true; }
5083 bool CanThrow() const OVERRIDE { return true; }
5084
5085 Primitive::Type GetFieldType() const { return GetType(); }
5086 uint32_t GetFieldIndex() const { return field_index_; }
5087
5088 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5089
5090 private:
5091 const uint32_t field_index_;
5092
5093 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5094};
5095
5096class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5097 public:
5098 HUnresolvedStaticFieldSet(HInstruction* value,
5099 Primitive::Type field_type,
5100 uint32_t field_index,
5101 uint32_t dex_pc)
5102 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5103 field_type_(field_type),
5104 field_index_(field_index) {
5105 DCHECK_EQ(field_type, value->GetType());
5106 SetRawInputAt(0, value);
5107 }
5108
5109 bool NeedsEnvironment() const OVERRIDE { return true; }
5110 bool CanThrow() const OVERRIDE { return true; }
5111
5112 Primitive::Type GetFieldType() const { return field_type_; }
5113 uint32_t GetFieldIndex() const { return field_index_; }
5114
5115 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5116
5117 private:
5118 const Primitive::Type field_type_;
5119 const uint32_t field_index_;
5120
5121 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5122};
5123
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005124// Implement the move-exception DEX instruction.
5125class HLoadException : public HExpression<0> {
5126 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005127 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5128 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005129
David Brazdilbbd733e2015-08-18 17:48:17 +01005130 bool CanBeNull() const OVERRIDE { return false; }
5131
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005132 DECLARE_INSTRUCTION(LoadException);
5133
5134 private:
5135 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5136};
5137
David Brazdilcb1c0552015-08-04 16:22:25 +01005138// Implicit part of move-exception which clears thread-local exception storage.
5139// Must not be removed because the runtime expects the TLS to get cleared.
5140class HClearException : public HTemplateInstruction<0> {
5141 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005142 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5143 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005144
5145 DECLARE_INSTRUCTION(ClearException);
5146
5147 private:
5148 DISALLOW_COPY_AND_ASSIGN(HClearException);
5149};
5150
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005151class HThrow : public HTemplateInstruction<1> {
5152 public:
5153 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005154 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005155 SetRawInputAt(0, exception);
5156 }
5157
5158 bool IsControlFlow() const OVERRIDE { return true; }
5159
5160 bool NeedsEnvironment() const OVERRIDE { return true; }
5161
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005162 bool CanThrow() const OVERRIDE { return true; }
5163
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005164
5165 DECLARE_INSTRUCTION(Throw);
5166
5167 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005168 DISALLOW_COPY_AND_ASSIGN(HThrow);
5169};
5170
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005171/**
5172 * Implementation strategies for the code generator of a HInstanceOf
5173 * or `HCheckCast`.
5174 */
5175enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005176 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005177 kExactCheck, // Can do a single class compare.
5178 kClassHierarchyCheck, // Can just walk the super class chain.
5179 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5180 kInterfaceCheck, // No optimization yet when checking against an interface.
5181 kArrayObjectCheck, // Can just check if the array is not primitive.
5182 kArrayCheck // No optimization yet when checking against a generic array.
5183};
5184
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005185class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005186 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005187 HInstanceOf(HInstruction* object,
5188 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005189 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005190 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005191 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005192 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005193 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005194 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005195 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005196 SetRawInputAt(0, object);
5197 SetRawInputAt(1, constant);
5198 }
5199
5200 bool CanBeMoved() const OVERRIDE { return true; }
5201
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005202 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005203 return true;
5204 }
5205
5206 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005207 return false;
5208 }
5209
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005210 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5211
5212 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005213
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005214 // Used only in code generation.
5215 bool MustDoNullCheck() const { return must_do_null_check_; }
5216 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5217
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005218 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5219 return (check_kind == TypeCheckKind::kExactCheck)
5220 ? SideEffects::None()
5221 // Mips currently does runtime calls for any other checks.
5222 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005223 }
5224
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005225 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005226
5227 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005228 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005229 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005230
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005231 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5232};
5233
Calin Juravleb1498f62015-02-16 13:13:29 +00005234class HBoundType : public HExpression<1> {
5235 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005236 // Constructs an HBoundType with the given upper_bound.
5237 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005238 HBoundType(HInstruction* input,
5239 ReferenceTypeInfo upper_bound,
5240 bool upper_can_be_null,
5241 uint32_t dex_pc = kNoDexPc)
5242 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005243 upper_bound_(upper_bound),
5244 upper_can_be_null_(upper_can_be_null),
5245 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005246 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005247 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005248 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005249 }
5250
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005251 // GetUpper* should only be used in reference type propagation.
5252 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5253 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005254
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005255 void SetCanBeNull(bool can_be_null) {
5256 DCHECK(upper_can_be_null_ || !can_be_null);
5257 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005258 }
5259
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005260 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5261
Calin Juravleb1498f62015-02-16 13:13:29 +00005262 DECLARE_INSTRUCTION(BoundType);
5263
5264 private:
5265 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005266 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5267 // It is used to bound the type in cases like:
5268 // if (x instanceof ClassX) {
5269 // // uper_bound_ will be ClassX
5270 // }
5271 const ReferenceTypeInfo upper_bound_;
5272 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5273 // is false then can_be_null_ cannot be true).
5274 const bool upper_can_be_null_;
5275 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005276
5277 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5278};
5279
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005280class HCheckCast : public HTemplateInstruction<2> {
5281 public:
5282 HCheckCast(HInstruction* object,
5283 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005284 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005285 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005286 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005287 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005288 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005289 SetRawInputAt(0, object);
5290 SetRawInputAt(1, constant);
5291 }
5292
5293 bool CanBeMoved() const OVERRIDE { return true; }
5294
5295 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5296 return true;
5297 }
5298
5299 bool NeedsEnvironment() const OVERRIDE {
5300 // Instruction may throw a CheckCastError.
5301 return true;
5302 }
5303
5304 bool CanThrow() const OVERRIDE { return true; }
5305
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005306 bool MustDoNullCheck() const { return must_do_null_check_; }
5307 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005308 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005309
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005310 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005311
5312 DECLARE_INSTRUCTION(CheckCast);
5313
5314 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005315 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005316 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005317
5318 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005319};
5320
Calin Juravle27df7582015-04-17 19:12:31 +01005321class HMemoryBarrier : public HTemplateInstruction<0> {
5322 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005323 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005324 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005325 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005326 barrier_kind_(barrier_kind) {}
5327
5328 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5329
5330 DECLARE_INSTRUCTION(MemoryBarrier);
5331
5332 private:
5333 const MemBarrierKind barrier_kind_;
5334
5335 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5336};
5337
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005338class HMonitorOperation : public HTemplateInstruction<1> {
5339 public:
5340 enum OperationKind {
5341 kEnter,
5342 kExit,
5343 };
5344
5345 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005346 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005347 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5348 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005349 SetRawInputAt(0, object);
5350 }
5351
5352 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005353 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5354
5355 bool CanThrow() const OVERRIDE {
5356 // Verifier guarantees that monitor-exit cannot throw.
5357 // This is important because it allows the HGraphBuilder to remove
5358 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5359 return IsEnter();
5360 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005361
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005362
5363 bool IsEnter() const { return kind_ == kEnter; }
5364
5365 DECLARE_INSTRUCTION(MonitorOperation);
5366
Calin Juravle52c48962014-12-16 17:02:57 +00005367 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005368 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005369
5370 private:
5371 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5372};
5373
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005374/**
5375 * A HInstruction used as a marker for the replacement of new + <init>
5376 * of a String to a call to a StringFactory. Only baseline will see
5377 * the node at code generation, where it will be be treated as null.
5378 * When compiling non-baseline, `HFakeString` instructions are being removed
5379 * in the instruction simplifier.
5380 */
5381class HFakeString : public HTemplateInstruction<0> {
5382 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005383 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5384 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005385
5386 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5387
5388 DECLARE_INSTRUCTION(FakeString);
5389
5390 private:
5391 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5392};
5393
Vladimir Markof9f64412015-09-02 14:05:49 +01005394class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005395 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005396 MoveOperands(Location source,
5397 Location destination,
5398 Primitive::Type type,
5399 HInstruction* instruction)
5400 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005401
5402 Location GetSource() const { return source_; }
5403 Location GetDestination() const { return destination_; }
5404
5405 void SetSource(Location value) { source_ = value; }
5406 void SetDestination(Location value) { destination_ = value; }
5407
5408 // The parallel move resolver marks moves as "in-progress" by clearing the
5409 // destination (but not the source).
5410 Location MarkPending() {
5411 DCHECK(!IsPending());
5412 Location dest = destination_;
5413 destination_ = Location::NoLocation();
5414 return dest;
5415 }
5416
5417 void ClearPending(Location dest) {
5418 DCHECK(IsPending());
5419 destination_ = dest;
5420 }
5421
5422 bool IsPending() const {
5423 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5424 return destination_.IsInvalid() && !source_.IsInvalid();
5425 }
5426
5427 // True if this blocks a move from the given location.
5428 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005429 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005430 }
5431
5432 // A move is redundant if it's been eliminated, if its source and
5433 // destination are the same, or if its destination is unneeded.
5434 bool IsRedundant() const {
5435 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5436 }
5437
5438 // We clear both operands to indicate move that's been eliminated.
5439 void Eliminate() {
5440 source_ = destination_ = Location::NoLocation();
5441 }
5442
5443 bool IsEliminated() const {
5444 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5445 return source_.IsInvalid();
5446 }
5447
Alexey Frunze4dda3372015-06-01 18:31:49 -07005448 Primitive::Type GetType() const { return type_; }
5449
Nicolas Geoffray90218252015-04-15 11:56:51 +01005450 bool Is64BitMove() const {
5451 return Primitive::Is64BitType(type_);
5452 }
5453
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005454 HInstruction* GetInstruction() const { return instruction_; }
5455
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005456 private:
5457 Location source_;
5458 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005459 // The type this move is for.
5460 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005461 // The instruction this move is assocatied with. Null when this move is
5462 // for moving an input in the expected locations of user (including a phi user).
5463 // This is only used in debug mode, to ensure we do not connect interval siblings
5464 // in the same parallel move.
5465 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005466};
5467
5468static constexpr size_t kDefaultNumberOfMoves = 4;
5469
5470class HParallelMove : public HTemplateInstruction<0> {
5471 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005472 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005473 : HTemplateInstruction(SideEffects::None(), dex_pc),
5474 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5475 moves_.reserve(kDefaultNumberOfMoves);
5476 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005477
Nicolas Geoffray90218252015-04-15 11:56:51 +01005478 void AddMove(Location source,
5479 Location destination,
5480 Primitive::Type type,
5481 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005482 DCHECK(source.IsValid());
5483 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005484 if (kIsDebugBuild) {
5485 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005486 for (const MoveOperands& move : moves_) {
5487 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005488 // Special case the situation where the move is for the spill slot
5489 // of the instruction.
5490 if ((GetPrevious() == instruction)
5491 || ((GetPrevious() == nullptr)
5492 && instruction->IsPhi()
5493 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005494 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005495 << "Doing parallel moves for the same instruction.";
5496 } else {
5497 DCHECK(false) << "Doing parallel moves for the same instruction.";
5498 }
5499 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005500 }
5501 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005502 for (const MoveOperands& move : moves_) {
5503 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005504 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005505 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005506 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005507 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005508 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005509 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005510 }
5511
Vladimir Marko225b6462015-09-28 12:17:40 +01005512 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005513 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005514 }
5515
Vladimir Marko225b6462015-09-28 12:17:40 +01005516 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005517
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005518 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005519
5520 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005521 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005522
5523 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5524};
5525
Mark Mendell0616ae02015-04-17 12:49:27 -04005526} // namespace art
5527
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005528#ifdef ART_ENABLE_CODEGEN_arm64
5529#include "nodes_arm64.h"
5530#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005531#ifdef ART_ENABLE_CODEGEN_x86
5532#include "nodes_x86.h"
5533#endif
5534
5535namespace art {
5536
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005537class HGraphVisitor : public ValueObject {
5538 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005539 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5540 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005541
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005542 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005543 virtual void VisitBasicBlock(HBasicBlock* block);
5544
Roland Levillain633021e2014-10-01 14:12:25 +01005545 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005546 void VisitInsertionOrder();
5547
Roland Levillain633021e2014-10-01 14:12:25 +01005548 // Visit the graph following dominator tree reverse post-order.
5549 void VisitReversePostOrder();
5550
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005551 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005552
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005553 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005554#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005555 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5556
5557 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5558
5559#undef DECLARE_VISIT_INSTRUCTION
5560
5561 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005562 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005563
5564 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5565};
5566
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005567class HGraphDelegateVisitor : public HGraphVisitor {
5568 public:
5569 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5570 virtual ~HGraphDelegateVisitor() {}
5571
5572 // Visit functions that delegate to to super class.
5573#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005574 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005575
5576 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5577
5578#undef DECLARE_VISIT_INSTRUCTION
5579
5580 private:
5581 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5582};
5583
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005584class HInsertionOrderIterator : public ValueObject {
5585 public:
5586 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5587
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005588 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005589 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005590 void Advance() { ++index_; }
5591
5592 private:
5593 const HGraph& graph_;
5594 size_t index_;
5595
5596 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5597};
5598
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005599class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005600 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005601 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5602 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005603 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005604 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005605
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005606 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5607 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005608 void Advance() { ++index_; }
5609
5610 private:
5611 const HGraph& graph_;
5612 size_t index_;
5613
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005614 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005615};
5616
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005617class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005618 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005619 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005620 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005621 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005622 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005623 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005624
5625 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005626 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005627 void Advance() { --index_; }
5628
5629 private:
5630 const HGraph& graph_;
5631 size_t index_;
5632
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005633 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005634};
5635
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005636class HLinearPostOrderIterator : public ValueObject {
5637 public:
5638 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005639 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005640
5641 bool Done() const { return index_ == 0; }
5642
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005643 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005644
5645 void Advance() {
5646 --index_;
5647 DCHECK_GE(index_, 0U);
5648 }
5649
5650 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005651 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005652 size_t index_;
5653
5654 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5655};
5656
5657class HLinearOrderIterator : public ValueObject {
5658 public:
5659 explicit HLinearOrderIterator(const HGraph& graph)
5660 : order_(graph.GetLinearOrder()), index_(0) {}
5661
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005662 bool Done() const { return index_ == order_.size(); }
5663 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005664 void Advance() { ++index_; }
5665
5666 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005667 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005668 size_t index_;
5669
5670 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5671};
5672
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005673// Iterator over the blocks that art part of the loop. Includes blocks part
5674// of an inner loop. The order in which the blocks are iterated is on their
5675// block id.
5676class HBlocksInLoopIterator : public ValueObject {
5677 public:
5678 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5679 : blocks_in_loop_(info.GetBlocks()),
5680 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5681 index_(0) {
5682 if (!blocks_in_loop_.IsBitSet(index_)) {
5683 Advance();
5684 }
5685 }
5686
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005687 bool Done() const { return index_ == blocks_.size(); }
5688 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005689 void Advance() {
5690 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005691 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005692 if (blocks_in_loop_.IsBitSet(index_)) {
5693 break;
5694 }
5695 }
5696 }
5697
5698 private:
5699 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005700 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005701 size_t index_;
5702
5703 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5704};
5705
Mingyao Yang3584bce2015-05-19 16:01:59 -07005706// Iterator over the blocks that art part of the loop. Includes blocks part
5707// of an inner loop. The order in which the blocks are iterated is reverse
5708// post order.
5709class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5710 public:
5711 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5712 : blocks_in_loop_(info.GetBlocks()),
5713 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5714 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005715 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005716 Advance();
5717 }
5718 }
5719
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005720 bool Done() const { return index_ == blocks_.size(); }
5721 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005722 void Advance() {
5723 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005724 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5725 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005726 break;
5727 }
5728 }
5729 }
5730
5731 private:
5732 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005733 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005734 size_t index_;
5735
5736 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5737};
5738
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005739inline int64_t Int64FromConstant(HConstant* constant) {
5740 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5741 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5742 : constant->AsLongConstant()->GetValue();
5743}
5744
Vladimir Marko58155012015-08-19 12:49:41 +00005745inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5746 // For the purposes of the compiler, the dex files must actually be the same object
5747 // if we want to safely treat them as the same. This is especially important for JIT
5748 // as custom class loaders can open the same underlying file (or memory) multiple
5749 // times and provide different class resolution but no two class loaders should ever
5750 // use the same DexFile object - doing so is an unsupported hack that can lead to
5751 // all sorts of weird failures.
5752 return &lhs == &rhs;
5753}
5754
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005755} // namespace art
5756
5757#endif // ART_COMPILER_OPTIMIZING_NODES_H_