blob: 441aa0493afcdf3ed2dd5051d25785fe2d9a9ae7 [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"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039
40namespace art {
41
David Brazdil1abb4192015-02-17 18:33:36 +000042class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010047class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000049class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000055class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010057class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010058class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010061class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Mathieu Chartier736b5602015-09-02 14:54:11 -070064namespace mirror {
65class DexCache;
66} // namespace mirror
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068static const int kDefaultNumberOfBlocks = 8;
69static const int kDefaultNumberOfSuccessors = 2;
70static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010071static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010072static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
Calin Juravle9aec02f2014-11-18 23:06:35 +000075static constexpr uint32_t kMaxIntShiftValue = 0x1f;
76static constexpr uint64_t kMaxLongShiftValue = 0x3f;
77
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010078static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070079static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010080
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010081static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
82
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060083static constexpr uint32_t kNoDexPc = -1;
84
Dave Allison20dfc792014-06-16 20:44:29 -070085enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070086 // All types.
87 kCondEQ, // ==
88 kCondNE, // !=
89 // Signed integers and floating-point numbers.
90 kCondLT, // <
91 kCondLE, // <=
92 kCondGT, // >
93 kCondGE, // >=
94 // Unsigned integers.
95 kCondB, // <
96 kCondBE, // <=
97 kCondA, // >
98 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070099};
100
Vladimir Markof9f64412015-09-02 14:05:49 +0100101class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100102 public:
103 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
104
105 void AddInstruction(HInstruction* instruction);
106 void RemoveInstruction(HInstruction* instruction);
107
David Brazdilc3d743f2015-04-22 13:40:50 +0100108 // Insert `instruction` before/after an existing instruction `cursor`.
109 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
110 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
111
Roland Levillain6b469232014-09-25 10:10:38 +0100112 // Return true if this list contains `instruction`.
113 bool Contains(HInstruction* instruction) const;
114
Roland Levillainccc07a92014-09-16 14:48:16 +0100115 // Return true if `instruction1` is found before `instruction2` in
116 // this instruction list and false otherwise. Abort if none
117 // of these instructions is found.
118 bool FoundBefore(const HInstruction* instruction1,
119 const HInstruction* instruction2) const;
120
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000121 bool IsEmpty() const { return first_instruction_ == nullptr; }
122 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
123
124 // Update the block of all instructions to be `block`.
125 void SetBlockOfInstructions(HBasicBlock* block) const;
126
127 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
128 void Add(const HInstructionList& instruction_list);
129
David Brazdil2d7352b2015-04-20 14:52:42 +0100130 // Return the number of instructions in the list. This is an expensive operation.
131 size_t CountSize() const;
132
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100133 private:
134 HInstruction* first_instruction_;
135 HInstruction* last_instruction_;
136
137 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000138 friend class HGraph;
139 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100141 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100142
143 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
144};
145
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000146// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100147class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000148 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100149 HGraph(ArenaAllocator* arena,
150 const DexFile& dex_file,
151 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100152 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100154 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100155 bool debuggable = false,
156 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000157 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100158 blocks_(arena->Adapter(kArenaAllocBlockList)),
159 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
160 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700161 entry_block_(nullptr),
162 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100163 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100164 number_of_vregs_(0),
165 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000166 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400167 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000168 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000169 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000170 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100171 dex_file_(dex_file),
172 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100173 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100174 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100175 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700176 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000177 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100178 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
179 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
180 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
181 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
182 cached_current_method_(nullptr) {
183 blocks_.reserve(kDefaultNumberOfBlocks);
184 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000185
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000186 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100187 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
188
David Brazdil69ba7b72015-06-23 18:27:30 +0100189 bool IsInSsaForm() const { return in_ssa_form_; }
190
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000191 HBasicBlock* GetEntryBlock() const { return entry_block_; }
192 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100193 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000194
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000195 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
196 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000197
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000198 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100199
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000200 // Try building the SSA form of this graph, with dominance computation and loop
201 // recognition. Returns whether it was successful in doing all these steps.
202 bool TryBuildingSsa() {
203 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000204 // The SSA builder requires loops to all be natural. Specifically, the dead phi
205 // elimination phase checks the consistency of the graph when doing a post-order
206 // visit for eliminating dead phis: a dead phi can only have loop header phi
207 // users remaining when being visited.
208 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100209 // Precompute per-block try membership before entering the SSA builder,
210 // which needs the information to build catch block phis from values of
211 // locals at throwing instructions inside try blocks.
212 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100214 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000215 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000216 }
217
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100218 void ComputeDominanceInformation();
219 void ClearDominanceInformation();
220
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000221 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000222 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100223 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100224 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000225
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000226 // Analyze all natural loops in this graph. Returns false if one
227 // loop is not natural, that is the header does not dominate the
228 // back edge.
229 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100230
David Brazdilffee3d32015-07-06 11:48:53 +0100231 // Iterate over blocks to compute try block membership. Needs reverse post
232 // order and loop information.
233 void ComputeTryBlockInformation();
234
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000235 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000236 // Returns the instruction used to replace the invoke expression or null if the
237 // invoke is for a void method.
238 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000239
Mingyao Yang3584bce2015-05-19 16:01:59 -0700240 // Need to add a couple of blocks to test if the loop body is entered and
241 // put deoptimization instructions, etc.
242 void TransformLoopHeaderForBCE(HBasicBlock* header);
243
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000244 // Removes `block` from the graph. Assumes `block` has been disconnected from
245 // other blocks and has no instructions or phis.
246 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000247
David Brazdilfc6a86a2015-06-26 10:33:45 +0000248 // Splits the edge between `block` and `successor` while preserving the
249 // indices in the predecessor/successor lists. If there are multiple edges
250 // between the blocks, the lowest indices are used.
251 // Returns the new block which is empty and has the same dex pc as `successor`.
252 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
253
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100254 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
255 void SimplifyLoop(HBasicBlock* header);
256
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000257 int32_t GetNextInstructionId() {
258 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000259 return current_instruction_id_++;
260 }
261
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000262 int32_t GetCurrentInstructionId() const {
263 return current_instruction_id_;
264 }
265
266 void SetCurrentInstructionId(int32_t id) {
267 current_instruction_id_ = id;
268 }
269
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100270 uint16_t GetMaximumNumberOfOutVRegs() const {
271 return maximum_number_of_out_vregs_;
272 }
273
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000274 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
275 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100276 }
277
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100278 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
279 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
280 }
281
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000282 void UpdateTemporariesVRegSlots(size_t slots) {
283 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100284 }
285
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000286 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100287 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000288 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100289 }
290
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100291 void SetNumberOfVRegs(uint16_t number_of_vregs) {
292 number_of_vregs_ = number_of_vregs;
293 }
294
295 uint16_t GetNumberOfVRegs() const {
296 return number_of_vregs_;
297 }
298
299 void SetNumberOfInVRegs(uint16_t value) {
300 number_of_in_vregs_ = value;
301 }
302
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100303 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100304 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100305 return number_of_vregs_ - number_of_in_vregs_;
306 }
307
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100308 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100309 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100310 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100311
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100312 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100313 return linear_order_;
314 }
315
Mark Mendell1152c922015-04-24 17:06:35 -0400316 bool HasBoundsChecks() const {
317 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800318 }
319
Mark Mendell1152c922015-04-24 17:06:35 -0400320 void SetHasBoundsChecks(bool value) {
321 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800322 }
323
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100324 bool ShouldGenerateConstructorBarrier() const {
325 return should_generate_constructor_barrier_;
326 }
327
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000328 bool IsDebuggable() const { return debuggable_; }
329
David Brazdil8d5b8b22015-03-24 10:51:52 +0000330 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000331 // already, it is created and inserted into the graph. This method is only for
332 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600333 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000334
335 // TODO: This is problematic for the consistency of reference type propagation
336 // because it can be created anytime after the pass and thus it will be left
337 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600338 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000339
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600340 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
341 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600343 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
344 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000345 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600346 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
347 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000348 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600349 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
350 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000351 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000352
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100353 HCurrentMethod* GetCurrentMethod();
354
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100355 const DexFile& GetDexFile() const {
356 return dex_file_;
357 }
358
359 uint32_t GetMethodIdx() const {
360 return method_idx_;
361 }
362
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100363 InvokeType GetInvokeType() const {
364 return invoke_type_;
365 }
366
Mark Mendellc4701932015-04-10 13:18:51 -0400367 InstructionSet GetInstructionSet() const {
368 return instruction_set_;
369 }
370
David Brazdil77a48ae2015-09-15 12:34:04 +0000371 bool HasTryCatch() const { return has_try_catch_; }
372 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100373
Mark Mendellf6529172015-11-17 11:16:56 -0500374 // Returns an instruction with the opposite boolean value from 'cond'.
375 // The instruction has been inserted into the graph, either as a constant, or
376 // before cursor.
377 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
378
David Brazdil2d7352b2015-04-20 14:52:42 +0100379 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100380 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000381 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100382 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000383
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000384 template <class InstructionType, typename ValueType>
385 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600386 ArenaSafeMap<ValueType, InstructionType*>* cache,
387 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000388 // Try to find an existing constant of the given value.
389 InstructionType* constant = nullptr;
390 auto cached_constant = cache->find(value);
391 if (cached_constant != cache->end()) {
392 constant = cached_constant->second;
393 }
394
395 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100396 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000397 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600398 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000399 cache->Overwrite(value, constant);
400 InsertConstant(constant);
401 }
402 return constant;
403 }
404
David Brazdil8d5b8b22015-03-24 10:51:52 +0000405 void InsertConstant(HConstant* instruction);
406
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000407 // Cache a float constant into the graph. This method should only be
408 // called by the SsaBuilder when creating "equivalent" instructions.
409 void CacheFloatConstant(HFloatConstant* constant);
410
411 // See CacheFloatConstant comment.
412 void CacheDoubleConstant(HDoubleConstant* constant);
413
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000414 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000415
416 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100417 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000418
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100420 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000421
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100422 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100423 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100424
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000425 HBasicBlock* entry_block_;
426 HBasicBlock* exit_block_;
427
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100428 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100429 uint16_t maximum_number_of_out_vregs_;
430
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100431 // The number of virtual registers in this method. Contains the parameters.
432 uint16_t number_of_vregs_;
433
434 // The number of virtual registers used by parameters of this method.
435 uint16_t number_of_in_vregs_;
436
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000437 // Number of vreg size slots that the temporaries use (used in baseline compiler).
438 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100439
Mark Mendell1152c922015-04-24 17:06:35 -0400440 // Has bounds checks. We can totally skip BCE if it's false.
441 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800442
David Brazdil77a48ae2015-09-15 12:34:04 +0000443 // Flag whether there are any try/catch blocks in the graph. We will skip
444 // try/catch-related passes if false.
445 bool has_try_catch_;
446
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000447 // Indicates whether the graph should be compiled in a way that
448 // ensures full debuggability. If false, we can apply more
449 // aggressive optimizations that may limit the level of debugging.
450 const bool debuggable_;
451
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000452 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000453 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000454
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100455 // The dex file from which the method is from.
456 const DexFile& dex_file_;
457
458 // The method index in the dex file.
459 const uint32_t method_idx_;
460
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100461 // If inlined, this encodes how the callee is being invoked.
462 const InvokeType invoke_type_;
463
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100464 // Whether the graph has been transformed to SSA form. Only used
465 // in debug mode to ensure we are not using properties only valid
466 // for non-SSA form (like the number of temporaries).
467 bool in_ssa_form_;
468
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100469 const bool should_generate_constructor_barrier_;
470
Mathieu Chartiere401d142015-04-22 13:56:20 -0700471 const InstructionSet instruction_set_;
472
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000473 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000474 HNullConstant* cached_null_constant_;
475 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000476 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000477 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000478 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000479
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100480 HCurrentMethod* cached_current_method_;
481
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000482 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100483 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000484 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000485 DISALLOW_COPY_AND_ASSIGN(HGraph);
486};
487
Vladimir Markof9f64412015-09-02 14:05:49 +0100488class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000489 public:
490 HLoopInformation(HBasicBlock* header, HGraph* graph)
491 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100492 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100493 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100494 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100495 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
496 back_edges_.reserve(kDefaultNumberOfBackEdges);
497 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100498
499 HBasicBlock* GetHeader() const {
500 return header_;
501 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000502
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000503 void SetHeader(HBasicBlock* block) {
504 header_ = block;
505 }
506
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100507 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
508 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
509 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
510
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000511 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100512 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000513 }
514
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100515 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100516 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100517 }
518
David Brazdil46e2a392015-03-16 17:31:52 +0000519 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100520 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100521 }
522
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000523 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100524 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000525 }
526
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100527 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100528
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100529 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100530 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100531 }
532
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100533 // Returns the lifetime position of the back edge that has the
534 // greatest lifetime position.
535 size_t GetLifetimeEnd() const;
536
537 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100538 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100539 }
540
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100541 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100542 // that is the header dominates the back edge.
543 bool Populate();
544
David Brazdila4b8c212015-05-07 09:59:30 +0100545 // Reanalyzes the loop by removing loop info from its blocks and re-running
546 // Populate(). If there are no back edges left, the loop info is completely
547 // removed as well as its SuspendCheck instruction. It must be run on nested
548 // inner loops first.
549 void Update();
550
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100551 // Returns whether this loop information contains `block`.
552 // Note that this loop information *must* be populated before entering this function.
553 bool Contains(const HBasicBlock& block) const;
554
555 // Returns whether this loop information is an inner loop of `other`.
556 // Note that `other` *must* be populated before entering this function.
557 bool IsIn(const HLoopInformation& other) const;
558
Aart Bik73f1f3b2015-10-28 15:28:08 -0700559 // Returns true if instruction is not defined within this loop or any loop nested inside
560 // this loop. If must_dominate is set, only definitions that actually dominate the loop
561 // header can be invariant. Otherwise, any definition outside the loop, including
562 // definitions that appear after the loop, is invariant.
563 bool IsLoopInvariant(HInstruction* instruction, bool must_dominate) const;
564
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100565 const ArenaBitVector& GetBlocks() const { return blocks_; }
566
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000567 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000568 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000569
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000570 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100571 // Internal recursive implementation of `Populate`.
572 void PopulateRecursive(HBasicBlock* block);
573
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000574 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100575 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100576 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100577 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000578
579 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
580};
581
David Brazdilec16f792015-08-19 15:04:01 +0100582// Stores try/catch information for basic blocks.
583// Note that HGraph is constructed so that catch blocks cannot simultaneously
584// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100585class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100586 public:
587 // Try block information constructor.
588 explicit TryCatchInformation(const HTryBoundary& try_entry)
589 : try_entry_(&try_entry),
590 catch_dex_file_(nullptr),
591 catch_type_index_(DexFile::kDexNoIndex16) {
592 DCHECK(try_entry_ != nullptr);
593 }
594
595 // Catch block information constructor.
596 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
597 : try_entry_(nullptr),
598 catch_dex_file_(&dex_file),
599 catch_type_index_(catch_type_index) {}
600
601 bool IsTryBlock() const { return try_entry_ != nullptr; }
602
603 const HTryBoundary& GetTryEntry() const {
604 DCHECK(IsTryBlock());
605 return *try_entry_;
606 }
607
608 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
609
610 bool IsCatchAllTypeIndex() const {
611 DCHECK(IsCatchBlock());
612 return catch_type_index_ == DexFile::kDexNoIndex16;
613 }
614
615 uint16_t GetCatchTypeIndex() const {
616 DCHECK(IsCatchBlock());
617 return catch_type_index_;
618 }
619
620 const DexFile& GetCatchDexFile() const {
621 DCHECK(IsCatchBlock());
622 return *catch_dex_file_;
623 }
624
625 private:
626 // One of possibly several TryBoundary instructions entering the block's try.
627 // Only set for try blocks.
628 const HTryBoundary* try_entry_;
629
630 // Exception type information. Only set for catch blocks.
631 const DexFile* catch_dex_file_;
632 const uint16_t catch_type_index_;
633};
634
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100635static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100636static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100637
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000638// A block in a method. Contains the list of instructions represented
639// as a double linked list. Each block knows its predecessors and
640// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100641
Vladimir Markof9f64412015-09-02 14:05:49 +0100642class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600644 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000645 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000646 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
647 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000648 loop_information_(nullptr),
649 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000650 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100651 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100652 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100653 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000654 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000655 try_catch_information_(nullptr) {
656 predecessors_.reserve(kDefaultNumberOfPredecessors);
657 successors_.reserve(kDefaultNumberOfSuccessors);
658 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
659 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000660
Vladimir Marko60584552015-09-03 13:35:12 +0000661 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100662 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000663 }
664
Vladimir Marko60584552015-09-03 13:35:12 +0000665 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100666 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000667 }
668
David Brazdild26a4112015-11-10 11:07:31 +0000669 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
670 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
671
Vladimir Marko60584552015-09-03 13:35:12 +0000672 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
673 return ContainsElement(successors_, block, start_from);
674 }
675
676 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100677 return dominated_blocks_;
678 }
679
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100680 bool IsEntryBlock() const {
681 return graph_->GetEntryBlock() == this;
682 }
683
684 bool IsExitBlock() const {
685 return graph_->GetExitBlock() == this;
686 }
687
David Brazdil46e2a392015-03-16 17:31:52 +0000688 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000689 bool IsSingleTryBoundary() const;
690
691 // Returns true if this block emits nothing but a jump.
692 bool IsSingleJump() const {
693 HLoopInformation* loop_info = GetLoopInformation();
694 return (IsSingleGoto() || IsSingleTryBoundary())
695 // Back edges generate a suspend check.
696 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
697 }
David Brazdil46e2a392015-03-16 17:31:52 +0000698
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000699 void AddBackEdge(HBasicBlock* back_edge) {
700 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000701 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000702 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100703 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000704 loop_information_->AddBackEdge(back_edge);
705 }
706
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000707 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000708 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000709
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100710 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000711 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600712 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000713
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000714 HBasicBlock* GetDominator() const { return dominator_; }
715 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000716 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
717
718 void RemoveDominatedBlock(HBasicBlock* block) {
719 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100720 }
Vladimir Marko60584552015-09-03 13:35:12 +0000721
722 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
723 ReplaceElement(dominated_blocks_, existing, new_block);
724 }
725
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100726 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000727
728 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100729 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000730 }
731
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100732 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
733 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100734 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100735 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100736 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
737 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000738
739 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000740 successors_.push_back(block);
741 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000742 }
743
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100744 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
745 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100746 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000747 new_block->predecessors_.push_back(this);
748 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000749 }
750
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000751 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
752 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000753 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000754 new_block->successors_.push_back(this);
755 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000756 }
757
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100758 // Insert `this` between `predecessor` and `successor. This method
759 // preserves the indicies, and will update the first edge found between
760 // `predecessor` and `successor`.
761 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
762 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100763 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000764 successor->predecessors_[predecessor_index] = this;
765 predecessor->successors_[successor_index] = this;
766 successors_.push_back(successor);
767 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100768 }
769
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100770 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000771 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100772 }
773
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000774 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000775 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000776 }
777
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100778 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000779 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780 }
781
782 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000783 predecessors_.push_back(block);
784 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100785 }
786
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100787 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000788 DCHECK_EQ(predecessors_.size(), 2u);
789 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100790 }
791
David Brazdil769c9e52015-04-27 13:54:09 +0100792 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000793 DCHECK_EQ(successors_.size(), 2u);
794 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100795 }
796
David Brazdilfc6a86a2015-06-26 10:33:45 +0000797 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000798 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100799 }
800
David Brazdilfc6a86a2015-06-26 10:33:45 +0000801 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000802 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100803 }
804
David Brazdilfc6a86a2015-06-26 10:33:45 +0000805 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000806 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100807 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000808 }
809
810 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000811 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100812 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813 }
814
815 // Returns whether the first occurrence of `predecessor` in the list of
816 // predecessors is at index `idx`.
817 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100818 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000819 return GetPredecessorIndexOf(predecessor) == idx;
820 }
821
David Brazdild7558da2015-09-22 13:04:14 +0100822 // Create a new block between this block and its predecessors. The new block
823 // is added to the graph, all predecessor edges are relinked to it and an edge
824 // is created to `this`. Returns the new empty block. Reverse post order or
825 // loop and try/catch information are not updated.
826 HBasicBlock* CreateImmediateDominator();
827
David Brazdilfc6a86a2015-06-26 10:33:45 +0000828 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100829 // created, latter block. Note that this method will add the block to the
830 // graph, create a Goto at the end of the former block and will create an edge
831 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100832 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000833 HBasicBlock* SplitBefore(HInstruction* cursor);
834
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000835 // Split the block into two blocks just after `cursor`. Returns the newly
836 // created block. Note that this method just updates raw block information,
837 // like predecessors, successors, dominators, and instruction list. It does not
838 // update the graph, reverse post order, loop information, nor make sure the
839 // blocks are consistent (for example ending with a control flow instruction).
840 HBasicBlock* SplitAfter(HInstruction* cursor);
841
David Brazdil9bc43612015-11-05 21:25:24 +0000842 // Split catch block into two blocks after the original move-exception bytecode
843 // instruction, or at the beginning if not present. Returns the newly created,
844 // latter block, or nullptr if such block could not be created (must be dead
845 // in that case). Note that this method just updates raw block information,
846 // like predecessors, successors, dominators, and instruction list. It does not
847 // update the graph, reverse post order, loop information, nor make sure the
848 // blocks are consistent (for example ending with a control flow instruction).
849 HBasicBlock* SplitCatchBlockAfterMoveException();
850
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000851 // Merge `other` at the end of `this`. Successors and dominated blocks of
852 // `other` are changed to be successors and dominated blocks of `this`. Note
853 // that this method does not update the graph, reverse post order, loop
854 // information, nor make sure the blocks are consistent (for example ending
855 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100856 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000857
858 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
859 // of `this` are moved to `other`.
860 // Note that this method does not update the graph, reverse post order, loop
861 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000862 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000863 void ReplaceWith(HBasicBlock* other);
864
David Brazdil2d7352b2015-04-20 14:52:42 +0100865 // Merge `other` at the end of `this`. This method updates loops, reverse post
866 // order, links to predecessors, successors, dominators and deletes the block
867 // from the graph. The two blocks must be successive, i.e. `this` the only
868 // predecessor of `other` and vice versa.
869 void MergeWith(HBasicBlock* other);
870
871 // Disconnects `this` from all its predecessors, successors and dominator,
872 // removes it from all loops it is included in and eventually from the graph.
873 // The block must not dominate any other block. Predecessors and successors
874 // are safely updated.
875 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000876
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000877 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100878 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100879 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100880 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100881 // Replace instruction `initial` with `replacement` within this block.
882 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
883 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100884 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100885 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000886 // RemoveInstruction and RemovePhi delete a given instruction from the respective
887 // instruction list. With 'ensure_safety' set to true, it verifies that the
888 // instruction is not in use and removes it from the use lists of its inputs.
889 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
890 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100891 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100892
893 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100894 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100895 }
896
Roland Levillain6b879dd2014-09-22 17:13:44 +0100897 bool IsLoopPreHeaderFirstPredecessor() const {
898 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100899 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100900 }
901
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100902 HLoopInformation* GetLoopInformation() const {
903 return loop_information_;
904 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000905
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000906 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100907 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000908 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100909 void SetInLoop(HLoopInformation* info) {
910 if (IsLoopHeader()) {
911 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100912 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100913 loop_information_ = info;
914 } else if (loop_information_->Contains(*info->GetHeader())) {
915 // Block is currently part of an outer loop. Make it part of this inner loop.
916 // Note that a non loop header having a loop information means this loop information
917 // has already been populated
918 loop_information_ = info;
919 } else {
920 // Block is part of an inner loop. Do not update the loop information.
921 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
922 // at this point, because this method is being called while populating `info`.
923 }
924 }
925
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000926 // Raw update of the loop information.
927 void SetLoopInformation(HLoopInformation* info) {
928 loop_information_ = info;
929 }
930
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100931 bool IsInLoop() const { return loop_information_ != nullptr; }
932
David Brazdilec16f792015-08-19 15:04:01 +0100933 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
934
935 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
936 try_catch_information_ = try_catch_information;
937 }
938
939 bool IsTryBlock() const {
940 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
941 }
942
943 bool IsCatchBlock() const {
944 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
945 }
David Brazdilffee3d32015-07-06 11:48:53 +0100946
947 // Returns the try entry that this block's successors should have. They will
948 // be in the same try, unless the block ends in a try boundary. In that case,
949 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100950 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100951
David Brazdild7558da2015-09-22 13:04:14 +0100952 bool HasThrowingInstructions() const;
953
David Brazdila4b8c212015-05-07 09:59:30 +0100954 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100955 bool Dominates(HBasicBlock* block) const;
956
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100957 size_t GetLifetimeStart() const { return lifetime_start_; }
958 size_t GetLifetimeEnd() const { return lifetime_end_; }
959
960 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
961 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
962
David Brazdil8d5b8b22015-03-24 10:51:52 +0000963 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000964 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100965 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000966 bool HasSinglePhi() const;
967
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000968 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000969 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000970 ArenaVector<HBasicBlock*> predecessors_;
971 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100972 HInstructionList instructions_;
973 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000974 HLoopInformation* loop_information_;
975 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000976 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100977 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100978 // The dex program counter of the first instruction of this block.
979 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100980 size_t lifetime_start_;
981 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100982 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100983
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000984 friend class HGraph;
985 friend class HInstruction;
986
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000987 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
988};
989
David Brazdilb2bd1c52015-03-25 11:17:37 +0000990// Iterates over the LoopInformation of all loops which contain 'block'
991// from the innermost to the outermost.
992class HLoopInformationOutwardIterator : public ValueObject {
993 public:
994 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
995 : current_(block.GetLoopInformation()) {}
996
997 bool Done() const { return current_ == nullptr; }
998
999 void Advance() {
1000 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001001 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001002 }
1003
1004 HLoopInformation* Current() const {
1005 DCHECK(!Done());
1006 return current_;
1007 }
1008
1009 private:
1010 HLoopInformation* current_;
1011
1012 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1013};
1014
Alexandre Ramesef20f712015-06-09 10:29:30 +01001015#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001016 M(Above, Condition) \
1017 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001018 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001019 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001020 M(ArrayGet, Instruction) \
1021 M(ArrayLength, Instruction) \
1022 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001023 M(Below, Condition) \
1024 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001025 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001026 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001027 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001028 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001029 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001030 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001031 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001032 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001033 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001034 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001035 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001036 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001037 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001038 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001039 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001040 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001041 M(FloatConstant, Constant) \
1042 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001043 M(GreaterThan, Condition) \
1044 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001045 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001046 M(InstanceFieldGet, Instruction) \
1047 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001048 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001049 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001050 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001051 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001052 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001053 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001054 M(LessThan, Condition) \
1055 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001056 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001057 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001058 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001059 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001060 M(Local, Instruction) \
1061 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001062 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001063 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001064 M(Mul, BinaryOperation) \
1065 M(Neg, UnaryOperation) \
1066 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001067 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001068 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001069 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001070 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001071 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001072 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001073 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001074 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001075 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001076 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001077 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001078 M(Return, Instruction) \
1079 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001080 M(Shl, BinaryOperation) \
1081 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001082 M(StaticFieldGet, Instruction) \
1083 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001084 M(UnresolvedInstanceFieldGet, Instruction) \
1085 M(UnresolvedInstanceFieldSet, Instruction) \
1086 M(UnresolvedStaticFieldGet, Instruction) \
1087 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001088 M(StoreLocal, Instruction) \
1089 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001090 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001091 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001092 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001093 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001094 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001095 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001096 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001097
Vladimir Markob4536b72015-11-24 13:45:23 +00001098#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001099#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001100#else
1101#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1102 M(ArmDexCacheArraysBase, Instruction)
1103#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001104
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001105#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001106#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001107#else
1108#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001109 M(Arm64DataProcWithShifterOp, Instruction) \
Alexandre Rames418318f2015-11-20 15:55:47 +00001110 M(Arm64IntermediateAddress, Instruction) \
1111 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001112#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001113
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001114#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1115
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001116#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1117
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001118#ifndef ART_ENABLE_CODEGEN_x86
1119#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1120#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001121#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1122 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001123 M(X86LoadFromConstantTable, Instruction) \
1124 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001125#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001126
1127#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1128
1129#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1130 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1131 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1132 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001133 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001134 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001135 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1136 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1137
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001138#define FOR_EACH_INSTRUCTION(M) \
1139 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1140 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001141 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001142 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001143 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001144
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001145#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001146FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1147#undef FORWARD_DECLARATION
1148
Roland Levillainccc07a92014-09-16 14:48:16 +01001149#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001150 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1151 const char* DebugName() const OVERRIDE { return #type; } \
1152 const H##type* As##type() const OVERRIDE { return this; } \
1153 H##type* As##type() OVERRIDE { return this; } \
1154 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001155 return other->Is##type(); \
1156 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001157 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001158
David Brazdiled596192015-01-23 10:39:45 +00001159template <typename T> class HUseList;
1160
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001161template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001162class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001163 public:
David Brazdiled596192015-01-23 10:39:45 +00001164 HUseListNode* GetPrevious() const { return prev_; }
1165 HUseListNode* GetNext() const { return next_; }
1166 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001167 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001168 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001169
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001170 private:
David Brazdiled596192015-01-23 10:39:45 +00001171 HUseListNode(T user, size_t index)
1172 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1173
1174 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001175 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001176 HUseListNode<T>* prev_;
1177 HUseListNode<T>* next_;
1178
1179 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001180
1181 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1182};
1183
David Brazdiled596192015-01-23 10:39:45 +00001184template <typename T>
1185class HUseList : public ValueObject {
1186 public:
1187 HUseList() : first_(nullptr) {}
1188
1189 void Clear() {
1190 first_ = nullptr;
1191 }
1192
1193 // Adds a new entry at the beginning of the use list and returns
1194 // the newly created node.
1195 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001196 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001197 if (IsEmpty()) {
1198 first_ = new_node;
1199 } else {
1200 first_->prev_ = new_node;
1201 new_node->next_ = first_;
1202 first_ = new_node;
1203 }
1204 return new_node;
1205 }
1206
1207 HUseListNode<T>* GetFirst() const {
1208 return first_;
1209 }
1210
1211 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001212 DCHECK(node != nullptr);
1213 DCHECK(Contains(node));
1214
David Brazdiled596192015-01-23 10:39:45 +00001215 if (node->prev_ != nullptr) {
1216 node->prev_->next_ = node->next_;
1217 }
1218 if (node->next_ != nullptr) {
1219 node->next_->prev_ = node->prev_;
1220 }
1221 if (node == first_) {
1222 first_ = node->next_;
1223 }
1224 }
1225
David Brazdil1abb4192015-02-17 18:33:36 +00001226 bool Contains(const HUseListNode<T>* node) const {
1227 if (node == nullptr) {
1228 return false;
1229 }
1230 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1231 if (current == node) {
1232 return true;
1233 }
1234 }
1235 return false;
1236 }
1237
David Brazdiled596192015-01-23 10:39:45 +00001238 bool IsEmpty() const {
1239 return first_ == nullptr;
1240 }
1241
1242 bool HasOnlyOneUse() const {
1243 return first_ != nullptr && first_->next_ == nullptr;
1244 }
1245
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001246 size_t SizeSlow() const {
1247 size_t count = 0;
1248 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1249 ++count;
1250 }
1251 return count;
1252 }
1253
David Brazdiled596192015-01-23 10:39:45 +00001254 private:
1255 HUseListNode<T>* first_;
1256};
1257
1258template<typename T>
1259class HUseIterator : public ValueObject {
1260 public:
1261 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1262
1263 bool Done() const { return current_ == nullptr; }
1264
1265 void Advance() {
1266 DCHECK(!Done());
1267 current_ = current_->GetNext();
1268 }
1269
1270 HUseListNode<T>* Current() const {
1271 DCHECK(!Done());
1272 return current_;
1273 }
1274
1275 private:
1276 HUseListNode<T>* current_;
1277
1278 friend class HValue;
1279};
1280
David Brazdil1abb4192015-02-17 18:33:36 +00001281// This class is used by HEnvironment and HInstruction classes to record the
1282// instructions they use and pointers to the corresponding HUseListNodes kept
1283// by the used instructions.
1284template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001285class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001286 public:
1287 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1288 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1289
1290 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1291 : instruction_(old_record.instruction_), use_node_(use_node) {
1292 DCHECK(instruction_ != nullptr);
1293 DCHECK(use_node_ != nullptr);
1294 DCHECK(old_record.use_node_ == nullptr);
1295 }
1296
1297 HInstruction* GetInstruction() const { return instruction_; }
1298 HUseListNode<T>* GetUseNode() const { return use_node_; }
1299
1300 private:
1301 // Instruction used by the user.
1302 HInstruction* instruction_;
1303
1304 // Corresponding entry in the use list kept by 'instruction_'.
1305 HUseListNode<T>* use_node_;
1306};
1307
Aart Bik854a02b2015-07-14 16:07:00 -07001308/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001309 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001310 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001311 * For write/read dependences on fields/arrays, the dependence analysis uses
1312 * type disambiguation (e.g. a float field write cannot modify the value of an
1313 * integer field read) and the access type (e.g. a reference array write cannot
1314 * modify the value of a reference field read [although it may modify the
1315 * reference fetch prior to reading the field, which is represented by its own
1316 * write/read dependence]). The analysis makes conservative points-to
1317 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1318 * the same, and any reference read depends on any reference read without
1319 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001320 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001321 * The internal representation uses 38-bit and is described in the table below.
1322 * The first line indicates the side effect, and for field/array accesses the
1323 * second line indicates the type of the access (in the order of the
1324 * Primitive::Type enum).
1325 * The two numbered lines below indicate the bit position in the bitfield (read
1326 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001327 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001328 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1329 * +-------------+---------+---------+--------------+---------+---------+
1330 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1331 * | 3 |333333322|222222221| 1 |111111110|000000000|
1332 * | 7 |654321098|765432109| 8 |765432109|876543210|
1333 *
1334 * Note that, to ease the implementation, 'changes' bits are least significant
1335 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001336 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001337class SideEffects : public ValueObject {
1338 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001339 SideEffects() : flags_(0) {}
1340
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001341 static SideEffects None() {
1342 return SideEffects(0);
1343 }
1344
1345 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001346 return SideEffects(kAllChangeBits | kAllDependOnBits);
1347 }
1348
1349 static SideEffects AllChanges() {
1350 return SideEffects(kAllChangeBits);
1351 }
1352
1353 static SideEffects AllDependencies() {
1354 return SideEffects(kAllDependOnBits);
1355 }
1356
1357 static SideEffects AllExceptGCDependency() {
1358 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1359 }
1360
1361 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001362 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001363 }
1364
Aart Bik34c3ba92015-07-20 14:08:59 -07001365 static SideEffects AllWrites() {
1366 return SideEffects(kAllWrites);
1367 }
1368
1369 static SideEffects AllReads() {
1370 return SideEffects(kAllReads);
1371 }
1372
1373 static SideEffects FieldWriteOfType(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, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001377 }
1378
Aart Bik854a02b2015-07-14 16:07:00 -07001379 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1380 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001381 }
1382
Aart Bik34c3ba92015-07-20 14:08:59 -07001383 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1384 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001385 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001386 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001387 }
1388
1389 static SideEffects ArrayReadOfType(Primitive::Type type) {
1390 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1391 }
1392
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001393 static SideEffects CanTriggerGC() {
1394 return SideEffects(1ULL << kCanTriggerGCBit);
1395 }
1396
1397 static SideEffects DependsOnGC() {
1398 return SideEffects(1ULL << kDependsOnGCBit);
1399 }
1400
Aart Bik854a02b2015-07-14 16:07:00 -07001401 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001402 SideEffects Union(SideEffects other) const {
1403 return SideEffects(flags_ | other.flags_);
1404 }
1405
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001406 SideEffects Exclusion(SideEffects other) const {
1407 return SideEffects(flags_ & ~other.flags_);
1408 }
1409
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001410 void Add(SideEffects other) {
1411 flags_ |= other.flags_;
1412 }
1413
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001414 bool Includes(SideEffects other) const {
1415 return (other.flags_ & flags_) == other.flags_;
1416 }
1417
1418 bool HasSideEffects() const {
1419 return (flags_ & kAllChangeBits);
1420 }
1421
1422 bool HasDependencies() const {
1423 return (flags_ & kAllDependOnBits);
1424 }
1425
1426 // Returns true if there are no side effects or dependencies.
1427 bool DoesNothing() const {
1428 return flags_ == 0;
1429 }
1430
Aart Bik854a02b2015-07-14 16:07:00 -07001431 // Returns true if something is written.
1432 bool DoesAnyWrite() const {
1433 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001434 }
1435
Aart Bik854a02b2015-07-14 16:07:00 -07001436 // Returns true if something is read.
1437 bool DoesAnyRead() const {
1438 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001439 }
1440
Aart Bik854a02b2015-07-14 16:07:00 -07001441 // Returns true if potentially everything is written and read
1442 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001443 bool DoesAllReadWrite() const {
1444 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1445 }
1446
Aart Bik854a02b2015-07-14 16:07:00 -07001447 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001448 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001449 }
1450
Roland Levillain0d5a2812015-11-13 10:07:31 +00001451 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001452 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001453 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1454 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001455 }
1456
1457 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001458 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001459 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001460 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001461 for (int s = kLastBit; s >= 0; s--) {
1462 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1463 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1464 // This is a bit for the GC side effect.
1465 if (current_bit_is_set) {
1466 flags += "GC";
1467 }
Aart Bik854a02b2015-07-14 16:07:00 -07001468 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001469 } else {
1470 // This is a bit for the array/field analysis.
1471 // The underscore character stands for the 'can trigger GC' bit.
1472 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1473 if (current_bit_is_set) {
1474 flags += kDebug[s];
1475 }
1476 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1477 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1478 flags += "|";
1479 }
1480 }
Aart Bik854a02b2015-07-14 16:07:00 -07001481 }
1482 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001483 }
1484
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001485 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001486
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001487 private:
1488 static constexpr int kFieldArrayAnalysisBits = 9;
1489
1490 static constexpr int kFieldWriteOffset = 0;
1491 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1492 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1493 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1494
1495 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1496
1497 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1498 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1499 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1500 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1501
1502 static constexpr int kLastBit = kDependsOnGCBit;
1503 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1504
1505 // Aliases.
1506
1507 static_assert(kChangeBits == kDependOnBits,
1508 "the 'change' bits should match the 'depend on' bits.");
1509
1510 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1511 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1512 static constexpr uint64_t kAllWrites =
1513 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1514 static constexpr uint64_t kAllReads =
1515 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001516
Aart Bik854a02b2015-07-14 16:07:00 -07001517 // Work around the fact that HIR aliases I/F and J/D.
1518 // TODO: remove this interceptor once HIR types are clean
1519 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1520 switch (type) {
1521 case Primitive::kPrimInt:
1522 case Primitive::kPrimFloat:
1523 return TypeFlag(Primitive::kPrimInt, offset) |
1524 TypeFlag(Primitive::kPrimFloat, offset);
1525 case Primitive::kPrimLong:
1526 case Primitive::kPrimDouble:
1527 return TypeFlag(Primitive::kPrimLong, offset) |
1528 TypeFlag(Primitive::kPrimDouble, offset);
1529 default:
1530 return TypeFlag(type, offset);
1531 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001532 }
1533
Aart Bik854a02b2015-07-14 16:07:00 -07001534 // Translates type to bit flag.
1535 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1536 CHECK_NE(type, Primitive::kPrimVoid);
1537 const uint64_t one = 1;
1538 const int shift = type; // 0-based consecutive enum
1539 DCHECK_LE(kFieldWriteOffset, shift);
1540 DCHECK_LT(shift, kArrayWriteOffset);
1541 return one << (type + offset);
1542 }
1543
1544 // Private constructor on direct flags value.
1545 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1546
1547 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001548};
1549
David Brazdiled596192015-01-23 10:39:45 +00001550// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001551class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001552 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001553 HEnvironment(ArenaAllocator* arena,
1554 size_t number_of_vregs,
1555 const DexFile& dex_file,
1556 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001557 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001558 InvokeType invoke_type,
1559 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001560 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1561 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001562 parent_(nullptr),
1563 dex_file_(dex_file),
1564 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001565 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001566 invoke_type_(invoke_type),
1567 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001568 }
1569
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001570 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001571 : HEnvironment(arena,
1572 to_copy.Size(),
1573 to_copy.GetDexFile(),
1574 to_copy.GetMethodIdx(),
1575 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001576 to_copy.GetInvokeType(),
1577 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001578
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001579 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001580 if (parent_ != nullptr) {
1581 parent_->SetAndCopyParentChain(allocator, parent);
1582 } else {
1583 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1584 parent_->CopyFrom(parent);
1585 if (parent->GetParent() != nullptr) {
1586 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1587 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001588 }
David Brazdiled596192015-01-23 10:39:45 +00001589 }
1590
Vladimir Marko71bf8092015-09-15 15:33:14 +01001591 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001592 void CopyFrom(HEnvironment* environment);
1593
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001594 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1595 // input to the loop phi instead. This is for inserting instructions that
1596 // require an environment (like HDeoptimization) in the loop pre-header.
1597 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001598
1599 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001600 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001601 }
1602
1603 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001604 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001605 }
1606
David Brazdil1abb4192015-02-17 18:33:36 +00001607 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001608
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001609 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001610
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001611 HEnvironment* GetParent() const { return parent_; }
1612
1613 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001614 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001615 }
1616
1617 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001618 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001619 }
1620
1621 uint32_t GetDexPc() const {
1622 return dex_pc_;
1623 }
1624
1625 uint32_t GetMethodIdx() const {
1626 return method_idx_;
1627 }
1628
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001629 InvokeType GetInvokeType() const {
1630 return invoke_type_;
1631 }
1632
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001633 const DexFile& GetDexFile() const {
1634 return dex_file_;
1635 }
1636
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001637 HInstruction* GetHolder() const {
1638 return holder_;
1639 }
1640
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001641
1642 bool IsFromInlinedInvoke() const {
1643 return GetParent() != nullptr;
1644 }
1645
David Brazdiled596192015-01-23 10:39:45 +00001646 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001647 // Record instructions' use entries of this environment for constant-time removal.
1648 // It should only be called by HInstruction when a new environment use is added.
1649 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1650 DCHECK(env_use->GetUser() == this);
1651 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001652 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001653 }
David Brazdiled596192015-01-23 10:39:45 +00001654
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001655 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1656 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001657 HEnvironment* parent_;
1658 const DexFile& dex_file_;
1659 const uint32_t method_idx_;
1660 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001661 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001662
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001663 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001664 HInstruction* const holder_;
1665
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001666 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001667
1668 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1669};
1670
Calin Juravleacf735c2015-02-12 15:25:22 +00001671class ReferenceTypeInfo : ValueObject {
1672 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001673 typedef Handle<mirror::Class> TypeHandle;
1674
Calin Juravle2e768302015-07-28 14:41:11 +00001675 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1676 // The constructor will check that the type_handle is valid.
1677 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001678 }
1679
Calin Juravle2e768302015-07-28 14:41:11 +00001680 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1681
1682 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1683 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001684 }
1685
Calin Juravle2e768302015-07-28 14:41:11 +00001686 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1687 return IsValidHandle(type_handle_);
1688 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001689
Calin Juravleacf735c2015-02-12 15:25:22 +00001690 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001691
1692 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1693 DCHECK(IsValid());
1694 return GetTypeHandle()->IsObjectClass();
1695 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001696
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001697 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001698 DCHECK(IsValid());
1699 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001700 }
1701
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001702 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1703 DCHECK(IsValid());
1704 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1705 }
1706
Mathieu Chartier90443472015-07-16 20:32:27 -07001707 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001708 DCHECK(IsValid());
1709 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001710 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001711
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001712 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001713 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001714 return GetTypeHandle()->IsArrayClass();
1715 }
1716
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001717 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1718 DCHECK(IsValid());
1719 return GetTypeHandle()->IsPrimitiveArray();
1720 }
1721
1722 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1723 DCHECK(IsValid());
1724 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1725 }
1726
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001727 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001728 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001729 if (!IsExact()) return false;
1730 if (!IsArrayClass()) return false;
1731 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1732 }
1733
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001734 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1735 DCHECK(IsValid());
1736 if (!IsExact()) return false;
1737 if (!IsArrayClass()) return false;
1738 if (!rti.IsArrayClass()) return false;
1739 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1740 rti.GetTypeHandle()->GetComponentType());
1741 }
1742
Calin Juravleacf735c2015-02-12 15:25:22 +00001743 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1744
Mathieu Chartier90443472015-07-16 20:32:27 -07001745 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001746 DCHECK(IsValid());
1747 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001748 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1749 }
1750
Calin Juravlecdfed3d2015-10-26 14:05:01 +00001751 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1752 DCHECK(IsValid());
1753 DCHECK(rti.IsValid());
1754 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
1755 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1756 }
1757
Calin Juravleacf735c2015-02-12 15:25:22 +00001758 // Returns true if the type information provide the same amount of details.
1759 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001760 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001761 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001762 if (!IsValid() && !rti.IsValid()) {
1763 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001764 return true;
1765 }
Calin Juravle2e768302015-07-28 14:41:11 +00001766 if (!IsValid() || !rti.IsValid()) {
1767 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001768 return false;
1769 }
Calin Juravle2e768302015-07-28 14:41:11 +00001770 return IsExact() == rti.IsExact()
1771 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001772 }
1773
1774 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001775 ReferenceTypeInfo();
1776 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001777
Calin Juravleacf735c2015-02-12 15:25:22 +00001778 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001779 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001780 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001781 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001782 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001783};
1784
1785std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1786
Vladimir Markof9f64412015-09-02 14:05:49 +01001787class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001788 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001789 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001790 : previous_(nullptr),
1791 next_(nullptr),
1792 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001793 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001794 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001795 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001796 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001797 locations_(nullptr),
1798 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001799 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001800 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001801 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001802
Dave Allison20dfc792014-06-16 20:44:29 -07001803 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001804
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001805#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001806 enum InstructionKind {
1807 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1808 };
1809#undef DECLARE_KIND
1810
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001811 HInstruction* GetNext() const { return next_; }
1812 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001813
Calin Juravle77520bc2015-01-12 18:45:46 +00001814 HInstruction* GetNextDisregardingMoves() const;
1815 HInstruction* GetPreviousDisregardingMoves() const;
1816
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001817 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001818 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001819 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001820 bool IsInBlock() const { return block_ != nullptr; }
1821 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001822 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001823
Roland Levillain6b879dd2014-09-22 17:13:44 +01001824 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001825 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001826
1827 virtual void Accept(HGraphVisitor* visitor) = 0;
1828 virtual const char* DebugName() const = 0;
1829
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001830 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001831 void SetRawInputAt(size_t index, HInstruction* input) {
1832 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1833 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001834
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001835 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001836
1837 uint32_t GetDexPc() const { return dex_pc_; }
1838
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001839 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001840
Roland Levillaine161a2a2014-10-03 12:45:18 +01001841 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001842 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001843
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001844 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001845 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001846
Calin Juravle10e244f2015-01-26 18:54:32 +00001847 // Does not apply for all instructions, but having this at top level greatly
1848 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001849 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001850 virtual bool CanBeNull() const {
1851 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1852 return true;
1853 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001854
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001855 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001856 return false;
1857 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001858
Calin Juravle2e768302015-07-28 14:41:11 +00001859 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001860
Calin Juravle61d544b2015-02-23 16:46:57 +00001861 ReferenceTypeInfo GetReferenceTypeInfo() const {
1862 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1863 return reference_type_info_;
1864 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001865
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001866 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001867 DCHECK(user != nullptr);
1868 HUseListNode<HInstruction*>* use =
1869 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1870 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001871 }
1872
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001873 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001874 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001875 HUseListNode<HEnvironment*>* env_use =
1876 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1877 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001878 }
1879
David Brazdil1abb4192015-02-17 18:33:36 +00001880 void RemoveAsUserOfInput(size_t input) {
1881 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1882 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1883 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001884
David Brazdil1abb4192015-02-17 18:33:36 +00001885 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1886 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001887
David Brazdiled596192015-01-23 10:39:45 +00001888 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1889 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001890 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001891 bool HasOnlyOneNonEnvironmentUse() const {
1892 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1893 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001894
Roland Levillain6c82d402014-10-13 16:10:27 +01001895 // Does this instruction strictly dominate `other_instruction`?
1896 // Returns false if this instruction and `other_instruction` are the same.
1897 // Aborts if this instruction and `other_instruction` are both phis.
1898 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001899
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001900 int GetId() const { return id_; }
1901 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001902
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001903 int GetSsaIndex() const { return ssa_index_; }
1904 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1905 bool HasSsaIndex() const { return ssa_index_ != -1; }
1906
1907 bool HasEnvironment() const { return environment_ != nullptr; }
1908 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001909 // Set the `environment_` field. Raw because this method does not
1910 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001911 void SetRawEnvironment(HEnvironment* environment) {
1912 DCHECK(environment_ == nullptr);
1913 DCHECK_EQ(environment->GetHolder(), this);
1914 environment_ = environment;
1915 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001916
1917 // Set the environment of this instruction, copying it from `environment`. While
1918 // copying, the uses lists are being updated.
1919 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001920 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001921 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001922 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001923 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001924 if (environment->GetParent() != nullptr) {
1925 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1926 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001927 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001928
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001929 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1930 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001931 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001932 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001933 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001934 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001935 if (environment->GetParent() != nullptr) {
1936 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1937 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001938 }
1939
Nicolas Geoffray39468442014-09-02 15:17:15 +01001940 // Returns the number of entries in the environment. Typically, that is the
1941 // number of dex registers in a method. It could be more in case of inlining.
1942 size_t EnvironmentSize() const;
1943
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001944 LocationSummary* GetLocations() const { return locations_; }
1945 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001946
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001947 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001948 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001949
Alexandre Rames188d4312015-04-09 18:30:21 +01001950 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1951 // uses of this instruction by `other` are *not* updated.
1952 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1953 ReplaceWith(other);
1954 other->ReplaceInput(this, use_index);
1955 }
1956
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001957 // Move `this` instruction before `cursor`.
1958 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001959
Vladimir Markofb337ea2015-11-25 15:25:10 +00001960 // Move `this` before its first user and out of any loops. If there is no
1961 // out-of-loop user that dominates all other users, move the instruction
1962 // to the end of the out-of-loop common dominator of the user's blocks.
1963 //
1964 // This can be used only on non-throwing instructions with no side effects that
1965 // have at least one use but no environment uses.
1966 void MoveBeforeFirstUserAndOutOfLoops();
1967
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001968#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001969 bool Is##type() const { return (As##type() != nullptr); } \
1970 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001971 virtual H##type* As##type() { return nullptr; }
1972
1973 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1974#undef INSTRUCTION_TYPE_CHECK
1975
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001976 // Returns whether the instruction can be moved within the graph.
1977 virtual bool CanBeMoved() const { return false; }
1978
1979 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001980 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001981 return false;
1982 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001983
1984 // Returns whether any data encoded in the two instructions is equal.
1985 // This method does not look at the inputs. Both instructions must be
1986 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001987 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001988 return false;
1989 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001990
1991 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001992 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001993 // 2) Their inputs are identical.
1994 bool Equals(HInstruction* other) const;
1995
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001996 virtual InstructionKind GetKind() const = 0;
1997
1998 virtual size_t ComputeHashCode() const {
1999 size_t result = GetKind();
2000 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2001 result = (result * 31) + InputAt(i)->GetId();
2002 }
2003 return result;
2004 }
2005
2006 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002007 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002008
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002009 size_t GetLifetimePosition() const { return lifetime_position_; }
2010 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2011 LiveInterval* GetLiveInterval() const { return live_interval_; }
2012 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2013 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2014
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002015 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2016
2017 // Returns whether the code generation of the instruction will require to have access
2018 // to the current method. Such instructions are:
2019 // (1): Instructions that require an environment, as calling the runtime requires
2020 // to walk the stack and have the current method stored at a specific stack address.
2021 // (2): Object literals like classes and strings, that are loaded from the dex cache
2022 // fields of the current method.
2023 bool NeedsCurrentMethod() const {
2024 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2025 }
2026
Vladimir Markodc151b22015-10-15 18:02:30 +01002027 // Returns whether the code generation of the instruction will require to have access
2028 // to the dex cache of the current method's declaring class via the current method.
2029 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002030
Mark Mendellc4701932015-04-10 13:18:51 -04002031 // Does this instruction have any use in an environment before
2032 // control flow hits 'other'?
2033 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2034
2035 // Remove all references to environment uses of this instruction.
2036 // The caller must ensure that this is safe to do.
2037 void RemoveEnvironmentUsers();
2038
David Brazdil1abb4192015-02-17 18:33:36 +00002039 protected:
2040 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2041 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2042
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002043 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002044 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2045
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002046 HInstruction* previous_;
2047 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002048 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002049 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002050
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002051 // An instruction gets an id when it is added to the graph.
2052 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002053 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002054 int id_;
2055
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002056 // When doing liveness analysis, instructions that have uses get an SSA index.
2057 int ssa_index_;
2058
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002059 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002060 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002061
2062 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002063 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002064
Nicolas Geoffray39468442014-09-02 15:17:15 +01002065 // The environment associated with this instruction. Not null if the instruction
2066 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002067 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002068
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002069 // Set by the code generator.
2070 LocationSummary* locations_;
2071
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002072 // Set by the liveness analysis.
2073 LiveInterval* live_interval_;
2074
2075 // Set by the liveness analysis, this is the position in a linear
2076 // order of blocks where this instruction's live interval start.
2077 size_t lifetime_position_;
2078
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002079 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002080
Calin Juravleacf735c2015-02-12 15:25:22 +00002081 // TODO: for primitive types this should be marked as invalid.
2082 ReferenceTypeInfo reference_type_info_;
2083
David Brazdil1abb4192015-02-17 18:33:36 +00002084 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002085 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002086 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002087 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002088 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002089
2090 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2091};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002092std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002093
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002094class HInputIterator : public ValueObject {
2095 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002096 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002097
2098 bool Done() const { return index_ == instruction_->InputCount(); }
2099 HInstruction* Current() const { return instruction_->InputAt(index_); }
2100 void Advance() { index_++; }
2101
2102 private:
2103 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002104 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002105
2106 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2107};
2108
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002109class HInstructionIterator : public ValueObject {
2110 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002111 explicit HInstructionIterator(const HInstructionList& instructions)
2112 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002113 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114 }
2115
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002116 bool Done() const { return instruction_ == nullptr; }
2117 HInstruction* Current() const { return instruction_; }
2118 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002119 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002120 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002121 }
2122
2123 private:
2124 HInstruction* instruction_;
2125 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002126
2127 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002128};
2129
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002130class HBackwardInstructionIterator : public ValueObject {
2131 public:
2132 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2133 : instruction_(instructions.last_instruction_) {
2134 next_ = Done() ? nullptr : instruction_->GetPrevious();
2135 }
2136
2137 bool Done() const { return instruction_ == nullptr; }
2138 HInstruction* Current() const { return instruction_; }
2139 void Advance() {
2140 instruction_ = next_;
2141 next_ = Done() ? nullptr : instruction_->GetPrevious();
2142 }
2143
2144 private:
2145 HInstruction* instruction_;
2146 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002147
2148 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002149};
2150
Vladimir Markof9f64412015-09-02 14:05:49 +01002151template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002152class HTemplateInstruction: public HInstruction {
2153 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002154 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002155 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002156 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002157
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002158 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002159
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002160 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002161 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2162 DCHECK_LT(i, N);
2163 return inputs_[i];
2164 }
David Brazdil1abb4192015-02-17 18:33:36 +00002165
2166 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002167 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002168 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002169 }
2170
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002171 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002172 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002173
2174 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002175};
2176
Vladimir Markof9f64412015-09-02 14:05:49 +01002177// HTemplateInstruction specialization for N=0.
2178template<>
2179class HTemplateInstruction<0>: public HInstruction {
2180 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002181 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002182 : HInstruction(side_effects, dex_pc) {}
2183
Vladimir Markof9f64412015-09-02 14:05:49 +01002184 virtual ~HTemplateInstruction() {}
2185
2186 size_t InputCount() const OVERRIDE { return 0; }
2187
2188 protected:
2189 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2190 LOG(FATAL) << "Unreachable";
2191 UNREACHABLE();
2192 }
2193
2194 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2195 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2196 LOG(FATAL) << "Unreachable";
2197 UNREACHABLE();
2198 }
2199
2200 private:
2201 friend class SsaBuilder;
2202};
2203
Dave Allison20dfc792014-06-16 20:44:29 -07002204template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002205class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002206 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002207 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002208 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002209 virtual ~HExpression() {}
2210
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002211 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002212
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002213 protected:
2214 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002215};
2216
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002217// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2218// instruction that branches to the exit block.
2219class HReturnVoid : public HTemplateInstruction<0> {
2220 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002221 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2222 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002223
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002224 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002225
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002226 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002227
2228 private:
2229 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2230};
2231
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002232// Represents dex's RETURN opcodes. A HReturn is a control flow
2233// instruction that branches to the exit block.
2234class HReturn : public HTemplateInstruction<1> {
2235 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002236 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2237 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002238 SetRawInputAt(0, value);
2239 }
2240
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002241 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002242
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002243 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002244
2245 private:
2246 DISALLOW_COPY_AND_ASSIGN(HReturn);
2247};
2248
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002249// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002250// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002251// exit block.
2252class HExit : public HTemplateInstruction<0> {
2253 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002254 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002255
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002256 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002257
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002258 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002259
2260 private:
2261 DISALLOW_COPY_AND_ASSIGN(HExit);
2262};
2263
2264// Jumps from one block to another.
2265class HGoto : public HTemplateInstruction<0> {
2266 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002267 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002268
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002269 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002270
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002271 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002272 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002273 }
2274
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002275 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002276
2277 private:
2278 DISALLOW_COPY_AND_ASSIGN(HGoto);
2279};
2280
Roland Levillain9867bc72015-08-05 10:21:34 +01002281class HConstant : public HExpression<0> {
2282 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002283 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2284 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002285
2286 bool CanBeMoved() const OVERRIDE { return true; }
2287
2288 virtual bool IsMinusOne() const { return false; }
2289 virtual bool IsZero() const { return false; }
2290 virtual bool IsOne() const { return false; }
2291
David Brazdil77a48ae2015-09-15 12:34:04 +00002292 virtual uint64_t GetValueAsUint64() const = 0;
2293
Roland Levillain9867bc72015-08-05 10:21:34 +01002294 DECLARE_INSTRUCTION(Constant);
2295
2296 private:
2297 DISALLOW_COPY_AND_ASSIGN(HConstant);
2298};
2299
2300class HNullConstant : public HConstant {
2301 public:
2302 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2303 return true;
2304 }
2305
David Brazdil77a48ae2015-09-15 12:34:04 +00002306 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2307
Roland Levillain9867bc72015-08-05 10:21:34 +01002308 size_t ComputeHashCode() const OVERRIDE { return 0; }
2309
2310 DECLARE_INSTRUCTION(NullConstant);
2311
2312 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002313 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002314
2315 friend class HGraph;
2316 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2317};
2318
2319// Constants of the type int. Those can be from Dex instructions, or
2320// synthesized (for example with the if-eqz instruction).
2321class HIntConstant : public HConstant {
2322 public:
2323 int32_t GetValue() const { return value_; }
2324
David Brazdil9f389d42015-10-01 14:32:56 +01002325 uint64_t GetValueAsUint64() const OVERRIDE {
2326 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2327 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002328
Roland Levillain9867bc72015-08-05 10:21:34 +01002329 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2330 DCHECK(other->IsIntConstant());
2331 return other->AsIntConstant()->value_ == value_;
2332 }
2333
2334 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2335
2336 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2337 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2338 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2339
2340 DECLARE_INSTRUCTION(IntConstant);
2341
2342 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002343 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2344 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2345 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2346 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002347
2348 const int32_t value_;
2349
2350 friend class HGraph;
2351 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2352 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2353 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2354};
2355
2356class HLongConstant : public HConstant {
2357 public:
2358 int64_t GetValue() const { return value_; }
2359
David Brazdil77a48ae2015-09-15 12:34:04 +00002360 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2361
Roland Levillain9867bc72015-08-05 10:21:34 +01002362 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2363 DCHECK(other->IsLongConstant());
2364 return other->AsLongConstant()->value_ == value_;
2365 }
2366
2367 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2368
2369 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2370 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2371 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2372
2373 DECLARE_INSTRUCTION(LongConstant);
2374
2375 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002376 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2377 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002378
2379 const int64_t value_;
2380
2381 friend class HGraph;
2382 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2383};
Dave Allison20dfc792014-06-16 20:44:29 -07002384
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002385// Conditional branch. A block ending with an HIf instruction must have
2386// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002387class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002388 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002389 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2390 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002391 SetRawInputAt(0, input);
2392 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002393
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002394 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002395
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002396 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002397 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002398 }
2399
2400 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002401 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002402 }
2403
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002404 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002405
2406 private:
2407 DISALLOW_COPY_AND_ASSIGN(HIf);
2408};
2409
David Brazdilfc6a86a2015-06-26 10:33:45 +00002410
2411// Abstract instruction which marks the beginning and/or end of a try block and
2412// links it to the respective exception handlers. Behaves the same as a Goto in
2413// non-exceptional control flow.
2414// Normal-flow successor is stored at index zero, exception handlers under
2415// higher indices in no particular order.
2416class HTryBoundary : public HTemplateInstruction<0> {
2417 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002418 enum BoundaryKind {
2419 kEntry,
2420 kExit,
2421 };
2422
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002423 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2424 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002425
2426 bool IsControlFlow() const OVERRIDE { return true; }
2427
2428 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002429 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002430
David Brazdild26a4112015-11-10 11:07:31 +00002431 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2432 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2433 }
2434
David Brazdilfc6a86a2015-06-26 10:33:45 +00002435 // Returns whether `handler` is among its exception handlers (non-zero index
2436 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002437 bool HasExceptionHandler(const HBasicBlock& handler) const {
2438 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002439 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002440 }
2441
2442 // If not present already, adds `handler` to its block's list of exception
2443 // handlers.
2444 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002445 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002446 GetBlock()->AddSuccessor(handler);
2447 }
2448 }
2449
David Brazdil56e1acc2015-06-30 15:41:36 +01002450 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002451
David Brazdilffee3d32015-07-06 11:48:53 +01002452 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2453
David Brazdilfc6a86a2015-06-26 10:33:45 +00002454 DECLARE_INSTRUCTION(TryBoundary);
2455
2456 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002457 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002458
2459 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2460};
2461
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002462// Deoptimize to interpreter, upon checking a condition.
2463class HDeoptimize : public HTemplateInstruction<1> {
2464 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002465 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2466 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002467 SetRawInputAt(0, cond);
2468 }
2469
2470 bool NeedsEnvironment() const OVERRIDE { return true; }
2471 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002472
2473 DECLARE_INSTRUCTION(Deoptimize);
2474
2475 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002476 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2477};
2478
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002479// Represents the ArtMethod that was passed as a first argument to
2480// the method. It is used by instructions that depend on it, like
2481// instructions that work with the dex cache.
2482class HCurrentMethod : public HExpression<0> {
2483 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002484 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2485 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002486
2487 DECLARE_INSTRUCTION(CurrentMethod);
2488
2489 private:
2490 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2491};
2492
Mark Mendellfe57faa2015-09-18 09:26:15 -04002493// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2494// have one successor for each entry in the switch table, and the final successor
2495// will be the block containing the next Dex opcode.
2496class HPackedSwitch : public HTemplateInstruction<1> {
2497 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002498 HPackedSwitch(int32_t start_value,
2499 uint32_t num_entries,
2500 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002501 uint32_t dex_pc = kNoDexPc)
2502 : HTemplateInstruction(SideEffects::None(), dex_pc),
2503 start_value_(start_value),
2504 num_entries_(num_entries) {
2505 SetRawInputAt(0, input);
2506 }
2507
2508 bool IsControlFlow() const OVERRIDE { return true; }
2509
2510 int32_t GetStartValue() const { return start_value_; }
2511
Vladimir Marko211c2112015-09-24 16:52:33 +01002512 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002513
2514 HBasicBlock* GetDefaultBlock() const {
2515 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002516 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002517 }
2518 DECLARE_INSTRUCTION(PackedSwitch);
2519
2520 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002521 const int32_t start_value_;
2522 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002523
2524 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2525};
2526
Roland Levillain88cb1752014-10-20 16:36:47 +01002527class HUnaryOperation : public HExpression<1> {
2528 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002529 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2530 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002531 SetRawInputAt(0, input);
2532 }
2533
2534 HInstruction* GetInput() const { return InputAt(0); }
2535 Primitive::Type GetResultType() const { return GetType(); }
2536
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002537 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002538 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002539 return true;
2540 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002541
Roland Levillain9240d6a2014-10-20 16:47:04 +01002542 // Try to statically evaluate `operation` and return a HConstant
2543 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002544 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002545 HConstant* TryStaticEvaluation() const;
2546
2547 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002548 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2549 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002550
Roland Levillain88cb1752014-10-20 16:36:47 +01002551 DECLARE_INSTRUCTION(UnaryOperation);
2552
2553 private:
2554 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2555};
2556
Dave Allison20dfc792014-06-16 20:44:29 -07002557class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002558 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002559 HBinaryOperation(Primitive::Type result_type,
2560 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002561 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002562 SideEffects side_effects = SideEffects::None(),
2563 uint32_t dex_pc = kNoDexPc)
2564 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002565 SetRawInputAt(0, left);
2566 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002567 }
2568
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002569 HInstruction* GetLeft() const { return InputAt(0); }
2570 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002571 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002572
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002573 virtual bool IsCommutative() const { return false; }
2574
2575 // Put constant on the right.
2576 // Returns whether order is changed.
2577 bool OrderInputsWithConstantOnTheRight() {
2578 HInstruction* left = InputAt(0);
2579 HInstruction* right = InputAt(1);
2580 if (left->IsConstant() && !right->IsConstant()) {
2581 ReplaceInput(right, 0);
2582 ReplaceInput(left, 1);
2583 return true;
2584 }
2585 return false;
2586 }
2587
2588 // Order inputs by instruction id, but favor constant on the right side.
2589 // This helps GVN for commutative ops.
2590 void OrderInputs() {
2591 DCHECK(IsCommutative());
2592 HInstruction* left = InputAt(0);
2593 HInstruction* right = InputAt(1);
2594 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2595 return;
2596 }
2597 if (OrderInputsWithConstantOnTheRight()) {
2598 return;
2599 }
2600 // Order according to instruction id.
2601 if (left->GetId() > right->GetId()) {
2602 ReplaceInput(right, 0);
2603 ReplaceInput(left, 1);
2604 }
2605 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002606
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002607 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002608 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002609 return true;
2610 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002611
Roland Levillain9240d6a2014-10-20 16:47:04 +01002612 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002613 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002614 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002615 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002616
2617 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002618 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2619 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2620 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2621 HLongConstant* y ATTRIBUTE_UNUSED) const {
2622 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2623 return nullptr;
2624 }
2625 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2626 HIntConstant* y ATTRIBUTE_UNUSED) const {
2627 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2628 return nullptr;
2629 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002630 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2631 HNullConstant* y ATTRIBUTE_UNUSED) const {
2632 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2633 return nullptr;
2634 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002635
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002636 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002637 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002638 HConstant* GetConstantRight() const;
2639
2640 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002641 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002642 HInstruction* GetLeastConstantLeft() const;
2643
Roland Levillainccc07a92014-09-16 14:48:16 +01002644 DECLARE_INSTRUCTION(BinaryOperation);
2645
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002646 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002647 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2648};
2649
Mark Mendellc4701932015-04-10 13:18:51 -04002650// The comparison bias applies for floating point operations and indicates how NaN
2651// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002652enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002653 kNoBias, // bias is not applicable (i.e. for long operation)
2654 kGtBias, // return 1 for NaN comparisons
2655 kLtBias, // return -1 for NaN comparisons
2656};
2657
Dave Allison20dfc792014-06-16 20:44:29 -07002658class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002659 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002660 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2661 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002662 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002663 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002664
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002665 bool NeedsMaterialization() const { return needs_materialization_; }
2666 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002667
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002668 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002669 // `instruction`, and disregard moves in between.
2670 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002671
Dave Allison20dfc792014-06-16 20:44:29 -07002672 DECLARE_INSTRUCTION(Condition);
2673
2674 virtual IfCondition GetCondition() const = 0;
2675
Mark Mendellc4701932015-04-10 13:18:51 -04002676 virtual IfCondition GetOppositeCondition() const = 0;
2677
Roland Levillain4fa13f62015-07-06 18:11:54 +01002678 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002679
2680 void SetBias(ComparisonBias bias) { bias_ = bias; }
2681
2682 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2683 return bias_ == other->AsCondition()->bias_;
2684 }
2685
Roland Levillain4fa13f62015-07-06 18:11:54 +01002686 bool IsFPConditionTrueIfNaN() const {
2687 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2688 IfCondition if_cond = GetCondition();
2689 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2690 }
2691
2692 bool IsFPConditionFalseIfNaN() const {
2693 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2694 IfCondition if_cond = GetCondition();
2695 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2696 }
2697
Dave Allison20dfc792014-06-16 20:44:29 -07002698 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002699 // For register allocation purposes, returns whether this instruction needs to be
2700 // materialized (that is, not just be in the processor flags).
2701 bool needs_materialization_;
2702
Mark Mendellc4701932015-04-10 13:18:51 -04002703 // Needed if we merge a HCompare into a HCondition.
2704 ComparisonBias bias_;
2705
Dave Allison20dfc792014-06-16 20:44:29 -07002706 DISALLOW_COPY_AND_ASSIGN(HCondition);
2707};
2708
2709// Instruction to check if two inputs are equal to each other.
2710class HEqual : public HCondition {
2711 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002712 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2713 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002714
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002715 bool IsCommutative() const OVERRIDE { return true; }
2716
Roland Levillain9867bc72015-08-05 10:21:34 +01002717 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002718 return GetBlock()->GetGraph()->GetIntConstant(
2719 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002720 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002721 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002722 return GetBlock()->GetGraph()->GetIntConstant(
2723 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002724 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002725 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2726 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002727 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002728 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002729
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002730 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002731
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002732 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002733 return kCondEQ;
2734 }
2735
Mark Mendellc4701932015-04-10 13:18:51 -04002736 IfCondition GetOppositeCondition() const OVERRIDE {
2737 return kCondNE;
2738 }
2739
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002740 private:
Aart Bike9f37602015-10-09 11:15:55 -07002741 template <typename T> bool Compute(T x, T y) const { return x == y; }
2742
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002743 DISALLOW_COPY_AND_ASSIGN(HEqual);
2744};
2745
Dave Allison20dfc792014-06-16 20:44:29 -07002746class HNotEqual : public HCondition {
2747 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002748 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2749 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002750
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002751 bool IsCommutative() const OVERRIDE { return true; }
2752
Roland Levillain9867bc72015-08-05 10:21:34 +01002753 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002754 return GetBlock()->GetGraph()->GetIntConstant(
2755 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002756 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002757 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002758 return GetBlock()->GetGraph()->GetIntConstant(
2759 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002760 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002761 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2762 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002763 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002764 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002765
Dave Allison20dfc792014-06-16 20:44:29 -07002766 DECLARE_INSTRUCTION(NotEqual);
2767
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002768 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002769 return kCondNE;
2770 }
2771
Mark Mendellc4701932015-04-10 13:18:51 -04002772 IfCondition GetOppositeCondition() const OVERRIDE {
2773 return kCondEQ;
2774 }
2775
Dave Allison20dfc792014-06-16 20:44:29 -07002776 private:
Aart Bike9f37602015-10-09 11:15:55 -07002777 template <typename T> bool Compute(T x, T y) const { return x != y; }
2778
Dave Allison20dfc792014-06-16 20:44:29 -07002779 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2780};
2781
2782class HLessThan : public HCondition {
2783 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002784 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2785 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002786
Roland Levillain9867bc72015-08-05 10:21:34 +01002787 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002788 return GetBlock()->GetGraph()->GetIntConstant(
2789 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002790 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002791 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002792 return GetBlock()->GetGraph()->GetIntConstant(
2793 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002794 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002795
Dave Allison20dfc792014-06-16 20:44:29 -07002796 DECLARE_INSTRUCTION(LessThan);
2797
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002798 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002799 return kCondLT;
2800 }
2801
Mark Mendellc4701932015-04-10 13:18:51 -04002802 IfCondition GetOppositeCondition() const OVERRIDE {
2803 return kCondGE;
2804 }
2805
Dave Allison20dfc792014-06-16 20:44:29 -07002806 private:
Aart Bike9f37602015-10-09 11:15:55 -07002807 template <typename T> bool Compute(T x, T y) const { return x < y; }
2808
Dave Allison20dfc792014-06-16 20:44:29 -07002809 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2810};
2811
2812class HLessThanOrEqual : public HCondition {
2813 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002814 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2815 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002816
Roland Levillain9867bc72015-08-05 10:21:34 +01002817 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002818 return GetBlock()->GetGraph()->GetIntConstant(
2819 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002820 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002821 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002822 return GetBlock()->GetGraph()->GetIntConstant(
2823 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002824 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002825
Dave Allison20dfc792014-06-16 20:44:29 -07002826 DECLARE_INSTRUCTION(LessThanOrEqual);
2827
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002828 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002829 return kCondLE;
2830 }
2831
Mark Mendellc4701932015-04-10 13:18:51 -04002832 IfCondition GetOppositeCondition() const OVERRIDE {
2833 return kCondGT;
2834 }
2835
Dave Allison20dfc792014-06-16 20:44:29 -07002836 private:
Aart Bike9f37602015-10-09 11:15:55 -07002837 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2838
Dave Allison20dfc792014-06-16 20:44:29 -07002839 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2840};
2841
2842class HGreaterThan : public HCondition {
2843 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002844 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2845 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002846
Roland Levillain9867bc72015-08-05 10:21:34 +01002847 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002848 return GetBlock()->GetGraph()->GetIntConstant(
2849 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002850 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002851 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002852 return GetBlock()->GetGraph()->GetIntConstant(
2853 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002854 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002855
Dave Allison20dfc792014-06-16 20:44:29 -07002856 DECLARE_INSTRUCTION(GreaterThan);
2857
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002858 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002859 return kCondGT;
2860 }
2861
Mark Mendellc4701932015-04-10 13:18:51 -04002862 IfCondition GetOppositeCondition() const OVERRIDE {
2863 return kCondLE;
2864 }
2865
Dave Allison20dfc792014-06-16 20:44:29 -07002866 private:
Aart Bike9f37602015-10-09 11:15:55 -07002867 template <typename T> bool Compute(T x, T y) const { return x > y; }
2868
Dave Allison20dfc792014-06-16 20:44:29 -07002869 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2870};
2871
2872class HGreaterThanOrEqual : public HCondition {
2873 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002874 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2875 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002876
Roland Levillain9867bc72015-08-05 10:21:34 +01002877 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002878 return GetBlock()->GetGraph()->GetIntConstant(
2879 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002880 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002881 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002882 return GetBlock()->GetGraph()->GetIntConstant(
2883 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002884 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002885
Dave Allison20dfc792014-06-16 20:44:29 -07002886 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2887
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002888 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002889 return kCondGE;
2890 }
2891
Mark Mendellc4701932015-04-10 13:18:51 -04002892 IfCondition GetOppositeCondition() const OVERRIDE {
2893 return kCondLT;
2894 }
2895
Dave Allison20dfc792014-06-16 20:44:29 -07002896 private:
Aart Bike9f37602015-10-09 11:15:55 -07002897 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2898
Dave Allison20dfc792014-06-16 20:44:29 -07002899 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2900};
2901
Aart Bike9f37602015-10-09 11:15:55 -07002902class HBelow : public HCondition {
2903 public:
2904 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2905 : HCondition(first, second, dex_pc) {}
2906
2907 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2908 return GetBlock()->GetGraph()->GetIntConstant(
2909 Compute(static_cast<uint32_t>(x->GetValue()),
2910 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2911 }
2912 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2913 return GetBlock()->GetGraph()->GetIntConstant(
2914 Compute(static_cast<uint64_t>(x->GetValue()),
2915 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2916 }
2917
2918 DECLARE_INSTRUCTION(Below);
2919
2920 IfCondition GetCondition() const OVERRIDE {
2921 return kCondB;
2922 }
2923
2924 IfCondition GetOppositeCondition() const OVERRIDE {
2925 return kCondAE;
2926 }
2927
2928 private:
2929 template <typename T> bool Compute(T x, T y) const { return x < y; }
2930
2931 DISALLOW_COPY_AND_ASSIGN(HBelow);
2932};
2933
2934class HBelowOrEqual : public HCondition {
2935 public:
2936 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2937 : HCondition(first, second, dex_pc) {}
2938
2939 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2940 return GetBlock()->GetGraph()->GetIntConstant(
2941 Compute(static_cast<uint32_t>(x->GetValue()),
2942 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2943 }
2944 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2945 return GetBlock()->GetGraph()->GetIntConstant(
2946 Compute(static_cast<uint64_t>(x->GetValue()),
2947 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2948 }
2949
2950 DECLARE_INSTRUCTION(BelowOrEqual);
2951
2952 IfCondition GetCondition() const OVERRIDE {
2953 return kCondBE;
2954 }
2955
2956 IfCondition GetOppositeCondition() const OVERRIDE {
2957 return kCondA;
2958 }
2959
2960 private:
2961 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2962
2963 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2964};
2965
2966class HAbove : public HCondition {
2967 public:
2968 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2969 : HCondition(first, second, dex_pc) {}
2970
2971 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2972 return GetBlock()->GetGraph()->GetIntConstant(
2973 Compute(static_cast<uint32_t>(x->GetValue()),
2974 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2975 }
2976 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2977 return GetBlock()->GetGraph()->GetIntConstant(
2978 Compute(static_cast<uint64_t>(x->GetValue()),
2979 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2980 }
2981
2982 DECLARE_INSTRUCTION(Above);
2983
2984 IfCondition GetCondition() const OVERRIDE {
2985 return kCondA;
2986 }
2987
2988 IfCondition GetOppositeCondition() const OVERRIDE {
2989 return kCondBE;
2990 }
2991
2992 private:
2993 template <typename T> bool Compute(T x, T y) const { return x > y; }
2994
2995 DISALLOW_COPY_AND_ASSIGN(HAbove);
2996};
2997
2998class HAboveOrEqual : public HCondition {
2999 public:
3000 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3001 : HCondition(first, second, dex_pc) {}
3002
3003 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3004 return GetBlock()->GetGraph()->GetIntConstant(
3005 Compute(static_cast<uint32_t>(x->GetValue()),
3006 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3007 }
3008 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3009 return GetBlock()->GetGraph()->GetIntConstant(
3010 Compute(static_cast<uint64_t>(x->GetValue()),
3011 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3012 }
3013
3014 DECLARE_INSTRUCTION(AboveOrEqual);
3015
3016 IfCondition GetCondition() const OVERRIDE {
3017 return kCondAE;
3018 }
3019
3020 IfCondition GetOppositeCondition() const OVERRIDE {
3021 return kCondB;
3022 }
3023
3024 private:
3025 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3026
3027 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3028};
Dave Allison20dfc792014-06-16 20:44:29 -07003029
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003030// Instruction to check how two inputs compare to each other.
3031// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3032class HCompare : public HBinaryOperation {
3033 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003034 HCompare(Primitive::Type type,
3035 HInstruction* first,
3036 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003037 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003038 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003039 : HBinaryOperation(Primitive::kPrimInt,
3040 first,
3041 second,
3042 SideEffectsForArchRuntimeCalls(type),
3043 dex_pc),
3044 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003045 DCHECK_EQ(type, first->GetType());
3046 DCHECK_EQ(type, second->GetType());
3047 }
3048
Roland Levillain9867bc72015-08-05 10:21:34 +01003049 template <typename T>
3050 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003051
Roland Levillain9867bc72015-08-05 10:21:34 +01003052 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003053 return GetBlock()->GetGraph()->GetIntConstant(
3054 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003055 }
3056 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003057 return GetBlock()->GetGraph()->GetIntConstant(
3058 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003059 }
3060
Calin Juravleddb7df22014-11-25 20:56:51 +00003061 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3062 return bias_ == other->AsCompare()->bias_;
3063 }
3064
Mark Mendellc4701932015-04-10 13:18:51 -04003065 ComparisonBias GetBias() const { return bias_; }
3066
Roland Levillain4fa13f62015-07-06 18:11:54 +01003067 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003068
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003069
3070 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3071 // MIPS64 uses a runtime call for FP comparisons.
3072 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3073 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003074
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003075 DECLARE_INSTRUCTION(Compare);
3076
3077 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003078 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003079
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003080 DISALLOW_COPY_AND_ASSIGN(HCompare);
3081};
3082
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003083// A local in the graph. Corresponds to a Dex register.
3084class HLocal : public HTemplateInstruction<0> {
3085 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003086 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003087 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003088
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003089 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003090
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003091 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003092
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003093 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003094 // The Dex register number.
3095 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003096
3097 DISALLOW_COPY_AND_ASSIGN(HLocal);
3098};
3099
3100// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003101class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003102 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003103 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3104 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003105 SetRawInputAt(0, local);
3106 }
3107
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003108 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3109
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003110 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003111
3112 private:
3113 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3114};
3115
3116// Store a value in a given local. This instruction has two inputs: the value
3117// and the local.
3118class HStoreLocal : public HTemplateInstruction<2> {
3119 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003120 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3121 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003122 SetRawInputAt(0, local);
3123 SetRawInputAt(1, value);
3124 }
3125
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003126 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3127
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003128 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003129
3130 private:
3131 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3132};
3133
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003134class HFloatConstant : public HConstant {
3135 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003136 float GetValue() const { return value_; }
3137
David Brazdil77a48ae2015-09-15 12:34:04 +00003138 uint64_t GetValueAsUint64() const OVERRIDE {
3139 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3140 }
3141
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003142 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003143 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003144 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003145 }
3146
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003147 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003148
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003149 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003150 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003151 }
3152 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003153 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003154 }
3155 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003156 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3157 }
3158 bool IsNaN() const {
3159 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003160 }
3161
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003162 DECLARE_INSTRUCTION(FloatConstant);
3163
3164 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003165 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3166 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3167 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3168 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003169
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003170 const float value_;
3171
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003172 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003173 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003174 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003175 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3176};
3177
3178class HDoubleConstant : public HConstant {
3179 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003180 double GetValue() const { return value_; }
3181
David Brazdil77a48ae2015-09-15 12:34:04 +00003182 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3183
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003184 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003185 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003186 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003187 }
3188
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003189 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003190
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003191 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003192 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003193 }
3194 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003195 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003196 }
3197 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003198 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3199 }
3200 bool IsNaN() const {
3201 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003202 }
3203
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003204 DECLARE_INSTRUCTION(DoubleConstant);
3205
3206 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003207 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3208 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3209 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3210 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003211
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003212 const double value_;
3213
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003214 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003215 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003216 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003217 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3218};
3219
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003220enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003221#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003222#include "intrinsics_list.h"
3223 kNone,
3224 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3225#undef INTRINSICS_LIST
3226#undef OPTIMIZING_INTRINSICS
3227};
3228std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3229
Agi Csaki05f20562015-08-19 14:58:14 -07003230enum IntrinsicNeedsEnvironmentOrCache {
3231 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3232 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003233};
3234
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003235class HInvoke : public HInstruction {
3236 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003237 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003238
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003239 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003240
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003241 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003242 SetRawInputAt(index, argument);
3243 }
3244
Roland Levillain3e3d7332015-04-28 11:00:54 +01003245 // Return the number of arguments. This number can be lower than
3246 // the number of inputs returned by InputCount(), as some invoke
3247 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3248 // inputs at the end of their list of inputs.
3249 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3250
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003251 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003252
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003253
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003254 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003255 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003256
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003257 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3258
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003259 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003260 return intrinsic_;
3261 }
3262
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003263 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003264
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003265 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003266 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003267 }
3268
3269 bool CanThrow() const OVERRIDE { return true; }
3270
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003271 uint32_t* GetIntrinsicOptimizations() {
3272 return &intrinsic_optimizations_;
3273 }
3274
3275 const uint32_t* GetIntrinsicOptimizations() const {
3276 return &intrinsic_optimizations_;
3277 }
3278
3279 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3280
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003281 DECLARE_INSTRUCTION(Invoke);
3282
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003283 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003284 HInvoke(ArenaAllocator* arena,
3285 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003286 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003287 Primitive::Type return_type,
3288 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003289 uint32_t dex_method_index,
3290 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003291 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003292 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003293 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003294 inputs_(number_of_arguments + number_of_other_inputs,
3295 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003296 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003297 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003298 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003299 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003300 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003301 }
3302
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003303 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003304 return inputs_[index];
3305 }
3306
David Brazdil1abb4192015-02-17 18:33:36 +00003307 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003308 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003309 }
3310
Roland Levillain3e3d7332015-04-28 11:00:54 +01003311 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003312 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003313 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003314 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003315 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003316 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003317
3318 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3319 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003320
3321 private:
3322 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3323};
3324
Calin Juravle175dc732015-08-25 15:42:32 +01003325class HInvokeUnresolved : public HInvoke {
3326 public:
3327 HInvokeUnresolved(ArenaAllocator* arena,
3328 uint32_t number_of_arguments,
3329 Primitive::Type return_type,
3330 uint32_t dex_pc,
3331 uint32_t dex_method_index,
3332 InvokeType invoke_type)
3333 : HInvoke(arena,
3334 number_of_arguments,
3335 0u /* number_of_other_inputs */,
3336 return_type,
3337 dex_pc,
3338 dex_method_index,
3339 invoke_type) {
3340 }
3341
3342 DECLARE_INSTRUCTION(InvokeUnresolved);
3343
3344 private:
3345 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3346};
3347
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003348class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003349 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003350 // Requirements of this method call regarding the class
3351 // initialization (clinit) check of its declaring class.
3352 enum class ClinitCheckRequirement {
3353 kNone, // Class already initialized.
3354 kExplicit, // Static call having explicit clinit check as last input.
3355 kImplicit, // Static call implicitly requiring a clinit check.
3356 };
3357
Vladimir Marko58155012015-08-19 12:49:41 +00003358 // Determines how to load the target ArtMethod*.
3359 enum class MethodLoadKind {
3360 // Use a String init ArtMethod* loaded from Thread entrypoints.
3361 kStringInit,
3362
3363 // Use the method's own ArtMethod* loaded by the register allocator.
3364 kRecursive,
3365
3366 // Use ArtMethod* at a known address, embed the direct address in the code.
3367 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3368 kDirectAddress,
3369
3370 // Use ArtMethod* at an address that will be known at link time, embed the direct
3371 // address in the code. If the image is relocatable, emit .patch_oat entry.
3372 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3373 // the image relocatable or not.
3374 kDirectAddressWithFixup,
3375
3376 // Load from resoved methods array in the dex cache using a PC-relative load.
3377 // Used when we need to use the dex cache, for example for invoke-static that
3378 // may cause class initialization (the entry may point to a resolution method),
3379 // and we know that we can access the dex cache arrays using a PC-relative load.
3380 kDexCachePcRelative,
3381
3382 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3383 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3384 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3385 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3386 kDexCacheViaMethod,
3387 };
3388
3389 // Determines the location of the code pointer.
3390 enum class CodePtrLocation {
3391 // Recursive call, use local PC-relative call instruction.
3392 kCallSelf,
3393
3394 // Use PC-relative call instruction patched at link time.
3395 // Used for calls within an oat file, boot->boot or app->app.
3396 kCallPCRelative,
3397
3398 // Call to a known target address, embed the direct address in code.
3399 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3400 kCallDirect,
3401
3402 // Call to a target address that will be known at link time, embed the direct
3403 // address in code. If the image is relocatable, emit .patch_oat entry.
3404 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3405 // the image relocatable or not.
3406 kCallDirectWithFixup,
3407
3408 // Use code pointer from the ArtMethod*.
3409 // Used when we don't know the target code. This is also the last-resort-kind used when
3410 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3411 kCallArtMethod,
3412 };
3413
3414 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003415 MethodLoadKind method_load_kind;
3416 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003417 // The method load data holds
3418 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3419 // Note that there are multiple string init methods, each having its own offset.
3420 // - the method address for kDirectAddress
3421 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003422 uint64_t method_load_data;
3423 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003424 };
3425
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003426 HInvokeStaticOrDirect(ArenaAllocator* arena,
3427 uint32_t number_of_arguments,
3428 Primitive::Type return_type,
3429 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003430 uint32_t method_index,
3431 MethodReference target_method,
3432 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003433 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003434 InvokeType invoke_type,
3435 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003436 : HInvoke(arena,
3437 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003438 // There is potentially one extra argument for the HCurrentMethod node, and
3439 // potentially one other if the clinit check is explicit, and potentially
3440 // one other if the method is a string factory.
3441 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3442 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3443 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003444 return_type,
3445 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003446 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003447 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003448 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003449 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003450 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003451 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003452
Vladimir Markodc151b22015-10-15 18:02:30 +01003453 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003454 bool had_current_method_input = HasCurrentMethodInput();
3455 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3456
3457 // Using the current method is the default and once we find a better
3458 // method load kind, we should not go back to using the current method.
3459 DCHECK(had_current_method_input || !needs_current_method_input);
3460
3461 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003462 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3463 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003464 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003465 dispatch_info_ = dispatch_info;
3466 }
3467
Vladimir Markoc53c0792015-11-19 15:48:33 +00003468 void AddSpecialInput(HInstruction* input) {
3469 // We allow only one special input.
3470 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3471 DCHECK(InputCount() == GetSpecialInputIndex() ||
3472 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3473 InsertInputAt(GetSpecialInputIndex(), input);
3474 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003475
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003476 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003477 // We access the method via the dex cache so we can't do an implicit null check.
3478 // TODO: for intrinsics we can generate implicit null checks.
3479 return false;
3480 }
3481
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003482 bool CanBeNull() const OVERRIDE {
3483 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3484 }
3485
Vladimir Markoc53c0792015-11-19 15:48:33 +00003486 // Get the index of the special input, if any.
3487 //
3488 // If the invoke IsStringInit(), it initially has a HFakeString special argument
3489 // which is removed by the instruction simplifier; if the invoke HasCurrentMethodInput(),
3490 // the "special input" is the current method pointer; otherwise there may be one
3491 // platform-specific special input, such as PC-relative addressing base.
3492 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3493
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003494 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003495 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3496 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3497 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003498 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003499 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003500 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003501 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003502 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3503 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003504 bool HasCurrentMethodInput() const {
3505 // This function can be called only after the invoke has been fully initialized by the builder.
3506 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003507 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003508 return true;
3509 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003510 DCHECK(InputCount() == GetSpecialInputIndex() ||
3511 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003512 return false;
3513 }
3514 }
Vladimir Marko58155012015-08-19 12:49:41 +00003515 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3516 MethodReference GetTargetMethod() const { return target_method_; }
3517
3518 int32_t GetStringInitOffset() const {
3519 DCHECK(IsStringInit());
3520 return dispatch_info_.method_load_data;
3521 }
3522
3523 uint64_t GetMethodAddress() const {
3524 DCHECK(HasMethodAddress());
3525 return dispatch_info_.method_load_data;
3526 }
3527
3528 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003529 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003530 return dispatch_info_.method_load_data;
3531 }
3532
3533 uint64_t GetDirectCodePtr() const {
3534 DCHECK(HasDirectCodePtr());
3535 return dispatch_info_.direct_code_ptr;
3536 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003537
Calin Juravle68ad6492015-08-18 17:08:12 +01003538 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3539
Roland Levillain4c0eb422015-04-24 16:43:49 +01003540 // Is this instruction a call to a static method?
3541 bool IsStatic() const {
3542 return GetInvokeType() == kStatic;
3543 }
3544
Vladimir Markofbb184a2015-11-13 14:47:00 +00003545 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3546 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3547 // instruction; only relevant for static calls with explicit clinit check.
3548 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003549 DCHECK(IsStaticWithExplicitClinitCheck());
3550 size_t last_input_index = InputCount() - 1;
3551 HInstruction* last_input = InputAt(last_input_index);
3552 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003553 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003554 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003555 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003556 clinit_check_requirement_ = new_requirement;
3557 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003558 }
3559
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003560 bool IsStringFactoryFor(HFakeString* str) const {
3561 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003562 DCHECK(!HasCurrentMethodInput());
3563 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003564 return InputAt(InputCount() - 1)->AsFakeString() == str;
3565 }
3566
3567 void RemoveFakeStringArgumentAsLastInput() {
3568 DCHECK(IsStringInit());
3569 size_t last_input_index = InputCount() - 1;
3570 HInstruction* last_input = InputAt(last_input_index);
3571 DCHECK(last_input != nullptr);
3572 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3573 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003574 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003575 }
3576
Roland Levillain4c0eb422015-04-24 16:43:49 +01003577 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003578 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003579 bool IsStaticWithExplicitClinitCheck() const {
3580 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3581 }
3582
3583 // Is this a call to a static method whose declaring class has an
3584 // implicit intialization check requirement?
3585 bool IsStaticWithImplicitClinitCheck() const {
3586 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3587 }
3588
Vladimir Markob554b5a2015-11-06 12:57:55 +00003589 // Does this method load kind need the current method as an input?
3590 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3591 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3592 }
3593
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003594 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003595
Roland Levillain4c0eb422015-04-24 16:43:49 +01003596 protected:
3597 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3598 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3599 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3600 HInstruction* input = input_record.GetInstruction();
3601 // `input` is the last input of a static invoke marked as having
3602 // an explicit clinit check. It must either be:
3603 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3604 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3605 DCHECK(input != nullptr);
3606 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3607 }
3608 return input_record;
3609 }
3610
Vladimir Markoc53c0792015-11-19 15:48:33 +00003611 void InsertInputAt(size_t index, HInstruction* input);
3612 void RemoveInputAt(size_t index);
3613
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003614 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003615 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003616 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003617 // The target method may refer to different dex file or method index than the original
3618 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3619 // in derived class to increase visibility.
3620 MethodReference target_method_;
3621 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003622
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003623 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003624};
Vladimir Markofbb184a2015-11-13 14:47:00 +00003625std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003626
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003627class HInvokeVirtual : public HInvoke {
3628 public:
3629 HInvokeVirtual(ArenaAllocator* arena,
3630 uint32_t number_of_arguments,
3631 Primitive::Type return_type,
3632 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003633 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003634 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003635 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003636 vtable_index_(vtable_index) {}
3637
Calin Juravle641547a2015-04-21 22:08:51 +01003638 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003639 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003640 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003641 }
3642
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003643 uint32_t GetVTableIndex() const { return vtable_index_; }
3644
3645 DECLARE_INSTRUCTION(InvokeVirtual);
3646
3647 private:
3648 const uint32_t vtable_index_;
3649
3650 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3651};
3652
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003653class HInvokeInterface : public HInvoke {
3654 public:
3655 HInvokeInterface(ArenaAllocator* arena,
3656 uint32_t number_of_arguments,
3657 Primitive::Type return_type,
3658 uint32_t dex_pc,
3659 uint32_t dex_method_index,
3660 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003661 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003662 imt_index_(imt_index) {}
3663
Calin Juravle641547a2015-04-21 22:08:51 +01003664 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003665 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003666 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003667 }
3668
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003669 uint32_t GetImtIndex() const { return imt_index_; }
3670 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3671
3672 DECLARE_INSTRUCTION(InvokeInterface);
3673
3674 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003675 const uint32_t imt_index_;
3676
3677 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3678};
3679
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003680class HNewInstance : public HExpression<2> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003681 public:
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003682 HNewInstance(HInstruction* cls,
3683 HCurrentMethod* current_method,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003684 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003685 uint16_t type_index,
3686 const DexFile& dex_file,
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003687 bool can_throw,
3688 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003689 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003690 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003691 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003692 dex_file_(dex_file),
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003693 can_throw_(can_throw),
3694 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003695 entrypoint_(entrypoint) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003696 SetRawInputAt(0, cls);
3697 SetRawInputAt(1, current_method);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003698 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003699
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003700 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003701 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003702
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003703 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003704 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003705
3706 // It may throw when called on type that's not instantiable/accessible.
3707 // It can throw OOME.
3708 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3709 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3710
3711 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003712
Calin Juravle10e244f2015-01-26 18:54:32 +00003713 bool CanBeNull() const OVERRIDE { return false; }
3714
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003715 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3716
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003717 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3718 entrypoint_ = entrypoint;
3719 }
3720
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003721 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003722
3723 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003724 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003725 const DexFile& dex_file_;
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003726 const bool can_throw_;
3727 const bool finalizable_;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003728 QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003729
3730 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3731};
3732
Roland Levillain88cb1752014-10-20 16:36:47 +01003733class HNeg : public HUnaryOperation {
3734 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003735 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3736 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003737
Roland Levillain9867bc72015-08-05 10:21:34 +01003738 template <typename T> T Compute(T x) const { return -x; }
3739
3740 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003741 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003742 }
3743 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003744 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003745 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003746
Roland Levillain88cb1752014-10-20 16:36:47 +01003747 DECLARE_INSTRUCTION(Neg);
3748
3749 private:
3750 DISALLOW_COPY_AND_ASSIGN(HNeg);
3751};
3752
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003753class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003754 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003755 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003756 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003757 uint32_t dex_pc,
3758 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003759 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003760 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003761 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003762 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003763 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003764 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003765 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003766 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003767 }
3768
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003769 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003770 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003771
3772 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003773 bool NeedsEnvironment() const OVERRIDE { return true; }
3774
Mingyao Yang0c365e62015-03-31 15:09:29 -07003775 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3776 bool CanThrow() const OVERRIDE { return true; }
3777
Calin Juravle10e244f2015-01-26 18:54:32 +00003778 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003779
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003780 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3781
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003782 DECLARE_INSTRUCTION(NewArray);
3783
3784 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003785 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003786 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003787 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003788
3789 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3790};
3791
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003792class HAdd : public HBinaryOperation {
3793 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003794 HAdd(Primitive::Type result_type,
3795 HInstruction* left,
3796 HInstruction* right,
3797 uint32_t dex_pc = kNoDexPc)
3798 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003799
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003800 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003801
Roland Levillain9867bc72015-08-05 10:21:34 +01003802 template <typename T> T Compute(T x, T y) const { return x + y; }
3803
3804 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003805 return GetBlock()->GetGraph()->GetIntConstant(
3806 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003807 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003808 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003809 return GetBlock()->GetGraph()->GetLongConstant(
3810 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003811 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003812
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003813 DECLARE_INSTRUCTION(Add);
3814
3815 private:
3816 DISALLOW_COPY_AND_ASSIGN(HAdd);
3817};
3818
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003819class HSub : public HBinaryOperation {
3820 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003821 HSub(Primitive::Type result_type,
3822 HInstruction* left,
3823 HInstruction* right,
3824 uint32_t dex_pc = kNoDexPc)
3825 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003826
Roland Levillain9867bc72015-08-05 10:21:34 +01003827 template <typename T> T Compute(T x, T y) const { return x - y; }
3828
3829 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003830 return GetBlock()->GetGraph()->GetIntConstant(
3831 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003832 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003833 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003834 return GetBlock()->GetGraph()->GetLongConstant(
3835 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003836 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003837
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003838 DECLARE_INSTRUCTION(Sub);
3839
3840 private:
3841 DISALLOW_COPY_AND_ASSIGN(HSub);
3842};
3843
Calin Juravle34bacdf2014-10-07 20:23:36 +01003844class HMul : public HBinaryOperation {
3845 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003846 HMul(Primitive::Type result_type,
3847 HInstruction* left,
3848 HInstruction* right,
3849 uint32_t dex_pc = kNoDexPc)
3850 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003851
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003852 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003853
Roland Levillain9867bc72015-08-05 10:21:34 +01003854 template <typename T> T Compute(T x, T y) const { return x * y; }
3855
3856 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003857 return GetBlock()->GetGraph()->GetIntConstant(
3858 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003859 }
3860 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003861 return GetBlock()->GetGraph()->GetLongConstant(
3862 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003863 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003864
3865 DECLARE_INSTRUCTION(Mul);
3866
3867 private:
3868 DISALLOW_COPY_AND_ASSIGN(HMul);
3869};
3870
Calin Juravle7c4954d2014-10-28 16:57:40 +00003871class HDiv : public HBinaryOperation {
3872 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003873 HDiv(Primitive::Type result_type,
3874 HInstruction* left,
3875 HInstruction* right,
3876 uint32_t dex_pc)
3877 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003878
Roland Levillain9867bc72015-08-05 10:21:34 +01003879 template <typename T>
3880 T Compute(T x, T y) const {
3881 // Our graph structure ensures we never have 0 for `y` during
3882 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003883 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003884 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003885 return (y == -1) ? -x : x / y;
3886 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003887
Roland Levillain9867bc72015-08-05 10:21:34 +01003888 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003889 return GetBlock()->GetGraph()->GetIntConstant(
3890 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003891 }
3892 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003893 return GetBlock()->GetGraph()->GetLongConstant(
3894 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003895 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003896
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003897 static SideEffects SideEffectsForArchRuntimeCalls() {
3898 // The generated code can use a runtime call.
3899 return SideEffects::CanTriggerGC();
3900 }
3901
Calin Juravle7c4954d2014-10-28 16:57:40 +00003902 DECLARE_INSTRUCTION(Div);
3903
3904 private:
3905 DISALLOW_COPY_AND_ASSIGN(HDiv);
3906};
3907
Calin Juravlebacfec32014-11-14 15:54:36 +00003908class HRem : public HBinaryOperation {
3909 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003910 HRem(Primitive::Type result_type,
3911 HInstruction* left,
3912 HInstruction* right,
3913 uint32_t dex_pc)
3914 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003915
Roland Levillain9867bc72015-08-05 10:21:34 +01003916 template <typename T>
3917 T Compute(T x, T y) const {
3918 // Our graph structure ensures we never have 0 for `y` during
3919 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003920 DCHECK_NE(y, 0);
3921 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3922 return (y == -1) ? 0 : x % y;
3923 }
3924
Roland Levillain9867bc72015-08-05 10:21:34 +01003925 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003926 return GetBlock()->GetGraph()->GetIntConstant(
3927 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003928 }
3929 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003930 return GetBlock()->GetGraph()->GetLongConstant(
3931 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003932 }
3933
Calin Juravlebacfec32014-11-14 15:54:36 +00003934
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003935 static SideEffects SideEffectsForArchRuntimeCalls() {
3936 return SideEffects::CanTriggerGC();
3937 }
3938
Calin Juravlebacfec32014-11-14 15:54:36 +00003939 DECLARE_INSTRUCTION(Rem);
3940
3941 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003942 DISALLOW_COPY_AND_ASSIGN(HRem);
3943};
3944
Calin Juravled0d48522014-11-04 16:40:20 +00003945class HDivZeroCheck : public HExpression<1> {
3946 public:
3947 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003948 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003949 SetRawInputAt(0, value);
3950 }
3951
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003952 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3953
Calin Juravled0d48522014-11-04 16:40:20 +00003954 bool CanBeMoved() const OVERRIDE { return true; }
3955
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003956 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003957 return true;
3958 }
3959
3960 bool NeedsEnvironment() const OVERRIDE { return true; }
3961 bool CanThrow() const OVERRIDE { return true; }
3962
Calin Juravled0d48522014-11-04 16:40:20 +00003963 DECLARE_INSTRUCTION(DivZeroCheck);
3964
3965 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003966 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3967};
3968
Calin Juravle9aec02f2014-11-18 23:06:35 +00003969class HShl : public HBinaryOperation {
3970 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003971 HShl(Primitive::Type result_type,
3972 HInstruction* left,
3973 HInstruction* right,
3974 uint32_t dex_pc = kNoDexPc)
3975 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003976
Roland Levillain9867bc72015-08-05 10:21:34 +01003977 template <typename T, typename U, typename V>
3978 T Compute(T x, U y, V max_shift_value) const {
3979 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3980 "V is not the unsigned integer type corresponding to T");
3981 return x << (y & max_shift_value);
3982 }
3983
3984 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3985 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003986 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003987 }
3988 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3989 // case is handled as `x << static_cast<int>(y)`.
3990 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3991 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003992 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003993 }
3994 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3995 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003996 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003997 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003998
3999 DECLARE_INSTRUCTION(Shl);
4000
4001 private:
4002 DISALLOW_COPY_AND_ASSIGN(HShl);
4003};
4004
4005class HShr : public HBinaryOperation {
4006 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004007 HShr(Primitive::Type result_type,
4008 HInstruction* left,
4009 HInstruction* right,
4010 uint32_t dex_pc = kNoDexPc)
4011 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004012
Roland Levillain9867bc72015-08-05 10:21:34 +01004013 template <typename T, typename U, typename V>
4014 T Compute(T x, U y, V max_shift_value) const {
4015 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4016 "V is not the unsigned integer type corresponding to T");
4017 return x >> (y & max_shift_value);
4018 }
4019
4020 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4021 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004022 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004023 }
4024 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4025 // case is handled as `x >> static_cast<int>(y)`.
4026 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4027 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004028 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004029 }
4030 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4031 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004032 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004033 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004034
4035 DECLARE_INSTRUCTION(Shr);
4036
4037 private:
4038 DISALLOW_COPY_AND_ASSIGN(HShr);
4039};
4040
4041class HUShr : public HBinaryOperation {
4042 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004043 HUShr(Primitive::Type result_type,
4044 HInstruction* left,
4045 HInstruction* right,
4046 uint32_t dex_pc = kNoDexPc)
4047 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004048
Roland Levillain9867bc72015-08-05 10:21:34 +01004049 template <typename T, typename U, typename V>
4050 T Compute(T x, U y, V max_shift_value) const {
4051 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4052 "V is not the unsigned integer type corresponding to T");
4053 V ux = static_cast<V>(x);
4054 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004055 }
4056
Roland Levillain9867bc72015-08-05 10:21:34 +01004057 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4058 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004059 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004060 }
4061 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4062 // case is handled as `x >>> static_cast<int>(y)`.
4063 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4064 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004065 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004066 }
4067 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4068 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004069 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004070 }
4071
4072 DECLARE_INSTRUCTION(UShr);
4073
4074 private:
4075 DISALLOW_COPY_AND_ASSIGN(HUShr);
4076};
4077
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004078class HAnd : public HBinaryOperation {
4079 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004080 HAnd(Primitive::Type result_type,
4081 HInstruction* left,
4082 HInstruction* right,
4083 uint32_t dex_pc = kNoDexPc)
4084 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004085
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004086 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004087
Roland Levillain9867bc72015-08-05 10:21:34 +01004088 template <typename T, typename U>
4089 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4090
4091 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004092 return GetBlock()->GetGraph()->GetIntConstant(
4093 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004094 }
4095 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004096 return GetBlock()->GetGraph()->GetLongConstant(
4097 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004098 }
4099 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004100 return GetBlock()->GetGraph()->GetLongConstant(
4101 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004102 }
4103 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004104 return GetBlock()->GetGraph()->GetLongConstant(
4105 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004106 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004107
4108 DECLARE_INSTRUCTION(And);
4109
4110 private:
4111 DISALLOW_COPY_AND_ASSIGN(HAnd);
4112};
4113
4114class HOr : public HBinaryOperation {
4115 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004116 HOr(Primitive::Type result_type,
4117 HInstruction* left,
4118 HInstruction* right,
4119 uint32_t dex_pc = kNoDexPc)
4120 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004121
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004122 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004123
Roland Levillain9867bc72015-08-05 10:21:34 +01004124 template <typename T, typename U>
4125 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4126
4127 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004128 return GetBlock()->GetGraph()->GetIntConstant(
4129 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004130 }
4131 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004132 return GetBlock()->GetGraph()->GetLongConstant(
4133 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004134 }
4135 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004136 return GetBlock()->GetGraph()->GetLongConstant(
4137 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004138 }
4139 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004140 return GetBlock()->GetGraph()->GetLongConstant(
4141 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004142 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004143
4144 DECLARE_INSTRUCTION(Or);
4145
4146 private:
4147 DISALLOW_COPY_AND_ASSIGN(HOr);
4148};
4149
4150class HXor : public HBinaryOperation {
4151 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004152 HXor(Primitive::Type result_type,
4153 HInstruction* left,
4154 HInstruction* right,
4155 uint32_t dex_pc = kNoDexPc)
4156 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004157
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004158 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004159
Roland Levillain9867bc72015-08-05 10:21:34 +01004160 template <typename T, typename U>
4161 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4162
4163 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004164 return GetBlock()->GetGraph()->GetIntConstant(
4165 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004166 }
4167 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004168 return GetBlock()->GetGraph()->GetLongConstant(
4169 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004170 }
4171 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004172 return GetBlock()->GetGraph()->GetLongConstant(
4173 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004174 }
4175 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004176 return GetBlock()->GetGraph()->GetLongConstant(
4177 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004178 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004179
4180 DECLARE_INSTRUCTION(Xor);
4181
4182 private:
4183 DISALLOW_COPY_AND_ASSIGN(HXor);
4184};
4185
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004186// The value of a parameter in this method. Its location depends on
4187// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004188class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004189 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004190 HParameterValue(const DexFile& dex_file,
4191 uint16_t type_index,
4192 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004193 Primitive::Type parameter_type,
4194 bool is_this = false)
4195 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004196 dex_file_(dex_file),
4197 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004198 index_(index),
4199 is_this_(is_this),
4200 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004201
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004202 const DexFile& GetDexFile() const { return dex_file_; }
4203 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004204 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004205 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004206
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004207 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4208 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004209
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004210 DECLARE_INSTRUCTION(ParameterValue);
4211
4212 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004213 const DexFile& dex_file_;
4214 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004215 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004216 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004217 const uint8_t index_;
4218
Calin Juravle10e244f2015-01-26 18:54:32 +00004219 // Whether or not the parameter value corresponds to 'this' argument.
4220 const bool is_this_;
4221
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004222 bool can_be_null_;
4223
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004224 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4225};
4226
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004227class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004228 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004229 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4230 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004231
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004232 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004233 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004234 return true;
4235 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004236
Roland Levillain9867bc72015-08-05 10:21:34 +01004237 template <typename T> T Compute(T x) const { return ~x; }
4238
4239 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004240 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004241 }
4242 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004243 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004244 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004245
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004246 DECLARE_INSTRUCTION(Not);
4247
4248 private:
4249 DISALLOW_COPY_AND_ASSIGN(HNot);
4250};
4251
David Brazdil66d126e2015-04-03 16:02:44 +01004252class HBooleanNot : public HUnaryOperation {
4253 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004254 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4255 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004256
4257 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004258 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004259 return true;
4260 }
4261
Roland Levillain9867bc72015-08-05 10:21:34 +01004262 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004263 DCHECK(IsUint<1>(x));
4264 return !x;
4265 }
4266
Roland Levillain9867bc72015-08-05 10:21:34 +01004267 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004268 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004269 }
4270 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4271 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004272 UNREACHABLE();
4273 }
4274
4275 DECLARE_INSTRUCTION(BooleanNot);
4276
4277 private:
4278 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4279};
4280
Roland Levillaindff1f282014-11-05 14:15:05 +00004281class HTypeConversion : public HExpression<1> {
4282 public:
4283 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004284 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004285 : HExpression(result_type,
4286 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4287 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004288 SetRawInputAt(0, input);
4289 DCHECK_NE(input->GetType(), result_type);
4290 }
4291
4292 HInstruction* GetInput() const { return InputAt(0); }
4293 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4294 Primitive::Type GetResultType() const { return GetType(); }
4295
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004296 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004297 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004298
Roland Levillaindff1f282014-11-05 14:15:05 +00004299 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004300 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004301
Mark Mendelle82549b2015-05-06 10:55:34 -04004302 // Try to statically evaluate the conversion and return a HConstant
4303 // containing the result. If the input cannot be converted, return nullptr.
4304 HConstant* TryStaticEvaluation() const;
4305
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004306 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4307 Primitive::Type result_type) {
4308 // Some architectures may not require the 'GC' side effects, but at this point
4309 // in the compilation process we do not know what architecture we will
4310 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004311 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4312 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004313 return SideEffects::CanTriggerGC();
4314 }
4315 return SideEffects::None();
4316 }
4317
Roland Levillaindff1f282014-11-05 14:15:05 +00004318 DECLARE_INSTRUCTION(TypeConversion);
4319
4320 private:
4321 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4322};
4323
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004324static constexpr uint32_t kNoRegNumber = -1;
4325
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004326class HPhi : public HInstruction {
4327 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004328 HPhi(ArenaAllocator* arena,
4329 uint32_t reg_number,
4330 size_t number_of_inputs,
4331 Primitive::Type type,
4332 uint32_t dex_pc = kNoDexPc)
4333 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004334 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004335 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004336 type_(ToPhiType(type)),
4337 // Phis are constructed live and marked dead if conflicting or unused.
4338 // Individual steps of SsaBuilder should assume that if a phi has been
4339 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4340 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004341 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004342 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004343 }
4344
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004345 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4346 static Primitive::Type ToPhiType(Primitive::Type type) {
4347 switch (type) {
4348 case Primitive::kPrimBoolean:
4349 case Primitive::kPrimByte:
4350 case Primitive::kPrimShort:
4351 case Primitive::kPrimChar:
4352 return Primitive::kPrimInt;
4353 default:
4354 return type;
4355 }
4356 }
4357
David Brazdilffee3d32015-07-06 11:48:53 +01004358 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4359
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004360 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004361
4362 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004363 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004364
Calin Juravle10e244f2015-01-26 18:54:32 +00004365 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004366 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004367
Calin Juravle10e244f2015-01-26 18:54:32 +00004368 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4369 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4370
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004371 uint32_t GetRegNumber() const { return reg_number_; }
4372
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004373 void SetDead() { is_live_ = false; }
4374 void SetLive() { is_live_ = true; }
4375 bool IsDead() const { return !is_live_; }
4376 bool IsLive() const { return is_live_; }
4377
David Brazdil77a48ae2015-09-15 12:34:04 +00004378 bool IsVRegEquivalentOf(HInstruction* other) const {
4379 return other != nullptr
4380 && other->IsPhi()
4381 && other->AsPhi()->GetBlock() == GetBlock()
4382 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4383 }
4384
Calin Juravlea4f88312015-04-16 12:57:19 +01004385 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4386 // An equivalent phi is a phi having the same dex register and type.
4387 // It assumes that phis with the same dex register are adjacent.
4388 HPhi* GetNextEquivalentPhiWithSameType() {
4389 HInstruction* next = GetNext();
4390 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4391 if (next->GetType() == GetType()) {
4392 return next->AsPhi();
4393 }
4394 next = next->GetNext();
4395 }
4396 return nullptr;
4397 }
4398
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004399 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004400
David Brazdil1abb4192015-02-17 18:33:36 +00004401 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004402 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004403 return inputs_[index];
4404 }
David Brazdil1abb4192015-02-17 18:33:36 +00004405
4406 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004407 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004408 }
4409
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004410 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004411 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004412 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004413 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004414 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004415 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004416
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004417 DISALLOW_COPY_AND_ASSIGN(HPhi);
4418};
4419
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004420class HNullCheck : public HExpression<1> {
4421 public:
4422 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004423 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004424 SetRawInputAt(0, value);
4425 }
4426
Calin Juravle10e244f2015-01-26 18:54:32 +00004427 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004428 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004429 return true;
4430 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004431
Calin Juravle10e244f2015-01-26 18:54:32 +00004432 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004433
Calin Juravle10e244f2015-01-26 18:54:32 +00004434 bool CanThrow() const OVERRIDE { return true; }
4435
4436 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004437
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004438
4439 DECLARE_INSTRUCTION(NullCheck);
4440
4441 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004442 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4443};
4444
4445class FieldInfo : public ValueObject {
4446 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004447 FieldInfo(MemberOffset field_offset,
4448 Primitive::Type field_type,
4449 bool is_volatile,
4450 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004451 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004452 const DexFile& dex_file,
4453 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004454 : field_offset_(field_offset),
4455 field_type_(field_type),
4456 is_volatile_(is_volatile),
4457 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004458 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004459 dex_file_(dex_file),
4460 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004461
4462 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004463 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004464 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004465 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004466 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004467 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004468 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004469
4470 private:
4471 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004472 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004473 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004474 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004475 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004476 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004477 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004478};
4479
4480class HInstanceFieldGet : public HExpression<1> {
4481 public:
4482 HInstanceFieldGet(HInstruction* value,
4483 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004484 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004485 bool is_volatile,
4486 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004487 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004488 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004489 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004490 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004491 : HExpression(field_type,
4492 SideEffects::FieldReadOfType(field_type, is_volatile),
4493 dex_pc),
4494 field_info_(field_offset,
4495 field_type,
4496 is_volatile,
4497 field_idx,
4498 declaring_class_def_index,
4499 dex_file,
4500 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004501 SetRawInputAt(0, value);
4502 }
4503
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004504 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004505
4506 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4507 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4508 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004509 }
4510
Calin Juravle641547a2015-04-21 22:08:51 +01004511 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4512 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004513 }
4514
4515 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004516 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4517 }
4518
Calin Juravle52c48962014-12-16 17:02:57 +00004519 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004520 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004521 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004522 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004523
4524 DECLARE_INSTRUCTION(InstanceFieldGet);
4525
4526 private:
4527 const FieldInfo field_info_;
4528
4529 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4530};
4531
4532class HInstanceFieldSet : public HTemplateInstruction<2> {
4533 public:
4534 HInstanceFieldSet(HInstruction* object,
4535 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004536 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004537 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004538 bool is_volatile,
4539 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004540 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004541 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004542 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004543 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004544 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4545 dex_pc),
4546 field_info_(field_offset,
4547 field_type,
4548 is_volatile,
4549 field_idx,
4550 declaring_class_def_index,
4551 dex_file,
4552 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004553 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004554 SetRawInputAt(0, object);
4555 SetRawInputAt(1, value);
4556 }
4557
Calin Juravle641547a2015-04-21 22:08:51 +01004558 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4559 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004560 }
4561
Calin Juravle52c48962014-12-16 17:02:57 +00004562 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004563 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004564 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004565 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004566 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004567 bool GetValueCanBeNull() const { return value_can_be_null_; }
4568 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004569
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004570 DECLARE_INSTRUCTION(InstanceFieldSet);
4571
4572 private:
4573 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004574 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004575
4576 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4577};
4578
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004579class HArrayGet : public HExpression<2> {
4580 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004581 HArrayGet(HInstruction* array,
4582 HInstruction* index,
4583 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004584 uint32_t dex_pc,
4585 SideEffects additional_side_effects = SideEffects::None())
4586 : HExpression(type,
4587 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004588 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004589 SetRawInputAt(0, array);
4590 SetRawInputAt(1, index);
4591 }
4592
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004593 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004594 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004595 return true;
4596 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004597 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004598 // TODO: We can be smarter here.
4599 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4600 // which generates the implicit null check. There are cases when these can be removed
4601 // to produce better code. If we ever add optimizations to do so we should allow an
4602 // implicit check here (as long as the address falls in the first page).
4603 return false;
4604 }
4605
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004606 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004607
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004608 HInstruction* GetArray() const { return InputAt(0); }
4609 HInstruction* GetIndex() const { return InputAt(1); }
4610
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004611 DECLARE_INSTRUCTION(ArrayGet);
4612
4613 private:
4614 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4615};
4616
4617class HArraySet : public HTemplateInstruction<3> {
4618 public:
4619 HArraySet(HInstruction* array,
4620 HInstruction* index,
4621 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004622 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004623 uint32_t dex_pc,
4624 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004625 : HTemplateInstruction(
4626 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004627 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4628 additional_side_effects),
4629 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004630 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004631 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004632 value_can_be_null_(true),
4633 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004634 SetRawInputAt(0, array);
4635 SetRawInputAt(1, index);
4636 SetRawInputAt(2, value);
4637 }
4638
Calin Juravle77520bc2015-01-12 18:45:46 +00004639 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004640 // We currently always call a runtime method to catch array store
4641 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004642 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004643 }
4644
Mingyao Yang81014cb2015-06-02 03:16:27 -07004645 // Can throw ArrayStoreException.
4646 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4647
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004648 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004649 // TODO: Same as for ArrayGet.
4650 return false;
4651 }
4652
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004653 void ClearNeedsTypeCheck() {
4654 needs_type_check_ = false;
4655 }
4656
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004657 void ClearValueCanBeNull() {
4658 value_can_be_null_ = false;
4659 }
4660
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004661 void SetStaticTypeOfArrayIsObjectArray() {
4662 static_type_of_array_is_object_array_ = true;
4663 }
4664
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004665 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004666 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004667 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004668
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004669 HInstruction* GetArray() const { return InputAt(0); }
4670 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004671 HInstruction* GetValue() const { return InputAt(2); }
4672
4673 Primitive::Type GetComponentType() const {
4674 // The Dex format does not type floating point index operations. Since the
4675 // `expected_component_type_` is set during building and can therefore not
4676 // be correct, we also check what is the value type. If it is a floating
4677 // point type, we must use that type.
4678 Primitive::Type value_type = GetValue()->GetType();
4679 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4680 ? value_type
4681 : expected_component_type_;
4682 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004683
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004684 Primitive::Type GetRawExpectedComponentType() const {
4685 return expected_component_type_;
4686 }
4687
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004688 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4689 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4690 }
4691
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004692 DECLARE_INSTRUCTION(ArraySet);
4693
4694 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004695 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004696 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004697 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004698 // Cached information for the reference_type_info_ so that codegen
4699 // does not need to inspect the static type.
4700 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004701
4702 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4703};
4704
4705class HArrayLength : public HExpression<1> {
4706 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004707 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004708 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004709 // Note that arrays do not change length, so the instruction does not
4710 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004711 SetRawInputAt(0, array);
4712 }
4713
Calin Juravle77520bc2015-01-12 18:45:46 +00004714 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004715 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004716 return true;
4717 }
Calin Juravle641547a2015-04-21 22:08:51 +01004718 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4719 return obj == InputAt(0);
4720 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004721
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004722 DECLARE_INSTRUCTION(ArrayLength);
4723
4724 private:
4725 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4726};
4727
4728class HBoundsCheck : public HExpression<2> {
4729 public:
4730 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004731 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004732 DCHECK(index->GetType() == Primitive::kPrimInt);
4733 SetRawInputAt(0, index);
4734 SetRawInputAt(1, length);
4735 }
4736
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004737 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004738 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004739 return true;
4740 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004741
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004742 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004743
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004744 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004745
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004746 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004747
4748 DECLARE_INSTRUCTION(BoundsCheck);
4749
4750 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004751 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4752};
4753
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004754/**
4755 * Some DEX instructions are folded into multiple HInstructions that need
4756 * to stay live until the last HInstruction. This class
4757 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004758 * HInstruction stays live. `index` represents the stack location index of the
4759 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004760 */
4761class HTemporary : public HTemplateInstruction<0> {
4762 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004763 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4764 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004765
4766 size_t GetIndex() const { return index_; }
4767
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004768 Primitive::Type GetType() const OVERRIDE {
4769 // The previous instruction is the one that will be stored in the temporary location.
4770 DCHECK(GetPrevious() != nullptr);
4771 return GetPrevious()->GetType();
4772 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004773
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004774 DECLARE_INSTRUCTION(Temporary);
4775
4776 private:
4777 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004778 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4779};
4780
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004781class HSuspendCheck : public HTemplateInstruction<0> {
4782 public:
4783 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004784 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004785
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004786 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004787 return true;
4788 }
4789
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004790 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4791 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004792
4793 DECLARE_INSTRUCTION(SuspendCheck);
4794
4795 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004796 // Only used for code generation, in order to share the same slow path between back edges
4797 // of a same loop.
4798 SlowPathCode* slow_path_;
4799
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004800 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4801};
4802
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004803/**
4804 * Instruction to load a Class object.
4805 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004806class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004807 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004808 HLoadClass(HCurrentMethod* current_method,
4809 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004810 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004811 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004812 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004813 bool needs_access_check,
4814 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004815 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004816 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004817 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004818 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004819 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004820 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004821 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004822 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004823 // Referrers class should not need access check. We never inline unverified
4824 // methods so we can't possibly end up in this situation.
4825 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004826 SetRawInputAt(0, current_method);
4827 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004828
4829 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004830
4831 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004832 // Note that we don't need to test for generate_clinit_check_.
4833 // Whether or not we need to generate the clinit check is processed in
4834 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004835 return other->AsLoadClass()->type_index_ == type_index_ &&
4836 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004837 }
4838
4839 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4840
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004841 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004842 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004843 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004844
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004845 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004846 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004847 }
4848
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004849 bool MustGenerateClinitCheck() const {
4850 return generate_clinit_check_;
4851 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004852
Calin Juravle0ba218d2015-05-19 18:46:01 +01004853 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004854 // The entrypoint the code generator is going to call does not do
4855 // clinit of the class.
4856 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004857 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004858 }
4859
4860 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004861 return MustGenerateClinitCheck() ||
4862 (!is_referrers_class_ && !is_in_dex_cache_) ||
4863 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004864 }
4865
4866 bool NeedsAccessCheck() const {
4867 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004868 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004869
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004870 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004871 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004872 }
4873
Calin Juravleacf735c2015-02-12 15:25:22 +00004874 ReferenceTypeInfo GetLoadedClassRTI() {
4875 return loaded_class_rti_;
4876 }
4877
4878 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4879 // Make sure we only set exact types (the loaded class should never be merged).
4880 DCHECK(rti.IsExact());
4881 loaded_class_rti_ = rti;
4882 }
4883
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004884 const DexFile& GetDexFile() { return dex_file_; }
4885
Vladimir Markodc151b22015-10-15 18:02:30 +01004886 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004887
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004888 static SideEffects SideEffectsForArchRuntimeCalls() {
4889 return SideEffects::CanTriggerGC();
4890 }
4891
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004892 bool IsInDexCache() const { return is_in_dex_cache_; }
4893
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004894 DECLARE_INSTRUCTION(LoadClass);
4895
4896 private:
4897 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004898 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004899 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004900 // Whether this instruction must generate the initialization check.
4901 // Used for code generation.
4902 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004903 const bool needs_access_check_;
4904 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004905
Calin Juravleacf735c2015-02-12 15:25:22 +00004906 ReferenceTypeInfo loaded_class_rti_;
4907
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004908 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4909};
4910
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004911class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004912 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004913 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004914 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4915 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004916 SetRawInputAt(0, current_method);
4917 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004918
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004919 bool CanBeMoved() const OVERRIDE { return true; }
4920
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004921 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4922 return other->AsLoadString()->string_index_ == string_index_;
4923 }
4924
4925 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4926
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004927 uint32_t GetStringIndex() const { return string_index_; }
4928
4929 // TODO: Can we deopt or debug when we resolve a string?
4930 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004931 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004932 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004933
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004934 static SideEffects SideEffectsForArchRuntimeCalls() {
4935 return SideEffects::CanTriggerGC();
4936 }
4937
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004938 DECLARE_INSTRUCTION(LoadString);
4939
4940 private:
4941 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004942
4943 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4944};
4945
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004946/**
4947 * Performs an initialization check on its Class object input.
4948 */
4949class HClinitCheck : public HExpression<1> {
4950 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004951 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004952 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004953 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004954 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4955 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004956 SetRawInputAt(0, constant);
4957 }
4958
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004959 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004960 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004961 return true;
4962 }
4963
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004964 bool NeedsEnvironment() const OVERRIDE {
4965 // May call runtime to initialize the class.
4966 return true;
4967 }
4968
Nicolas Geoffray729645a2015-11-19 13:29:02 +00004969 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004970
4971 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4972
4973 DECLARE_INSTRUCTION(ClinitCheck);
4974
4975 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004976 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4977};
4978
4979class HStaticFieldGet : public HExpression<1> {
4980 public:
4981 HStaticFieldGet(HInstruction* cls,
4982 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004983 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004984 bool is_volatile,
4985 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004986 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004987 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004988 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004989 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004990 : HExpression(field_type,
4991 SideEffects::FieldReadOfType(field_type, is_volatile),
4992 dex_pc),
4993 field_info_(field_offset,
4994 field_type,
4995 is_volatile,
4996 field_idx,
4997 declaring_class_def_index,
4998 dex_file,
4999 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005000 SetRawInputAt(0, cls);
5001 }
5002
Calin Juravle52c48962014-12-16 17:02:57 +00005003
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005004 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005005
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005006 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005007 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5008 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005009 }
5010
5011 size_t ComputeHashCode() const OVERRIDE {
5012 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5013 }
5014
Calin Juravle52c48962014-12-16 17:02:57 +00005015 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005016 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5017 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005018 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005019
5020 DECLARE_INSTRUCTION(StaticFieldGet);
5021
5022 private:
5023 const FieldInfo field_info_;
5024
5025 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5026};
5027
5028class HStaticFieldSet : public HTemplateInstruction<2> {
5029 public:
5030 HStaticFieldSet(HInstruction* cls,
5031 HInstruction* value,
5032 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005033 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005034 bool is_volatile,
5035 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005036 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005037 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005038 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005039 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005040 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5041 dex_pc),
5042 field_info_(field_offset,
5043 field_type,
5044 is_volatile,
5045 field_idx,
5046 declaring_class_def_index,
5047 dex_file,
5048 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005049 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005050 SetRawInputAt(0, cls);
5051 SetRawInputAt(1, value);
5052 }
5053
Calin Juravle52c48962014-12-16 17:02:57 +00005054 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005055 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5056 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005057 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005058
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005059 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005060 bool GetValueCanBeNull() const { return value_can_be_null_; }
5061 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005062
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005063 DECLARE_INSTRUCTION(StaticFieldSet);
5064
5065 private:
5066 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005067 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005068
5069 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5070};
5071
Calin Juravlee460d1d2015-09-29 04:52:17 +01005072class HUnresolvedInstanceFieldGet : public HExpression<1> {
5073 public:
5074 HUnresolvedInstanceFieldGet(HInstruction* obj,
5075 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 SetRawInputAt(0, obj);
5081 }
5082
5083 bool NeedsEnvironment() const OVERRIDE { return true; }
5084 bool CanThrow() const OVERRIDE { return true; }
5085
5086 Primitive::Type GetFieldType() const { return GetType(); }
5087 uint32_t GetFieldIndex() const { return field_index_; }
5088
5089 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5090
5091 private:
5092 const uint32_t field_index_;
5093
5094 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5095};
5096
5097class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5098 public:
5099 HUnresolvedInstanceFieldSet(HInstruction* obj,
5100 HInstruction* value,
5101 Primitive::Type field_type,
5102 uint32_t field_index,
5103 uint32_t dex_pc)
5104 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5105 field_type_(field_type),
5106 field_index_(field_index) {
5107 DCHECK_EQ(field_type, value->GetType());
5108 SetRawInputAt(0, obj);
5109 SetRawInputAt(1, value);
5110 }
5111
5112 bool NeedsEnvironment() const OVERRIDE { return true; }
5113 bool CanThrow() const OVERRIDE { return true; }
5114
5115 Primitive::Type GetFieldType() const { return field_type_; }
5116 uint32_t GetFieldIndex() const { return field_index_; }
5117
5118 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5119
5120 private:
5121 const Primitive::Type field_type_;
5122 const uint32_t field_index_;
5123
5124 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5125};
5126
5127class HUnresolvedStaticFieldGet : public HExpression<0> {
5128 public:
5129 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5130 uint32_t field_index,
5131 uint32_t dex_pc)
5132 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5133 field_index_(field_index) {
5134 }
5135
5136 bool NeedsEnvironment() const OVERRIDE { return true; }
5137 bool CanThrow() const OVERRIDE { return true; }
5138
5139 Primitive::Type GetFieldType() const { return GetType(); }
5140 uint32_t GetFieldIndex() const { return field_index_; }
5141
5142 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5143
5144 private:
5145 const uint32_t field_index_;
5146
5147 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5148};
5149
5150class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5151 public:
5152 HUnresolvedStaticFieldSet(HInstruction* value,
5153 Primitive::Type field_type,
5154 uint32_t field_index,
5155 uint32_t dex_pc)
5156 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5157 field_type_(field_type),
5158 field_index_(field_index) {
5159 DCHECK_EQ(field_type, value->GetType());
5160 SetRawInputAt(0, value);
5161 }
5162
5163 bool NeedsEnvironment() const OVERRIDE { return true; }
5164 bool CanThrow() const OVERRIDE { return true; }
5165
5166 Primitive::Type GetFieldType() const { return field_type_; }
5167 uint32_t GetFieldIndex() const { return field_index_; }
5168
5169 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5170
5171 private:
5172 const Primitive::Type field_type_;
5173 const uint32_t field_index_;
5174
5175 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5176};
5177
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005178// Implement the move-exception DEX instruction.
5179class HLoadException : public HExpression<0> {
5180 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005181 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5182 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005183
David Brazdilbbd733e2015-08-18 17:48:17 +01005184 bool CanBeNull() const OVERRIDE { return false; }
5185
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005186 DECLARE_INSTRUCTION(LoadException);
5187
5188 private:
5189 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5190};
5191
David Brazdilcb1c0552015-08-04 16:22:25 +01005192// Implicit part of move-exception which clears thread-local exception storage.
5193// Must not be removed because the runtime expects the TLS to get cleared.
5194class HClearException : public HTemplateInstruction<0> {
5195 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005196 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5197 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005198
5199 DECLARE_INSTRUCTION(ClearException);
5200
5201 private:
5202 DISALLOW_COPY_AND_ASSIGN(HClearException);
5203};
5204
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005205class HThrow : public HTemplateInstruction<1> {
5206 public:
5207 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005208 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005209 SetRawInputAt(0, exception);
5210 }
5211
5212 bool IsControlFlow() const OVERRIDE { return true; }
5213
5214 bool NeedsEnvironment() const OVERRIDE { return true; }
5215
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005216 bool CanThrow() const OVERRIDE { return true; }
5217
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005218
5219 DECLARE_INSTRUCTION(Throw);
5220
5221 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005222 DISALLOW_COPY_AND_ASSIGN(HThrow);
5223};
5224
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005225/**
5226 * Implementation strategies for the code generator of a HInstanceOf
5227 * or `HCheckCast`.
5228 */
5229enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005230 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005231 kExactCheck, // Can do a single class compare.
5232 kClassHierarchyCheck, // Can just walk the super class chain.
5233 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5234 kInterfaceCheck, // No optimization yet when checking against an interface.
5235 kArrayObjectCheck, // Can just check if the array is not primitive.
5236 kArrayCheck // No optimization yet when checking against a generic array.
5237};
5238
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005239class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005240 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005241 HInstanceOf(HInstruction* object,
5242 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005243 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005244 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005245 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005246 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005247 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005248 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005249 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005250 SetRawInputAt(0, object);
5251 SetRawInputAt(1, constant);
5252 }
5253
5254 bool CanBeMoved() const OVERRIDE { return true; }
5255
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005256 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005257 return true;
5258 }
5259
5260 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005261 return false;
5262 }
5263
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005264 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5265
5266 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005267
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005268 // Used only in code generation.
5269 bool MustDoNullCheck() const { return must_do_null_check_; }
5270 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5271
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005272 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5273 return (check_kind == TypeCheckKind::kExactCheck)
5274 ? SideEffects::None()
5275 // Mips currently does runtime calls for any other checks.
5276 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005277 }
5278
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005279 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005280
5281 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005282 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005283 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005284
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005285 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5286};
5287
Calin Juravleb1498f62015-02-16 13:13:29 +00005288class HBoundType : public HExpression<1> {
5289 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005290 // Constructs an HBoundType with the given upper_bound.
5291 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005292 HBoundType(HInstruction* input,
5293 ReferenceTypeInfo upper_bound,
5294 bool upper_can_be_null,
5295 uint32_t dex_pc = kNoDexPc)
5296 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005297 upper_bound_(upper_bound),
5298 upper_can_be_null_(upper_can_be_null),
5299 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005300 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005301 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005302 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005303 }
5304
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005305 // GetUpper* should only be used in reference type propagation.
5306 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5307 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005308
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005309 void SetCanBeNull(bool can_be_null) {
5310 DCHECK(upper_can_be_null_ || !can_be_null);
5311 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005312 }
5313
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005314 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5315
Calin Juravleb1498f62015-02-16 13:13:29 +00005316 DECLARE_INSTRUCTION(BoundType);
5317
5318 private:
5319 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005320 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5321 // It is used to bound the type in cases like:
5322 // if (x instanceof ClassX) {
5323 // // uper_bound_ will be ClassX
5324 // }
5325 const ReferenceTypeInfo upper_bound_;
5326 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5327 // is false then can_be_null_ cannot be true).
5328 const bool upper_can_be_null_;
5329 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005330
5331 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5332};
5333
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005334class HCheckCast : public HTemplateInstruction<2> {
5335 public:
5336 HCheckCast(HInstruction* object,
5337 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005338 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005339 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005340 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005341 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005342 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005343 SetRawInputAt(0, object);
5344 SetRawInputAt(1, constant);
5345 }
5346
5347 bool CanBeMoved() const OVERRIDE { return true; }
5348
5349 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5350 return true;
5351 }
5352
5353 bool NeedsEnvironment() const OVERRIDE {
5354 // Instruction may throw a CheckCastError.
5355 return true;
5356 }
5357
5358 bool CanThrow() const OVERRIDE { return true; }
5359
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005360 bool MustDoNullCheck() const { return must_do_null_check_; }
5361 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005362 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005363
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005364 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005365
5366 DECLARE_INSTRUCTION(CheckCast);
5367
5368 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005369 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005370 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005371
5372 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005373};
5374
Calin Juravle27df7582015-04-17 19:12:31 +01005375class HMemoryBarrier : public HTemplateInstruction<0> {
5376 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005377 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005378 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005379 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005380 barrier_kind_(barrier_kind) {}
5381
5382 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5383
5384 DECLARE_INSTRUCTION(MemoryBarrier);
5385
5386 private:
5387 const MemBarrierKind barrier_kind_;
5388
5389 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5390};
5391
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005392class HMonitorOperation : public HTemplateInstruction<1> {
5393 public:
5394 enum OperationKind {
5395 kEnter,
5396 kExit,
5397 };
5398
5399 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005400 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005401 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5402 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005403 SetRawInputAt(0, object);
5404 }
5405
5406 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005407 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5408
5409 bool CanThrow() const OVERRIDE {
5410 // Verifier guarantees that monitor-exit cannot throw.
5411 // This is important because it allows the HGraphBuilder to remove
5412 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5413 return IsEnter();
5414 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005415
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005416
5417 bool IsEnter() const { return kind_ == kEnter; }
5418
5419 DECLARE_INSTRUCTION(MonitorOperation);
5420
Calin Juravle52c48962014-12-16 17:02:57 +00005421 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005422 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005423
5424 private:
5425 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5426};
5427
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005428/**
5429 * A HInstruction used as a marker for the replacement of new + <init>
5430 * of a String to a call to a StringFactory. Only baseline will see
5431 * the node at code generation, where it will be be treated as null.
5432 * When compiling non-baseline, `HFakeString` instructions are being removed
5433 * in the instruction simplifier.
5434 */
5435class HFakeString : public HTemplateInstruction<0> {
5436 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005437 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5438 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005439
5440 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5441
5442 DECLARE_INSTRUCTION(FakeString);
5443
5444 private:
5445 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5446};
5447
Vladimir Markof9f64412015-09-02 14:05:49 +01005448class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005449 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005450 MoveOperands(Location source,
5451 Location destination,
5452 Primitive::Type type,
5453 HInstruction* instruction)
5454 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005455
5456 Location GetSource() const { return source_; }
5457 Location GetDestination() const { return destination_; }
5458
5459 void SetSource(Location value) { source_ = value; }
5460 void SetDestination(Location value) { destination_ = value; }
5461
5462 // The parallel move resolver marks moves as "in-progress" by clearing the
5463 // destination (but not the source).
5464 Location MarkPending() {
5465 DCHECK(!IsPending());
5466 Location dest = destination_;
5467 destination_ = Location::NoLocation();
5468 return dest;
5469 }
5470
5471 void ClearPending(Location dest) {
5472 DCHECK(IsPending());
5473 destination_ = dest;
5474 }
5475
5476 bool IsPending() const {
5477 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5478 return destination_.IsInvalid() && !source_.IsInvalid();
5479 }
5480
5481 // True if this blocks a move from the given location.
5482 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005483 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005484 }
5485
5486 // A move is redundant if it's been eliminated, if its source and
5487 // destination are the same, or if its destination is unneeded.
5488 bool IsRedundant() const {
5489 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5490 }
5491
5492 // We clear both operands to indicate move that's been eliminated.
5493 void Eliminate() {
5494 source_ = destination_ = Location::NoLocation();
5495 }
5496
5497 bool IsEliminated() const {
5498 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5499 return source_.IsInvalid();
5500 }
5501
Alexey Frunze4dda3372015-06-01 18:31:49 -07005502 Primitive::Type GetType() const { return type_; }
5503
Nicolas Geoffray90218252015-04-15 11:56:51 +01005504 bool Is64BitMove() const {
5505 return Primitive::Is64BitType(type_);
5506 }
5507
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005508 HInstruction* GetInstruction() const { return instruction_; }
5509
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005510 private:
5511 Location source_;
5512 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005513 // The type this move is for.
5514 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005515 // The instruction this move is assocatied with. Null when this move is
5516 // for moving an input in the expected locations of user (including a phi user).
5517 // This is only used in debug mode, to ensure we do not connect interval siblings
5518 // in the same parallel move.
5519 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005520};
5521
5522static constexpr size_t kDefaultNumberOfMoves = 4;
5523
5524class HParallelMove : public HTemplateInstruction<0> {
5525 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005526 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005527 : HTemplateInstruction(SideEffects::None(), dex_pc),
5528 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5529 moves_.reserve(kDefaultNumberOfMoves);
5530 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005531
Nicolas Geoffray90218252015-04-15 11:56:51 +01005532 void AddMove(Location source,
5533 Location destination,
5534 Primitive::Type type,
5535 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005536 DCHECK(source.IsValid());
5537 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005538 if (kIsDebugBuild) {
5539 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005540 for (const MoveOperands& move : moves_) {
5541 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005542 // Special case the situation where the move is for the spill slot
5543 // of the instruction.
5544 if ((GetPrevious() == instruction)
5545 || ((GetPrevious() == nullptr)
5546 && instruction->IsPhi()
5547 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005548 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005549 << "Doing parallel moves for the same instruction.";
5550 } else {
5551 DCHECK(false) << "Doing parallel moves for the same instruction.";
5552 }
5553 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005554 }
5555 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005556 for (const MoveOperands& move : moves_) {
5557 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005558 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005559 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005560 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005561 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005562 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005563 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005564 }
5565
Vladimir Marko225b6462015-09-28 12:17:40 +01005566 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005567 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005568 }
5569
Vladimir Marko225b6462015-09-28 12:17:40 +01005570 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005571
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005572 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005573
5574 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005575 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005576
5577 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5578};
5579
Mark Mendell0616ae02015-04-17 12:49:27 -04005580} // namespace art
5581
Vladimir Markob4536b72015-11-24 13:45:23 +00005582#ifdef ART_ENABLE_CODEGEN_arm
5583#include "nodes_arm.h"
5584#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005585#ifdef ART_ENABLE_CODEGEN_arm64
5586#include "nodes_arm64.h"
5587#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005588#ifdef ART_ENABLE_CODEGEN_x86
5589#include "nodes_x86.h"
5590#endif
5591
5592namespace art {
5593
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005594class HGraphVisitor : public ValueObject {
5595 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005596 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5597 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005598
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005599 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005600 virtual void VisitBasicBlock(HBasicBlock* block);
5601
Roland Levillain633021e2014-10-01 14:12:25 +01005602 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005603 void VisitInsertionOrder();
5604
Roland Levillain633021e2014-10-01 14:12:25 +01005605 // Visit the graph following dominator tree reverse post-order.
5606 void VisitReversePostOrder();
5607
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005608 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005609
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005610 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005611#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005612 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5613
5614 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5615
5616#undef DECLARE_VISIT_INSTRUCTION
5617
5618 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005619 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005620
5621 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5622};
5623
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005624class HGraphDelegateVisitor : public HGraphVisitor {
5625 public:
5626 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5627 virtual ~HGraphDelegateVisitor() {}
5628
5629 // Visit functions that delegate to to super class.
5630#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005631 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005632
5633 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5634
5635#undef DECLARE_VISIT_INSTRUCTION
5636
5637 private:
5638 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5639};
5640
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005641class HInsertionOrderIterator : public ValueObject {
5642 public:
5643 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5644
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005645 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005646 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005647 void Advance() { ++index_; }
5648
5649 private:
5650 const HGraph& graph_;
5651 size_t index_;
5652
5653 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5654};
5655
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005656class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005657 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005658 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5659 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005660 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005661 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005662
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005663 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5664 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005665 void Advance() { ++index_; }
5666
5667 private:
5668 const HGraph& graph_;
5669 size_t index_;
5670
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005671 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005672};
5673
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005674class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005675 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005676 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005677 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005678 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005679 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005680 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005681
5682 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005683 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005684 void Advance() { --index_; }
5685
5686 private:
5687 const HGraph& graph_;
5688 size_t index_;
5689
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005690 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005691};
5692
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005693class HLinearPostOrderIterator : public ValueObject {
5694 public:
5695 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005696 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005697
5698 bool Done() const { return index_ == 0; }
5699
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005700 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005701
5702 void Advance() {
5703 --index_;
5704 DCHECK_GE(index_, 0U);
5705 }
5706
5707 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005708 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005709 size_t index_;
5710
5711 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5712};
5713
5714class HLinearOrderIterator : public ValueObject {
5715 public:
5716 explicit HLinearOrderIterator(const HGraph& graph)
5717 : order_(graph.GetLinearOrder()), index_(0) {}
5718
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005719 bool Done() const { return index_ == order_.size(); }
5720 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005721 void Advance() { ++index_; }
5722
5723 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005724 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005725 size_t index_;
5726
5727 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5728};
5729
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005730// Iterator over the blocks that art part of the loop. Includes blocks part
5731// of an inner loop. The order in which the blocks are iterated is on their
5732// block id.
5733class HBlocksInLoopIterator : public ValueObject {
5734 public:
5735 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5736 : blocks_in_loop_(info.GetBlocks()),
5737 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5738 index_(0) {
5739 if (!blocks_in_loop_.IsBitSet(index_)) {
5740 Advance();
5741 }
5742 }
5743
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005744 bool Done() const { return index_ == blocks_.size(); }
5745 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005746 void Advance() {
5747 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005748 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005749 if (blocks_in_loop_.IsBitSet(index_)) {
5750 break;
5751 }
5752 }
5753 }
5754
5755 private:
5756 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005757 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005758 size_t index_;
5759
5760 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5761};
5762
Mingyao Yang3584bce2015-05-19 16:01:59 -07005763// Iterator over the blocks that art part of the loop. Includes blocks part
5764// of an inner loop. The order in which the blocks are iterated is reverse
5765// post order.
5766class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5767 public:
5768 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5769 : blocks_in_loop_(info.GetBlocks()),
5770 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5771 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005772 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005773 Advance();
5774 }
5775 }
5776
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005777 bool Done() const { return index_ == blocks_.size(); }
5778 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005779 void Advance() {
5780 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005781 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5782 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005783 break;
5784 }
5785 }
5786 }
5787
5788 private:
5789 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005790 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005791 size_t index_;
5792
5793 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5794};
5795
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005796inline int64_t Int64FromConstant(HConstant* constant) {
5797 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5798 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5799 : constant->AsLongConstant()->GetValue();
5800}
5801
Vladimir Marko58155012015-08-19 12:49:41 +00005802inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5803 // For the purposes of the compiler, the dex files must actually be the same object
5804 // if we want to safely treat them as the same. This is especially important for JIT
5805 // as custom class loaders can open the same underlying file (or memory) multiple
5806 // times and provide different class resolution but no two class loaders should ever
5807 // use the same DexFile object - doing so is an unsupported hack that can lead to
5808 // all sorts of weird failures.
5809 return &lhs == &rhs;
5810}
5811
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005812} // namespace art
5813
5814#endif // ART_COMPILER_OPTIMIZING_NODES_H_