blob: f2622034f32eec0540dc441499199cd89de0cf9f [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 Rames418318f2015-11-20 15:55:47 +00001109 M(Arm64IntermediateAddress, Instruction) \
1110 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001111#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001112
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001113#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1114
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001115#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1116
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001117#ifndef ART_ENABLE_CODEGEN_x86
1118#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1119#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001120#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1121 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001122 M(X86LoadFromConstantTable, Instruction) \
1123 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001124#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001125
1126#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1127
1128#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1129 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1130 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1131 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001132 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001133 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001134 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1135 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1136
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001137#define FOR_EACH_INSTRUCTION(M) \
1138 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1139 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001140 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001141 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001142 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001143
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001144#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001145FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1146#undef FORWARD_DECLARATION
1147
Roland Levillainccc07a92014-09-16 14:48:16 +01001148#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001149 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1150 const char* DebugName() const OVERRIDE { return #type; } \
1151 const H##type* As##type() const OVERRIDE { return this; } \
1152 H##type* As##type() OVERRIDE { return this; } \
1153 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001154 return other->Is##type(); \
1155 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001156 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001157
David Brazdiled596192015-01-23 10:39:45 +00001158template <typename T> class HUseList;
1159
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001160template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001161class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001162 public:
David Brazdiled596192015-01-23 10:39:45 +00001163 HUseListNode* GetPrevious() const { return prev_; }
1164 HUseListNode* GetNext() const { return next_; }
1165 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001166 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001167 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001168
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001169 private:
David Brazdiled596192015-01-23 10:39:45 +00001170 HUseListNode(T user, size_t index)
1171 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1172
1173 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001174 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001175 HUseListNode<T>* prev_;
1176 HUseListNode<T>* next_;
1177
1178 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001179
1180 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1181};
1182
David Brazdiled596192015-01-23 10:39:45 +00001183template <typename T>
1184class HUseList : public ValueObject {
1185 public:
1186 HUseList() : first_(nullptr) {}
1187
1188 void Clear() {
1189 first_ = nullptr;
1190 }
1191
1192 // Adds a new entry at the beginning of the use list and returns
1193 // the newly created node.
1194 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001195 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001196 if (IsEmpty()) {
1197 first_ = new_node;
1198 } else {
1199 first_->prev_ = new_node;
1200 new_node->next_ = first_;
1201 first_ = new_node;
1202 }
1203 return new_node;
1204 }
1205
1206 HUseListNode<T>* GetFirst() const {
1207 return first_;
1208 }
1209
1210 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001211 DCHECK(node != nullptr);
1212 DCHECK(Contains(node));
1213
David Brazdiled596192015-01-23 10:39:45 +00001214 if (node->prev_ != nullptr) {
1215 node->prev_->next_ = node->next_;
1216 }
1217 if (node->next_ != nullptr) {
1218 node->next_->prev_ = node->prev_;
1219 }
1220 if (node == first_) {
1221 first_ = node->next_;
1222 }
1223 }
1224
David Brazdil1abb4192015-02-17 18:33:36 +00001225 bool Contains(const HUseListNode<T>* node) const {
1226 if (node == nullptr) {
1227 return false;
1228 }
1229 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1230 if (current == node) {
1231 return true;
1232 }
1233 }
1234 return false;
1235 }
1236
David Brazdiled596192015-01-23 10:39:45 +00001237 bool IsEmpty() const {
1238 return first_ == nullptr;
1239 }
1240
1241 bool HasOnlyOneUse() const {
1242 return first_ != nullptr && first_->next_ == nullptr;
1243 }
1244
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001245 size_t SizeSlow() const {
1246 size_t count = 0;
1247 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1248 ++count;
1249 }
1250 return count;
1251 }
1252
David Brazdiled596192015-01-23 10:39:45 +00001253 private:
1254 HUseListNode<T>* first_;
1255};
1256
1257template<typename T>
1258class HUseIterator : public ValueObject {
1259 public:
1260 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1261
1262 bool Done() const { return current_ == nullptr; }
1263
1264 void Advance() {
1265 DCHECK(!Done());
1266 current_ = current_->GetNext();
1267 }
1268
1269 HUseListNode<T>* Current() const {
1270 DCHECK(!Done());
1271 return current_;
1272 }
1273
1274 private:
1275 HUseListNode<T>* current_;
1276
1277 friend class HValue;
1278};
1279
David Brazdil1abb4192015-02-17 18:33:36 +00001280// This class is used by HEnvironment and HInstruction classes to record the
1281// instructions they use and pointers to the corresponding HUseListNodes kept
1282// by the used instructions.
1283template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001284class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001285 public:
1286 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1287 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1288
1289 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1290 : instruction_(old_record.instruction_), use_node_(use_node) {
1291 DCHECK(instruction_ != nullptr);
1292 DCHECK(use_node_ != nullptr);
1293 DCHECK(old_record.use_node_ == nullptr);
1294 }
1295
1296 HInstruction* GetInstruction() const { return instruction_; }
1297 HUseListNode<T>* GetUseNode() const { return use_node_; }
1298
1299 private:
1300 // Instruction used by the user.
1301 HInstruction* instruction_;
1302
1303 // Corresponding entry in the use list kept by 'instruction_'.
1304 HUseListNode<T>* use_node_;
1305};
1306
Aart Bik854a02b2015-07-14 16:07:00 -07001307/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001308 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001309 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001310 * For write/read dependences on fields/arrays, the dependence analysis uses
1311 * type disambiguation (e.g. a float field write cannot modify the value of an
1312 * integer field read) and the access type (e.g. a reference array write cannot
1313 * modify the value of a reference field read [although it may modify the
1314 * reference fetch prior to reading the field, which is represented by its own
1315 * write/read dependence]). The analysis makes conservative points-to
1316 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1317 * the same, and any reference read depends on any reference read without
1318 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001319 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001320 * The internal representation uses 38-bit and is described in the table below.
1321 * The first line indicates the side effect, and for field/array accesses the
1322 * second line indicates the type of the access (in the order of the
1323 * Primitive::Type enum).
1324 * The two numbered lines below indicate the bit position in the bitfield (read
1325 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001326 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001327 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1328 * +-------------+---------+---------+--------------+---------+---------+
1329 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1330 * | 3 |333333322|222222221| 1 |111111110|000000000|
1331 * | 7 |654321098|765432109| 8 |765432109|876543210|
1332 *
1333 * Note that, to ease the implementation, 'changes' bits are least significant
1334 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001335 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001336class SideEffects : public ValueObject {
1337 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001338 SideEffects() : flags_(0) {}
1339
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001340 static SideEffects None() {
1341 return SideEffects(0);
1342 }
1343
1344 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001345 return SideEffects(kAllChangeBits | kAllDependOnBits);
1346 }
1347
1348 static SideEffects AllChanges() {
1349 return SideEffects(kAllChangeBits);
1350 }
1351
1352 static SideEffects AllDependencies() {
1353 return SideEffects(kAllDependOnBits);
1354 }
1355
1356 static SideEffects AllExceptGCDependency() {
1357 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1358 }
1359
1360 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001361 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001362 }
1363
Aart Bik34c3ba92015-07-20 14:08:59 -07001364 static SideEffects AllWrites() {
1365 return SideEffects(kAllWrites);
1366 }
1367
1368 static SideEffects AllReads() {
1369 return SideEffects(kAllReads);
1370 }
1371
1372 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1373 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001374 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001375 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001376 }
1377
Aart Bik854a02b2015-07-14 16:07:00 -07001378 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1379 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001380 }
1381
Aart Bik34c3ba92015-07-20 14:08:59 -07001382 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1383 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001384 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001385 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001386 }
1387
1388 static SideEffects ArrayReadOfType(Primitive::Type type) {
1389 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1390 }
1391
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001392 static SideEffects CanTriggerGC() {
1393 return SideEffects(1ULL << kCanTriggerGCBit);
1394 }
1395
1396 static SideEffects DependsOnGC() {
1397 return SideEffects(1ULL << kDependsOnGCBit);
1398 }
1399
Aart Bik854a02b2015-07-14 16:07:00 -07001400 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001401 SideEffects Union(SideEffects other) const {
1402 return SideEffects(flags_ | other.flags_);
1403 }
1404
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001405 SideEffects Exclusion(SideEffects other) const {
1406 return SideEffects(flags_ & ~other.flags_);
1407 }
1408
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001409 void Add(SideEffects other) {
1410 flags_ |= other.flags_;
1411 }
1412
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001413 bool Includes(SideEffects other) const {
1414 return (other.flags_ & flags_) == other.flags_;
1415 }
1416
1417 bool HasSideEffects() const {
1418 return (flags_ & kAllChangeBits);
1419 }
1420
1421 bool HasDependencies() const {
1422 return (flags_ & kAllDependOnBits);
1423 }
1424
1425 // Returns true if there are no side effects or dependencies.
1426 bool DoesNothing() const {
1427 return flags_ == 0;
1428 }
1429
Aart Bik854a02b2015-07-14 16:07:00 -07001430 // Returns true if something is written.
1431 bool DoesAnyWrite() const {
1432 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001433 }
1434
Aart Bik854a02b2015-07-14 16:07:00 -07001435 // Returns true if something is read.
1436 bool DoesAnyRead() const {
1437 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001438 }
1439
Aart Bik854a02b2015-07-14 16:07:00 -07001440 // Returns true if potentially everything is written and read
1441 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001442 bool DoesAllReadWrite() const {
1443 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1444 }
1445
Aart Bik854a02b2015-07-14 16:07:00 -07001446 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001447 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001448 }
1449
Roland Levillain0d5a2812015-11-13 10:07:31 +00001450 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001451 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001452 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1453 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001454 }
1455
1456 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001457 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001458 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001459 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001460 for (int s = kLastBit; s >= 0; s--) {
1461 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1462 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1463 // This is a bit for the GC side effect.
1464 if (current_bit_is_set) {
1465 flags += "GC";
1466 }
Aart Bik854a02b2015-07-14 16:07:00 -07001467 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001468 } else {
1469 // This is a bit for the array/field analysis.
1470 // The underscore character stands for the 'can trigger GC' bit.
1471 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1472 if (current_bit_is_set) {
1473 flags += kDebug[s];
1474 }
1475 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1476 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1477 flags += "|";
1478 }
1479 }
Aart Bik854a02b2015-07-14 16:07:00 -07001480 }
1481 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001482 }
1483
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001484 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001485
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001486 private:
1487 static constexpr int kFieldArrayAnalysisBits = 9;
1488
1489 static constexpr int kFieldWriteOffset = 0;
1490 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1491 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1492 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1493
1494 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1495
1496 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1497 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1498 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1499 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1500
1501 static constexpr int kLastBit = kDependsOnGCBit;
1502 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1503
1504 // Aliases.
1505
1506 static_assert(kChangeBits == kDependOnBits,
1507 "the 'change' bits should match the 'depend on' bits.");
1508
1509 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1510 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1511 static constexpr uint64_t kAllWrites =
1512 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1513 static constexpr uint64_t kAllReads =
1514 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001515
Aart Bik854a02b2015-07-14 16:07:00 -07001516 // Work around the fact that HIR aliases I/F and J/D.
1517 // TODO: remove this interceptor once HIR types are clean
1518 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1519 switch (type) {
1520 case Primitive::kPrimInt:
1521 case Primitive::kPrimFloat:
1522 return TypeFlag(Primitive::kPrimInt, offset) |
1523 TypeFlag(Primitive::kPrimFloat, offset);
1524 case Primitive::kPrimLong:
1525 case Primitive::kPrimDouble:
1526 return TypeFlag(Primitive::kPrimLong, offset) |
1527 TypeFlag(Primitive::kPrimDouble, offset);
1528 default:
1529 return TypeFlag(type, offset);
1530 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001531 }
1532
Aart Bik854a02b2015-07-14 16:07:00 -07001533 // Translates type to bit flag.
1534 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1535 CHECK_NE(type, Primitive::kPrimVoid);
1536 const uint64_t one = 1;
1537 const int shift = type; // 0-based consecutive enum
1538 DCHECK_LE(kFieldWriteOffset, shift);
1539 DCHECK_LT(shift, kArrayWriteOffset);
1540 return one << (type + offset);
1541 }
1542
1543 // Private constructor on direct flags value.
1544 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1545
1546 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001547};
1548
David Brazdiled596192015-01-23 10:39:45 +00001549// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001550class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001551 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001552 HEnvironment(ArenaAllocator* arena,
1553 size_t number_of_vregs,
1554 const DexFile& dex_file,
1555 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001556 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001557 InvokeType invoke_type,
1558 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001559 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1560 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001561 parent_(nullptr),
1562 dex_file_(dex_file),
1563 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001564 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001565 invoke_type_(invoke_type),
1566 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001567 }
1568
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001569 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001570 : HEnvironment(arena,
1571 to_copy.Size(),
1572 to_copy.GetDexFile(),
1573 to_copy.GetMethodIdx(),
1574 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001575 to_copy.GetInvokeType(),
1576 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001577
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001578 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001579 if (parent_ != nullptr) {
1580 parent_->SetAndCopyParentChain(allocator, parent);
1581 } else {
1582 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1583 parent_->CopyFrom(parent);
1584 if (parent->GetParent() != nullptr) {
1585 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1586 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001587 }
David Brazdiled596192015-01-23 10:39:45 +00001588 }
1589
Vladimir Marko71bf8092015-09-15 15:33:14 +01001590 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001591 void CopyFrom(HEnvironment* environment);
1592
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001593 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1594 // input to the loop phi instead. This is for inserting instructions that
1595 // require an environment (like HDeoptimization) in the loop pre-header.
1596 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001597
1598 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001599 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001600 }
1601
1602 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001603 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001604 }
1605
David Brazdil1abb4192015-02-17 18:33:36 +00001606 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001607
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001608 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001609
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001610 HEnvironment* GetParent() const { return parent_; }
1611
1612 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001613 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001614 }
1615
1616 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001617 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001618 }
1619
1620 uint32_t GetDexPc() const {
1621 return dex_pc_;
1622 }
1623
1624 uint32_t GetMethodIdx() const {
1625 return method_idx_;
1626 }
1627
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001628 InvokeType GetInvokeType() const {
1629 return invoke_type_;
1630 }
1631
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001632 const DexFile& GetDexFile() const {
1633 return dex_file_;
1634 }
1635
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001636 HInstruction* GetHolder() const {
1637 return holder_;
1638 }
1639
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001640
1641 bool IsFromInlinedInvoke() const {
1642 return GetParent() != nullptr;
1643 }
1644
David Brazdiled596192015-01-23 10:39:45 +00001645 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001646 // Record instructions' use entries of this environment for constant-time removal.
1647 // It should only be called by HInstruction when a new environment use is added.
1648 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1649 DCHECK(env_use->GetUser() == this);
1650 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001651 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001652 }
David Brazdiled596192015-01-23 10:39:45 +00001653
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001654 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1655 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001656 HEnvironment* parent_;
1657 const DexFile& dex_file_;
1658 const uint32_t method_idx_;
1659 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001660 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001661
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001662 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001663 HInstruction* const holder_;
1664
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001665 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001666
1667 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1668};
1669
Calin Juravleacf735c2015-02-12 15:25:22 +00001670class ReferenceTypeInfo : ValueObject {
1671 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001672 typedef Handle<mirror::Class> TypeHandle;
1673
Calin Juravle2e768302015-07-28 14:41:11 +00001674 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1675 // The constructor will check that the type_handle is valid.
1676 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001677 }
1678
Calin Juravle2e768302015-07-28 14:41:11 +00001679 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1680
1681 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1682 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001683 }
1684
Calin Juravle2e768302015-07-28 14:41:11 +00001685 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1686 return IsValidHandle(type_handle_);
1687 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001688
Calin Juravleacf735c2015-02-12 15:25:22 +00001689 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001690
1691 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1692 DCHECK(IsValid());
1693 return GetTypeHandle()->IsObjectClass();
1694 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001695
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001696 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001697 DCHECK(IsValid());
1698 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001699 }
1700
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001701 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1702 DCHECK(IsValid());
1703 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1704 }
1705
Mathieu Chartier90443472015-07-16 20:32:27 -07001706 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001707 DCHECK(IsValid());
1708 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001709 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001710
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001711 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001712 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001713 return GetTypeHandle()->IsArrayClass();
1714 }
1715
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001716 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1717 DCHECK(IsValid());
1718 return GetTypeHandle()->IsPrimitiveArray();
1719 }
1720
1721 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1722 DCHECK(IsValid());
1723 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1724 }
1725
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001726 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001727 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001728 if (!IsExact()) return false;
1729 if (!IsArrayClass()) return false;
1730 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1731 }
1732
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001733 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1734 DCHECK(IsValid());
1735 if (!IsExact()) return false;
1736 if (!IsArrayClass()) return false;
1737 if (!rti.IsArrayClass()) return false;
1738 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1739 rti.GetTypeHandle()->GetComponentType());
1740 }
1741
Calin Juravleacf735c2015-02-12 15:25:22 +00001742 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1743
Mathieu Chartier90443472015-07-16 20:32:27 -07001744 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001745 DCHECK(IsValid());
1746 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001747 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1748 }
1749
Calin Juravlecdfed3d2015-10-26 14:05:01 +00001750 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1751 DCHECK(IsValid());
1752 DCHECK(rti.IsValid());
1753 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
1754 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1755 }
1756
Calin Juravleacf735c2015-02-12 15:25:22 +00001757 // Returns true if the type information provide the same amount of details.
1758 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001759 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001760 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001761 if (!IsValid() && !rti.IsValid()) {
1762 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001763 return true;
1764 }
Calin Juravle2e768302015-07-28 14:41:11 +00001765 if (!IsValid() || !rti.IsValid()) {
1766 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001767 return false;
1768 }
Calin Juravle2e768302015-07-28 14:41:11 +00001769 return IsExact() == rti.IsExact()
1770 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001771 }
1772
1773 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001774 ReferenceTypeInfo();
1775 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001776
Calin Juravleacf735c2015-02-12 15:25:22 +00001777 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001778 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001779 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001780 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001781 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001782};
1783
1784std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1785
Vladimir Markof9f64412015-09-02 14:05:49 +01001786class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001787 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001788 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001789 : previous_(nullptr),
1790 next_(nullptr),
1791 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001792 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001793 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001794 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001795 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001796 locations_(nullptr),
1797 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001798 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001799 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001800 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001801
Dave Allison20dfc792014-06-16 20:44:29 -07001802 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001803
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001804#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001805 enum InstructionKind {
1806 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1807 };
1808#undef DECLARE_KIND
1809
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001810 HInstruction* GetNext() const { return next_; }
1811 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001812
Calin Juravle77520bc2015-01-12 18:45:46 +00001813 HInstruction* GetNextDisregardingMoves() const;
1814 HInstruction* GetPreviousDisregardingMoves() const;
1815
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001816 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001817 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001818 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001819 bool IsInBlock() const { return block_ != nullptr; }
1820 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001821 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001822
Roland Levillain6b879dd2014-09-22 17:13:44 +01001823 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001824 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001825
1826 virtual void Accept(HGraphVisitor* visitor) = 0;
1827 virtual const char* DebugName() const = 0;
1828
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001829 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001830 void SetRawInputAt(size_t index, HInstruction* input) {
1831 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1832 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001833
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001834 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001835
1836 uint32_t GetDexPc() const { return dex_pc_; }
1837
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001838 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001839
Roland Levillaine161a2a2014-10-03 12:45:18 +01001840 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001841 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001842
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001843 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001844 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001845
Calin Juravle10e244f2015-01-26 18:54:32 +00001846 // Does not apply for all instructions, but having this at top level greatly
1847 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001848 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001849 virtual bool CanBeNull() const {
1850 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1851 return true;
1852 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001853
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001854 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001855 return false;
1856 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001857
Calin Juravle2e768302015-07-28 14:41:11 +00001858 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001859
Calin Juravle61d544b2015-02-23 16:46:57 +00001860 ReferenceTypeInfo GetReferenceTypeInfo() const {
1861 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1862 return reference_type_info_;
1863 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001864
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001865 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001866 DCHECK(user != nullptr);
1867 HUseListNode<HInstruction*>* use =
1868 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1869 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001870 }
1871
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001872 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001873 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001874 HUseListNode<HEnvironment*>* env_use =
1875 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1876 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001877 }
1878
David Brazdil1abb4192015-02-17 18:33:36 +00001879 void RemoveAsUserOfInput(size_t input) {
1880 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1881 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1882 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001883
David Brazdil1abb4192015-02-17 18:33:36 +00001884 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1885 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001886
David Brazdiled596192015-01-23 10:39:45 +00001887 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1888 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001889 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001890 bool HasOnlyOneNonEnvironmentUse() const {
1891 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1892 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001893
Roland Levillain6c82d402014-10-13 16:10:27 +01001894 // Does this instruction strictly dominate `other_instruction`?
1895 // Returns false if this instruction and `other_instruction` are the same.
1896 // Aborts if this instruction and `other_instruction` are both phis.
1897 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001898
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001899 int GetId() const { return id_; }
1900 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001901
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001902 int GetSsaIndex() const { return ssa_index_; }
1903 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1904 bool HasSsaIndex() const { return ssa_index_ != -1; }
1905
1906 bool HasEnvironment() const { return environment_ != nullptr; }
1907 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001908 // Set the `environment_` field. Raw because this method does not
1909 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001910 void SetRawEnvironment(HEnvironment* environment) {
1911 DCHECK(environment_ == nullptr);
1912 DCHECK_EQ(environment->GetHolder(), this);
1913 environment_ = environment;
1914 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001915
1916 // Set the environment of this instruction, copying it from `environment`. While
1917 // copying, the uses lists are being updated.
1918 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001919 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001920 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001921 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001922 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001923 if (environment->GetParent() != nullptr) {
1924 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1925 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001926 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001927
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001928 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1929 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001930 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001931 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001932 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001933 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001934 if (environment->GetParent() != nullptr) {
1935 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1936 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001937 }
1938
Nicolas Geoffray39468442014-09-02 15:17:15 +01001939 // Returns the number of entries in the environment. Typically, that is the
1940 // number of dex registers in a method. It could be more in case of inlining.
1941 size_t EnvironmentSize() const;
1942
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001943 LocationSummary* GetLocations() const { return locations_; }
1944 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001945
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001946 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001947 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001948
Alexandre Rames188d4312015-04-09 18:30:21 +01001949 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1950 // uses of this instruction by `other` are *not* updated.
1951 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1952 ReplaceWith(other);
1953 other->ReplaceInput(this, use_index);
1954 }
1955
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001956 // Move `this` instruction before `cursor`.
1957 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001958
Vladimir Markofb337ea2015-11-25 15:25:10 +00001959 // Move `this` before its first user and out of any loops. If there is no
1960 // out-of-loop user that dominates all other users, move the instruction
1961 // to the end of the out-of-loop common dominator of the user's blocks.
1962 //
1963 // This can be used only on non-throwing instructions with no side effects that
1964 // have at least one use but no environment uses.
1965 void MoveBeforeFirstUserAndOutOfLoops();
1966
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001967#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001968 bool Is##type() const { return (As##type() != nullptr); } \
1969 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001970 virtual H##type* As##type() { return nullptr; }
1971
1972 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1973#undef INSTRUCTION_TYPE_CHECK
1974
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001975 // Returns whether the instruction can be moved within the graph.
1976 virtual bool CanBeMoved() const { return false; }
1977
1978 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001979 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001980 return false;
1981 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001982
1983 // Returns whether any data encoded in the two instructions is equal.
1984 // This method does not look at the inputs. Both instructions must be
1985 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001986 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001987 return false;
1988 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001989
1990 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001991 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001992 // 2) Their inputs are identical.
1993 bool Equals(HInstruction* other) const;
1994
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001995 virtual InstructionKind GetKind() const = 0;
1996
1997 virtual size_t ComputeHashCode() const {
1998 size_t result = GetKind();
1999 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2000 result = (result * 31) + InputAt(i)->GetId();
2001 }
2002 return result;
2003 }
2004
2005 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002006 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002007
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002008 size_t GetLifetimePosition() const { return lifetime_position_; }
2009 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2010 LiveInterval* GetLiveInterval() const { return live_interval_; }
2011 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2012 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2013
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002014 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2015
2016 // Returns whether the code generation of the instruction will require to have access
2017 // to the current method. Such instructions are:
2018 // (1): Instructions that require an environment, as calling the runtime requires
2019 // to walk the stack and have the current method stored at a specific stack address.
2020 // (2): Object literals like classes and strings, that are loaded from the dex cache
2021 // fields of the current method.
2022 bool NeedsCurrentMethod() const {
2023 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2024 }
2025
Vladimir Markodc151b22015-10-15 18:02:30 +01002026 // Returns whether the code generation of the instruction will require to have access
2027 // to the dex cache of the current method's declaring class via the current method.
2028 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002029
Mark Mendellc4701932015-04-10 13:18:51 -04002030 // Does this instruction have any use in an environment before
2031 // control flow hits 'other'?
2032 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2033
2034 // Remove all references to environment uses of this instruction.
2035 // The caller must ensure that this is safe to do.
2036 void RemoveEnvironmentUsers();
2037
David Brazdil1abb4192015-02-17 18:33:36 +00002038 protected:
2039 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2040 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2041
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002042 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002043 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2044
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002045 HInstruction* previous_;
2046 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002047 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002048 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002049
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002050 // An instruction gets an id when it is added to the graph.
2051 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002052 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002053 int id_;
2054
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002055 // When doing liveness analysis, instructions that have uses get an SSA index.
2056 int ssa_index_;
2057
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002058 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002059 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002060
2061 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002062 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002063
Nicolas Geoffray39468442014-09-02 15:17:15 +01002064 // The environment associated with this instruction. Not null if the instruction
2065 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002066 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002067
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002068 // Set by the code generator.
2069 LocationSummary* locations_;
2070
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002071 // Set by the liveness analysis.
2072 LiveInterval* live_interval_;
2073
2074 // Set by the liveness analysis, this is the position in a linear
2075 // order of blocks where this instruction's live interval start.
2076 size_t lifetime_position_;
2077
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002078 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002079
Calin Juravleacf735c2015-02-12 15:25:22 +00002080 // TODO: for primitive types this should be marked as invalid.
2081 ReferenceTypeInfo reference_type_info_;
2082
David Brazdil1abb4192015-02-17 18:33:36 +00002083 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002084 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002085 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002086 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002087 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002088
2089 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2090};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002091std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002092
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002093class HInputIterator : public ValueObject {
2094 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002095 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002096
2097 bool Done() const { return index_ == instruction_->InputCount(); }
2098 HInstruction* Current() const { return instruction_->InputAt(index_); }
2099 void Advance() { index_++; }
2100
2101 private:
2102 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002103 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002104
2105 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2106};
2107
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002108class HInstructionIterator : public ValueObject {
2109 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002110 explicit HInstructionIterator(const HInstructionList& instructions)
2111 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002112 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002113 }
2114
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002115 bool Done() const { return instruction_ == nullptr; }
2116 HInstruction* Current() const { return instruction_; }
2117 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002118 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002119 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002120 }
2121
2122 private:
2123 HInstruction* instruction_;
2124 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002125
2126 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002127};
2128
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002129class HBackwardInstructionIterator : public ValueObject {
2130 public:
2131 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2132 : instruction_(instructions.last_instruction_) {
2133 next_ = Done() ? nullptr : instruction_->GetPrevious();
2134 }
2135
2136 bool Done() const { return instruction_ == nullptr; }
2137 HInstruction* Current() const { return instruction_; }
2138 void Advance() {
2139 instruction_ = next_;
2140 next_ = Done() ? nullptr : instruction_->GetPrevious();
2141 }
2142
2143 private:
2144 HInstruction* instruction_;
2145 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002146
2147 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002148};
2149
Vladimir Markof9f64412015-09-02 14:05:49 +01002150template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002151class HTemplateInstruction: public HInstruction {
2152 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002153 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002154 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002155 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002157 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002158
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002159 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002160 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2161 DCHECK_LT(i, N);
2162 return inputs_[i];
2163 }
David Brazdil1abb4192015-02-17 18:33:36 +00002164
2165 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002166 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002167 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002168 }
2169
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002170 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002171 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002172
2173 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002174};
2175
Vladimir Markof9f64412015-09-02 14:05:49 +01002176// HTemplateInstruction specialization for N=0.
2177template<>
2178class HTemplateInstruction<0>: public HInstruction {
2179 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002180 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002181 : HInstruction(side_effects, dex_pc) {}
2182
Vladimir Markof9f64412015-09-02 14:05:49 +01002183 virtual ~HTemplateInstruction() {}
2184
2185 size_t InputCount() const OVERRIDE { return 0; }
2186
2187 protected:
2188 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2189 LOG(FATAL) << "Unreachable";
2190 UNREACHABLE();
2191 }
2192
2193 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2194 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2195 LOG(FATAL) << "Unreachable";
2196 UNREACHABLE();
2197 }
2198
2199 private:
2200 friend class SsaBuilder;
2201};
2202
Dave Allison20dfc792014-06-16 20:44:29 -07002203template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002204class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002205 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002206 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002207 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002208 virtual ~HExpression() {}
2209
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002210 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002211
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002212 protected:
2213 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002214};
2215
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002216// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2217// instruction that branches to the exit block.
2218class HReturnVoid : public HTemplateInstruction<0> {
2219 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002220 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2221 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002222
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002223 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002224
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002225 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002226
2227 private:
2228 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2229};
2230
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002231// Represents dex's RETURN opcodes. A HReturn is a control flow
2232// instruction that branches to the exit block.
2233class HReturn : public HTemplateInstruction<1> {
2234 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002235 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2236 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002237 SetRawInputAt(0, value);
2238 }
2239
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002240 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002241
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002242 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002243
2244 private:
2245 DISALLOW_COPY_AND_ASSIGN(HReturn);
2246};
2247
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002248// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002249// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002250// exit block.
2251class HExit : public HTemplateInstruction<0> {
2252 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002253 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002254
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002255 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002256
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002257 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002258
2259 private:
2260 DISALLOW_COPY_AND_ASSIGN(HExit);
2261};
2262
2263// Jumps from one block to another.
2264class HGoto : public HTemplateInstruction<0> {
2265 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002266 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002267
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002268 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002269
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002270 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002271 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002272 }
2273
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002274 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002275
2276 private:
2277 DISALLOW_COPY_AND_ASSIGN(HGoto);
2278};
2279
Roland Levillain9867bc72015-08-05 10:21:34 +01002280class HConstant : public HExpression<0> {
2281 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002282 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2283 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002284
2285 bool CanBeMoved() const OVERRIDE { return true; }
2286
2287 virtual bool IsMinusOne() const { return false; }
2288 virtual bool IsZero() const { return false; }
2289 virtual bool IsOne() const { return false; }
2290
David Brazdil77a48ae2015-09-15 12:34:04 +00002291 virtual uint64_t GetValueAsUint64() const = 0;
2292
Roland Levillain9867bc72015-08-05 10:21:34 +01002293 DECLARE_INSTRUCTION(Constant);
2294
2295 private:
2296 DISALLOW_COPY_AND_ASSIGN(HConstant);
2297};
2298
2299class HNullConstant : public HConstant {
2300 public:
2301 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2302 return true;
2303 }
2304
David Brazdil77a48ae2015-09-15 12:34:04 +00002305 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2306
Roland Levillain9867bc72015-08-05 10:21:34 +01002307 size_t ComputeHashCode() const OVERRIDE { return 0; }
2308
2309 DECLARE_INSTRUCTION(NullConstant);
2310
2311 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002312 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002313
2314 friend class HGraph;
2315 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2316};
2317
2318// Constants of the type int. Those can be from Dex instructions, or
2319// synthesized (for example with the if-eqz instruction).
2320class HIntConstant : public HConstant {
2321 public:
2322 int32_t GetValue() const { return value_; }
2323
David Brazdil9f389d42015-10-01 14:32:56 +01002324 uint64_t GetValueAsUint64() const OVERRIDE {
2325 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2326 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002327
Roland Levillain9867bc72015-08-05 10:21:34 +01002328 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2329 DCHECK(other->IsIntConstant());
2330 return other->AsIntConstant()->value_ == value_;
2331 }
2332
2333 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2334
2335 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2336 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2337 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2338
2339 DECLARE_INSTRUCTION(IntConstant);
2340
2341 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002342 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2343 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2344 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2345 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002346
2347 const int32_t value_;
2348
2349 friend class HGraph;
2350 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2351 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2352 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2353};
2354
2355class HLongConstant : public HConstant {
2356 public:
2357 int64_t GetValue() const { return value_; }
2358
David Brazdil77a48ae2015-09-15 12:34:04 +00002359 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2360
Roland Levillain9867bc72015-08-05 10:21:34 +01002361 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2362 DCHECK(other->IsLongConstant());
2363 return other->AsLongConstant()->value_ == value_;
2364 }
2365
2366 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2367
2368 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2369 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2370 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2371
2372 DECLARE_INSTRUCTION(LongConstant);
2373
2374 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002375 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2376 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002377
2378 const int64_t value_;
2379
2380 friend class HGraph;
2381 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2382};
Dave Allison20dfc792014-06-16 20:44:29 -07002383
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002384// Conditional branch. A block ending with an HIf instruction must have
2385// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002386class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002387 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002388 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2389 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002390 SetRawInputAt(0, input);
2391 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002392
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002393 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002394
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002395 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002396 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002397 }
2398
2399 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002400 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002401 }
2402
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002403 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002404
2405 private:
2406 DISALLOW_COPY_AND_ASSIGN(HIf);
2407};
2408
David Brazdilfc6a86a2015-06-26 10:33:45 +00002409
2410// Abstract instruction which marks the beginning and/or end of a try block and
2411// links it to the respective exception handlers. Behaves the same as a Goto in
2412// non-exceptional control flow.
2413// Normal-flow successor is stored at index zero, exception handlers under
2414// higher indices in no particular order.
2415class HTryBoundary : public HTemplateInstruction<0> {
2416 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002417 enum BoundaryKind {
2418 kEntry,
2419 kExit,
2420 };
2421
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002422 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2423 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002424
2425 bool IsControlFlow() const OVERRIDE { return true; }
2426
2427 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002428 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002429
David Brazdild26a4112015-11-10 11:07:31 +00002430 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2431 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2432 }
2433
David Brazdilfc6a86a2015-06-26 10:33:45 +00002434 // Returns whether `handler` is among its exception handlers (non-zero index
2435 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002436 bool HasExceptionHandler(const HBasicBlock& handler) const {
2437 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002438 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002439 }
2440
2441 // If not present already, adds `handler` to its block's list of exception
2442 // handlers.
2443 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002444 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002445 GetBlock()->AddSuccessor(handler);
2446 }
2447 }
2448
David Brazdil56e1acc2015-06-30 15:41:36 +01002449 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002450
David Brazdilffee3d32015-07-06 11:48:53 +01002451 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2452
David Brazdilfc6a86a2015-06-26 10:33:45 +00002453 DECLARE_INSTRUCTION(TryBoundary);
2454
2455 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002456 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002457
2458 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2459};
2460
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002461// Deoptimize to interpreter, upon checking a condition.
2462class HDeoptimize : public HTemplateInstruction<1> {
2463 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002464 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2465 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002466 SetRawInputAt(0, cond);
2467 }
2468
2469 bool NeedsEnvironment() const OVERRIDE { return true; }
2470 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002471
2472 DECLARE_INSTRUCTION(Deoptimize);
2473
2474 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002475 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2476};
2477
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002478// Represents the ArtMethod that was passed as a first argument to
2479// the method. It is used by instructions that depend on it, like
2480// instructions that work with the dex cache.
2481class HCurrentMethod : public HExpression<0> {
2482 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002483 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2484 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002485
2486 DECLARE_INSTRUCTION(CurrentMethod);
2487
2488 private:
2489 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2490};
2491
Mark Mendellfe57faa2015-09-18 09:26:15 -04002492// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2493// have one successor for each entry in the switch table, and the final successor
2494// will be the block containing the next Dex opcode.
2495class HPackedSwitch : public HTemplateInstruction<1> {
2496 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002497 HPackedSwitch(int32_t start_value,
2498 uint32_t num_entries,
2499 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002500 uint32_t dex_pc = kNoDexPc)
2501 : HTemplateInstruction(SideEffects::None(), dex_pc),
2502 start_value_(start_value),
2503 num_entries_(num_entries) {
2504 SetRawInputAt(0, input);
2505 }
2506
2507 bool IsControlFlow() const OVERRIDE { return true; }
2508
2509 int32_t GetStartValue() const { return start_value_; }
2510
Vladimir Marko211c2112015-09-24 16:52:33 +01002511 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002512
2513 HBasicBlock* GetDefaultBlock() const {
2514 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002515 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002516 }
2517 DECLARE_INSTRUCTION(PackedSwitch);
2518
2519 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002520 const int32_t start_value_;
2521 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002522
2523 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2524};
2525
Roland Levillain88cb1752014-10-20 16:36:47 +01002526class HUnaryOperation : public HExpression<1> {
2527 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002528 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2529 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002530 SetRawInputAt(0, input);
2531 }
2532
2533 HInstruction* GetInput() const { return InputAt(0); }
2534 Primitive::Type GetResultType() const { return GetType(); }
2535
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002536 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002537 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002538 return true;
2539 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002540
Roland Levillain9240d6a2014-10-20 16:47:04 +01002541 // Try to statically evaluate `operation` and return a HConstant
2542 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002543 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002544 HConstant* TryStaticEvaluation() const;
2545
2546 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002547 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2548 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002549
Roland Levillain88cb1752014-10-20 16:36:47 +01002550 DECLARE_INSTRUCTION(UnaryOperation);
2551
2552 private:
2553 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2554};
2555
Dave Allison20dfc792014-06-16 20:44:29 -07002556class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002557 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002558 HBinaryOperation(Primitive::Type result_type,
2559 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002560 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002561 SideEffects side_effects = SideEffects::None(),
2562 uint32_t dex_pc = kNoDexPc)
2563 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002564 SetRawInputAt(0, left);
2565 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002566 }
2567
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002568 HInstruction* GetLeft() const { return InputAt(0); }
2569 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002570 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002571
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002572 virtual bool IsCommutative() const { return false; }
2573
2574 // Put constant on the right.
2575 // Returns whether order is changed.
2576 bool OrderInputsWithConstantOnTheRight() {
2577 HInstruction* left = InputAt(0);
2578 HInstruction* right = InputAt(1);
2579 if (left->IsConstant() && !right->IsConstant()) {
2580 ReplaceInput(right, 0);
2581 ReplaceInput(left, 1);
2582 return true;
2583 }
2584 return false;
2585 }
2586
2587 // Order inputs by instruction id, but favor constant on the right side.
2588 // This helps GVN for commutative ops.
2589 void OrderInputs() {
2590 DCHECK(IsCommutative());
2591 HInstruction* left = InputAt(0);
2592 HInstruction* right = InputAt(1);
2593 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2594 return;
2595 }
2596 if (OrderInputsWithConstantOnTheRight()) {
2597 return;
2598 }
2599 // Order according to instruction id.
2600 if (left->GetId() > right->GetId()) {
2601 ReplaceInput(right, 0);
2602 ReplaceInput(left, 1);
2603 }
2604 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002605
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002606 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002607 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002608 return true;
2609 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002610
Roland Levillain9240d6a2014-10-20 16:47:04 +01002611 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002612 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002613 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002614 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002615
2616 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002617 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2618 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2619 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2620 HLongConstant* y ATTRIBUTE_UNUSED) const {
2621 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2622 return nullptr;
2623 }
2624 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2625 HIntConstant* y ATTRIBUTE_UNUSED) const {
2626 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2627 return nullptr;
2628 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002629 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2630 HNullConstant* y ATTRIBUTE_UNUSED) const {
2631 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2632 return nullptr;
2633 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002634
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002635 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002636 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002637 HConstant* GetConstantRight() const;
2638
2639 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002640 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002641 HInstruction* GetLeastConstantLeft() const;
2642
Roland Levillainccc07a92014-09-16 14:48:16 +01002643 DECLARE_INSTRUCTION(BinaryOperation);
2644
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002645 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002646 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2647};
2648
Mark Mendellc4701932015-04-10 13:18:51 -04002649// The comparison bias applies for floating point operations and indicates how NaN
2650// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002651enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002652 kNoBias, // bias is not applicable (i.e. for long operation)
2653 kGtBias, // return 1 for NaN comparisons
2654 kLtBias, // return -1 for NaN comparisons
2655};
2656
Dave Allison20dfc792014-06-16 20:44:29 -07002657class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002658 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002659 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2660 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002661 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002662 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002663
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002664 bool NeedsMaterialization() const { return needs_materialization_; }
2665 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002666
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002667 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002668 // `instruction`, and disregard moves in between.
2669 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002670
Dave Allison20dfc792014-06-16 20:44:29 -07002671 DECLARE_INSTRUCTION(Condition);
2672
2673 virtual IfCondition GetCondition() const = 0;
2674
Mark Mendellc4701932015-04-10 13:18:51 -04002675 virtual IfCondition GetOppositeCondition() const = 0;
2676
Roland Levillain4fa13f62015-07-06 18:11:54 +01002677 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002678
2679 void SetBias(ComparisonBias bias) { bias_ = bias; }
2680
2681 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2682 return bias_ == other->AsCondition()->bias_;
2683 }
2684
Roland Levillain4fa13f62015-07-06 18:11:54 +01002685 bool IsFPConditionTrueIfNaN() const {
2686 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2687 IfCondition if_cond = GetCondition();
2688 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2689 }
2690
2691 bool IsFPConditionFalseIfNaN() const {
2692 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2693 IfCondition if_cond = GetCondition();
2694 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2695 }
2696
Dave Allison20dfc792014-06-16 20:44:29 -07002697 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002698 // For register allocation purposes, returns whether this instruction needs to be
2699 // materialized (that is, not just be in the processor flags).
2700 bool needs_materialization_;
2701
Mark Mendellc4701932015-04-10 13:18:51 -04002702 // Needed if we merge a HCompare into a HCondition.
2703 ComparisonBias bias_;
2704
Dave Allison20dfc792014-06-16 20:44:29 -07002705 DISALLOW_COPY_AND_ASSIGN(HCondition);
2706};
2707
2708// Instruction to check if two inputs are equal to each other.
2709class HEqual : public HCondition {
2710 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002711 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2712 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002713
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002714 bool IsCommutative() const OVERRIDE { return true; }
2715
Roland Levillain9867bc72015-08-05 10:21:34 +01002716 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002717 return GetBlock()->GetGraph()->GetIntConstant(
2718 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002719 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002720 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002721 return GetBlock()->GetGraph()->GetIntConstant(
2722 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002723 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002724 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2725 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002726 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002727 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002728
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002729 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002730
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002731 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002732 return kCondEQ;
2733 }
2734
Mark Mendellc4701932015-04-10 13:18:51 -04002735 IfCondition GetOppositeCondition() const OVERRIDE {
2736 return kCondNE;
2737 }
2738
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002739 private:
Aart Bike9f37602015-10-09 11:15:55 -07002740 template <typename T> bool Compute(T x, T y) const { return x == y; }
2741
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002742 DISALLOW_COPY_AND_ASSIGN(HEqual);
2743};
2744
Dave Allison20dfc792014-06-16 20:44:29 -07002745class HNotEqual : public HCondition {
2746 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002747 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2748 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002749
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002750 bool IsCommutative() const OVERRIDE { return true; }
2751
Roland Levillain9867bc72015-08-05 10:21:34 +01002752 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002753 return GetBlock()->GetGraph()->GetIntConstant(
2754 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002755 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002756 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002757 return GetBlock()->GetGraph()->GetIntConstant(
2758 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002759 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002760 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2761 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002762 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002763 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002764
Dave Allison20dfc792014-06-16 20:44:29 -07002765 DECLARE_INSTRUCTION(NotEqual);
2766
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002767 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002768 return kCondNE;
2769 }
2770
Mark Mendellc4701932015-04-10 13:18:51 -04002771 IfCondition GetOppositeCondition() const OVERRIDE {
2772 return kCondEQ;
2773 }
2774
Dave Allison20dfc792014-06-16 20:44:29 -07002775 private:
Aart Bike9f37602015-10-09 11:15:55 -07002776 template <typename T> bool Compute(T x, T y) const { return x != y; }
2777
Dave Allison20dfc792014-06-16 20:44:29 -07002778 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2779};
2780
2781class HLessThan : public HCondition {
2782 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002783 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2784 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002785
Roland Levillain9867bc72015-08-05 10:21:34 +01002786 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002787 return GetBlock()->GetGraph()->GetIntConstant(
2788 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002789 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002790 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002791 return GetBlock()->GetGraph()->GetIntConstant(
2792 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002793 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002794
Dave Allison20dfc792014-06-16 20:44:29 -07002795 DECLARE_INSTRUCTION(LessThan);
2796
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002797 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002798 return kCondLT;
2799 }
2800
Mark Mendellc4701932015-04-10 13:18:51 -04002801 IfCondition GetOppositeCondition() const OVERRIDE {
2802 return kCondGE;
2803 }
2804
Dave Allison20dfc792014-06-16 20:44:29 -07002805 private:
Aart Bike9f37602015-10-09 11:15:55 -07002806 template <typename T> bool Compute(T x, T y) const { return x < y; }
2807
Dave Allison20dfc792014-06-16 20:44:29 -07002808 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2809};
2810
2811class HLessThanOrEqual : public HCondition {
2812 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002813 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2814 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002815
Roland Levillain9867bc72015-08-05 10:21:34 +01002816 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002817 return GetBlock()->GetGraph()->GetIntConstant(
2818 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002819 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002820 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002821 return GetBlock()->GetGraph()->GetIntConstant(
2822 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002823 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002824
Dave Allison20dfc792014-06-16 20:44:29 -07002825 DECLARE_INSTRUCTION(LessThanOrEqual);
2826
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002827 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002828 return kCondLE;
2829 }
2830
Mark Mendellc4701932015-04-10 13:18:51 -04002831 IfCondition GetOppositeCondition() const OVERRIDE {
2832 return kCondGT;
2833 }
2834
Dave Allison20dfc792014-06-16 20:44:29 -07002835 private:
Aart Bike9f37602015-10-09 11:15:55 -07002836 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2837
Dave Allison20dfc792014-06-16 20:44:29 -07002838 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2839};
2840
2841class HGreaterThan : public HCondition {
2842 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002843 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2844 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002845
Roland Levillain9867bc72015-08-05 10:21:34 +01002846 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002847 return GetBlock()->GetGraph()->GetIntConstant(
2848 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002849 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002850 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002851 return GetBlock()->GetGraph()->GetIntConstant(
2852 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002853 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002854
Dave Allison20dfc792014-06-16 20:44:29 -07002855 DECLARE_INSTRUCTION(GreaterThan);
2856
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002857 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002858 return kCondGT;
2859 }
2860
Mark Mendellc4701932015-04-10 13:18:51 -04002861 IfCondition GetOppositeCondition() const OVERRIDE {
2862 return kCondLE;
2863 }
2864
Dave Allison20dfc792014-06-16 20:44:29 -07002865 private:
Aart Bike9f37602015-10-09 11:15:55 -07002866 template <typename T> bool Compute(T x, T y) const { return x > y; }
2867
Dave Allison20dfc792014-06-16 20:44:29 -07002868 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2869};
2870
2871class HGreaterThanOrEqual : public HCondition {
2872 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002873 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2874 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002875
Roland Levillain9867bc72015-08-05 10:21:34 +01002876 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002877 return GetBlock()->GetGraph()->GetIntConstant(
2878 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002879 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002880 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002881 return GetBlock()->GetGraph()->GetIntConstant(
2882 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002883 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002884
Dave Allison20dfc792014-06-16 20:44:29 -07002885 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2886
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002887 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002888 return kCondGE;
2889 }
2890
Mark Mendellc4701932015-04-10 13:18:51 -04002891 IfCondition GetOppositeCondition() const OVERRIDE {
2892 return kCondLT;
2893 }
2894
Dave Allison20dfc792014-06-16 20:44:29 -07002895 private:
Aart Bike9f37602015-10-09 11:15:55 -07002896 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2897
Dave Allison20dfc792014-06-16 20:44:29 -07002898 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2899};
2900
Aart Bike9f37602015-10-09 11:15:55 -07002901class HBelow : public HCondition {
2902 public:
2903 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2904 : HCondition(first, second, dex_pc) {}
2905
2906 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2907 return GetBlock()->GetGraph()->GetIntConstant(
2908 Compute(static_cast<uint32_t>(x->GetValue()),
2909 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2910 }
2911 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2912 return GetBlock()->GetGraph()->GetIntConstant(
2913 Compute(static_cast<uint64_t>(x->GetValue()),
2914 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2915 }
2916
2917 DECLARE_INSTRUCTION(Below);
2918
2919 IfCondition GetCondition() const OVERRIDE {
2920 return kCondB;
2921 }
2922
2923 IfCondition GetOppositeCondition() const OVERRIDE {
2924 return kCondAE;
2925 }
2926
2927 private:
2928 template <typename T> bool Compute(T x, T y) const { return x < y; }
2929
2930 DISALLOW_COPY_AND_ASSIGN(HBelow);
2931};
2932
2933class HBelowOrEqual : public HCondition {
2934 public:
2935 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2936 : HCondition(first, second, dex_pc) {}
2937
2938 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2939 return GetBlock()->GetGraph()->GetIntConstant(
2940 Compute(static_cast<uint32_t>(x->GetValue()),
2941 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2942 }
2943 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2944 return GetBlock()->GetGraph()->GetIntConstant(
2945 Compute(static_cast<uint64_t>(x->GetValue()),
2946 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2947 }
2948
2949 DECLARE_INSTRUCTION(BelowOrEqual);
2950
2951 IfCondition GetCondition() const OVERRIDE {
2952 return kCondBE;
2953 }
2954
2955 IfCondition GetOppositeCondition() const OVERRIDE {
2956 return kCondA;
2957 }
2958
2959 private:
2960 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2961
2962 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2963};
2964
2965class HAbove : public HCondition {
2966 public:
2967 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2968 : HCondition(first, second, dex_pc) {}
2969
2970 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2971 return GetBlock()->GetGraph()->GetIntConstant(
2972 Compute(static_cast<uint32_t>(x->GetValue()),
2973 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2974 }
2975 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2976 return GetBlock()->GetGraph()->GetIntConstant(
2977 Compute(static_cast<uint64_t>(x->GetValue()),
2978 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2979 }
2980
2981 DECLARE_INSTRUCTION(Above);
2982
2983 IfCondition GetCondition() const OVERRIDE {
2984 return kCondA;
2985 }
2986
2987 IfCondition GetOppositeCondition() const OVERRIDE {
2988 return kCondBE;
2989 }
2990
2991 private:
2992 template <typename T> bool Compute(T x, T y) const { return x > y; }
2993
2994 DISALLOW_COPY_AND_ASSIGN(HAbove);
2995};
2996
2997class HAboveOrEqual : public HCondition {
2998 public:
2999 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3000 : HCondition(first, second, dex_pc) {}
3001
3002 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3003 return GetBlock()->GetGraph()->GetIntConstant(
3004 Compute(static_cast<uint32_t>(x->GetValue()),
3005 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3006 }
3007 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3008 return GetBlock()->GetGraph()->GetIntConstant(
3009 Compute(static_cast<uint64_t>(x->GetValue()),
3010 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3011 }
3012
3013 DECLARE_INSTRUCTION(AboveOrEqual);
3014
3015 IfCondition GetCondition() const OVERRIDE {
3016 return kCondAE;
3017 }
3018
3019 IfCondition GetOppositeCondition() const OVERRIDE {
3020 return kCondB;
3021 }
3022
3023 private:
3024 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3025
3026 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3027};
Dave Allison20dfc792014-06-16 20:44:29 -07003028
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003029// Instruction to check how two inputs compare to each other.
3030// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3031class HCompare : public HBinaryOperation {
3032 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003033 HCompare(Primitive::Type type,
3034 HInstruction* first,
3035 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003036 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003037 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003038 : HBinaryOperation(Primitive::kPrimInt,
3039 first,
3040 second,
3041 SideEffectsForArchRuntimeCalls(type),
3042 dex_pc),
3043 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003044 DCHECK_EQ(type, first->GetType());
3045 DCHECK_EQ(type, second->GetType());
3046 }
3047
Roland Levillain9867bc72015-08-05 10:21:34 +01003048 template <typename T>
3049 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003050
Roland Levillain9867bc72015-08-05 10:21:34 +01003051 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003052 return GetBlock()->GetGraph()->GetIntConstant(
3053 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003054 }
3055 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003056 return GetBlock()->GetGraph()->GetIntConstant(
3057 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003058 }
3059
Calin Juravleddb7df22014-11-25 20:56:51 +00003060 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3061 return bias_ == other->AsCompare()->bias_;
3062 }
3063
Mark Mendellc4701932015-04-10 13:18:51 -04003064 ComparisonBias GetBias() const { return bias_; }
3065
Roland Levillain4fa13f62015-07-06 18:11:54 +01003066 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003067
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003068
3069 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3070 // MIPS64 uses a runtime call for FP comparisons.
3071 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3072 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003073
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003074 DECLARE_INSTRUCTION(Compare);
3075
3076 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003077 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003078
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003079 DISALLOW_COPY_AND_ASSIGN(HCompare);
3080};
3081
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003082// A local in the graph. Corresponds to a Dex register.
3083class HLocal : public HTemplateInstruction<0> {
3084 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003085 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003086 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003087
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003088 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003089
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003090 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003091
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003092 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003093 // The Dex register number.
3094 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003095
3096 DISALLOW_COPY_AND_ASSIGN(HLocal);
3097};
3098
3099// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003100class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003101 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003102 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3103 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003104 SetRawInputAt(0, local);
3105 }
3106
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003107 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3108
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003109 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003110
3111 private:
3112 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3113};
3114
3115// Store a value in a given local. This instruction has two inputs: the value
3116// and the local.
3117class HStoreLocal : public HTemplateInstruction<2> {
3118 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003119 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3120 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003121 SetRawInputAt(0, local);
3122 SetRawInputAt(1, value);
3123 }
3124
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003125 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3126
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003127 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003128
3129 private:
3130 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3131};
3132
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003133class HFloatConstant : public HConstant {
3134 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003135 float GetValue() const { return value_; }
3136
David Brazdil77a48ae2015-09-15 12:34:04 +00003137 uint64_t GetValueAsUint64() const OVERRIDE {
3138 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3139 }
3140
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003141 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003142 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003143 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003144 }
3145
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003146 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003147
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003148 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003149 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003150 }
3151 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003152 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003153 }
3154 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003155 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3156 }
3157 bool IsNaN() const {
3158 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003159 }
3160
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003161 DECLARE_INSTRUCTION(FloatConstant);
3162
3163 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003164 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3165 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3166 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3167 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003168
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003169 const float value_;
3170
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003171 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003172 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003173 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003174 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3175};
3176
3177class HDoubleConstant : public HConstant {
3178 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003179 double GetValue() const { return value_; }
3180
David Brazdil77a48ae2015-09-15 12:34:04 +00003181 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3182
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003183 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003184 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003185 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003186 }
3187
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003188 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003189
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003190 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003191 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003192 }
3193 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003194 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003195 }
3196 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003197 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3198 }
3199 bool IsNaN() const {
3200 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003201 }
3202
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003203 DECLARE_INSTRUCTION(DoubleConstant);
3204
3205 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003206 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3207 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3208 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3209 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003210
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003211 const double value_;
3212
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003213 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003214 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003215 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003216 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3217};
3218
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003219enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003220#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003221#include "intrinsics_list.h"
3222 kNone,
3223 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3224#undef INTRINSICS_LIST
3225#undef OPTIMIZING_INTRINSICS
3226};
3227std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3228
Agi Csaki05f20562015-08-19 14:58:14 -07003229enum IntrinsicNeedsEnvironmentOrCache {
3230 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3231 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003232};
3233
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003234class HInvoke : public HInstruction {
3235 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003236 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003237
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003238 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003239
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003240 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003241 SetRawInputAt(index, argument);
3242 }
3243
Roland Levillain3e3d7332015-04-28 11:00:54 +01003244 // Return the number of arguments. This number can be lower than
3245 // the number of inputs returned by InputCount(), as some invoke
3246 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3247 // inputs at the end of their list of inputs.
3248 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3249
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003250 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003251
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003252
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003253 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003254 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003255
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003256 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3257
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003258 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003259 return intrinsic_;
3260 }
3261
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003262 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003263
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003264 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003265 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003266 }
3267
3268 bool CanThrow() const OVERRIDE { return true; }
3269
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003270 uint32_t* GetIntrinsicOptimizations() {
3271 return &intrinsic_optimizations_;
3272 }
3273
3274 const uint32_t* GetIntrinsicOptimizations() const {
3275 return &intrinsic_optimizations_;
3276 }
3277
3278 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3279
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003280 DECLARE_INSTRUCTION(Invoke);
3281
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003282 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003283 HInvoke(ArenaAllocator* arena,
3284 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003285 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003286 Primitive::Type return_type,
3287 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003288 uint32_t dex_method_index,
3289 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003290 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003291 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003292 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003293 inputs_(number_of_arguments + number_of_other_inputs,
3294 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003295 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003296 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003297 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003298 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003299 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003300 }
3301
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003302 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003303 return inputs_[index];
3304 }
3305
David Brazdil1abb4192015-02-17 18:33:36 +00003306 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003307 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003308 }
3309
Roland Levillain3e3d7332015-04-28 11:00:54 +01003310 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003311 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003312 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003313 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003314 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003315 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003316
3317 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3318 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003319
3320 private:
3321 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3322};
3323
Calin Juravle175dc732015-08-25 15:42:32 +01003324class HInvokeUnresolved : public HInvoke {
3325 public:
3326 HInvokeUnresolved(ArenaAllocator* arena,
3327 uint32_t number_of_arguments,
3328 Primitive::Type return_type,
3329 uint32_t dex_pc,
3330 uint32_t dex_method_index,
3331 InvokeType invoke_type)
3332 : HInvoke(arena,
3333 number_of_arguments,
3334 0u /* number_of_other_inputs */,
3335 return_type,
3336 dex_pc,
3337 dex_method_index,
3338 invoke_type) {
3339 }
3340
3341 DECLARE_INSTRUCTION(InvokeUnresolved);
3342
3343 private:
3344 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3345};
3346
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003347class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003348 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003349 // Requirements of this method call regarding the class
3350 // initialization (clinit) check of its declaring class.
3351 enum class ClinitCheckRequirement {
3352 kNone, // Class already initialized.
3353 kExplicit, // Static call having explicit clinit check as last input.
3354 kImplicit, // Static call implicitly requiring a clinit check.
3355 };
3356
Vladimir Marko58155012015-08-19 12:49:41 +00003357 // Determines how to load the target ArtMethod*.
3358 enum class MethodLoadKind {
3359 // Use a String init ArtMethod* loaded from Thread entrypoints.
3360 kStringInit,
3361
3362 // Use the method's own ArtMethod* loaded by the register allocator.
3363 kRecursive,
3364
3365 // Use ArtMethod* at a known address, embed the direct address in the code.
3366 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3367 kDirectAddress,
3368
3369 // Use ArtMethod* at an address that will be known at link time, embed the direct
3370 // address in the code. If the image is relocatable, emit .patch_oat entry.
3371 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3372 // the image relocatable or not.
3373 kDirectAddressWithFixup,
3374
3375 // Load from resoved methods array in the dex cache using a PC-relative load.
3376 // Used when we need to use the dex cache, for example for invoke-static that
3377 // may cause class initialization (the entry may point to a resolution method),
3378 // and we know that we can access the dex cache arrays using a PC-relative load.
3379 kDexCachePcRelative,
3380
3381 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3382 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3383 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3384 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3385 kDexCacheViaMethod,
3386 };
3387
3388 // Determines the location of the code pointer.
3389 enum class CodePtrLocation {
3390 // Recursive call, use local PC-relative call instruction.
3391 kCallSelf,
3392
3393 // Use PC-relative call instruction patched at link time.
3394 // Used for calls within an oat file, boot->boot or app->app.
3395 kCallPCRelative,
3396
3397 // Call to a known target address, embed the direct address in code.
3398 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3399 kCallDirect,
3400
3401 // Call to a target address that will be known at link time, embed the direct
3402 // address in code. If the image is relocatable, emit .patch_oat entry.
3403 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3404 // the image relocatable or not.
3405 kCallDirectWithFixup,
3406
3407 // Use code pointer from the ArtMethod*.
3408 // Used when we don't know the target code. This is also the last-resort-kind used when
3409 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3410 kCallArtMethod,
3411 };
3412
3413 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003414 MethodLoadKind method_load_kind;
3415 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003416 // The method load data holds
3417 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3418 // Note that there are multiple string init methods, each having its own offset.
3419 // - the method address for kDirectAddress
3420 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003421 uint64_t method_load_data;
3422 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003423 };
3424
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003425 HInvokeStaticOrDirect(ArenaAllocator* arena,
3426 uint32_t number_of_arguments,
3427 Primitive::Type return_type,
3428 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003429 uint32_t method_index,
3430 MethodReference target_method,
3431 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003432 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003433 InvokeType invoke_type,
3434 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003435 : HInvoke(arena,
3436 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003437 // There is potentially one extra argument for the HCurrentMethod node, and
3438 // potentially one other if the clinit check is explicit, and potentially
3439 // one other if the method is a string factory.
3440 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3441 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3442 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003443 return_type,
3444 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003445 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003446 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003447 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003448 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003449 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003450 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003451
Vladimir Markodc151b22015-10-15 18:02:30 +01003452 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003453 bool had_current_method_input = HasCurrentMethodInput();
3454 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3455
3456 // Using the current method is the default and once we find a better
3457 // method load kind, we should not go back to using the current method.
3458 DCHECK(had_current_method_input || !needs_current_method_input);
3459
3460 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003461 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3462 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003463 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003464 dispatch_info_ = dispatch_info;
3465 }
3466
Vladimir Markoc53c0792015-11-19 15:48:33 +00003467 void AddSpecialInput(HInstruction* input) {
3468 // We allow only one special input.
3469 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3470 DCHECK(InputCount() == GetSpecialInputIndex() ||
3471 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3472 InsertInputAt(GetSpecialInputIndex(), input);
3473 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003474
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003475 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003476 // We access the method via the dex cache so we can't do an implicit null check.
3477 // TODO: for intrinsics we can generate implicit null checks.
3478 return false;
3479 }
3480
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003481 bool CanBeNull() const OVERRIDE {
3482 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3483 }
3484
Vladimir Markoc53c0792015-11-19 15:48:33 +00003485 // Get the index of the special input, if any.
3486 //
3487 // If the invoke IsStringInit(), it initially has a HFakeString special argument
3488 // which is removed by the instruction simplifier; if the invoke HasCurrentMethodInput(),
3489 // the "special input" is the current method pointer; otherwise there may be one
3490 // platform-specific special input, such as PC-relative addressing base.
3491 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3492
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003493 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003494 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3495 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3496 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003497 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003498 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003499 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003500 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003501 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3502 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003503 bool HasCurrentMethodInput() const {
3504 // This function can be called only after the invoke has been fully initialized by the builder.
3505 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003506 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003507 return true;
3508 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003509 DCHECK(InputCount() == GetSpecialInputIndex() ||
3510 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003511 return false;
3512 }
3513 }
Vladimir Marko58155012015-08-19 12:49:41 +00003514 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3515 MethodReference GetTargetMethod() const { return target_method_; }
3516
3517 int32_t GetStringInitOffset() const {
3518 DCHECK(IsStringInit());
3519 return dispatch_info_.method_load_data;
3520 }
3521
3522 uint64_t GetMethodAddress() const {
3523 DCHECK(HasMethodAddress());
3524 return dispatch_info_.method_load_data;
3525 }
3526
3527 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003528 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003529 return dispatch_info_.method_load_data;
3530 }
3531
3532 uint64_t GetDirectCodePtr() const {
3533 DCHECK(HasDirectCodePtr());
3534 return dispatch_info_.direct_code_ptr;
3535 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003536
Calin Juravle68ad6492015-08-18 17:08:12 +01003537 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3538
Roland Levillain4c0eb422015-04-24 16:43:49 +01003539 // Is this instruction a call to a static method?
3540 bool IsStatic() const {
3541 return GetInvokeType() == kStatic;
3542 }
3543
Vladimir Markofbb184a2015-11-13 14:47:00 +00003544 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3545 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3546 // instruction; only relevant for static calls with explicit clinit check.
3547 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003548 DCHECK(IsStaticWithExplicitClinitCheck());
3549 size_t last_input_index = InputCount() - 1;
3550 HInstruction* last_input = InputAt(last_input_index);
3551 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003552 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003553 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003554 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003555 clinit_check_requirement_ = new_requirement;
3556 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003557 }
3558
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003559 bool IsStringFactoryFor(HFakeString* str) const {
3560 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003561 DCHECK(!HasCurrentMethodInput());
3562 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003563 return InputAt(InputCount() - 1)->AsFakeString() == str;
3564 }
3565
3566 void RemoveFakeStringArgumentAsLastInput() {
3567 DCHECK(IsStringInit());
3568 size_t last_input_index = InputCount() - 1;
3569 HInstruction* last_input = InputAt(last_input_index);
3570 DCHECK(last_input != nullptr);
3571 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3572 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003573 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003574 }
3575
Roland Levillain4c0eb422015-04-24 16:43:49 +01003576 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003577 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003578 bool IsStaticWithExplicitClinitCheck() const {
3579 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3580 }
3581
3582 // Is this a call to a static method whose declaring class has an
3583 // implicit intialization check requirement?
3584 bool IsStaticWithImplicitClinitCheck() const {
3585 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3586 }
3587
Vladimir Markob554b5a2015-11-06 12:57:55 +00003588 // Does this method load kind need the current method as an input?
3589 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3590 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3591 }
3592
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003593 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003594
Roland Levillain4c0eb422015-04-24 16:43:49 +01003595 protected:
3596 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3597 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3598 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3599 HInstruction* input = input_record.GetInstruction();
3600 // `input` is the last input of a static invoke marked as having
3601 // an explicit clinit check. It must either be:
3602 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3603 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3604 DCHECK(input != nullptr);
3605 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3606 }
3607 return input_record;
3608 }
3609
Vladimir Markoc53c0792015-11-19 15:48:33 +00003610 void InsertInputAt(size_t index, HInstruction* input);
3611 void RemoveInputAt(size_t index);
3612
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003613 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003614 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003615 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003616 // The target method may refer to different dex file or method index than the original
3617 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3618 // in derived class to increase visibility.
3619 MethodReference target_method_;
3620 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003621
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003622 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003623};
Vladimir Markofbb184a2015-11-13 14:47:00 +00003624std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003625
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003626class HInvokeVirtual : public HInvoke {
3627 public:
3628 HInvokeVirtual(ArenaAllocator* arena,
3629 uint32_t number_of_arguments,
3630 Primitive::Type return_type,
3631 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003632 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003633 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003634 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003635 vtable_index_(vtable_index) {}
3636
Calin Juravle641547a2015-04-21 22:08:51 +01003637 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003638 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003639 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003640 }
3641
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003642 uint32_t GetVTableIndex() const { return vtable_index_; }
3643
3644 DECLARE_INSTRUCTION(InvokeVirtual);
3645
3646 private:
3647 const uint32_t vtable_index_;
3648
3649 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3650};
3651
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003652class HInvokeInterface : public HInvoke {
3653 public:
3654 HInvokeInterface(ArenaAllocator* arena,
3655 uint32_t number_of_arguments,
3656 Primitive::Type return_type,
3657 uint32_t dex_pc,
3658 uint32_t dex_method_index,
3659 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003660 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003661 imt_index_(imt_index) {}
3662
Calin Juravle641547a2015-04-21 22:08:51 +01003663 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003664 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003665 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003666 }
3667
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003668 uint32_t GetImtIndex() const { return imt_index_; }
3669 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3670
3671 DECLARE_INSTRUCTION(InvokeInterface);
3672
3673 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003674 const uint32_t imt_index_;
3675
3676 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3677};
3678
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003679class HNewInstance : public HExpression<2> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003680 public:
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003681 HNewInstance(HInstruction* cls,
3682 HCurrentMethod* current_method,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003683 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003684 uint16_t type_index,
3685 const DexFile& dex_file,
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003686 bool can_throw,
3687 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003688 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003689 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003690 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003691 dex_file_(dex_file),
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003692 can_throw_(can_throw),
3693 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003694 entrypoint_(entrypoint) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003695 SetRawInputAt(0, cls);
3696 SetRawInputAt(1, current_method);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003697 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003698
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003699 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003700 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003701
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003702 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003703 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003704
3705 // It may throw when called on type that's not instantiable/accessible.
3706 // It can throw OOME.
3707 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3708 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3709
3710 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003711
Calin Juravle10e244f2015-01-26 18:54:32 +00003712 bool CanBeNull() const OVERRIDE { return false; }
3713
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003714 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3715
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003716 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3717 entrypoint_ = entrypoint;
3718 }
3719
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003720 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003721
3722 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003723 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003724 const DexFile& dex_file_;
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003725 const bool can_throw_;
3726 const bool finalizable_;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003727 QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003728
3729 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3730};
3731
Roland Levillain88cb1752014-10-20 16:36:47 +01003732class HNeg : public HUnaryOperation {
3733 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003734 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3735 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003736
Roland Levillain9867bc72015-08-05 10:21:34 +01003737 template <typename T> T Compute(T x) const { return -x; }
3738
3739 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003740 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003741 }
3742 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003743 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003744 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003745
Roland Levillain88cb1752014-10-20 16:36:47 +01003746 DECLARE_INSTRUCTION(Neg);
3747
3748 private:
3749 DISALLOW_COPY_AND_ASSIGN(HNeg);
3750};
3751
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003752class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003753 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003754 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003755 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003756 uint32_t dex_pc,
3757 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003758 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003759 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003760 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003761 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003762 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003763 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003764 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003765 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003766 }
3767
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003768 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003769 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003770
3771 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003772 bool NeedsEnvironment() const OVERRIDE { return true; }
3773
Mingyao Yang0c365e62015-03-31 15:09:29 -07003774 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3775 bool CanThrow() const OVERRIDE { return true; }
3776
Calin Juravle10e244f2015-01-26 18:54:32 +00003777 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003778
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003779 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3780
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003781 DECLARE_INSTRUCTION(NewArray);
3782
3783 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003784 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003785 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003786 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003787
3788 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3789};
3790
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003791class HAdd : public HBinaryOperation {
3792 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003793 HAdd(Primitive::Type result_type,
3794 HInstruction* left,
3795 HInstruction* right,
3796 uint32_t dex_pc = kNoDexPc)
3797 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003798
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003799 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003800
Roland Levillain9867bc72015-08-05 10:21:34 +01003801 template <typename T> T Compute(T x, T y) const { return x + y; }
3802
3803 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003804 return GetBlock()->GetGraph()->GetIntConstant(
3805 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003806 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003807 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003808 return GetBlock()->GetGraph()->GetLongConstant(
3809 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003810 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003811
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003812 DECLARE_INSTRUCTION(Add);
3813
3814 private:
3815 DISALLOW_COPY_AND_ASSIGN(HAdd);
3816};
3817
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003818class HSub : public HBinaryOperation {
3819 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003820 HSub(Primitive::Type result_type,
3821 HInstruction* left,
3822 HInstruction* right,
3823 uint32_t dex_pc = kNoDexPc)
3824 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003825
Roland Levillain9867bc72015-08-05 10:21:34 +01003826 template <typename T> T Compute(T x, T y) const { return x - y; }
3827
3828 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003829 return GetBlock()->GetGraph()->GetIntConstant(
3830 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003831 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003832 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003833 return GetBlock()->GetGraph()->GetLongConstant(
3834 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003835 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003836
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003837 DECLARE_INSTRUCTION(Sub);
3838
3839 private:
3840 DISALLOW_COPY_AND_ASSIGN(HSub);
3841};
3842
Calin Juravle34bacdf2014-10-07 20:23:36 +01003843class HMul : public HBinaryOperation {
3844 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003845 HMul(Primitive::Type result_type,
3846 HInstruction* left,
3847 HInstruction* right,
3848 uint32_t dex_pc = kNoDexPc)
3849 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003850
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003851 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003852
Roland Levillain9867bc72015-08-05 10:21:34 +01003853 template <typename T> T Compute(T x, T y) const { return x * y; }
3854
3855 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003856 return GetBlock()->GetGraph()->GetIntConstant(
3857 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003858 }
3859 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003860 return GetBlock()->GetGraph()->GetLongConstant(
3861 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003862 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003863
3864 DECLARE_INSTRUCTION(Mul);
3865
3866 private:
3867 DISALLOW_COPY_AND_ASSIGN(HMul);
3868};
3869
Calin Juravle7c4954d2014-10-28 16:57:40 +00003870class HDiv : public HBinaryOperation {
3871 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003872 HDiv(Primitive::Type result_type,
3873 HInstruction* left,
3874 HInstruction* right,
3875 uint32_t dex_pc)
3876 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003877
Roland Levillain9867bc72015-08-05 10:21:34 +01003878 template <typename T>
3879 T Compute(T x, T y) const {
3880 // Our graph structure ensures we never have 0 for `y` during
3881 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003882 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003883 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003884 return (y == -1) ? -x : x / y;
3885 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003886
Roland Levillain9867bc72015-08-05 10:21:34 +01003887 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003888 return GetBlock()->GetGraph()->GetIntConstant(
3889 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003890 }
3891 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003892 return GetBlock()->GetGraph()->GetLongConstant(
3893 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003894 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003895
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003896 static SideEffects SideEffectsForArchRuntimeCalls() {
3897 // The generated code can use a runtime call.
3898 return SideEffects::CanTriggerGC();
3899 }
3900
Calin Juravle7c4954d2014-10-28 16:57:40 +00003901 DECLARE_INSTRUCTION(Div);
3902
3903 private:
3904 DISALLOW_COPY_AND_ASSIGN(HDiv);
3905};
3906
Calin Juravlebacfec32014-11-14 15:54:36 +00003907class HRem : public HBinaryOperation {
3908 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003909 HRem(Primitive::Type result_type,
3910 HInstruction* left,
3911 HInstruction* right,
3912 uint32_t dex_pc)
3913 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003914
Roland Levillain9867bc72015-08-05 10:21:34 +01003915 template <typename T>
3916 T Compute(T x, T y) const {
3917 // Our graph structure ensures we never have 0 for `y` during
3918 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003919 DCHECK_NE(y, 0);
3920 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3921 return (y == -1) ? 0 : x % y;
3922 }
3923
Roland Levillain9867bc72015-08-05 10:21:34 +01003924 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003925 return GetBlock()->GetGraph()->GetIntConstant(
3926 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003927 }
3928 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003929 return GetBlock()->GetGraph()->GetLongConstant(
3930 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003931 }
3932
Calin Juravlebacfec32014-11-14 15:54:36 +00003933
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003934 static SideEffects SideEffectsForArchRuntimeCalls() {
3935 return SideEffects::CanTriggerGC();
3936 }
3937
Calin Juravlebacfec32014-11-14 15:54:36 +00003938 DECLARE_INSTRUCTION(Rem);
3939
3940 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003941 DISALLOW_COPY_AND_ASSIGN(HRem);
3942};
3943
Calin Juravled0d48522014-11-04 16:40:20 +00003944class HDivZeroCheck : public HExpression<1> {
3945 public:
3946 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003947 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003948 SetRawInputAt(0, value);
3949 }
3950
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003951 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3952
Calin Juravled0d48522014-11-04 16:40:20 +00003953 bool CanBeMoved() const OVERRIDE { return true; }
3954
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003955 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003956 return true;
3957 }
3958
3959 bool NeedsEnvironment() const OVERRIDE { return true; }
3960 bool CanThrow() const OVERRIDE { return true; }
3961
Calin Juravled0d48522014-11-04 16:40:20 +00003962 DECLARE_INSTRUCTION(DivZeroCheck);
3963
3964 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003965 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3966};
3967
Calin Juravle9aec02f2014-11-18 23:06:35 +00003968class HShl : public HBinaryOperation {
3969 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003970 HShl(Primitive::Type result_type,
3971 HInstruction* left,
3972 HInstruction* right,
3973 uint32_t dex_pc = kNoDexPc)
3974 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003975
Roland Levillain9867bc72015-08-05 10:21:34 +01003976 template <typename T, typename U, typename V>
3977 T Compute(T x, U y, V max_shift_value) const {
3978 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3979 "V is not the unsigned integer type corresponding to T");
3980 return x << (y & max_shift_value);
3981 }
3982
3983 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3984 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003985 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003986 }
3987 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3988 // case is handled as `x << static_cast<int>(y)`.
3989 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3990 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003991 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003992 }
3993 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3994 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003995 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003996 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003997
3998 DECLARE_INSTRUCTION(Shl);
3999
4000 private:
4001 DISALLOW_COPY_AND_ASSIGN(HShl);
4002};
4003
4004class HShr : public HBinaryOperation {
4005 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004006 HShr(Primitive::Type result_type,
4007 HInstruction* left,
4008 HInstruction* right,
4009 uint32_t dex_pc = kNoDexPc)
4010 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004011
Roland Levillain9867bc72015-08-05 10:21:34 +01004012 template <typename T, typename U, typename V>
4013 T Compute(T x, U y, V max_shift_value) const {
4014 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4015 "V is not the unsigned integer type corresponding to T");
4016 return x >> (y & max_shift_value);
4017 }
4018
4019 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4020 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004021 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004022 }
4023 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4024 // case is handled as `x >> static_cast<int>(y)`.
4025 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4026 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004027 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004028 }
4029 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4030 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004031 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004032 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004033
4034 DECLARE_INSTRUCTION(Shr);
4035
4036 private:
4037 DISALLOW_COPY_AND_ASSIGN(HShr);
4038};
4039
4040class HUShr : public HBinaryOperation {
4041 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004042 HUShr(Primitive::Type result_type,
4043 HInstruction* left,
4044 HInstruction* right,
4045 uint32_t dex_pc = kNoDexPc)
4046 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004047
Roland Levillain9867bc72015-08-05 10:21:34 +01004048 template <typename T, typename U, typename V>
4049 T Compute(T x, U y, V max_shift_value) const {
4050 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4051 "V is not the unsigned integer type corresponding to T");
4052 V ux = static_cast<V>(x);
4053 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004054 }
4055
Roland Levillain9867bc72015-08-05 10:21:34 +01004056 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4057 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004058 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004059 }
4060 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4061 // case is handled as `x >>> static_cast<int>(y)`.
4062 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4063 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004064 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004065 }
4066 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4067 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004068 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004069 }
4070
4071 DECLARE_INSTRUCTION(UShr);
4072
4073 private:
4074 DISALLOW_COPY_AND_ASSIGN(HUShr);
4075};
4076
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004077class HAnd : public HBinaryOperation {
4078 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004079 HAnd(Primitive::Type result_type,
4080 HInstruction* left,
4081 HInstruction* right,
4082 uint32_t dex_pc = kNoDexPc)
4083 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004084
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004085 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004086
Roland Levillain9867bc72015-08-05 10:21:34 +01004087 template <typename T, typename U>
4088 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4089
4090 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004091 return GetBlock()->GetGraph()->GetIntConstant(
4092 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004093 }
4094 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004095 return GetBlock()->GetGraph()->GetLongConstant(
4096 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004097 }
4098 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004099 return GetBlock()->GetGraph()->GetLongConstant(
4100 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004101 }
4102 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004103 return GetBlock()->GetGraph()->GetLongConstant(
4104 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004105 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004106
4107 DECLARE_INSTRUCTION(And);
4108
4109 private:
4110 DISALLOW_COPY_AND_ASSIGN(HAnd);
4111};
4112
4113class HOr : public HBinaryOperation {
4114 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004115 HOr(Primitive::Type result_type,
4116 HInstruction* left,
4117 HInstruction* right,
4118 uint32_t dex_pc = kNoDexPc)
4119 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004120
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004121 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004122
Roland Levillain9867bc72015-08-05 10:21:34 +01004123 template <typename T, typename U>
4124 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4125
4126 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004127 return GetBlock()->GetGraph()->GetIntConstant(
4128 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004129 }
4130 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004131 return GetBlock()->GetGraph()->GetLongConstant(
4132 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004133 }
4134 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004135 return GetBlock()->GetGraph()->GetLongConstant(
4136 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004137 }
4138 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004139 return GetBlock()->GetGraph()->GetLongConstant(
4140 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004141 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004142
4143 DECLARE_INSTRUCTION(Or);
4144
4145 private:
4146 DISALLOW_COPY_AND_ASSIGN(HOr);
4147};
4148
4149class HXor : public HBinaryOperation {
4150 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004151 HXor(Primitive::Type result_type,
4152 HInstruction* left,
4153 HInstruction* right,
4154 uint32_t dex_pc = kNoDexPc)
4155 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004156
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004157 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004158
Roland Levillain9867bc72015-08-05 10:21:34 +01004159 template <typename T, typename U>
4160 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4161
4162 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004163 return GetBlock()->GetGraph()->GetIntConstant(
4164 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004165 }
4166 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004167 return GetBlock()->GetGraph()->GetLongConstant(
4168 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004169 }
4170 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004171 return GetBlock()->GetGraph()->GetLongConstant(
4172 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004173 }
4174 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004175 return GetBlock()->GetGraph()->GetLongConstant(
4176 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004177 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004178
4179 DECLARE_INSTRUCTION(Xor);
4180
4181 private:
4182 DISALLOW_COPY_AND_ASSIGN(HXor);
4183};
4184
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004185// The value of a parameter in this method. Its location depends on
4186// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004187class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004188 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004189 HParameterValue(const DexFile& dex_file,
4190 uint16_t type_index,
4191 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004192 Primitive::Type parameter_type,
4193 bool is_this = false)
4194 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004195 dex_file_(dex_file),
4196 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004197 index_(index),
4198 is_this_(is_this),
4199 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004200
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004201 const DexFile& GetDexFile() const { return dex_file_; }
4202 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004203 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004204 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004205
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004206 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4207 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004208
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004209 DECLARE_INSTRUCTION(ParameterValue);
4210
4211 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004212 const DexFile& dex_file_;
4213 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004214 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004215 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004216 const uint8_t index_;
4217
Calin Juravle10e244f2015-01-26 18:54:32 +00004218 // Whether or not the parameter value corresponds to 'this' argument.
4219 const bool is_this_;
4220
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004221 bool can_be_null_;
4222
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004223 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4224};
4225
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004226class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004227 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004228 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4229 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004230
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004231 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004232 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004233 return true;
4234 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004235
Roland Levillain9867bc72015-08-05 10:21:34 +01004236 template <typename T> T Compute(T x) const { return ~x; }
4237
4238 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004239 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004240 }
4241 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004242 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004243 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004244
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004245 DECLARE_INSTRUCTION(Not);
4246
4247 private:
4248 DISALLOW_COPY_AND_ASSIGN(HNot);
4249};
4250
David Brazdil66d126e2015-04-03 16:02:44 +01004251class HBooleanNot : public HUnaryOperation {
4252 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004253 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4254 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004255
4256 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004257 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004258 return true;
4259 }
4260
Roland Levillain9867bc72015-08-05 10:21:34 +01004261 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004262 DCHECK(IsUint<1>(x));
4263 return !x;
4264 }
4265
Roland Levillain9867bc72015-08-05 10:21:34 +01004266 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004267 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004268 }
4269 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4270 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004271 UNREACHABLE();
4272 }
4273
4274 DECLARE_INSTRUCTION(BooleanNot);
4275
4276 private:
4277 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4278};
4279
Roland Levillaindff1f282014-11-05 14:15:05 +00004280class HTypeConversion : public HExpression<1> {
4281 public:
4282 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004283 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004284 : HExpression(result_type,
4285 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4286 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004287 SetRawInputAt(0, input);
4288 DCHECK_NE(input->GetType(), result_type);
4289 }
4290
4291 HInstruction* GetInput() const { return InputAt(0); }
4292 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4293 Primitive::Type GetResultType() const { return GetType(); }
4294
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004295 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004296 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004297
Roland Levillaindff1f282014-11-05 14:15:05 +00004298 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004299 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004300
Mark Mendelle82549b2015-05-06 10:55:34 -04004301 // Try to statically evaluate the conversion and return a HConstant
4302 // containing the result. If the input cannot be converted, return nullptr.
4303 HConstant* TryStaticEvaluation() const;
4304
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004305 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4306 Primitive::Type result_type) {
4307 // Some architectures may not require the 'GC' side effects, but at this point
4308 // in the compilation process we do not know what architecture we will
4309 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004310 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4311 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004312 return SideEffects::CanTriggerGC();
4313 }
4314 return SideEffects::None();
4315 }
4316
Roland Levillaindff1f282014-11-05 14:15:05 +00004317 DECLARE_INSTRUCTION(TypeConversion);
4318
4319 private:
4320 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4321};
4322
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004323static constexpr uint32_t kNoRegNumber = -1;
4324
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004325class HPhi : public HInstruction {
4326 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004327 HPhi(ArenaAllocator* arena,
4328 uint32_t reg_number,
4329 size_t number_of_inputs,
4330 Primitive::Type type,
4331 uint32_t dex_pc = kNoDexPc)
4332 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004333 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004334 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004335 type_(ToPhiType(type)),
4336 // Phis are constructed live and marked dead if conflicting or unused.
4337 // Individual steps of SsaBuilder should assume that if a phi has been
4338 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4339 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004340 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004341 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004342 }
4343
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004344 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4345 static Primitive::Type ToPhiType(Primitive::Type type) {
4346 switch (type) {
4347 case Primitive::kPrimBoolean:
4348 case Primitive::kPrimByte:
4349 case Primitive::kPrimShort:
4350 case Primitive::kPrimChar:
4351 return Primitive::kPrimInt;
4352 default:
4353 return type;
4354 }
4355 }
4356
David Brazdilffee3d32015-07-06 11:48:53 +01004357 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4358
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004359 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004360
4361 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004362 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004363
Calin Juravle10e244f2015-01-26 18:54:32 +00004364 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004365 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004366
Calin Juravle10e244f2015-01-26 18:54:32 +00004367 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4368 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4369
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004370 uint32_t GetRegNumber() const { return reg_number_; }
4371
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004372 void SetDead() { is_live_ = false; }
4373 void SetLive() { is_live_ = true; }
4374 bool IsDead() const { return !is_live_; }
4375 bool IsLive() const { return is_live_; }
4376
David Brazdil77a48ae2015-09-15 12:34:04 +00004377 bool IsVRegEquivalentOf(HInstruction* other) const {
4378 return other != nullptr
4379 && other->IsPhi()
4380 && other->AsPhi()->GetBlock() == GetBlock()
4381 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4382 }
4383
Calin Juravlea4f88312015-04-16 12:57:19 +01004384 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4385 // An equivalent phi is a phi having the same dex register and type.
4386 // It assumes that phis with the same dex register are adjacent.
4387 HPhi* GetNextEquivalentPhiWithSameType() {
4388 HInstruction* next = GetNext();
4389 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4390 if (next->GetType() == GetType()) {
4391 return next->AsPhi();
4392 }
4393 next = next->GetNext();
4394 }
4395 return nullptr;
4396 }
4397
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004398 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004399
David Brazdil1abb4192015-02-17 18:33:36 +00004400 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004401 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004402 return inputs_[index];
4403 }
David Brazdil1abb4192015-02-17 18:33:36 +00004404
4405 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004406 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004407 }
4408
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004409 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004410 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004411 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004412 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004413 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004414 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004415
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004416 DISALLOW_COPY_AND_ASSIGN(HPhi);
4417};
4418
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004419class HNullCheck : public HExpression<1> {
4420 public:
4421 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004422 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004423 SetRawInputAt(0, value);
4424 }
4425
Calin Juravle10e244f2015-01-26 18:54:32 +00004426 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004427 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004428 return true;
4429 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004430
Calin Juravle10e244f2015-01-26 18:54:32 +00004431 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004432
Calin Juravle10e244f2015-01-26 18:54:32 +00004433 bool CanThrow() const OVERRIDE { return true; }
4434
4435 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004436
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004437
4438 DECLARE_INSTRUCTION(NullCheck);
4439
4440 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004441 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4442};
4443
4444class FieldInfo : public ValueObject {
4445 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004446 FieldInfo(MemberOffset field_offset,
4447 Primitive::Type field_type,
4448 bool is_volatile,
4449 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004450 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004451 const DexFile& dex_file,
4452 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004453 : field_offset_(field_offset),
4454 field_type_(field_type),
4455 is_volatile_(is_volatile),
4456 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004457 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004458 dex_file_(dex_file),
4459 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004460
4461 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004462 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004463 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004464 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004465 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004466 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004467 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004468
4469 private:
4470 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004471 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004472 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004473 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004474 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004475 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004476 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004477};
4478
4479class HInstanceFieldGet : public HExpression<1> {
4480 public:
4481 HInstanceFieldGet(HInstruction* value,
4482 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004483 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004484 bool is_volatile,
4485 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004486 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004487 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004488 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004489 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004490 : HExpression(field_type,
4491 SideEffects::FieldReadOfType(field_type, is_volatile),
4492 dex_pc),
4493 field_info_(field_offset,
4494 field_type,
4495 is_volatile,
4496 field_idx,
4497 declaring_class_def_index,
4498 dex_file,
4499 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004500 SetRawInputAt(0, value);
4501 }
4502
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004503 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004504
4505 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4506 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4507 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004508 }
4509
Calin Juravle641547a2015-04-21 22:08:51 +01004510 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4511 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004512 }
4513
4514 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004515 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4516 }
4517
Calin Juravle52c48962014-12-16 17:02:57 +00004518 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004519 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004520 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004521 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004522
4523 DECLARE_INSTRUCTION(InstanceFieldGet);
4524
4525 private:
4526 const FieldInfo field_info_;
4527
4528 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4529};
4530
4531class HInstanceFieldSet : public HTemplateInstruction<2> {
4532 public:
4533 HInstanceFieldSet(HInstruction* object,
4534 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004535 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004536 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004537 bool is_volatile,
4538 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004539 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004540 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004541 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004542 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004543 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4544 dex_pc),
4545 field_info_(field_offset,
4546 field_type,
4547 is_volatile,
4548 field_idx,
4549 declaring_class_def_index,
4550 dex_file,
4551 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004552 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004553 SetRawInputAt(0, object);
4554 SetRawInputAt(1, value);
4555 }
4556
Calin Juravle641547a2015-04-21 22:08:51 +01004557 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4558 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004559 }
4560
Calin Juravle52c48962014-12-16 17:02:57 +00004561 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004562 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004563 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004564 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004565 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004566 bool GetValueCanBeNull() const { return value_can_be_null_; }
4567 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004568
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004569 DECLARE_INSTRUCTION(InstanceFieldSet);
4570
4571 private:
4572 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004573 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004574
4575 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4576};
4577
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004578class HArrayGet : public HExpression<2> {
4579 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004580 HArrayGet(HInstruction* array,
4581 HInstruction* index,
4582 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004583 uint32_t dex_pc,
4584 SideEffects additional_side_effects = SideEffects::None())
4585 : HExpression(type,
4586 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004587 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004588 SetRawInputAt(0, array);
4589 SetRawInputAt(1, index);
4590 }
4591
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004592 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004593 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004594 return true;
4595 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004596 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004597 // TODO: We can be smarter here.
4598 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4599 // which generates the implicit null check. There are cases when these can be removed
4600 // to produce better code. If we ever add optimizations to do so we should allow an
4601 // implicit check here (as long as the address falls in the first page).
4602 return false;
4603 }
4604
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004605 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004606
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004607 HInstruction* GetArray() const { return InputAt(0); }
4608 HInstruction* GetIndex() const { return InputAt(1); }
4609
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004610 DECLARE_INSTRUCTION(ArrayGet);
4611
4612 private:
4613 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4614};
4615
4616class HArraySet : public HTemplateInstruction<3> {
4617 public:
4618 HArraySet(HInstruction* array,
4619 HInstruction* index,
4620 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004621 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004622 uint32_t dex_pc,
4623 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004624 : HTemplateInstruction(
4625 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004626 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4627 additional_side_effects),
4628 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004629 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004630 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004631 value_can_be_null_(true),
4632 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004633 SetRawInputAt(0, array);
4634 SetRawInputAt(1, index);
4635 SetRawInputAt(2, value);
4636 }
4637
Calin Juravle77520bc2015-01-12 18:45:46 +00004638 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004639 // We currently always call a runtime method to catch array store
4640 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004641 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004642 }
4643
Mingyao Yang81014cb2015-06-02 03:16:27 -07004644 // Can throw ArrayStoreException.
4645 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4646
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004647 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004648 // TODO: Same as for ArrayGet.
4649 return false;
4650 }
4651
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004652 void ClearNeedsTypeCheck() {
4653 needs_type_check_ = false;
4654 }
4655
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004656 void ClearValueCanBeNull() {
4657 value_can_be_null_ = false;
4658 }
4659
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004660 void SetStaticTypeOfArrayIsObjectArray() {
4661 static_type_of_array_is_object_array_ = true;
4662 }
4663
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004664 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004665 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004666 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004667
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004668 HInstruction* GetArray() const { return InputAt(0); }
4669 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004670 HInstruction* GetValue() const { return InputAt(2); }
4671
4672 Primitive::Type GetComponentType() const {
4673 // The Dex format does not type floating point index operations. Since the
4674 // `expected_component_type_` is set during building and can therefore not
4675 // be correct, we also check what is the value type. If it is a floating
4676 // point type, we must use that type.
4677 Primitive::Type value_type = GetValue()->GetType();
4678 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4679 ? value_type
4680 : expected_component_type_;
4681 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004682
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004683 Primitive::Type GetRawExpectedComponentType() const {
4684 return expected_component_type_;
4685 }
4686
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004687 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4688 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4689 }
4690
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004691 DECLARE_INSTRUCTION(ArraySet);
4692
4693 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004694 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004695 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004696 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004697 // Cached information for the reference_type_info_ so that codegen
4698 // does not need to inspect the static type.
4699 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004700
4701 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4702};
4703
4704class HArrayLength : public HExpression<1> {
4705 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004706 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004707 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004708 // Note that arrays do not change length, so the instruction does not
4709 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004710 SetRawInputAt(0, array);
4711 }
4712
Calin Juravle77520bc2015-01-12 18:45:46 +00004713 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004714 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004715 return true;
4716 }
Calin Juravle641547a2015-04-21 22:08:51 +01004717 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4718 return obj == InputAt(0);
4719 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004720
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004721 DECLARE_INSTRUCTION(ArrayLength);
4722
4723 private:
4724 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4725};
4726
4727class HBoundsCheck : public HExpression<2> {
4728 public:
4729 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004730 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004731 DCHECK(index->GetType() == Primitive::kPrimInt);
4732 SetRawInputAt(0, index);
4733 SetRawInputAt(1, length);
4734 }
4735
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004736 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004737 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004738 return true;
4739 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004740
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004741 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004742
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004743 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004744
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004745 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004746
4747 DECLARE_INSTRUCTION(BoundsCheck);
4748
4749 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004750 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4751};
4752
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004753/**
4754 * Some DEX instructions are folded into multiple HInstructions that need
4755 * to stay live until the last HInstruction. This class
4756 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004757 * HInstruction stays live. `index` represents the stack location index of the
4758 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004759 */
4760class HTemporary : public HTemplateInstruction<0> {
4761 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004762 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4763 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004764
4765 size_t GetIndex() const { return index_; }
4766
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004767 Primitive::Type GetType() const OVERRIDE {
4768 // The previous instruction is the one that will be stored in the temporary location.
4769 DCHECK(GetPrevious() != nullptr);
4770 return GetPrevious()->GetType();
4771 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004772
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004773 DECLARE_INSTRUCTION(Temporary);
4774
4775 private:
4776 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004777 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4778};
4779
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004780class HSuspendCheck : public HTemplateInstruction<0> {
4781 public:
4782 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004783 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004784
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004785 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004786 return true;
4787 }
4788
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004789 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4790 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004791
4792 DECLARE_INSTRUCTION(SuspendCheck);
4793
4794 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004795 // Only used for code generation, in order to share the same slow path between back edges
4796 // of a same loop.
4797 SlowPathCode* slow_path_;
4798
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004799 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4800};
4801
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004802/**
4803 * Instruction to load a Class object.
4804 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004805class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004806 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004807 HLoadClass(HCurrentMethod* current_method,
4808 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004809 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004810 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004811 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004812 bool needs_access_check,
4813 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004814 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004815 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004816 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004817 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004818 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004819 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004820 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004821 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004822 // Referrers class should not need access check. We never inline unverified
4823 // methods so we can't possibly end up in this situation.
4824 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004825 SetRawInputAt(0, current_method);
4826 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004827
4828 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004829
4830 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004831 // Note that we don't need to test for generate_clinit_check_.
4832 // Whether or not we need to generate the clinit check is processed in
4833 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004834 return other->AsLoadClass()->type_index_ == type_index_ &&
4835 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004836 }
4837
4838 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4839
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004840 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004841 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004842 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004843
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004844 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004845 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004846 }
4847
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004848 bool MustGenerateClinitCheck() const {
4849 return generate_clinit_check_;
4850 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004851
Calin Juravle0ba218d2015-05-19 18:46:01 +01004852 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004853 // The entrypoint the code generator is going to call does not do
4854 // clinit of the class.
4855 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004856 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004857 }
4858
4859 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004860 return MustGenerateClinitCheck() ||
4861 (!is_referrers_class_ && !is_in_dex_cache_) ||
4862 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004863 }
4864
4865 bool NeedsAccessCheck() const {
4866 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004867 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004868
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004869 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004870 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004871 }
4872
Calin Juravleacf735c2015-02-12 15:25:22 +00004873 ReferenceTypeInfo GetLoadedClassRTI() {
4874 return loaded_class_rti_;
4875 }
4876
4877 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4878 // Make sure we only set exact types (the loaded class should never be merged).
4879 DCHECK(rti.IsExact());
4880 loaded_class_rti_ = rti;
4881 }
4882
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004883 const DexFile& GetDexFile() { return dex_file_; }
4884
Vladimir Markodc151b22015-10-15 18:02:30 +01004885 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004886
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004887 static SideEffects SideEffectsForArchRuntimeCalls() {
4888 return SideEffects::CanTriggerGC();
4889 }
4890
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004891 bool IsInDexCache() const { return is_in_dex_cache_; }
4892
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004893 DECLARE_INSTRUCTION(LoadClass);
4894
4895 private:
4896 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004897 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004898 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004899 // Whether this instruction must generate the initialization check.
4900 // Used for code generation.
4901 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004902 const bool needs_access_check_;
4903 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004904
Calin Juravleacf735c2015-02-12 15:25:22 +00004905 ReferenceTypeInfo loaded_class_rti_;
4906
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004907 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4908};
4909
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004910class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004911 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004912 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004913 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4914 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004915 SetRawInputAt(0, current_method);
4916 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004917
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004918 bool CanBeMoved() const OVERRIDE { return true; }
4919
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004920 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4921 return other->AsLoadString()->string_index_ == string_index_;
4922 }
4923
4924 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4925
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004926 uint32_t GetStringIndex() const { return string_index_; }
4927
4928 // TODO: Can we deopt or debug when we resolve a string?
4929 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004930 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004931 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004932
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004933 static SideEffects SideEffectsForArchRuntimeCalls() {
4934 return SideEffects::CanTriggerGC();
4935 }
4936
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004937 DECLARE_INSTRUCTION(LoadString);
4938
4939 private:
4940 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004941
4942 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4943};
4944
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004945/**
4946 * Performs an initialization check on its Class object input.
4947 */
4948class HClinitCheck : public HExpression<1> {
4949 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004950 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004951 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004952 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004953 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4954 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004955 SetRawInputAt(0, constant);
4956 }
4957
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004958 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004959 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004960 return true;
4961 }
4962
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004963 bool NeedsEnvironment() const OVERRIDE {
4964 // May call runtime to initialize the class.
4965 return true;
4966 }
4967
Nicolas Geoffray729645a2015-11-19 13:29:02 +00004968 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004969
4970 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4971
4972 DECLARE_INSTRUCTION(ClinitCheck);
4973
4974 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004975 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4976};
4977
4978class HStaticFieldGet : public HExpression<1> {
4979 public:
4980 HStaticFieldGet(HInstruction* cls,
4981 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004982 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004983 bool is_volatile,
4984 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004985 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004986 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004987 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004988 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004989 : HExpression(field_type,
4990 SideEffects::FieldReadOfType(field_type, is_volatile),
4991 dex_pc),
4992 field_info_(field_offset,
4993 field_type,
4994 is_volatile,
4995 field_idx,
4996 declaring_class_def_index,
4997 dex_file,
4998 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004999 SetRawInputAt(0, cls);
5000 }
5001
Calin Juravle52c48962014-12-16 17:02:57 +00005002
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005003 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005004
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005005 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005006 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5007 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005008 }
5009
5010 size_t ComputeHashCode() const OVERRIDE {
5011 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5012 }
5013
Calin Juravle52c48962014-12-16 17:02:57 +00005014 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005015 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5016 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005017 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005018
5019 DECLARE_INSTRUCTION(StaticFieldGet);
5020
5021 private:
5022 const FieldInfo field_info_;
5023
5024 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5025};
5026
5027class HStaticFieldSet : public HTemplateInstruction<2> {
5028 public:
5029 HStaticFieldSet(HInstruction* cls,
5030 HInstruction* value,
5031 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005032 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005033 bool is_volatile,
5034 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005035 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005036 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005037 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005038 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005039 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5040 dex_pc),
5041 field_info_(field_offset,
5042 field_type,
5043 is_volatile,
5044 field_idx,
5045 declaring_class_def_index,
5046 dex_file,
5047 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005048 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005049 SetRawInputAt(0, cls);
5050 SetRawInputAt(1, value);
5051 }
5052
Calin Juravle52c48962014-12-16 17:02:57 +00005053 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005054 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5055 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005056 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005057
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005058 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005059 bool GetValueCanBeNull() const { return value_can_be_null_; }
5060 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005061
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005062 DECLARE_INSTRUCTION(StaticFieldSet);
5063
5064 private:
5065 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005066 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005067
5068 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5069};
5070
Calin Juravlee460d1d2015-09-29 04:52:17 +01005071class HUnresolvedInstanceFieldGet : public HExpression<1> {
5072 public:
5073 HUnresolvedInstanceFieldGet(HInstruction* obj,
5074 Primitive::Type field_type,
5075 uint32_t field_index,
5076 uint32_t dex_pc)
5077 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5078 field_index_(field_index) {
5079 SetRawInputAt(0, obj);
5080 }
5081
5082 bool NeedsEnvironment() const OVERRIDE { return true; }
5083 bool CanThrow() const OVERRIDE { return true; }
5084
5085 Primitive::Type GetFieldType() const { return GetType(); }
5086 uint32_t GetFieldIndex() const { return field_index_; }
5087
5088 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5089
5090 private:
5091 const uint32_t field_index_;
5092
5093 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5094};
5095
5096class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5097 public:
5098 HUnresolvedInstanceFieldSet(HInstruction* obj,
5099 HInstruction* value,
5100 Primitive::Type field_type,
5101 uint32_t field_index,
5102 uint32_t dex_pc)
5103 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5104 field_type_(field_type),
5105 field_index_(field_index) {
5106 DCHECK_EQ(field_type, value->GetType());
5107 SetRawInputAt(0, obj);
5108 SetRawInputAt(1, value);
5109 }
5110
5111 bool NeedsEnvironment() const OVERRIDE { return true; }
5112 bool CanThrow() const OVERRIDE { return true; }
5113
5114 Primitive::Type GetFieldType() const { return field_type_; }
5115 uint32_t GetFieldIndex() const { return field_index_; }
5116
5117 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5118
5119 private:
5120 const Primitive::Type field_type_;
5121 const uint32_t field_index_;
5122
5123 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5124};
5125
5126class HUnresolvedStaticFieldGet : public HExpression<0> {
5127 public:
5128 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5129 uint32_t field_index,
5130 uint32_t dex_pc)
5131 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5132 field_index_(field_index) {
5133 }
5134
5135 bool NeedsEnvironment() const OVERRIDE { return true; }
5136 bool CanThrow() const OVERRIDE { return true; }
5137
5138 Primitive::Type GetFieldType() const { return GetType(); }
5139 uint32_t GetFieldIndex() const { return field_index_; }
5140
5141 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5142
5143 private:
5144 const uint32_t field_index_;
5145
5146 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5147};
5148
5149class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5150 public:
5151 HUnresolvedStaticFieldSet(HInstruction* value,
5152 Primitive::Type field_type,
5153 uint32_t field_index,
5154 uint32_t dex_pc)
5155 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5156 field_type_(field_type),
5157 field_index_(field_index) {
5158 DCHECK_EQ(field_type, value->GetType());
5159 SetRawInputAt(0, value);
5160 }
5161
5162 bool NeedsEnvironment() const OVERRIDE { return true; }
5163 bool CanThrow() const OVERRIDE { return true; }
5164
5165 Primitive::Type GetFieldType() const { return field_type_; }
5166 uint32_t GetFieldIndex() const { return field_index_; }
5167
5168 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5169
5170 private:
5171 const Primitive::Type field_type_;
5172 const uint32_t field_index_;
5173
5174 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5175};
5176
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005177// Implement the move-exception DEX instruction.
5178class HLoadException : public HExpression<0> {
5179 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005180 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5181 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005182
David Brazdilbbd733e2015-08-18 17:48:17 +01005183 bool CanBeNull() const OVERRIDE { return false; }
5184
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005185 DECLARE_INSTRUCTION(LoadException);
5186
5187 private:
5188 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5189};
5190
David Brazdilcb1c0552015-08-04 16:22:25 +01005191// Implicit part of move-exception which clears thread-local exception storage.
5192// Must not be removed because the runtime expects the TLS to get cleared.
5193class HClearException : public HTemplateInstruction<0> {
5194 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005195 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5196 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005197
5198 DECLARE_INSTRUCTION(ClearException);
5199
5200 private:
5201 DISALLOW_COPY_AND_ASSIGN(HClearException);
5202};
5203
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005204class HThrow : public HTemplateInstruction<1> {
5205 public:
5206 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005207 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005208 SetRawInputAt(0, exception);
5209 }
5210
5211 bool IsControlFlow() const OVERRIDE { return true; }
5212
5213 bool NeedsEnvironment() const OVERRIDE { return true; }
5214
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005215 bool CanThrow() const OVERRIDE { return true; }
5216
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005217
5218 DECLARE_INSTRUCTION(Throw);
5219
5220 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005221 DISALLOW_COPY_AND_ASSIGN(HThrow);
5222};
5223
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005224/**
5225 * Implementation strategies for the code generator of a HInstanceOf
5226 * or `HCheckCast`.
5227 */
5228enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005229 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005230 kExactCheck, // Can do a single class compare.
5231 kClassHierarchyCheck, // Can just walk the super class chain.
5232 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5233 kInterfaceCheck, // No optimization yet when checking against an interface.
5234 kArrayObjectCheck, // Can just check if the array is not primitive.
5235 kArrayCheck // No optimization yet when checking against a generic array.
5236};
5237
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005238class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005239 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005240 HInstanceOf(HInstruction* object,
5241 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005242 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005243 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005244 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005245 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005246 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005247 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005248 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005249 SetRawInputAt(0, object);
5250 SetRawInputAt(1, constant);
5251 }
5252
5253 bool CanBeMoved() const OVERRIDE { return true; }
5254
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005255 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005256 return true;
5257 }
5258
5259 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005260 return false;
5261 }
5262
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005263 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5264
5265 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005266
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005267 // Used only in code generation.
5268 bool MustDoNullCheck() const { return must_do_null_check_; }
5269 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5270
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005271 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5272 return (check_kind == TypeCheckKind::kExactCheck)
5273 ? SideEffects::None()
5274 // Mips currently does runtime calls for any other checks.
5275 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005276 }
5277
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005278 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005279
5280 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005281 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005282 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005283
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005284 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5285};
5286
Calin Juravleb1498f62015-02-16 13:13:29 +00005287class HBoundType : public HExpression<1> {
5288 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005289 // Constructs an HBoundType with the given upper_bound.
5290 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005291 HBoundType(HInstruction* input,
5292 ReferenceTypeInfo upper_bound,
5293 bool upper_can_be_null,
5294 uint32_t dex_pc = kNoDexPc)
5295 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005296 upper_bound_(upper_bound),
5297 upper_can_be_null_(upper_can_be_null),
5298 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005299 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005300 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005301 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005302 }
5303
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005304 // GetUpper* should only be used in reference type propagation.
5305 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5306 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005307
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005308 void SetCanBeNull(bool can_be_null) {
5309 DCHECK(upper_can_be_null_ || !can_be_null);
5310 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005311 }
5312
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005313 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5314
Calin Juravleb1498f62015-02-16 13:13:29 +00005315 DECLARE_INSTRUCTION(BoundType);
5316
5317 private:
5318 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005319 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5320 // It is used to bound the type in cases like:
5321 // if (x instanceof ClassX) {
5322 // // uper_bound_ will be ClassX
5323 // }
5324 const ReferenceTypeInfo upper_bound_;
5325 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5326 // is false then can_be_null_ cannot be true).
5327 const bool upper_can_be_null_;
5328 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005329
5330 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5331};
5332
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005333class HCheckCast : public HTemplateInstruction<2> {
5334 public:
5335 HCheckCast(HInstruction* object,
5336 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005337 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005338 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005339 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005340 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005341 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005342 SetRawInputAt(0, object);
5343 SetRawInputAt(1, constant);
5344 }
5345
5346 bool CanBeMoved() const OVERRIDE { return true; }
5347
5348 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5349 return true;
5350 }
5351
5352 bool NeedsEnvironment() const OVERRIDE {
5353 // Instruction may throw a CheckCastError.
5354 return true;
5355 }
5356
5357 bool CanThrow() const OVERRIDE { return true; }
5358
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005359 bool MustDoNullCheck() const { return must_do_null_check_; }
5360 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005361 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005362
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005363 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005364
5365 DECLARE_INSTRUCTION(CheckCast);
5366
5367 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005368 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005369 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005370
5371 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005372};
5373
Calin Juravle27df7582015-04-17 19:12:31 +01005374class HMemoryBarrier : public HTemplateInstruction<0> {
5375 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005376 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005377 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005378 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005379 barrier_kind_(barrier_kind) {}
5380
5381 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5382
5383 DECLARE_INSTRUCTION(MemoryBarrier);
5384
5385 private:
5386 const MemBarrierKind barrier_kind_;
5387
5388 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5389};
5390
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005391class HMonitorOperation : public HTemplateInstruction<1> {
5392 public:
5393 enum OperationKind {
5394 kEnter,
5395 kExit,
5396 };
5397
5398 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005399 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005400 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5401 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005402 SetRawInputAt(0, object);
5403 }
5404
5405 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005406 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5407
5408 bool CanThrow() const OVERRIDE {
5409 // Verifier guarantees that monitor-exit cannot throw.
5410 // This is important because it allows the HGraphBuilder to remove
5411 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5412 return IsEnter();
5413 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005414
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005415
5416 bool IsEnter() const { return kind_ == kEnter; }
5417
5418 DECLARE_INSTRUCTION(MonitorOperation);
5419
Calin Juravle52c48962014-12-16 17:02:57 +00005420 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005421 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005422
5423 private:
5424 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5425};
5426
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005427/**
5428 * A HInstruction used as a marker for the replacement of new + <init>
5429 * of a String to a call to a StringFactory. Only baseline will see
5430 * the node at code generation, where it will be be treated as null.
5431 * When compiling non-baseline, `HFakeString` instructions are being removed
5432 * in the instruction simplifier.
5433 */
5434class HFakeString : public HTemplateInstruction<0> {
5435 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005436 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5437 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005438
5439 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5440
5441 DECLARE_INSTRUCTION(FakeString);
5442
5443 private:
5444 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5445};
5446
Vladimir Markof9f64412015-09-02 14:05:49 +01005447class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005448 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005449 MoveOperands(Location source,
5450 Location destination,
5451 Primitive::Type type,
5452 HInstruction* instruction)
5453 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005454
5455 Location GetSource() const { return source_; }
5456 Location GetDestination() const { return destination_; }
5457
5458 void SetSource(Location value) { source_ = value; }
5459 void SetDestination(Location value) { destination_ = value; }
5460
5461 // The parallel move resolver marks moves as "in-progress" by clearing the
5462 // destination (but not the source).
5463 Location MarkPending() {
5464 DCHECK(!IsPending());
5465 Location dest = destination_;
5466 destination_ = Location::NoLocation();
5467 return dest;
5468 }
5469
5470 void ClearPending(Location dest) {
5471 DCHECK(IsPending());
5472 destination_ = dest;
5473 }
5474
5475 bool IsPending() const {
5476 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5477 return destination_.IsInvalid() && !source_.IsInvalid();
5478 }
5479
5480 // True if this blocks a move from the given location.
5481 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005482 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005483 }
5484
5485 // A move is redundant if it's been eliminated, if its source and
5486 // destination are the same, or if its destination is unneeded.
5487 bool IsRedundant() const {
5488 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5489 }
5490
5491 // We clear both operands to indicate move that's been eliminated.
5492 void Eliminate() {
5493 source_ = destination_ = Location::NoLocation();
5494 }
5495
5496 bool IsEliminated() const {
5497 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5498 return source_.IsInvalid();
5499 }
5500
Alexey Frunze4dda3372015-06-01 18:31:49 -07005501 Primitive::Type GetType() const { return type_; }
5502
Nicolas Geoffray90218252015-04-15 11:56:51 +01005503 bool Is64BitMove() const {
5504 return Primitive::Is64BitType(type_);
5505 }
5506
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005507 HInstruction* GetInstruction() const { return instruction_; }
5508
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005509 private:
5510 Location source_;
5511 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005512 // The type this move is for.
5513 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005514 // The instruction this move is assocatied with. Null when this move is
5515 // for moving an input in the expected locations of user (including a phi user).
5516 // This is only used in debug mode, to ensure we do not connect interval siblings
5517 // in the same parallel move.
5518 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005519};
5520
5521static constexpr size_t kDefaultNumberOfMoves = 4;
5522
5523class HParallelMove : public HTemplateInstruction<0> {
5524 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005525 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005526 : HTemplateInstruction(SideEffects::None(), dex_pc),
5527 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5528 moves_.reserve(kDefaultNumberOfMoves);
5529 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005530
Nicolas Geoffray90218252015-04-15 11:56:51 +01005531 void AddMove(Location source,
5532 Location destination,
5533 Primitive::Type type,
5534 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005535 DCHECK(source.IsValid());
5536 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005537 if (kIsDebugBuild) {
5538 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005539 for (const MoveOperands& move : moves_) {
5540 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005541 // Special case the situation where the move is for the spill slot
5542 // of the instruction.
5543 if ((GetPrevious() == instruction)
5544 || ((GetPrevious() == nullptr)
5545 && instruction->IsPhi()
5546 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005547 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005548 << "Doing parallel moves for the same instruction.";
5549 } else {
5550 DCHECK(false) << "Doing parallel moves for the same instruction.";
5551 }
5552 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005553 }
5554 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005555 for (const MoveOperands& move : moves_) {
5556 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005557 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005558 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005559 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005560 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005561 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005562 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005563 }
5564
Vladimir Marko225b6462015-09-28 12:17:40 +01005565 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005566 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005567 }
5568
Vladimir Marko225b6462015-09-28 12:17:40 +01005569 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005570
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005571 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005572
5573 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005574 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005575
5576 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5577};
5578
Mark Mendell0616ae02015-04-17 12:49:27 -04005579} // namespace art
5580
Vladimir Markob4536b72015-11-24 13:45:23 +00005581#ifdef ART_ENABLE_CODEGEN_arm
5582#include "nodes_arm.h"
5583#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005584#ifdef ART_ENABLE_CODEGEN_arm64
5585#include "nodes_arm64.h"
5586#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005587#ifdef ART_ENABLE_CODEGEN_x86
5588#include "nodes_x86.h"
5589#endif
5590
5591namespace art {
5592
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005593class HGraphVisitor : public ValueObject {
5594 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005595 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5596 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005597
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005598 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005599 virtual void VisitBasicBlock(HBasicBlock* block);
5600
Roland Levillain633021e2014-10-01 14:12:25 +01005601 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005602 void VisitInsertionOrder();
5603
Roland Levillain633021e2014-10-01 14:12:25 +01005604 // Visit the graph following dominator tree reverse post-order.
5605 void VisitReversePostOrder();
5606
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005607 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005608
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005609 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005610#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005611 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5612
5613 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5614
5615#undef DECLARE_VISIT_INSTRUCTION
5616
5617 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005618 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005619
5620 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5621};
5622
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005623class HGraphDelegateVisitor : public HGraphVisitor {
5624 public:
5625 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5626 virtual ~HGraphDelegateVisitor() {}
5627
5628 // Visit functions that delegate to to super class.
5629#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005630 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005631
5632 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5633
5634#undef DECLARE_VISIT_INSTRUCTION
5635
5636 private:
5637 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5638};
5639
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005640class HInsertionOrderIterator : public ValueObject {
5641 public:
5642 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5643
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005644 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005645 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005646 void Advance() { ++index_; }
5647
5648 private:
5649 const HGraph& graph_;
5650 size_t index_;
5651
5652 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5653};
5654
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005655class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005656 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005657 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5658 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005659 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005660 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005661
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005662 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5663 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005664 void Advance() { ++index_; }
5665
5666 private:
5667 const HGraph& graph_;
5668 size_t index_;
5669
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005670 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005671};
5672
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005673class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005674 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005675 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005676 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005677 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005678 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005679 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005680
5681 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005682 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005683 void Advance() { --index_; }
5684
5685 private:
5686 const HGraph& graph_;
5687 size_t index_;
5688
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005689 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005690};
5691
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005692class HLinearPostOrderIterator : public ValueObject {
5693 public:
5694 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005695 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005696
5697 bool Done() const { return index_ == 0; }
5698
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005699 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005700
5701 void Advance() {
5702 --index_;
5703 DCHECK_GE(index_, 0U);
5704 }
5705
5706 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005707 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005708 size_t index_;
5709
5710 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5711};
5712
5713class HLinearOrderIterator : public ValueObject {
5714 public:
5715 explicit HLinearOrderIterator(const HGraph& graph)
5716 : order_(graph.GetLinearOrder()), index_(0) {}
5717
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005718 bool Done() const { return index_ == order_.size(); }
5719 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005720 void Advance() { ++index_; }
5721
5722 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005723 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005724 size_t index_;
5725
5726 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5727};
5728
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005729// Iterator over the blocks that art part of the loop. Includes blocks part
5730// of an inner loop. The order in which the blocks are iterated is on their
5731// block id.
5732class HBlocksInLoopIterator : public ValueObject {
5733 public:
5734 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5735 : blocks_in_loop_(info.GetBlocks()),
5736 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5737 index_(0) {
5738 if (!blocks_in_loop_.IsBitSet(index_)) {
5739 Advance();
5740 }
5741 }
5742
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005743 bool Done() const { return index_ == blocks_.size(); }
5744 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005745 void Advance() {
5746 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005747 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005748 if (blocks_in_loop_.IsBitSet(index_)) {
5749 break;
5750 }
5751 }
5752 }
5753
5754 private:
5755 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005756 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005757 size_t index_;
5758
5759 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5760};
5761
Mingyao Yang3584bce2015-05-19 16:01:59 -07005762// Iterator over the blocks that art part of the loop. Includes blocks part
5763// of an inner loop. The order in which the blocks are iterated is reverse
5764// post order.
5765class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5766 public:
5767 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5768 : blocks_in_loop_(info.GetBlocks()),
5769 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5770 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005771 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005772 Advance();
5773 }
5774 }
5775
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005776 bool Done() const { return index_ == blocks_.size(); }
5777 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005778 void Advance() {
5779 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005780 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5781 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005782 break;
5783 }
5784 }
5785 }
5786
5787 private:
5788 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005789 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005790 size_t index_;
5791
5792 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5793};
5794
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005795inline int64_t Int64FromConstant(HConstant* constant) {
5796 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5797 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5798 : constant->AsLongConstant()->GetValue();
5799}
5800
Vladimir Marko58155012015-08-19 12:49:41 +00005801inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5802 // For the purposes of the compiler, the dex files must actually be the same object
5803 // if we want to safely treat them as the same. This is especially important for JIT
5804 // as custom class loaders can open the same underlying file (or memory) multiple
5805 // times and provide different class resolution but no two class loaders should ever
5806 // use the same DexFile object - doing so is an unsupported hack that can lead to
5807 // all sorts of weird failures.
5808 return &lhs == &rhs;
5809}
5810
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005811} // namespace art
5812
5813#endif // ART_COMPILER_OPTIMIZING_NODES_H_