blob: 263795d47b81bfc934a88b347e859b3ddb0621fe [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
David Brazdil2d7352b2015-04-20 14:52:42 +0100374 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100375 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000376 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100377 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000378
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000379 template <class InstructionType, typename ValueType>
380 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600381 ArenaSafeMap<ValueType, InstructionType*>* cache,
382 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000383 // Try to find an existing constant of the given value.
384 InstructionType* constant = nullptr;
385 auto cached_constant = cache->find(value);
386 if (cached_constant != cache->end()) {
387 constant = cached_constant->second;
388 }
389
390 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100391 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000392 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600393 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000394 cache->Overwrite(value, constant);
395 InsertConstant(constant);
396 }
397 return constant;
398 }
399
David Brazdil8d5b8b22015-03-24 10:51:52 +0000400 void InsertConstant(HConstant* instruction);
401
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000402 // Cache a float constant into the graph. This method should only be
403 // called by the SsaBuilder when creating "equivalent" instructions.
404 void CacheFloatConstant(HFloatConstant* constant);
405
406 // See CacheFloatConstant comment.
407 void CacheDoubleConstant(HDoubleConstant* constant);
408
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000409 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000410
411 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100412 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000413
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100414 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100415 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000416
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100417 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100418 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100419
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000420 HBasicBlock* entry_block_;
421 HBasicBlock* exit_block_;
422
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100423 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100424 uint16_t maximum_number_of_out_vregs_;
425
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100426 // The number of virtual registers in this method. Contains the parameters.
427 uint16_t number_of_vregs_;
428
429 // The number of virtual registers used by parameters of this method.
430 uint16_t number_of_in_vregs_;
431
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000432 // Number of vreg size slots that the temporaries use (used in baseline compiler).
433 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100434
Mark Mendell1152c922015-04-24 17:06:35 -0400435 // Has bounds checks. We can totally skip BCE if it's false.
436 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800437
David Brazdil77a48ae2015-09-15 12:34:04 +0000438 // Flag whether there are any try/catch blocks in the graph. We will skip
439 // try/catch-related passes if false.
440 bool has_try_catch_;
441
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000442 // Indicates whether the graph should be compiled in a way that
443 // ensures full debuggability. If false, we can apply more
444 // aggressive optimizations that may limit the level of debugging.
445 const bool debuggable_;
446
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000447 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000448 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000449
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100450 // The dex file from which the method is from.
451 const DexFile& dex_file_;
452
453 // The method index in the dex file.
454 const uint32_t method_idx_;
455
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100456 // If inlined, this encodes how the callee is being invoked.
457 const InvokeType invoke_type_;
458
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100459 // Whether the graph has been transformed to SSA form. Only used
460 // in debug mode to ensure we are not using properties only valid
461 // for non-SSA form (like the number of temporaries).
462 bool in_ssa_form_;
463
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100464 const bool should_generate_constructor_barrier_;
465
Mathieu Chartiere401d142015-04-22 13:56:20 -0700466 const InstructionSet instruction_set_;
467
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000468 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000469 HNullConstant* cached_null_constant_;
470 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000471 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000472 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000473 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000474
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100475 HCurrentMethod* cached_current_method_;
476
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000477 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100478 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000479 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000480 DISALLOW_COPY_AND_ASSIGN(HGraph);
481};
482
Vladimir Markof9f64412015-09-02 14:05:49 +0100483class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000484 public:
485 HLoopInformation(HBasicBlock* header, HGraph* graph)
486 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100487 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100488 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100489 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100490 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
491 back_edges_.reserve(kDefaultNumberOfBackEdges);
492 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100493
494 HBasicBlock* GetHeader() const {
495 return header_;
496 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000497
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000498 void SetHeader(HBasicBlock* block) {
499 header_ = block;
500 }
501
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100502 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
503 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
504 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
505
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000506 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100507 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000508 }
509
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100510 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100511 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100512 }
513
David Brazdil46e2a392015-03-16 17:31:52 +0000514 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100515 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100516 }
517
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000518 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100519 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000520 }
521
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100522 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100523
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100524 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100525 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100526 }
527
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100528 // Returns the lifetime position of the back edge that has the
529 // greatest lifetime position.
530 size_t GetLifetimeEnd() const;
531
532 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100533 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100534 }
535
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100536 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100537 // that is the header dominates the back edge.
538 bool Populate();
539
David Brazdila4b8c212015-05-07 09:59:30 +0100540 // Reanalyzes the loop by removing loop info from its blocks and re-running
541 // Populate(). If there are no back edges left, the loop info is completely
542 // removed as well as its SuspendCheck instruction. It must be run on nested
543 // inner loops first.
544 void Update();
545
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100546 // Returns whether this loop information contains `block`.
547 // Note that this loop information *must* be populated before entering this function.
548 bool Contains(const HBasicBlock& block) const;
549
550 // Returns whether this loop information is an inner loop of `other`.
551 // Note that `other` *must* be populated before entering this function.
552 bool IsIn(const HLoopInformation& other) const;
553
Aart Bik73f1f3b2015-10-28 15:28:08 -0700554 // Returns true if instruction is not defined within this loop or any loop nested inside
555 // this loop. If must_dominate is set, only definitions that actually dominate the loop
556 // header can be invariant. Otherwise, any definition outside the loop, including
557 // definitions that appear after the loop, is invariant.
558 bool IsLoopInvariant(HInstruction* instruction, bool must_dominate) const;
559
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100560 const ArenaBitVector& GetBlocks() const { return blocks_; }
561
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000562 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000563 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000564
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000565 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100566 // Internal recursive implementation of `Populate`.
567 void PopulateRecursive(HBasicBlock* block);
568
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000569 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100570 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100571 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100572 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000573
574 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
575};
576
David Brazdilec16f792015-08-19 15:04:01 +0100577// Stores try/catch information for basic blocks.
578// Note that HGraph is constructed so that catch blocks cannot simultaneously
579// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100580class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100581 public:
582 // Try block information constructor.
583 explicit TryCatchInformation(const HTryBoundary& try_entry)
584 : try_entry_(&try_entry),
585 catch_dex_file_(nullptr),
586 catch_type_index_(DexFile::kDexNoIndex16) {
587 DCHECK(try_entry_ != nullptr);
588 }
589
590 // Catch block information constructor.
591 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
592 : try_entry_(nullptr),
593 catch_dex_file_(&dex_file),
594 catch_type_index_(catch_type_index) {}
595
596 bool IsTryBlock() const { return try_entry_ != nullptr; }
597
598 const HTryBoundary& GetTryEntry() const {
599 DCHECK(IsTryBlock());
600 return *try_entry_;
601 }
602
603 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
604
605 bool IsCatchAllTypeIndex() const {
606 DCHECK(IsCatchBlock());
607 return catch_type_index_ == DexFile::kDexNoIndex16;
608 }
609
610 uint16_t GetCatchTypeIndex() const {
611 DCHECK(IsCatchBlock());
612 return catch_type_index_;
613 }
614
615 const DexFile& GetCatchDexFile() const {
616 DCHECK(IsCatchBlock());
617 return *catch_dex_file_;
618 }
619
620 private:
621 // One of possibly several TryBoundary instructions entering the block's try.
622 // Only set for try blocks.
623 const HTryBoundary* try_entry_;
624
625 // Exception type information. Only set for catch blocks.
626 const DexFile* catch_dex_file_;
627 const uint16_t catch_type_index_;
628};
629
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100630static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100631static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100632
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000633// A block in a method. Contains the list of instructions represented
634// as a double linked list. Each block knows its predecessors and
635// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100636
Vladimir Markof9f64412015-09-02 14:05:49 +0100637class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000638 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600639 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000640 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000641 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
642 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000643 loop_information_(nullptr),
644 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000645 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100646 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100647 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100648 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000649 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000650 try_catch_information_(nullptr) {
651 predecessors_.reserve(kDefaultNumberOfPredecessors);
652 successors_.reserve(kDefaultNumberOfSuccessors);
653 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
654 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000655
Vladimir Marko60584552015-09-03 13:35:12 +0000656 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100657 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000658 }
659
Vladimir Marko60584552015-09-03 13:35:12 +0000660 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100661 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000662 }
663
David Brazdild26a4112015-11-10 11:07:31 +0000664 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
665 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
666
Vladimir Marko60584552015-09-03 13:35:12 +0000667 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
668 return ContainsElement(successors_, block, start_from);
669 }
670
671 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100672 return dominated_blocks_;
673 }
674
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100675 bool IsEntryBlock() const {
676 return graph_->GetEntryBlock() == this;
677 }
678
679 bool IsExitBlock() const {
680 return graph_->GetExitBlock() == this;
681 }
682
David Brazdil46e2a392015-03-16 17:31:52 +0000683 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000684 bool IsSingleTryBoundary() const;
685
686 // Returns true if this block emits nothing but a jump.
687 bool IsSingleJump() const {
688 HLoopInformation* loop_info = GetLoopInformation();
689 return (IsSingleGoto() || IsSingleTryBoundary())
690 // Back edges generate a suspend check.
691 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
692 }
David Brazdil46e2a392015-03-16 17:31:52 +0000693
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000694 void AddBackEdge(HBasicBlock* back_edge) {
695 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000696 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000697 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100698 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000699 loop_information_->AddBackEdge(back_edge);
700 }
701
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000702 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000703 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000704
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100705 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000706 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600707 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000708
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000709 HBasicBlock* GetDominator() const { return dominator_; }
710 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000711 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
712
713 void RemoveDominatedBlock(HBasicBlock* block) {
714 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100715 }
Vladimir Marko60584552015-09-03 13:35:12 +0000716
717 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
718 ReplaceElement(dominated_blocks_, existing, new_block);
719 }
720
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100721 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000722
723 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100724 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000725 }
726
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100727 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
728 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100729 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100730 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100731 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
732 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000733
734 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000735 successors_.push_back(block);
736 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000737 }
738
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100739 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
740 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100741 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000742 new_block->predecessors_.push_back(this);
743 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000744 }
745
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000746 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
747 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000748 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000749 new_block->successors_.push_back(this);
750 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000751 }
752
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100753 // Insert `this` between `predecessor` and `successor. This method
754 // preserves the indicies, and will update the first edge found between
755 // `predecessor` and `successor`.
756 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
757 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100758 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000759 successor->predecessors_[predecessor_index] = this;
760 predecessor->successors_[successor_index] = this;
761 successors_.push_back(successor);
762 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100763 }
764
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100765 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000766 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100767 }
768
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000769 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000770 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000771 }
772
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100773 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000774 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100775 }
776
777 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000778 predecessors_.push_back(block);
779 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780 }
781
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100782 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000783 DCHECK_EQ(predecessors_.size(), 2u);
784 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100785 }
786
David Brazdil769c9e52015-04-27 13:54:09 +0100787 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000788 DCHECK_EQ(successors_.size(), 2u);
789 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100790 }
791
David Brazdilfc6a86a2015-06-26 10:33:45 +0000792 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000793 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100794 }
795
David Brazdilfc6a86a2015-06-26 10:33:45 +0000796 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000797 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100798 }
799
David Brazdilfc6a86a2015-06-26 10:33:45 +0000800 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000801 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100802 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000803 }
804
805 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000806 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100807 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000808 }
809
810 // Returns whether the first occurrence of `predecessor` in the list of
811 // predecessors is at index `idx`.
812 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100813 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000814 return GetPredecessorIndexOf(predecessor) == idx;
815 }
816
David Brazdild7558da2015-09-22 13:04:14 +0100817 // Create a new block between this block and its predecessors. The new block
818 // is added to the graph, all predecessor edges are relinked to it and an edge
819 // is created to `this`. Returns the new empty block. Reverse post order or
820 // loop and try/catch information are not updated.
821 HBasicBlock* CreateImmediateDominator();
822
David Brazdilfc6a86a2015-06-26 10:33:45 +0000823 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100824 // created, latter block. Note that this method will add the block to the
825 // graph, create a Goto at the end of the former block and will create an edge
826 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100827 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000828 HBasicBlock* SplitBefore(HInstruction* cursor);
829
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000830 // Split the block into two blocks just after `cursor`. Returns the newly
831 // created block. Note that this method just updates raw block information,
832 // like predecessors, successors, dominators, and instruction list. It does not
833 // update the graph, reverse post order, loop information, nor make sure the
834 // blocks are consistent (for example ending with a control flow instruction).
835 HBasicBlock* SplitAfter(HInstruction* cursor);
836
David Brazdil9bc43612015-11-05 21:25:24 +0000837 // Split catch block into two blocks after the original move-exception bytecode
838 // instruction, or at the beginning if not present. Returns the newly created,
839 // latter block, or nullptr if such block could not be created (must be dead
840 // in that case). Note that this method just updates raw block information,
841 // like predecessors, successors, dominators, and instruction list. It does not
842 // update the graph, reverse post order, loop information, nor make sure the
843 // blocks are consistent (for example ending with a control flow instruction).
844 HBasicBlock* SplitCatchBlockAfterMoveException();
845
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000846 // Merge `other` at the end of `this`. Successors and dominated blocks of
847 // `other` are changed to be successors and dominated blocks of `this`. Note
848 // that this method does not update the graph, reverse post order, loop
849 // information, nor make sure the blocks are consistent (for example ending
850 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100851 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000852
853 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
854 // of `this` are moved to `other`.
855 // Note that this method does not update the graph, reverse post order, loop
856 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000857 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000858 void ReplaceWith(HBasicBlock* other);
859
David Brazdil2d7352b2015-04-20 14:52:42 +0100860 // Merge `other` at the end of `this`. This method updates loops, reverse post
861 // order, links to predecessors, successors, dominators and deletes the block
862 // from the graph. The two blocks must be successive, i.e. `this` the only
863 // predecessor of `other` and vice versa.
864 void MergeWith(HBasicBlock* other);
865
866 // Disconnects `this` from all its predecessors, successors and dominator,
867 // removes it from all loops it is included in and eventually from the graph.
868 // The block must not dominate any other block. Predecessors and successors
869 // are safely updated.
870 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000871
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000872 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100873 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100874 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100875 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100876 // Replace instruction `initial` with `replacement` within this block.
877 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
878 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100879 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100880 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000881 // RemoveInstruction and RemovePhi delete a given instruction from the respective
882 // instruction list. With 'ensure_safety' set to true, it verifies that the
883 // instruction is not in use and removes it from the use lists of its inputs.
884 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
885 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100886 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100887
888 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100889 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100890 }
891
Roland Levillain6b879dd2014-09-22 17:13:44 +0100892 bool IsLoopPreHeaderFirstPredecessor() const {
893 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100894 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100895 }
896
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100897 HLoopInformation* GetLoopInformation() const {
898 return loop_information_;
899 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000901 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100902 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000903 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100904 void SetInLoop(HLoopInformation* info) {
905 if (IsLoopHeader()) {
906 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100907 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100908 loop_information_ = info;
909 } else if (loop_information_->Contains(*info->GetHeader())) {
910 // Block is currently part of an outer loop. Make it part of this inner loop.
911 // Note that a non loop header having a loop information means this loop information
912 // has already been populated
913 loop_information_ = info;
914 } else {
915 // Block is part of an inner loop. Do not update the loop information.
916 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
917 // at this point, because this method is being called while populating `info`.
918 }
919 }
920
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000921 // Raw update of the loop information.
922 void SetLoopInformation(HLoopInformation* info) {
923 loop_information_ = info;
924 }
925
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100926 bool IsInLoop() const { return loop_information_ != nullptr; }
927
David Brazdilec16f792015-08-19 15:04:01 +0100928 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
929
930 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
931 try_catch_information_ = try_catch_information;
932 }
933
934 bool IsTryBlock() const {
935 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
936 }
937
938 bool IsCatchBlock() const {
939 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
940 }
David Brazdilffee3d32015-07-06 11:48:53 +0100941
942 // Returns the try entry that this block's successors should have. They will
943 // be in the same try, unless the block ends in a try boundary. In that case,
944 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100945 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100946
David Brazdild7558da2015-09-22 13:04:14 +0100947 bool HasThrowingInstructions() const;
948
David Brazdila4b8c212015-05-07 09:59:30 +0100949 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100950 bool Dominates(HBasicBlock* block) const;
951
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100952 size_t GetLifetimeStart() const { return lifetime_start_; }
953 size_t GetLifetimeEnd() const { return lifetime_end_; }
954
955 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
956 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
957
David Brazdil8d5b8b22015-03-24 10:51:52 +0000958 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000959 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100960 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000961 bool HasSinglePhi() const;
962
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000963 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000964 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000965 ArenaVector<HBasicBlock*> predecessors_;
966 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100967 HInstructionList instructions_;
968 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000969 HLoopInformation* loop_information_;
970 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000971 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100972 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100973 // The dex program counter of the first instruction of this block.
974 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100975 size_t lifetime_start_;
976 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100977 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100978
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000979 friend class HGraph;
980 friend class HInstruction;
981
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000982 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
983};
984
David Brazdilb2bd1c52015-03-25 11:17:37 +0000985// Iterates over the LoopInformation of all loops which contain 'block'
986// from the innermost to the outermost.
987class HLoopInformationOutwardIterator : public ValueObject {
988 public:
989 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
990 : current_(block.GetLoopInformation()) {}
991
992 bool Done() const { return current_ == nullptr; }
993
994 void Advance() {
995 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100996 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000997 }
998
999 HLoopInformation* Current() const {
1000 DCHECK(!Done());
1001 return current_;
1002 }
1003
1004 private:
1005 HLoopInformation* current_;
1006
1007 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1008};
1009
Alexandre Ramesef20f712015-06-09 10:29:30 +01001010#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001011 M(Above, Condition) \
1012 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001013 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001014 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001015 M(ArrayGet, Instruction) \
1016 M(ArrayLength, Instruction) \
1017 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001018 M(Below, Condition) \
1019 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001020 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001021 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001022 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001023 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001024 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001025 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001026 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001027 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001028 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001029 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001030 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001031 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001032 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001033 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001034 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001035 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001036 M(FloatConstant, Constant) \
1037 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001038 M(GreaterThan, Condition) \
1039 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001040 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001041 M(InstanceFieldGet, Instruction) \
1042 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001043 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001044 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001045 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001046 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001047 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001048 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001049 M(LessThan, Condition) \
1050 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001051 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001052 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001053 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001054 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001055 M(Local, Instruction) \
1056 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001057 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001058 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001059 M(Mul, BinaryOperation) \
1060 M(Neg, UnaryOperation) \
1061 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001062 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001063 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001064 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001065 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001066 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001067 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001068 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001069 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001070 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001071 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001072 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001073 M(Return, Instruction) \
1074 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001075 M(Shl, BinaryOperation) \
1076 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001077 M(StaticFieldGet, Instruction) \
1078 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001079 M(UnresolvedInstanceFieldGet, Instruction) \
1080 M(UnresolvedInstanceFieldSet, Instruction) \
1081 M(UnresolvedStaticFieldGet, Instruction) \
1082 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001083 M(StoreLocal, Instruction) \
1084 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001085 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001086 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001087 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001088 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001089 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001090 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001091 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001092
Alexandre Ramesef20f712015-06-09 10:29:30 +01001093#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1094
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001095#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001096#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001097#else
1098#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1099 M(Arm64IntermediateAddress, Instruction)
1100#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001101
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001102#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1103
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001104#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1105
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001106#ifndef ART_ENABLE_CODEGEN_x86
1107#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1108#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001109#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1110 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001111 M(X86LoadFromConstantTable, Instruction) \
1112 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001113#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001114
1115#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1116
1117#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1118 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1119 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1120 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001121 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001122 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001123 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1124 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1125
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001126#define FOR_EACH_INSTRUCTION(M) \
1127 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1128 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001129 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001130 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001131 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001132
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001133#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001134FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1135#undef FORWARD_DECLARATION
1136
Roland Levillainccc07a92014-09-16 14:48:16 +01001137#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001138 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1139 const char* DebugName() const OVERRIDE { return #type; } \
1140 const H##type* As##type() const OVERRIDE { return this; } \
1141 H##type* As##type() OVERRIDE { return this; } \
1142 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001143 return other->Is##type(); \
1144 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001145 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001146
David Brazdiled596192015-01-23 10:39:45 +00001147template <typename T> class HUseList;
1148
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001149template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001150class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001151 public:
David Brazdiled596192015-01-23 10:39:45 +00001152 HUseListNode* GetPrevious() const { return prev_; }
1153 HUseListNode* GetNext() const { return next_; }
1154 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001155 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001156 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001157
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001158 private:
David Brazdiled596192015-01-23 10:39:45 +00001159 HUseListNode(T user, size_t index)
1160 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1161
1162 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001163 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001164 HUseListNode<T>* prev_;
1165 HUseListNode<T>* next_;
1166
1167 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001168
1169 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1170};
1171
David Brazdiled596192015-01-23 10:39:45 +00001172template <typename T>
1173class HUseList : public ValueObject {
1174 public:
1175 HUseList() : first_(nullptr) {}
1176
1177 void Clear() {
1178 first_ = nullptr;
1179 }
1180
1181 // Adds a new entry at the beginning of the use list and returns
1182 // the newly created node.
1183 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001184 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001185 if (IsEmpty()) {
1186 first_ = new_node;
1187 } else {
1188 first_->prev_ = new_node;
1189 new_node->next_ = first_;
1190 first_ = new_node;
1191 }
1192 return new_node;
1193 }
1194
1195 HUseListNode<T>* GetFirst() const {
1196 return first_;
1197 }
1198
1199 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001200 DCHECK(node != nullptr);
1201 DCHECK(Contains(node));
1202
David Brazdiled596192015-01-23 10:39:45 +00001203 if (node->prev_ != nullptr) {
1204 node->prev_->next_ = node->next_;
1205 }
1206 if (node->next_ != nullptr) {
1207 node->next_->prev_ = node->prev_;
1208 }
1209 if (node == first_) {
1210 first_ = node->next_;
1211 }
1212 }
1213
David Brazdil1abb4192015-02-17 18:33:36 +00001214 bool Contains(const HUseListNode<T>* node) const {
1215 if (node == nullptr) {
1216 return false;
1217 }
1218 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1219 if (current == node) {
1220 return true;
1221 }
1222 }
1223 return false;
1224 }
1225
David Brazdiled596192015-01-23 10:39:45 +00001226 bool IsEmpty() const {
1227 return first_ == nullptr;
1228 }
1229
1230 bool HasOnlyOneUse() const {
1231 return first_ != nullptr && first_->next_ == nullptr;
1232 }
1233
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001234 size_t SizeSlow() const {
1235 size_t count = 0;
1236 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1237 ++count;
1238 }
1239 return count;
1240 }
1241
David Brazdiled596192015-01-23 10:39:45 +00001242 private:
1243 HUseListNode<T>* first_;
1244};
1245
1246template<typename T>
1247class HUseIterator : public ValueObject {
1248 public:
1249 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1250
1251 bool Done() const { return current_ == nullptr; }
1252
1253 void Advance() {
1254 DCHECK(!Done());
1255 current_ = current_->GetNext();
1256 }
1257
1258 HUseListNode<T>* Current() const {
1259 DCHECK(!Done());
1260 return current_;
1261 }
1262
1263 private:
1264 HUseListNode<T>* current_;
1265
1266 friend class HValue;
1267};
1268
David Brazdil1abb4192015-02-17 18:33:36 +00001269// This class is used by HEnvironment and HInstruction classes to record the
1270// instructions they use and pointers to the corresponding HUseListNodes kept
1271// by the used instructions.
1272template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001273class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001274 public:
1275 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1276 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1277
1278 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1279 : instruction_(old_record.instruction_), use_node_(use_node) {
1280 DCHECK(instruction_ != nullptr);
1281 DCHECK(use_node_ != nullptr);
1282 DCHECK(old_record.use_node_ == nullptr);
1283 }
1284
1285 HInstruction* GetInstruction() const { return instruction_; }
1286 HUseListNode<T>* GetUseNode() const { return use_node_; }
1287
1288 private:
1289 // Instruction used by the user.
1290 HInstruction* instruction_;
1291
1292 // Corresponding entry in the use list kept by 'instruction_'.
1293 HUseListNode<T>* use_node_;
1294};
1295
Aart Bik854a02b2015-07-14 16:07:00 -07001296/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001297 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001298 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001299 * For write/read dependences on fields/arrays, the dependence analysis uses
1300 * type disambiguation (e.g. a float field write cannot modify the value of an
1301 * integer field read) and the access type (e.g. a reference array write cannot
1302 * modify the value of a reference field read [although it may modify the
1303 * reference fetch prior to reading the field, which is represented by its own
1304 * write/read dependence]). The analysis makes conservative points-to
1305 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1306 * the same, and any reference read depends on any reference read without
1307 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001308 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001309 * The internal representation uses 38-bit and is described in the table below.
1310 * The first line indicates the side effect, and for field/array accesses the
1311 * second line indicates the type of the access (in the order of the
1312 * Primitive::Type enum).
1313 * The two numbered lines below indicate the bit position in the bitfield (read
1314 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001315 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001316 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1317 * +-------------+---------+---------+--------------+---------+---------+
1318 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1319 * | 3 |333333322|222222221| 1 |111111110|000000000|
1320 * | 7 |654321098|765432109| 8 |765432109|876543210|
1321 *
1322 * Note that, to ease the implementation, 'changes' bits are least significant
1323 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001324 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001325class SideEffects : public ValueObject {
1326 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001327 SideEffects() : flags_(0) {}
1328
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001329 static SideEffects None() {
1330 return SideEffects(0);
1331 }
1332
1333 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001334 return SideEffects(kAllChangeBits | kAllDependOnBits);
1335 }
1336
1337 static SideEffects AllChanges() {
1338 return SideEffects(kAllChangeBits);
1339 }
1340
1341 static SideEffects AllDependencies() {
1342 return SideEffects(kAllDependOnBits);
1343 }
1344
1345 static SideEffects AllExceptGCDependency() {
1346 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1347 }
1348
1349 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001350 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001351 }
1352
Aart Bik34c3ba92015-07-20 14:08:59 -07001353 static SideEffects AllWrites() {
1354 return SideEffects(kAllWrites);
1355 }
1356
1357 static SideEffects AllReads() {
1358 return SideEffects(kAllReads);
1359 }
1360
1361 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1362 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001363 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001364 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001365 }
1366
Aart Bik854a02b2015-07-14 16:07:00 -07001367 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1368 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001369 }
1370
Aart Bik34c3ba92015-07-20 14:08:59 -07001371 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1372 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001373 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001374 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001375 }
1376
1377 static SideEffects ArrayReadOfType(Primitive::Type type) {
1378 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1379 }
1380
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001381 static SideEffects CanTriggerGC() {
1382 return SideEffects(1ULL << kCanTriggerGCBit);
1383 }
1384
1385 static SideEffects DependsOnGC() {
1386 return SideEffects(1ULL << kDependsOnGCBit);
1387 }
1388
Aart Bik854a02b2015-07-14 16:07:00 -07001389 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001390 SideEffects Union(SideEffects other) const {
1391 return SideEffects(flags_ | other.flags_);
1392 }
1393
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001394 SideEffects Exclusion(SideEffects other) const {
1395 return SideEffects(flags_ & ~other.flags_);
1396 }
1397
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001398 void Add(SideEffects other) {
1399 flags_ |= other.flags_;
1400 }
1401
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001402 bool Includes(SideEffects other) const {
1403 return (other.flags_ & flags_) == other.flags_;
1404 }
1405
1406 bool HasSideEffects() const {
1407 return (flags_ & kAllChangeBits);
1408 }
1409
1410 bool HasDependencies() const {
1411 return (flags_ & kAllDependOnBits);
1412 }
1413
1414 // Returns true if there are no side effects or dependencies.
1415 bool DoesNothing() const {
1416 return flags_ == 0;
1417 }
1418
Aart Bik854a02b2015-07-14 16:07:00 -07001419 // Returns true if something is written.
1420 bool DoesAnyWrite() const {
1421 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001422 }
1423
Aart Bik854a02b2015-07-14 16:07:00 -07001424 // Returns true if something is read.
1425 bool DoesAnyRead() const {
1426 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001427 }
1428
Aart Bik854a02b2015-07-14 16:07:00 -07001429 // Returns true if potentially everything is written and read
1430 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001431 bool DoesAllReadWrite() const {
1432 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1433 }
1434
Aart Bik854a02b2015-07-14 16:07:00 -07001435 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001436 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001437 }
1438
Roland Levillain0d5a2812015-11-13 10:07:31 +00001439 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001440 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001441 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1442 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001443 }
1444
1445 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001446 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001447 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001448 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001449 for (int s = kLastBit; s >= 0; s--) {
1450 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1451 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1452 // This is a bit for the GC side effect.
1453 if (current_bit_is_set) {
1454 flags += "GC";
1455 }
Aart Bik854a02b2015-07-14 16:07:00 -07001456 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001457 } else {
1458 // This is a bit for the array/field analysis.
1459 // The underscore character stands for the 'can trigger GC' bit.
1460 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1461 if (current_bit_is_set) {
1462 flags += kDebug[s];
1463 }
1464 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1465 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1466 flags += "|";
1467 }
1468 }
Aart Bik854a02b2015-07-14 16:07:00 -07001469 }
1470 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001471 }
1472
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001473 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001474
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001475 private:
1476 static constexpr int kFieldArrayAnalysisBits = 9;
1477
1478 static constexpr int kFieldWriteOffset = 0;
1479 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1480 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1481 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1482
1483 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1484
1485 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1486 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1487 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1488 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1489
1490 static constexpr int kLastBit = kDependsOnGCBit;
1491 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1492
1493 // Aliases.
1494
1495 static_assert(kChangeBits == kDependOnBits,
1496 "the 'change' bits should match the 'depend on' bits.");
1497
1498 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1499 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1500 static constexpr uint64_t kAllWrites =
1501 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1502 static constexpr uint64_t kAllReads =
1503 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001504
Aart Bik854a02b2015-07-14 16:07:00 -07001505 // Work around the fact that HIR aliases I/F and J/D.
1506 // TODO: remove this interceptor once HIR types are clean
1507 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1508 switch (type) {
1509 case Primitive::kPrimInt:
1510 case Primitive::kPrimFloat:
1511 return TypeFlag(Primitive::kPrimInt, offset) |
1512 TypeFlag(Primitive::kPrimFloat, offset);
1513 case Primitive::kPrimLong:
1514 case Primitive::kPrimDouble:
1515 return TypeFlag(Primitive::kPrimLong, offset) |
1516 TypeFlag(Primitive::kPrimDouble, offset);
1517 default:
1518 return TypeFlag(type, offset);
1519 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001520 }
1521
Aart Bik854a02b2015-07-14 16:07:00 -07001522 // Translates type to bit flag.
1523 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1524 CHECK_NE(type, Primitive::kPrimVoid);
1525 const uint64_t one = 1;
1526 const int shift = type; // 0-based consecutive enum
1527 DCHECK_LE(kFieldWriteOffset, shift);
1528 DCHECK_LT(shift, kArrayWriteOffset);
1529 return one << (type + offset);
1530 }
1531
1532 // Private constructor on direct flags value.
1533 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1534
1535 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001536};
1537
David Brazdiled596192015-01-23 10:39:45 +00001538// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001539class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001540 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001541 HEnvironment(ArenaAllocator* arena,
1542 size_t number_of_vregs,
1543 const DexFile& dex_file,
1544 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001545 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001546 InvokeType invoke_type,
1547 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001548 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1549 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001550 parent_(nullptr),
1551 dex_file_(dex_file),
1552 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001553 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001554 invoke_type_(invoke_type),
1555 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001556 }
1557
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001558 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001559 : HEnvironment(arena,
1560 to_copy.Size(),
1561 to_copy.GetDexFile(),
1562 to_copy.GetMethodIdx(),
1563 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001564 to_copy.GetInvokeType(),
1565 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001566
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001567 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001568 if (parent_ != nullptr) {
1569 parent_->SetAndCopyParentChain(allocator, parent);
1570 } else {
1571 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1572 parent_->CopyFrom(parent);
1573 if (parent->GetParent() != nullptr) {
1574 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1575 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001576 }
David Brazdiled596192015-01-23 10:39:45 +00001577 }
1578
Vladimir Marko71bf8092015-09-15 15:33:14 +01001579 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001580 void CopyFrom(HEnvironment* environment);
1581
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001582 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1583 // input to the loop phi instead. This is for inserting instructions that
1584 // require an environment (like HDeoptimization) in the loop pre-header.
1585 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001586
1587 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001588 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001589 }
1590
1591 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001592 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001593 }
1594
David Brazdil1abb4192015-02-17 18:33:36 +00001595 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001596
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001597 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001598
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001599 HEnvironment* GetParent() const { return parent_; }
1600
1601 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001602 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001603 }
1604
1605 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001606 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001607 }
1608
1609 uint32_t GetDexPc() const {
1610 return dex_pc_;
1611 }
1612
1613 uint32_t GetMethodIdx() const {
1614 return method_idx_;
1615 }
1616
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001617 InvokeType GetInvokeType() const {
1618 return invoke_type_;
1619 }
1620
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001621 const DexFile& GetDexFile() const {
1622 return dex_file_;
1623 }
1624
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001625 HInstruction* GetHolder() const {
1626 return holder_;
1627 }
1628
David Brazdiled596192015-01-23 10:39:45 +00001629 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001630 // Record instructions' use entries of this environment for constant-time removal.
1631 // It should only be called by HInstruction when a new environment use is added.
1632 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1633 DCHECK(env_use->GetUser() == this);
1634 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001635 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001636 }
David Brazdiled596192015-01-23 10:39:45 +00001637
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001638 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1639 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001640 HEnvironment* parent_;
1641 const DexFile& dex_file_;
1642 const uint32_t method_idx_;
1643 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001644 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001645
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001646 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001647 HInstruction* const holder_;
1648
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001649 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001650
1651 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1652};
1653
Calin Juravleacf735c2015-02-12 15:25:22 +00001654class ReferenceTypeInfo : ValueObject {
1655 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001656 typedef Handle<mirror::Class> TypeHandle;
1657
Calin Juravle2e768302015-07-28 14:41:11 +00001658 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1659 // The constructor will check that the type_handle is valid.
1660 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001661 }
1662
Calin Juravle2e768302015-07-28 14:41:11 +00001663 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1664
1665 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1666 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001667 }
1668
Calin Juravle2e768302015-07-28 14:41:11 +00001669 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1670 return IsValidHandle(type_handle_);
1671 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001672
Calin Juravleacf735c2015-02-12 15:25:22 +00001673 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001674
1675 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1676 DCHECK(IsValid());
1677 return GetTypeHandle()->IsObjectClass();
1678 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001679
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001680 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001681 DCHECK(IsValid());
1682 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001683 }
1684
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001685 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1686 DCHECK(IsValid());
1687 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1688 }
1689
Mathieu Chartier90443472015-07-16 20:32:27 -07001690 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001691 DCHECK(IsValid());
1692 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001693 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001694
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001695 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001696 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001697 return GetTypeHandle()->IsArrayClass();
1698 }
1699
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001700 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1701 DCHECK(IsValid());
1702 return GetTypeHandle()->IsPrimitiveArray();
1703 }
1704
1705 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1706 DCHECK(IsValid());
1707 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1708 }
1709
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001710 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001711 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001712 if (!IsExact()) return false;
1713 if (!IsArrayClass()) return false;
1714 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1715 }
1716
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001717 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1718 DCHECK(IsValid());
1719 if (!IsExact()) return false;
1720 if (!IsArrayClass()) return false;
1721 if (!rti.IsArrayClass()) return false;
1722 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1723 rti.GetTypeHandle()->GetComponentType());
1724 }
1725
Calin Juravleacf735c2015-02-12 15:25:22 +00001726 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1727
Mathieu Chartier90443472015-07-16 20:32:27 -07001728 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001729 DCHECK(IsValid());
1730 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001731 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1732 }
1733
Calin Juravlecdfed3d2015-10-26 14:05:01 +00001734 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1735 DCHECK(IsValid());
1736 DCHECK(rti.IsValid());
1737 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
1738 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1739 }
1740
Calin Juravleacf735c2015-02-12 15:25:22 +00001741 // Returns true if the type information provide the same amount of details.
1742 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001743 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001744 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001745 if (!IsValid() && !rti.IsValid()) {
1746 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001747 return true;
1748 }
Calin Juravle2e768302015-07-28 14:41:11 +00001749 if (!IsValid() || !rti.IsValid()) {
1750 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001751 return false;
1752 }
Calin Juravle2e768302015-07-28 14:41:11 +00001753 return IsExact() == rti.IsExact()
1754 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001755 }
1756
1757 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001758 ReferenceTypeInfo();
1759 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001760
Calin Juravleacf735c2015-02-12 15:25:22 +00001761 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001762 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001763 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001764 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001765 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001766};
1767
1768std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1769
Vladimir Markof9f64412015-09-02 14:05:49 +01001770class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001771 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001772 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001773 : previous_(nullptr),
1774 next_(nullptr),
1775 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001776 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001777 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001778 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001779 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001780 locations_(nullptr),
1781 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001782 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001783 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001784 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001785
Dave Allison20dfc792014-06-16 20:44:29 -07001786 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001787
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001788#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001789 enum InstructionKind {
1790 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1791 };
1792#undef DECLARE_KIND
1793
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001794 HInstruction* GetNext() const { return next_; }
1795 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001796
Calin Juravle77520bc2015-01-12 18:45:46 +00001797 HInstruction* GetNextDisregardingMoves() const;
1798 HInstruction* GetPreviousDisregardingMoves() const;
1799
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001800 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001801 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001802 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001803 bool IsInBlock() const { return block_ != nullptr; }
1804 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001805 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001806
Roland Levillain6b879dd2014-09-22 17:13:44 +01001807 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001808 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001809
1810 virtual void Accept(HGraphVisitor* visitor) = 0;
1811 virtual const char* DebugName() const = 0;
1812
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001813 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001814 void SetRawInputAt(size_t index, HInstruction* input) {
1815 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1816 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001817
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001818 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001819
1820 uint32_t GetDexPc() const { return dex_pc_; }
1821
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001822 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001823
Roland Levillaine161a2a2014-10-03 12:45:18 +01001824 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001825 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001826
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001827 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001828 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001829
Calin Juravle10e244f2015-01-26 18:54:32 +00001830 // Does not apply for all instructions, but having this at top level greatly
1831 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001832 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001833 virtual bool CanBeNull() const {
1834 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1835 return true;
1836 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001837
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001838 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001839 return false;
1840 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001841
Calin Juravle2e768302015-07-28 14:41:11 +00001842 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001843
Calin Juravle61d544b2015-02-23 16:46:57 +00001844 ReferenceTypeInfo GetReferenceTypeInfo() const {
1845 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1846 return reference_type_info_;
1847 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001848
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001849 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001850 DCHECK(user != nullptr);
1851 HUseListNode<HInstruction*>* use =
1852 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1853 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001854 }
1855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001856 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001857 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001858 HUseListNode<HEnvironment*>* env_use =
1859 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1860 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001861 }
1862
David Brazdil1abb4192015-02-17 18:33:36 +00001863 void RemoveAsUserOfInput(size_t input) {
1864 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1865 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1866 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001867
David Brazdil1abb4192015-02-17 18:33:36 +00001868 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1869 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001870
David Brazdiled596192015-01-23 10:39:45 +00001871 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1872 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001873 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001874 bool HasOnlyOneNonEnvironmentUse() const {
1875 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1876 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001877
Roland Levillain6c82d402014-10-13 16:10:27 +01001878 // Does this instruction strictly dominate `other_instruction`?
1879 // Returns false if this instruction and `other_instruction` are the same.
1880 // Aborts if this instruction and `other_instruction` are both phis.
1881 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001882
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001883 int GetId() const { return id_; }
1884 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001885
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001886 int GetSsaIndex() const { return ssa_index_; }
1887 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1888 bool HasSsaIndex() const { return ssa_index_ != -1; }
1889
1890 bool HasEnvironment() const { return environment_ != nullptr; }
1891 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001892 // Set the `environment_` field. Raw because this method does not
1893 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001894 void SetRawEnvironment(HEnvironment* environment) {
1895 DCHECK(environment_ == nullptr);
1896 DCHECK_EQ(environment->GetHolder(), this);
1897 environment_ = environment;
1898 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001899
1900 // Set the environment of this instruction, copying it from `environment`. While
1901 // copying, the uses lists are being updated.
1902 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001903 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001904 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001905 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001906 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001907 if (environment->GetParent() != nullptr) {
1908 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1909 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001910 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001911
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001912 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1913 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001914 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001915 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001916 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001917 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001918 if (environment->GetParent() != nullptr) {
1919 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1920 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001921 }
1922
Nicolas Geoffray39468442014-09-02 15:17:15 +01001923 // Returns the number of entries in the environment. Typically, that is the
1924 // number of dex registers in a method. It could be more in case of inlining.
1925 size_t EnvironmentSize() const;
1926
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001927 LocationSummary* GetLocations() const { return locations_; }
1928 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001929
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001930 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001931 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001932
Alexandre Rames188d4312015-04-09 18:30:21 +01001933 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1934 // uses of this instruction by `other` are *not* updated.
1935 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1936 ReplaceWith(other);
1937 other->ReplaceInput(this, use_index);
1938 }
1939
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001940 // Move `this` instruction before `cursor`.
1941 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001942
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001943#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001944 bool Is##type() const { return (As##type() != nullptr); } \
1945 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001946 virtual H##type* As##type() { return nullptr; }
1947
1948 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1949#undef INSTRUCTION_TYPE_CHECK
1950
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001951 // Returns whether the instruction can be moved within the graph.
1952 virtual bool CanBeMoved() const { return false; }
1953
1954 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001955 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001956 return false;
1957 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001958
1959 // Returns whether any data encoded in the two instructions is equal.
1960 // This method does not look at the inputs. Both instructions must be
1961 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001962 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001963 return false;
1964 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001965
1966 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001967 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001968 // 2) Their inputs are identical.
1969 bool Equals(HInstruction* other) const;
1970
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001971 virtual InstructionKind GetKind() const = 0;
1972
1973 virtual size_t ComputeHashCode() const {
1974 size_t result = GetKind();
1975 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1976 result = (result * 31) + InputAt(i)->GetId();
1977 }
1978 return result;
1979 }
1980
1981 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001982 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001983
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001984 size_t GetLifetimePosition() const { return lifetime_position_; }
1985 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1986 LiveInterval* GetLiveInterval() const { return live_interval_; }
1987 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1988 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1989
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001990 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1991
1992 // Returns whether the code generation of the instruction will require to have access
1993 // to the current method. Such instructions are:
1994 // (1): Instructions that require an environment, as calling the runtime requires
1995 // to walk the stack and have the current method stored at a specific stack address.
1996 // (2): Object literals like classes and strings, that are loaded from the dex cache
1997 // fields of the current method.
1998 bool NeedsCurrentMethod() const {
1999 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2000 }
2001
Vladimir Markodc151b22015-10-15 18:02:30 +01002002 // Returns whether the code generation of the instruction will require to have access
2003 // to the dex cache of the current method's declaring class via the current method.
2004 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002005
Mark Mendellc4701932015-04-10 13:18:51 -04002006 // Does this instruction have any use in an environment before
2007 // control flow hits 'other'?
2008 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2009
2010 // Remove all references to environment uses of this instruction.
2011 // The caller must ensure that this is safe to do.
2012 void RemoveEnvironmentUsers();
2013
David Brazdil1abb4192015-02-17 18:33:36 +00002014 protected:
2015 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2016 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2017
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002018 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002019 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2020
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002021 HInstruction* previous_;
2022 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002023 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002024 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002025
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002026 // An instruction gets an id when it is added to the graph.
2027 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002028 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002029 int id_;
2030
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002031 // When doing liveness analysis, instructions that have uses get an SSA index.
2032 int ssa_index_;
2033
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002034 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002035 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002036
2037 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002038 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002039
Nicolas Geoffray39468442014-09-02 15:17:15 +01002040 // The environment associated with this instruction. Not null if the instruction
2041 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002042 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002043
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002044 // Set by the code generator.
2045 LocationSummary* locations_;
2046
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002047 // Set by the liveness analysis.
2048 LiveInterval* live_interval_;
2049
2050 // Set by the liveness analysis, this is the position in a linear
2051 // order of blocks where this instruction's live interval start.
2052 size_t lifetime_position_;
2053
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002054 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002055
Calin Juravleacf735c2015-02-12 15:25:22 +00002056 // TODO: for primitive types this should be marked as invalid.
2057 ReferenceTypeInfo reference_type_info_;
2058
David Brazdil1abb4192015-02-17 18:33:36 +00002059 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002060 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002061 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002062 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002063 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002064
2065 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2066};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002067std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002068
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002069class HInputIterator : public ValueObject {
2070 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002071 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002072
2073 bool Done() const { return index_ == instruction_->InputCount(); }
2074 HInstruction* Current() const { return instruction_->InputAt(index_); }
2075 void Advance() { index_++; }
2076
2077 private:
2078 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002079 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002080
2081 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2082};
2083
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002084class HInstructionIterator : public ValueObject {
2085 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002086 explicit HInstructionIterator(const HInstructionList& instructions)
2087 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002088 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002089 }
2090
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002091 bool Done() const { return instruction_ == nullptr; }
2092 HInstruction* Current() const { return instruction_; }
2093 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002094 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002095 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002096 }
2097
2098 private:
2099 HInstruction* instruction_;
2100 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002101
2102 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002103};
2104
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002105class HBackwardInstructionIterator : public ValueObject {
2106 public:
2107 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2108 : instruction_(instructions.last_instruction_) {
2109 next_ = Done() ? nullptr : instruction_->GetPrevious();
2110 }
2111
2112 bool Done() const { return instruction_ == nullptr; }
2113 HInstruction* Current() const { return instruction_; }
2114 void Advance() {
2115 instruction_ = next_;
2116 next_ = Done() ? nullptr : instruction_->GetPrevious();
2117 }
2118
2119 private:
2120 HInstruction* instruction_;
2121 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002122
2123 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002124};
2125
Vladimir Markof9f64412015-09-02 14:05:49 +01002126template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002127class HTemplateInstruction: public HInstruction {
2128 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002129 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002130 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002131 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002132
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002133 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002134
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002135 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002136 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2137 DCHECK_LT(i, N);
2138 return inputs_[i];
2139 }
David Brazdil1abb4192015-02-17 18:33:36 +00002140
2141 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002142 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002143 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002144 }
2145
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002146 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002147 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002148
2149 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002150};
2151
Vladimir Markof9f64412015-09-02 14:05:49 +01002152// HTemplateInstruction specialization for N=0.
2153template<>
2154class HTemplateInstruction<0>: public HInstruction {
2155 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002156 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002157 : HInstruction(side_effects, dex_pc) {}
2158
Vladimir Markof9f64412015-09-02 14:05:49 +01002159 virtual ~HTemplateInstruction() {}
2160
2161 size_t InputCount() const OVERRIDE { return 0; }
2162
2163 protected:
2164 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2165 LOG(FATAL) << "Unreachable";
2166 UNREACHABLE();
2167 }
2168
2169 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2170 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2171 LOG(FATAL) << "Unreachable";
2172 UNREACHABLE();
2173 }
2174
2175 private:
2176 friend class SsaBuilder;
2177};
2178
Dave Allison20dfc792014-06-16 20:44:29 -07002179template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002180class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002181 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002182 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002183 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002184 virtual ~HExpression() {}
2185
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002186 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002187
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002188 protected:
2189 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002190};
2191
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002192// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2193// instruction that branches to the exit block.
2194class HReturnVoid : public HTemplateInstruction<0> {
2195 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002196 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2197 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002198
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002199 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002200
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002201 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002202
2203 private:
2204 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2205};
2206
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002207// Represents dex's RETURN opcodes. A HReturn is a control flow
2208// instruction that branches to the exit block.
2209class HReturn : public HTemplateInstruction<1> {
2210 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002211 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2212 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002213 SetRawInputAt(0, value);
2214 }
2215
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002216 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002217
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002218 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002219
2220 private:
2221 DISALLOW_COPY_AND_ASSIGN(HReturn);
2222};
2223
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002224// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002225// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002226// exit block.
2227class HExit : public HTemplateInstruction<0> {
2228 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002229 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002230
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002231 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002232
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002233 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002234
2235 private:
2236 DISALLOW_COPY_AND_ASSIGN(HExit);
2237};
2238
2239// Jumps from one block to another.
2240class HGoto : public HTemplateInstruction<0> {
2241 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002242 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002243
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002244 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002245
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002246 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002247 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002248 }
2249
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002250 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002251
2252 private:
2253 DISALLOW_COPY_AND_ASSIGN(HGoto);
2254};
2255
Roland Levillain9867bc72015-08-05 10:21:34 +01002256class HConstant : public HExpression<0> {
2257 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002258 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2259 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002260
2261 bool CanBeMoved() const OVERRIDE { return true; }
2262
2263 virtual bool IsMinusOne() const { return false; }
2264 virtual bool IsZero() const { return false; }
2265 virtual bool IsOne() const { return false; }
2266
David Brazdil77a48ae2015-09-15 12:34:04 +00002267 virtual uint64_t GetValueAsUint64() const = 0;
2268
Roland Levillain9867bc72015-08-05 10:21:34 +01002269 DECLARE_INSTRUCTION(Constant);
2270
2271 private:
2272 DISALLOW_COPY_AND_ASSIGN(HConstant);
2273};
2274
2275class HNullConstant : public HConstant {
2276 public:
2277 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2278 return true;
2279 }
2280
David Brazdil77a48ae2015-09-15 12:34:04 +00002281 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2282
Roland Levillain9867bc72015-08-05 10:21:34 +01002283 size_t ComputeHashCode() const OVERRIDE { return 0; }
2284
2285 DECLARE_INSTRUCTION(NullConstant);
2286
2287 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002288 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002289
2290 friend class HGraph;
2291 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2292};
2293
2294// Constants of the type int. Those can be from Dex instructions, or
2295// synthesized (for example with the if-eqz instruction).
2296class HIntConstant : public HConstant {
2297 public:
2298 int32_t GetValue() const { return value_; }
2299
David Brazdil9f389d42015-10-01 14:32:56 +01002300 uint64_t GetValueAsUint64() const OVERRIDE {
2301 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2302 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002303
Roland Levillain9867bc72015-08-05 10:21:34 +01002304 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2305 DCHECK(other->IsIntConstant());
2306 return other->AsIntConstant()->value_ == value_;
2307 }
2308
2309 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2310
2311 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2312 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2313 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2314
2315 DECLARE_INSTRUCTION(IntConstant);
2316
2317 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002318 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2319 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2320 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2321 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002322
2323 const int32_t value_;
2324
2325 friend class HGraph;
2326 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2327 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2328 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2329};
2330
2331class HLongConstant : public HConstant {
2332 public:
2333 int64_t GetValue() const { return value_; }
2334
David Brazdil77a48ae2015-09-15 12:34:04 +00002335 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2336
Roland Levillain9867bc72015-08-05 10:21:34 +01002337 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2338 DCHECK(other->IsLongConstant());
2339 return other->AsLongConstant()->value_ == value_;
2340 }
2341
2342 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2343
2344 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2345 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2346 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2347
2348 DECLARE_INSTRUCTION(LongConstant);
2349
2350 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002351 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2352 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002353
2354 const int64_t value_;
2355
2356 friend class HGraph;
2357 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2358};
Dave Allison20dfc792014-06-16 20:44:29 -07002359
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002360// Conditional branch. A block ending with an HIf instruction must have
2361// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002362class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002363 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002364 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2365 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002366 SetRawInputAt(0, input);
2367 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002368
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002369 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002370
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002371 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002372 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002373 }
2374
2375 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002376 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002377 }
2378
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002379 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002380
2381 private:
2382 DISALLOW_COPY_AND_ASSIGN(HIf);
2383};
2384
David Brazdilfc6a86a2015-06-26 10:33:45 +00002385
2386// Abstract instruction which marks the beginning and/or end of a try block and
2387// links it to the respective exception handlers. Behaves the same as a Goto in
2388// non-exceptional control flow.
2389// Normal-flow successor is stored at index zero, exception handlers under
2390// higher indices in no particular order.
2391class HTryBoundary : public HTemplateInstruction<0> {
2392 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002393 enum BoundaryKind {
2394 kEntry,
2395 kExit,
2396 };
2397
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002398 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2399 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002400
2401 bool IsControlFlow() const OVERRIDE { return true; }
2402
2403 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002404 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002405
David Brazdild26a4112015-11-10 11:07:31 +00002406 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2407 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2408 }
2409
David Brazdilfc6a86a2015-06-26 10:33:45 +00002410 // Returns whether `handler` is among its exception handlers (non-zero index
2411 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002412 bool HasExceptionHandler(const HBasicBlock& handler) const {
2413 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002414 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002415 }
2416
2417 // If not present already, adds `handler` to its block's list of exception
2418 // handlers.
2419 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002420 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002421 GetBlock()->AddSuccessor(handler);
2422 }
2423 }
2424
David Brazdil56e1acc2015-06-30 15:41:36 +01002425 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002426
David Brazdilffee3d32015-07-06 11:48:53 +01002427 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2428
David Brazdilfc6a86a2015-06-26 10:33:45 +00002429 DECLARE_INSTRUCTION(TryBoundary);
2430
2431 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002432 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002433
2434 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2435};
2436
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002437// Deoptimize to interpreter, upon checking a condition.
2438class HDeoptimize : public HTemplateInstruction<1> {
2439 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002440 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2441 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002442 SetRawInputAt(0, cond);
2443 }
2444
2445 bool NeedsEnvironment() const OVERRIDE { return true; }
2446 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002447
2448 DECLARE_INSTRUCTION(Deoptimize);
2449
2450 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002451 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2452};
2453
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002454// Represents the ArtMethod that was passed as a first argument to
2455// the method. It is used by instructions that depend on it, like
2456// instructions that work with the dex cache.
2457class HCurrentMethod : public HExpression<0> {
2458 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002459 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2460 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002461
2462 DECLARE_INSTRUCTION(CurrentMethod);
2463
2464 private:
2465 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2466};
2467
Mark Mendellfe57faa2015-09-18 09:26:15 -04002468// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2469// have one successor for each entry in the switch table, and the final successor
2470// will be the block containing the next Dex opcode.
2471class HPackedSwitch : public HTemplateInstruction<1> {
2472 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002473 HPackedSwitch(int32_t start_value,
2474 uint32_t num_entries,
2475 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002476 uint32_t dex_pc = kNoDexPc)
2477 : HTemplateInstruction(SideEffects::None(), dex_pc),
2478 start_value_(start_value),
2479 num_entries_(num_entries) {
2480 SetRawInputAt(0, input);
2481 }
2482
2483 bool IsControlFlow() const OVERRIDE { return true; }
2484
2485 int32_t GetStartValue() const { return start_value_; }
2486
Vladimir Marko211c2112015-09-24 16:52:33 +01002487 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002488
2489 HBasicBlock* GetDefaultBlock() const {
2490 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002491 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002492 }
2493 DECLARE_INSTRUCTION(PackedSwitch);
2494
2495 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002496 const int32_t start_value_;
2497 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002498
2499 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2500};
2501
Roland Levillain88cb1752014-10-20 16:36:47 +01002502class HUnaryOperation : public HExpression<1> {
2503 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002504 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2505 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002506 SetRawInputAt(0, input);
2507 }
2508
2509 HInstruction* GetInput() const { return InputAt(0); }
2510 Primitive::Type GetResultType() const { return GetType(); }
2511
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002512 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002513 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002514 return true;
2515 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002516
Roland Levillain9240d6a2014-10-20 16:47:04 +01002517 // Try to statically evaluate `operation` and return a HConstant
2518 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002519 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002520 HConstant* TryStaticEvaluation() const;
2521
2522 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002523 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2524 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002525
Roland Levillain88cb1752014-10-20 16:36:47 +01002526 DECLARE_INSTRUCTION(UnaryOperation);
2527
2528 private:
2529 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2530};
2531
Dave Allison20dfc792014-06-16 20:44:29 -07002532class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002533 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002534 HBinaryOperation(Primitive::Type result_type,
2535 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002536 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002537 SideEffects side_effects = SideEffects::None(),
2538 uint32_t dex_pc = kNoDexPc)
2539 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002540 SetRawInputAt(0, left);
2541 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002542 }
2543
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002544 HInstruction* GetLeft() const { return InputAt(0); }
2545 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002546 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002547
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002548 virtual bool IsCommutative() const { return false; }
2549
2550 // Put constant on the right.
2551 // Returns whether order is changed.
2552 bool OrderInputsWithConstantOnTheRight() {
2553 HInstruction* left = InputAt(0);
2554 HInstruction* right = InputAt(1);
2555 if (left->IsConstant() && !right->IsConstant()) {
2556 ReplaceInput(right, 0);
2557 ReplaceInput(left, 1);
2558 return true;
2559 }
2560 return false;
2561 }
2562
2563 // Order inputs by instruction id, but favor constant on the right side.
2564 // This helps GVN for commutative ops.
2565 void OrderInputs() {
2566 DCHECK(IsCommutative());
2567 HInstruction* left = InputAt(0);
2568 HInstruction* right = InputAt(1);
2569 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2570 return;
2571 }
2572 if (OrderInputsWithConstantOnTheRight()) {
2573 return;
2574 }
2575 // Order according to instruction id.
2576 if (left->GetId() > right->GetId()) {
2577 ReplaceInput(right, 0);
2578 ReplaceInput(left, 1);
2579 }
2580 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002581
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002582 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002583 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002584 return true;
2585 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002586
Roland Levillain9240d6a2014-10-20 16:47:04 +01002587 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002588 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002589 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002590 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002591
2592 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002593 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2594 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2595 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2596 HLongConstant* y ATTRIBUTE_UNUSED) const {
2597 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2598 return nullptr;
2599 }
2600 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2601 HIntConstant* y ATTRIBUTE_UNUSED) const {
2602 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2603 return nullptr;
2604 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002605 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2606 HNullConstant* y ATTRIBUTE_UNUSED) const {
2607 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2608 return nullptr;
2609 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002610
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002611 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002612 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002613 HConstant* GetConstantRight() const;
2614
2615 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002616 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002617 HInstruction* GetLeastConstantLeft() const;
2618
Roland Levillainccc07a92014-09-16 14:48:16 +01002619 DECLARE_INSTRUCTION(BinaryOperation);
2620
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002621 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002622 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2623};
2624
Mark Mendellc4701932015-04-10 13:18:51 -04002625// The comparison bias applies for floating point operations and indicates how NaN
2626// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002627enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002628 kNoBias, // bias is not applicable (i.e. for long operation)
2629 kGtBias, // return 1 for NaN comparisons
2630 kLtBias, // return -1 for NaN comparisons
2631};
2632
Dave Allison20dfc792014-06-16 20:44:29 -07002633class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002634 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002635 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2636 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002637 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002638 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002639
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002640 bool NeedsMaterialization() const { return needs_materialization_; }
2641 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002642
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002643 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002644 // `instruction`, and disregard moves in between.
2645 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002646
Dave Allison20dfc792014-06-16 20:44:29 -07002647 DECLARE_INSTRUCTION(Condition);
2648
2649 virtual IfCondition GetCondition() const = 0;
2650
Mark Mendellc4701932015-04-10 13:18:51 -04002651 virtual IfCondition GetOppositeCondition() const = 0;
2652
Roland Levillain4fa13f62015-07-06 18:11:54 +01002653 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002654
2655 void SetBias(ComparisonBias bias) { bias_ = bias; }
2656
2657 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2658 return bias_ == other->AsCondition()->bias_;
2659 }
2660
Roland Levillain4fa13f62015-07-06 18:11:54 +01002661 bool IsFPConditionTrueIfNaN() const {
2662 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2663 IfCondition if_cond = GetCondition();
2664 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2665 }
2666
2667 bool IsFPConditionFalseIfNaN() const {
2668 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2669 IfCondition if_cond = GetCondition();
2670 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2671 }
2672
Dave Allison20dfc792014-06-16 20:44:29 -07002673 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002674 // For register allocation purposes, returns whether this instruction needs to be
2675 // materialized (that is, not just be in the processor flags).
2676 bool needs_materialization_;
2677
Mark Mendellc4701932015-04-10 13:18:51 -04002678 // Needed if we merge a HCompare into a HCondition.
2679 ComparisonBias bias_;
2680
Dave Allison20dfc792014-06-16 20:44:29 -07002681 DISALLOW_COPY_AND_ASSIGN(HCondition);
2682};
2683
2684// Instruction to check if two inputs are equal to each other.
2685class HEqual : public HCondition {
2686 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002687 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2688 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002689
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002690 bool IsCommutative() const OVERRIDE { return true; }
2691
Roland Levillain9867bc72015-08-05 10:21:34 +01002692 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002693 return GetBlock()->GetGraph()->GetIntConstant(
2694 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002695 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002696 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002697 return GetBlock()->GetGraph()->GetIntConstant(
2698 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002699 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002700 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2701 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002702 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002703 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002704
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002705 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002706
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002707 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002708 return kCondEQ;
2709 }
2710
Mark Mendellc4701932015-04-10 13:18:51 -04002711 IfCondition GetOppositeCondition() const OVERRIDE {
2712 return kCondNE;
2713 }
2714
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002715 private:
Aart Bike9f37602015-10-09 11:15:55 -07002716 template <typename T> bool Compute(T x, T y) const { return x == y; }
2717
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002718 DISALLOW_COPY_AND_ASSIGN(HEqual);
2719};
2720
Dave Allison20dfc792014-06-16 20:44:29 -07002721class HNotEqual : public HCondition {
2722 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002723 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2724 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002725
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002726 bool IsCommutative() const OVERRIDE { return true; }
2727
Roland Levillain9867bc72015-08-05 10:21:34 +01002728 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002729 return GetBlock()->GetGraph()->GetIntConstant(
2730 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002731 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002732 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002733 return GetBlock()->GetGraph()->GetIntConstant(
2734 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002735 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002736 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2737 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002738 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002739 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002740
Dave Allison20dfc792014-06-16 20:44:29 -07002741 DECLARE_INSTRUCTION(NotEqual);
2742
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002743 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002744 return kCondNE;
2745 }
2746
Mark Mendellc4701932015-04-10 13:18:51 -04002747 IfCondition GetOppositeCondition() const OVERRIDE {
2748 return kCondEQ;
2749 }
2750
Dave Allison20dfc792014-06-16 20:44:29 -07002751 private:
Aart Bike9f37602015-10-09 11:15:55 -07002752 template <typename T> bool Compute(T x, T y) const { return x != y; }
2753
Dave Allison20dfc792014-06-16 20:44:29 -07002754 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2755};
2756
2757class HLessThan : public HCondition {
2758 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002759 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2760 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002761
Roland Levillain9867bc72015-08-05 10:21:34 +01002762 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002763 return GetBlock()->GetGraph()->GetIntConstant(
2764 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002765 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002766 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002767 return GetBlock()->GetGraph()->GetIntConstant(
2768 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002769 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002770
Dave Allison20dfc792014-06-16 20:44:29 -07002771 DECLARE_INSTRUCTION(LessThan);
2772
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002773 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002774 return kCondLT;
2775 }
2776
Mark Mendellc4701932015-04-10 13:18:51 -04002777 IfCondition GetOppositeCondition() const OVERRIDE {
2778 return kCondGE;
2779 }
2780
Dave Allison20dfc792014-06-16 20:44:29 -07002781 private:
Aart Bike9f37602015-10-09 11:15:55 -07002782 template <typename T> bool Compute(T x, T y) const { return x < y; }
2783
Dave Allison20dfc792014-06-16 20:44:29 -07002784 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2785};
2786
2787class HLessThanOrEqual : public HCondition {
2788 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002789 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2790 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002791
Roland Levillain9867bc72015-08-05 10:21:34 +01002792 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002793 return GetBlock()->GetGraph()->GetIntConstant(
2794 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002795 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002796 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002797 return GetBlock()->GetGraph()->GetIntConstant(
2798 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002799 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002800
Dave Allison20dfc792014-06-16 20:44:29 -07002801 DECLARE_INSTRUCTION(LessThanOrEqual);
2802
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002803 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002804 return kCondLE;
2805 }
2806
Mark Mendellc4701932015-04-10 13:18:51 -04002807 IfCondition GetOppositeCondition() const OVERRIDE {
2808 return kCondGT;
2809 }
2810
Dave Allison20dfc792014-06-16 20:44:29 -07002811 private:
Aart Bike9f37602015-10-09 11:15:55 -07002812 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2813
Dave Allison20dfc792014-06-16 20:44:29 -07002814 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2815};
2816
2817class HGreaterThan : public HCondition {
2818 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002819 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2820 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002821
Roland Levillain9867bc72015-08-05 10:21:34 +01002822 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002823 return GetBlock()->GetGraph()->GetIntConstant(
2824 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002825 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002826 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002827 return GetBlock()->GetGraph()->GetIntConstant(
2828 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002829 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002830
Dave Allison20dfc792014-06-16 20:44:29 -07002831 DECLARE_INSTRUCTION(GreaterThan);
2832
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002833 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002834 return kCondGT;
2835 }
2836
Mark Mendellc4701932015-04-10 13:18:51 -04002837 IfCondition GetOppositeCondition() const OVERRIDE {
2838 return kCondLE;
2839 }
2840
Dave Allison20dfc792014-06-16 20:44:29 -07002841 private:
Aart Bike9f37602015-10-09 11:15:55 -07002842 template <typename T> bool Compute(T x, T y) const { return x > y; }
2843
Dave Allison20dfc792014-06-16 20:44:29 -07002844 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2845};
2846
2847class HGreaterThanOrEqual : public HCondition {
2848 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002849 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2850 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002851
Roland Levillain9867bc72015-08-05 10:21:34 +01002852 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002853 return GetBlock()->GetGraph()->GetIntConstant(
2854 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002855 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002856 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002857 return GetBlock()->GetGraph()->GetIntConstant(
2858 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002859 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002860
Dave Allison20dfc792014-06-16 20:44:29 -07002861 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2862
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002863 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002864 return kCondGE;
2865 }
2866
Mark Mendellc4701932015-04-10 13:18:51 -04002867 IfCondition GetOppositeCondition() const OVERRIDE {
2868 return kCondLT;
2869 }
2870
Dave Allison20dfc792014-06-16 20:44:29 -07002871 private:
Aart Bike9f37602015-10-09 11:15:55 -07002872 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2873
Dave Allison20dfc792014-06-16 20:44:29 -07002874 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2875};
2876
Aart Bike9f37602015-10-09 11:15:55 -07002877class HBelow : public HCondition {
2878 public:
2879 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2880 : HCondition(first, second, dex_pc) {}
2881
2882 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2883 return GetBlock()->GetGraph()->GetIntConstant(
2884 Compute(static_cast<uint32_t>(x->GetValue()),
2885 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2886 }
2887 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2888 return GetBlock()->GetGraph()->GetIntConstant(
2889 Compute(static_cast<uint64_t>(x->GetValue()),
2890 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2891 }
2892
2893 DECLARE_INSTRUCTION(Below);
2894
2895 IfCondition GetCondition() const OVERRIDE {
2896 return kCondB;
2897 }
2898
2899 IfCondition GetOppositeCondition() const OVERRIDE {
2900 return kCondAE;
2901 }
2902
2903 private:
2904 template <typename T> bool Compute(T x, T y) const { return x < y; }
2905
2906 DISALLOW_COPY_AND_ASSIGN(HBelow);
2907};
2908
2909class HBelowOrEqual : public HCondition {
2910 public:
2911 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2912 : HCondition(first, second, dex_pc) {}
2913
2914 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2915 return GetBlock()->GetGraph()->GetIntConstant(
2916 Compute(static_cast<uint32_t>(x->GetValue()),
2917 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2918 }
2919 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2920 return GetBlock()->GetGraph()->GetIntConstant(
2921 Compute(static_cast<uint64_t>(x->GetValue()),
2922 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2923 }
2924
2925 DECLARE_INSTRUCTION(BelowOrEqual);
2926
2927 IfCondition GetCondition() const OVERRIDE {
2928 return kCondBE;
2929 }
2930
2931 IfCondition GetOppositeCondition() const OVERRIDE {
2932 return kCondA;
2933 }
2934
2935 private:
2936 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2937
2938 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2939};
2940
2941class HAbove : public HCondition {
2942 public:
2943 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2944 : HCondition(first, second, dex_pc) {}
2945
2946 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2947 return GetBlock()->GetGraph()->GetIntConstant(
2948 Compute(static_cast<uint32_t>(x->GetValue()),
2949 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2950 }
2951 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2952 return GetBlock()->GetGraph()->GetIntConstant(
2953 Compute(static_cast<uint64_t>(x->GetValue()),
2954 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2955 }
2956
2957 DECLARE_INSTRUCTION(Above);
2958
2959 IfCondition GetCondition() const OVERRIDE {
2960 return kCondA;
2961 }
2962
2963 IfCondition GetOppositeCondition() const OVERRIDE {
2964 return kCondBE;
2965 }
2966
2967 private:
2968 template <typename T> bool Compute(T x, T y) const { return x > y; }
2969
2970 DISALLOW_COPY_AND_ASSIGN(HAbove);
2971};
2972
2973class HAboveOrEqual : public HCondition {
2974 public:
2975 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2976 : HCondition(first, second, dex_pc) {}
2977
2978 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2979 return GetBlock()->GetGraph()->GetIntConstant(
2980 Compute(static_cast<uint32_t>(x->GetValue()),
2981 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2982 }
2983 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2984 return GetBlock()->GetGraph()->GetIntConstant(
2985 Compute(static_cast<uint64_t>(x->GetValue()),
2986 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2987 }
2988
2989 DECLARE_INSTRUCTION(AboveOrEqual);
2990
2991 IfCondition GetCondition() const OVERRIDE {
2992 return kCondAE;
2993 }
2994
2995 IfCondition GetOppositeCondition() const OVERRIDE {
2996 return kCondB;
2997 }
2998
2999 private:
3000 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3001
3002 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3003};
Dave Allison20dfc792014-06-16 20:44:29 -07003004
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003005// Instruction to check how two inputs compare to each other.
3006// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3007class HCompare : public HBinaryOperation {
3008 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003009 HCompare(Primitive::Type type,
3010 HInstruction* first,
3011 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003012 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003013 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003014 : HBinaryOperation(Primitive::kPrimInt,
3015 first,
3016 second,
3017 SideEffectsForArchRuntimeCalls(type),
3018 dex_pc),
3019 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003020 DCHECK_EQ(type, first->GetType());
3021 DCHECK_EQ(type, second->GetType());
3022 }
3023
Roland Levillain9867bc72015-08-05 10:21:34 +01003024 template <typename T>
3025 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003026
Roland Levillain9867bc72015-08-05 10:21:34 +01003027 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003028 return GetBlock()->GetGraph()->GetIntConstant(
3029 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003030 }
3031 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003032 return GetBlock()->GetGraph()->GetIntConstant(
3033 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003034 }
3035
Calin Juravleddb7df22014-11-25 20:56:51 +00003036 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3037 return bias_ == other->AsCompare()->bias_;
3038 }
3039
Mark Mendellc4701932015-04-10 13:18:51 -04003040 ComparisonBias GetBias() const { return bias_; }
3041
Roland Levillain4fa13f62015-07-06 18:11:54 +01003042 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003043
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003044
3045 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3046 // MIPS64 uses a runtime call for FP comparisons.
3047 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3048 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003049
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003050 DECLARE_INSTRUCTION(Compare);
3051
3052 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003053 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003054
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003055 DISALLOW_COPY_AND_ASSIGN(HCompare);
3056};
3057
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003058// A local in the graph. Corresponds to a Dex register.
3059class HLocal : public HTemplateInstruction<0> {
3060 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003061 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003062 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003063
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003064 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003065
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003066 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003067
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003068 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003069 // The Dex register number.
3070 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003071
3072 DISALLOW_COPY_AND_ASSIGN(HLocal);
3073};
3074
3075// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003076class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003077 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003078 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3079 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003080 SetRawInputAt(0, local);
3081 }
3082
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003083 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3084
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003085 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003086
3087 private:
3088 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3089};
3090
3091// Store a value in a given local. This instruction has two inputs: the value
3092// and the local.
3093class HStoreLocal : public HTemplateInstruction<2> {
3094 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003095 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3096 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003097 SetRawInputAt(0, local);
3098 SetRawInputAt(1, value);
3099 }
3100
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003101 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3102
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003103 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003104
3105 private:
3106 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3107};
3108
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003109class HFloatConstant : public HConstant {
3110 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003111 float GetValue() const { return value_; }
3112
David Brazdil77a48ae2015-09-15 12:34:04 +00003113 uint64_t GetValueAsUint64() const OVERRIDE {
3114 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3115 }
3116
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003117 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003118 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003119 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003120 }
3121
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003122 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003123
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003124 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003125 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003126 }
3127 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003128 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003129 }
3130 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003131 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3132 }
3133 bool IsNaN() const {
3134 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003135 }
3136
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003137 DECLARE_INSTRUCTION(FloatConstant);
3138
3139 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003140 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3141 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3142 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3143 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003144
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003145 const float value_;
3146
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003147 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003148 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003149 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003150 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3151};
3152
3153class HDoubleConstant : public HConstant {
3154 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003155 double GetValue() const { return value_; }
3156
David Brazdil77a48ae2015-09-15 12:34:04 +00003157 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3158
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003159 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003160 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003161 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003162 }
3163
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003164 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003165
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003166 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003167 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003168 }
3169 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003170 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003171 }
3172 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003173 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3174 }
3175 bool IsNaN() const {
3176 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003177 }
3178
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003179 DECLARE_INSTRUCTION(DoubleConstant);
3180
3181 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003182 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3183 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3184 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3185 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003186
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003187 const double value_;
3188
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003189 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003190 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003191 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003192 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3193};
3194
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003195enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003196#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003197#include "intrinsics_list.h"
3198 kNone,
3199 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3200#undef INTRINSICS_LIST
3201#undef OPTIMIZING_INTRINSICS
3202};
3203std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3204
Agi Csaki05f20562015-08-19 14:58:14 -07003205enum IntrinsicNeedsEnvironmentOrCache {
3206 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3207 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003208};
3209
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003210class HInvoke : public HInstruction {
3211 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003212 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003213
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003214 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003215
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003216 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003217 SetRawInputAt(index, argument);
3218 }
3219
Roland Levillain3e3d7332015-04-28 11:00:54 +01003220 // Return the number of arguments. This number can be lower than
3221 // the number of inputs returned by InputCount(), as some invoke
3222 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3223 // inputs at the end of their list of inputs.
3224 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3225
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003226 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003227
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003228
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003229 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003230 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003231
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003232 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3233
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003234 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003235 return intrinsic_;
3236 }
3237
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003238 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003239
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003240 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003241 return GetEnvironment()->GetParent() != nullptr;
3242 }
3243
3244 bool CanThrow() const OVERRIDE { return true; }
3245
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003246 uint32_t* GetIntrinsicOptimizations() {
3247 return &intrinsic_optimizations_;
3248 }
3249
3250 const uint32_t* GetIntrinsicOptimizations() const {
3251 return &intrinsic_optimizations_;
3252 }
3253
3254 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3255
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003256 DECLARE_INSTRUCTION(Invoke);
3257
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003258 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003259 HInvoke(ArenaAllocator* arena,
3260 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003261 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003262 Primitive::Type return_type,
3263 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003264 uint32_t dex_method_index,
3265 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003266 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003267 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003268 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003269 inputs_(number_of_arguments + number_of_other_inputs,
3270 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003271 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003272 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003273 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003274 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003275 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003276 }
3277
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003278 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003279 return inputs_[index];
3280 }
3281
David Brazdil1abb4192015-02-17 18:33:36 +00003282 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003283 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003284 }
3285
Roland Levillain3e3d7332015-04-28 11:00:54 +01003286 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003287 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003288 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003289 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003290 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003291 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003292
3293 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3294 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003295
3296 private:
3297 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3298};
3299
Calin Juravle175dc732015-08-25 15:42:32 +01003300class HInvokeUnresolved : public HInvoke {
3301 public:
3302 HInvokeUnresolved(ArenaAllocator* arena,
3303 uint32_t number_of_arguments,
3304 Primitive::Type return_type,
3305 uint32_t dex_pc,
3306 uint32_t dex_method_index,
3307 InvokeType invoke_type)
3308 : HInvoke(arena,
3309 number_of_arguments,
3310 0u /* number_of_other_inputs */,
3311 return_type,
3312 dex_pc,
3313 dex_method_index,
3314 invoke_type) {
3315 }
3316
3317 DECLARE_INSTRUCTION(InvokeUnresolved);
3318
3319 private:
3320 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3321};
3322
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003323class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003324 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003325 // Requirements of this method call regarding the class
3326 // initialization (clinit) check of its declaring class.
3327 enum class ClinitCheckRequirement {
3328 kNone, // Class already initialized.
3329 kExplicit, // Static call having explicit clinit check as last input.
3330 kImplicit, // Static call implicitly requiring a clinit check.
3331 };
3332
Vladimir Marko58155012015-08-19 12:49:41 +00003333 // Determines how to load the target ArtMethod*.
3334 enum class MethodLoadKind {
3335 // Use a String init ArtMethod* loaded from Thread entrypoints.
3336 kStringInit,
3337
3338 // Use the method's own ArtMethod* loaded by the register allocator.
3339 kRecursive,
3340
3341 // Use ArtMethod* at a known address, embed the direct address in the code.
3342 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3343 kDirectAddress,
3344
3345 // Use ArtMethod* at an address that will be known at link time, embed the direct
3346 // address in the code. If the image is relocatable, emit .patch_oat entry.
3347 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3348 // the image relocatable or not.
3349 kDirectAddressWithFixup,
3350
3351 // Load from resoved methods array in the dex cache using a PC-relative load.
3352 // Used when we need to use the dex cache, for example for invoke-static that
3353 // may cause class initialization (the entry may point to a resolution method),
3354 // and we know that we can access the dex cache arrays using a PC-relative load.
3355 kDexCachePcRelative,
3356
3357 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3358 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3359 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3360 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3361 kDexCacheViaMethod,
3362 };
3363
3364 // Determines the location of the code pointer.
3365 enum class CodePtrLocation {
3366 // Recursive call, use local PC-relative call instruction.
3367 kCallSelf,
3368
3369 // Use PC-relative call instruction patched at link time.
3370 // Used for calls within an oat file, boot->boot or app->app.
3371 kCallPCRelative,
3372
3373 // Call to a known target address, embed the direct address in code.
3374 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3375 kCallDirect,
3376
3377 // Call to a target address that will be known at link time, embed the direct
3378 // address in code. If the image is relocatable, emit .patch_oat entry.
3379 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3380 // the image relocatable or not.
3381 kCallDirectWithFixup,
3382
3383 // Use code pointer from the ArtMethod*.
3384 // Used when we don't know the target code. This is also the last-resort-kind used when
3385 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3386 kCallArtMethod,
3387 };
3388
3389 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003390 MethodLoadKind method_load_kind;
3391 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003392 // The method load data holds
3393 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3394 // Note that there are multiple string init methods, each having its own offset.
3395 // - the method address for kDirectAddress
3396 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003397 uint64_t method_load_data;
3398 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003399 };
3400
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003401 HInvokeStaticOrDirect(ArenaAllocator* arena,
3402 uint32_t number_of_arguments,
3403 Primitive::Type return_type,
3404 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003405 uint32_t method_index,
3406 MethodReference target_method,
3407 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003408 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003409 InvokeType invoke_type,
3410 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003411 : HInvoke(arena,
3412 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003413 // There is potentially one extra argument for the HCurrentMethod node, and
3414 // potentially one other if the clinit check is explicit, and potentially
3415 // one other if the method is a string factory.
3416 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3417 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3418 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003419 return_type,
3420 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003421 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003422 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003423 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003424 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003425 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003426 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003427
Vladimir Markodc151b22015-10-15 18:02:30 +01003428 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003429 bool had_current_method_input = HasCurrentMethodInput();
3430 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3431
3432 // Using the current method is the default and once we find a better
3433 // method load kind, we should not go back to using the current method.
3434 DCHECK(had_current_method_input || !needs_current_method_input);
3435
3436 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003437 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3438 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003439 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003440 dispatch_info_ = dispatch_info;
3441 }
3442
Vladimir Markoc53c0792015-11-19 15:48:33 +00003443 void AddSpecialInput(HInstruction* input) {
3444 // We allow only one special input.
3445 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3446 DCHECK(InputCount() == GetSpecialInputIndex() ||
3447 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3448 InsertInputAt(GetSpecialInputIndex(), input);
3449 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003450
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003451 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003452 // We access the method via the dex cache so we can't do an implicit null check.
3453 // TODO: for intrinsics we can generate implicit null checks.
3454 return false;
3455 }
3456
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003457 bool CanBeNull() const OVERRIDE {
3458 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3459 }
3460
Vladimir Markoc53c0792015-11-19 15:48:33 +00003461 // Get the index of the special input, if any.
3462 //
3463 // If the invoke IsStringInit(), it initially has a HFakeString special argument
3464 // which is removed by the instruction simplifier; if the invoke HasCurrentMethodInput(),
3465 // the "special input" is the current method pointer; otherwise there may be one
3466 // platform-specific special input, such as PC-relative addressing base.
3467 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3468
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003469 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003470 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3471 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3472 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003473 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003474 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003475 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003476 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003477 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3478 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003479 bool HasCurrentMethodInput() const {
3480 // This function can be called only after the invoke has been fully initialized by the builder.
3481 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003482 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003483 return true;
3484 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003485 DCHECK(InputCount() == GetSpecialInputIndex() ||
3486 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003487 return false;
3488 }
3489 }
Vladimir Marko58155012015-08-19 12:49:41 +00003490 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3491 MethodReference GetTargetMethod() const { return target_method_; }
3492
3493 int32_t GetStringInitOffset() const {
3494 DCHECK(IsStringInit());
3495 return dispatch_info_.method_load_data;
3496 }
3497
3498 uint64_t GetMethodAddress() const {
3499 DCHECK(HasMethodAddress());
3500 return dispatch_info_.method_load_data;
3501 }
3502
3503 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003504 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003505 return dispatch_info_.method_load_data;
3506 }
3507
3508 uint64_t GetDirectCodePtr() const {
3509 DCHECK(HasDirectCodePtr());
3510 return dispatch_info_.direct_code_ptr;
3511 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003512
Calin Juravle68ad6492015-08-18 17:08:12 +01003513 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3514
Roland Levillain4c0eb422015-04-24 16:43:49 +01003515 // Is this instruction a call to a static method?
3516 bool IsStatic() const {
3517 return GetInvokeType() == kStatic;
3518 }
3519
Vladimir Markofbb184a2015-11-13 14:47:00 +00003520 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3521 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3522 // instruction; only relevant for static calls with explicit clinit check.
3523 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003524 DCHECK(IsStaticWithExplicitClinitCheck());
3525 size_t last_input_index = InputCount() - 1;
3526 HInstruction* last_input = InputAt(last_input_index);
3527 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003528 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003529 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003530 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003531 clinit_check_requirement_ = new_requirement;
3532 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003533 }
3534
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003535 bool IsStringFactoryFor(HFakeString* str) const {
3536 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003537 DCHECK(!HasCurrentMethodInput());
3538 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003539 return InputAt(InputCount() - 1)->AsFakeString() == str;
3540 }
3541
3542 void RemoveFakeStringArgumentAsLastInput() {
3543 DCHECK(IsStringInit());
3544 size_t last_input_index = InputCount() - 1;
3545 HInstruction* last_input = InputAt(last_input_index);
3546 DCHECK(last_input != nullptr);
3547 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3548 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003549 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003550 }
3551
Roland Levillain4c0eb422015-04-24 16:43:49 +01003552 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003553 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003554 bool IsStaticWithExplicitClinitCheck() const {
3555 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3556 }
3557
3558 // Is this a call to a static method whose declaring class has an
3559 // implicit intialization check requirement?
3560 bool IsStaticWithImplicitClinitCheck() const {
3561 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3562 }
3563
Vladimir Markob554b5a2015-11-06 12:57:55 +00003564 // Does this method load kind need the current method as an input?
3565 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3566 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3567 }
3568
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003569 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003570
Roland Levillain4c0eb422015-04-24 16:43:49 +01003571 protected:
3572 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3573 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3574 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3575 HInstruction* input = input_record.GetInstruction();
3576 // `input` is the last input of a static invoke marked as having
3577 // an explicit clinit check. It must either be:
3578 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3579 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3580 DCHECK(input != nullptr);
3581 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3582 }
3583 return input_record;
3584 }
3585
Vladimir Markoc53c0792015-11-19 15:48:33 +00003586 void InsertInputAt(size_t index, HInstruction* input);
3587 void RemoveInputAt(size_t index);
3588
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003589 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003590 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003591 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003592 // The target method may refer to different dex file or method index than the original
3593 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3594 // in derived class to increase visibility.
3595 MethodReference target_method_;
3596 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003597
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003598 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003599};
Vladimir Markofbb184a2015-11-13 14:47:00 +00003600std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003601
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003602class HInvokeVirtual : public HInvoke {
3603 public:
3604 HInvokeVirtual(ArenaAllocator* arena,
3605 uint32_t number_of_arguments,
3606 Primitive::Type return_type,
3607 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003608 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003609 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003610 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003611 vtable_index_(vtable_index) {}
3612
Calin Juravle641547a2015-04-21 22:08:51 +01003613 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003614 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003615 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003616 }
3617
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003618 uint32_t GetVTableIndex() const { return vtable_index_; }
3619
3620 DECLARE_INSTRUCTION(InvokeVirtual);
3621
3622 private:
3623 const uint32_t vtable_index_;
3624
3625 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3626};
3627
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003628class HInvokeInterface : public HInvoke {
3629 public:
3630 HInvokeInterface(ArenaAllocator* arena,
3631 uint32_t number_of_arguments,
3632 Primitive::Type return_type,
3633 uint32_t dex_pc,
3634 uint32_t dex_method_index,
3635 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003636 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003637 imt_index_(imt_index) {}
3638
Calin Juravle641547a2015-04-21 22:08:51 +01003639 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003640 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003641 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003642 }
3643
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003644 uint32_t GetImtIndex() const { return imt_index_; }
3645 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3646
3647 DECLARE_INSTRUCTION(InvokeInterface);
3648
3649 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003650 const uint32_t imt_index_;
3651
3652 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3653};
3654
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003655class HNewInstance : public HExpression<2> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003656 public:
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003657 HNewInstance(HInstruction* cls,
3658 HCurrentMethod* current_method,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003659 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003660 uint16_t type_index,
3661 const DexFile& dex_file,
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003662 bool can_throw,
3663 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003664 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003665 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003666 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003667 dex_file_(dex_file),
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003668 can_throw_(can_throw),
3669 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003670 entrypoint_(entrypoint) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003671 SetRawInputAt(0, cls);
3672 SetRawInputAt(1, current_method);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003673 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003674
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003675 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003676 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003677
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003678 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003679 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003680
3681 // It may throw when called on type that's not instantiable/accessible.
3682 // It can throw OOME.
3683 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3684 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3685
3686 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003687
Calin Juravle10e244f2015-01-26 18:54:32 +00003688 bool CanBeNull() const OVERRIDE { return false; }
3689
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003690 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3691
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003692 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3693 entrypoint_ = entrypoint;
3694 }
3695
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003696 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003697
3698 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003699 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003700 const DexFile& dex_file_;
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003701 const bool can_throw_;
3702 const bool finalizable_;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003703 QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003704
3705 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3706};
3707
Roland Levillain88cb1752014-10-20 16:36:47 +01003708class HNeg : public HUnaryOperation {
3709 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003710 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3711 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003712
Roland Levillain9867bc72015-08-05 10:21:34 +01003713 template <typename T> T Compute(T x) const { return -x; }
3714
3715 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003716 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003717 }
3718 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003719 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003720 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003721
Roland Levillain88cb1752014-10-20 16:36:47 +01003722 DECLARE_INSTRUCTION(Neg);
3723
3724 private:
3725 DISALLOW_COPY_AND_ASSIGN(HNeg);
3726};
3727
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003728class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003729 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003730 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003731 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003732 uint32_t dex_pc,
3733 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003734 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003735 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003736 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003737 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003738 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003739 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003740 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003741 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003742 }
3743
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003744 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003745 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003746
3747 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003748 bool NeedsEnvironment() const OVERRIDE { return true; }
3749
Mingyao Yang0c365e62015-03-31 15:09:29 -07003750 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3751 bool CanThrow() const OVERRIDE { return true; }
3752
Calin Juravle10e244f2015-01-26 18:54:32 +00003753 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003754
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003755 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3756
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003757 DECLARE_INSTRUCTION(NewArray);
3758
3759 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003760 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003761 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003762 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003763
3764 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3765};
3766
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003767class HAdd : public HBinaryOperation {
3768 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003769 HAdd(Primitive::Type result_type,
3770 HInstruction* left,
3771 HInstruction* right,
3772 uint32_t dex_pc = kNoDexPc)
3773 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003774
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003775 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003776
Roland Levillain9867bc72015-08-05 10:21:34 +01003777 template <typename T> T Compute(T x, T y) const { return x + y; }
3778
3779 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003780 return GetBlock()->GetGraph()->GetIntConstant(
3781 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003782 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003783 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003784 return GetBlock()->GetGraph()->GetLongConstant(
3785 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003786 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003787
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003788 DECLARE_INSTRUCTION(Add);
3789
3790 private:
3791 DISALLOW_COPY_AND_ASSIGN(HAdd);
3792};
3793
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003794class HSub : public HBinaryOperation {
3795 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003796 HSub(Primitive::Type result_type,
3797 HInstruction* left,
3798 HInstruction* right,
3799 uint32_t dex_pc = kNoDexPc)
3800 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003801
Roland Levillain9867bc72015-08-05 10:21:34 +01003802 template <typename T> T Compute(T x, T y) const { return x - y; }
3803
3804 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003805 return GetBlock()->GetGraph()->GetIntConstant(
3806 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003807 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003808 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003809 return GetBlock()->GetGraph()->GetLongConstant(
3810 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003811 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003812
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003813 DECLARE_INSTRUCTION(Sub);
3814
3815 private:
3816 DISALLOW_COPY_AND_ASSIGN(HSub);
3817};
3818
Calin Juravle34bacdf2014-10-07 20:23:36 +01003819class HMul : public HBinaryOperation {
3820 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003821 HMul(Primitive::Type result_type,
3822 HInstruction* left,
3823 HInstruction* right,
3824 uint32_t dex_pc = kNoDexPc)
3825 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003826
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003827 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003828
Roland Levillain9867bc72015-08-05 10:21:34 +01003829 template <typename T> T Compute(T x, T y) const { return x * y; }
3830
3831 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003832 return GetBlock()->GetGraph()->GetIntConstant(
3833 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003834 }
3835 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003836 return GetBlock()->GetGraph()->GetLongConstant(
3837 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003838 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003839
3840 DECLARE_INSTRUCTION(Mul);
3841
3842 private:
3843 DISALLOW_COPY_AND_ASSIGN(HMul);
3844};
3845
Calin Juravle7c4954d2014-10-28 16:57:40 +00003846class HDiv : public HBinaryOperation {
3847 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003848 HDiv(Primitive::Type result_type,
3849 HInstruction* left,
3850 HInstruction* right,
3851 uint32_t dex_pc)
3852 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003853
Roland Levillain9867bc72015-08-05 10:21:34 +01003854 template <typename T>
3855 T Compute(T x, T y) const {
3856 // Our graph structure ensures we never have 0 for `y` during
3857 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003858 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003859 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003860 return (y == -1) ? -x : x / y;
3861 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003862
Roland Levillain9867bc72015-08-05 10:21:34 +01003863 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003864 return GetBlock()->GetGraph()->GetIntConstant(
3865 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003866 }
3867 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003868 return GetBlock()->GetGraph()->GetLongConstant(
3869 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003870 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003871
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003872 static SideEffects SideEffectsForArchRuntimeCalls() {
3873 // The generated code can use a runtime call.
3874 return SideEffects::CanTriggerGC();
3875 }
3876
Calin Juravle7c4954d2014-10-28 16:57:40 +00003877 DECLARE_INSTRUCTION(Div);
3878
3879 private:
3880 DISALLOW_COPY_AND_ASSIGN(HDiv);
3881};
3882
Calin Juravlebacfec32014-11-14 15:54:36 +00003883class HRem : public HBinaryOperation {
3884 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003885 HRem(Primitive::Type result_type,
3886 HInstruction* left,
3887 HInstruction* right,
3888 uint32_t dex_pc)
3889 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003890
Roland Levillain9867bc72015-08-05 10:21:34 +01003891 template <typename T>
3892 T Compute(T x, T y) const {
3893 // Our graph structure ensures we never have 0 for `y` during
3894 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003895 DCHECK_NE(y, 0);
3896 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3897 return (y == -1) ? 0 : x % y;
3898 }
3899
Roland Levillain9867bc72015-08-05 10:21:34 +01003900 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003901 return GetBlock()->GetGraph()->GetIntConstant(
3902 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003903 }
3904 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003905 return GetBlock()->GetGraph()->GetLongConstant(
3906 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003907 }
3908
Calin Juravlebacfec32014-11-14 15:54:36 +00003909
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003910 static SideEffects SideEffectsForArchRuntimeCalls() {
3911 return SideEffects::CanTriggerGC();
3912 }
3913
Calin Juravlebacfec32014-11-14 15:54:36 +00003914 DECLARE_INSTRUCTION(Rem);
3915
3916 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003917 DISALLOW_COPY_AND_ASSIGN(HRem);
3918};
3919
Calin Juravled0d48522014-11-04 16:40:20 +00003920class HDivZeroCheck : public HExpression<1> {
3921 public:
3922 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003923 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003924 SetRawInputAt(0, value);
3925 }
3926
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003927 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3928
Calin Juravled0d48522014-11-04 16:40:20 +00003929 bool CanBeMoved() const OVERRIDE { return true; }
3930
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003931 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003932 return true;
3933 }
3934
3935 bool NeedsEnvironment() const OVERRIDE { return true; }
3936 bool CanThrow() const OVERRIDE { return true; }
3937
Calin Juravled0d48522014-11-04 16:40:20 +00003938 DECLARE_INSTRUCTION(DivZeroCheck);
3939
3940 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003941 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3942};
3943
Calin Juravle9aec02f2014-11-18 23:06:35 +00003944class HShl : public HBinaryOperation {
3945 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003946 HShl(Primitive::Type result_type,
3947 HInstruction* left,
3948 HInstruction* right,
3949 uint32_t dex_pc = kNoDexPc)
3950 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003951
Roland Levillain9867bc72015-08-05 10:21:34 +01003952 template <typename T, typename U, typename V>
3953 T Compute(T x, U y, V max_shift_value) const {
3954 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3955 "V is not the unsigned integer type corresponding to T");
3956 return x << (y & max_shift_value);
3957 }
3958
3959 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3960 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003961 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003962 }
3963 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3964 // case is handled as `x << static_cast<int>(y)`.
3965 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3966 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003967 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003968 }
3969 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3970 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003971 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003972 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003973
3974 DECLARE_INSTRUCTION(Shl);
3975
3976 private:
3977 DISALLOW_COPY_AND_ASSIGN(HShl);
3978};
3979
3980class HShr : public HBinaryOperation {
3981 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003982 HShr(Primitive::Type result_type,
3983 HInstruction* left,
3984 HInstruction* right,
3985 uint32_t dex_pc = kNoDexPc)
3986 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003987
Roland Levillain9867bc72015-08-05 10:21:34 +01003988 template <typename T, typename U, typename V>
3989 T Compute(T x, U y, V max_shift_value) const {
3990 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3991 "V is not the unsigned integer type corresponding to T");
3992 return x >> (y & max_shift_value);
3993 }
3994
3995 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3996 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003997 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003998 }
3999 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4000 // case is handled as `x >> static_cast<int>(y)`.
4001 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4002 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004003 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004004 }
4005 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4006 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004007 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004008 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004009
4010 DECLARE_INSTRUCTION(Shr);
4011
4012 private:
4013 DISALLOW_COPY_AND_ASSIGN(HShr);
4014};
4015
4016class HUShr : public HBinaryOperation {
4017 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004018 HUShr(Primitive::Type result_type,
4019 HInstruction* left,
4020 HInstruction* right,
4021 uint32_t dex_pc = kNoDexPc)
4022 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004023
Roland Levillain9867bc72015-08-05 10:21:34 +01004024 template <typename T, typename U, typename V>
4025 T Compute(T x, U y, V max_shift_value) const {
4026 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4027 "V is not the unsigned integer type corresponding to T");
4028 V ux = static_cast<V>(x);
4029 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004030 }
4031
Roland Levillain9867bc72015-08-05 10:21:34 +01004032 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4033 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004034 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004035 }
4036 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4037 // case is handled as `x >>> static_cast<int>(y)`.
4038 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4039 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004040 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004041 }
4042 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4043 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004044 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004045 }
4046
4047 DECLARE_INSTRUCTION(UShr);
4048
4049 private:
4050 DISALLOW_COPY_AND_ASSIGN(HUShr);
4051};
4052
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004053class HAnd : public HBinaryOperation {
4054 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004055 HAnd(Primitive::Type result_type,
4056 HInstruction* left,
4057 HInstruction* right,
4058 uint32_t dex_pc = kNoDexPc)
4059 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004060
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004061 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004062
Roland Levillain9867bc72015-08-05 10:21:34 +01004063 template <typename T, typename U>
4064 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4065
4066 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004067 return GetBlock()->GetGraph()->GetIntConstant(
4068 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004069 }
4070 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004071 return GetBlock()->GetGraph()->GetLongConstant(
4072 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004073 }
4074 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004075 return GetBlock()->GetGraph()->GetLongConstant(
4076 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004077 }
4078 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004079 return GetBlock()->GetGraph()->GetLongConstant(
4080 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004081 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004082
4083 DECLARE_INSTRUCTION(And);
4084
4085 private:
4086 DISALLOW_COPY_AND_ASSIGN(HAnd);
4087};
4088
4089class HOr : public HBinaryOperation {
4090 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004091 HOr(Primitive::Type result_type,
4092 HInstruction* left,
4093 HInstruction* right,
4094 uint32_t dex_pc = kNoDexPc)
4095 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004096
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004097 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004098
Roland Levillain9867bc72015-08-05 10:21:34 +01004099 template <typename T, typename U>
4100 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4101
4102 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004103 return GetBlock()->GetGraph()->GetIntConstant(
4104 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004105 }
4106 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004107 return GetBlock()->GetGraph()->GetLongConstant(
4108 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004109 }
4110 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004111 return GetBlock()->GetGraph()->GetLongConstant(
4112 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004113 }
4114 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004115 return GetBlock()->GetGraph()->GetLongConstant(
4116 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004117 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004118
4119 DECLARE_INSTRUCTION(Or);
4120
4121 private:
4122 DISALLOW_COPY_AND_ASSIGN(HOr);
4123};
4124
4125class HXor : public HBinaryOperation {
4126 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004127 HXor(Primitive::Type result_type,
4128 HInstruction* left,
4129 HInstruction* right,
4130 uint32_t dex_pc = kNoDexPc)
4131 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004132
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004133 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004134
Roland Levillain9867bc72015-08-05 10:21:34 +01004135 template <typename T, typename U>
4136 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4137
4138 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004139 return GetBlock()->GetGraph()->GetIntConstant(
4140 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004141 }
4142 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004143 return GetBlock()->GetGraph()->GetLongConstant(
4144 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004145 }
4146 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004147 return GetBlock()->GetGraph()->GetLongConstant(
4148 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004149 }
4150 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004151 return GetBlock()->GetGraph()->GetLongConstant(
4152 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004153 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004154
4155 DECLARE_INSTRUCTION(Xor);
4156
4157 private:
4158 DISALLOW_COPY_AND_ASSIGN(HXor);
4159};
4160
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004161// The value of a parameter in this method. Its location depends on
4162// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004163class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004164 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004165 HParameterValue(const DexFile& dex_file,
4166 uint16_t type_index,
4167 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004168 Primitive::Type parameter_type,
4169 bool is_this = false)
4170 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004171 dex_file_(dex_file),
4172 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004173 index_(index),
4174 is_this_(is_this),
4175 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004176
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004177 const DexFile& GetDexFile() const { return dex_file_; }
4178 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004179 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004180 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004181
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004182 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4183 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004184
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004185 DECLARE_INSTRUCTION(ParameterValue);
4186
4187 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004188 const DexFile& dex_file_;
4189 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004190 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004191 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004192 const uint8_t index_;
4193
Calin Juravle10e244f2015-01-26 18:54:32 +00004194 // Whether or not the parameter value corresponds to 'this' argument.
4195 const bool is_this_;
4196
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004197 bool can_be_null_;
4198
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004199 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4200};
4201
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004202class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004203 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004204 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4205 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004206
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004207 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004208 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004209 return true;
4210 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004211
Roland Levillain9867bc72015-08-05 10:21:34 +01004212 template <typename T> T Compute(T x) const { return ~x; }
4213
4214 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004215 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004216 }
4217 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004218 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004219 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004220
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004221 DECLARE_INSTRUCTION(Not);
4222
4223 private:
4224 DISALLOW_COPY_AND_ASSIGN(HNot);
4225};
4226
David Brazdil66d126e2015-04-03 16:02:44 +01004227class HBooleanNot : public HUnaryOperation {
4228 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004229 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4230 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004231
4232 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004233 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004234 return true;
4235 }
4236
Roland Levillain9867bc72015-08-05 10:21:34 +01004237 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004238 DCHECK(IsUint<1>(x));
4239 return !x;
4240 }
4241
Roland Levillain9867bc72015-08-05 10:21:34 +01004242 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004243 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004244 }
4245 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4246 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004247 UNREACHABLE();
4248 }
4249
4250 DECLARE_INSTRUCTION(BooleanNot);
4251
4252 private:
4253 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4254};
4255
Roland Levillaindff1f282014-11-05 14:15:05 +00004256class HTypeConversion : public HExpression<1> {
4257 public:
4258 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004259 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004260 : HExpression(result_type,
4261 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4262 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004263 SetRawInputAt(0, input);
4264 DCHECK_NE(input->GetType(), result_type);
4265 }
4266
4267 HInstruction* GetInput() const { return InputAt(0); }
4268 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4269 Primitive::Type GetResultType() const { return GetType(); }
4270
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004271 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004272 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004273
Roland Levillaindff1f282014-11-05 14:15:05 +00004274 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004275 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004276
Mark Mendelle82549b2015-05-06 10:55:34 -04004277 // Try to statically evaluate the conversion and return a HConstant
4278 // containing the result. If the input cannot be converted, return nullptr.
4279 HConstant* TryStaticEvaluation() const;
4280
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004281 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4282 Primitive::Type result_type) {
4283 // Some architectures may not require the 'GC' side effects, but at this point
4284 // in the compilation process we do not know what architecture we will
4285 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004286 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4287 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004288 return SideEffects::CanTriggerGC();
4289 }
4290 return SideEffects::None();
4291 }
4292
Roland Levillaindff1f282014-11-05 14:15:05 +00004293 DECLARE_INSTRUCTION(TypeConversion);
4294
4295 private:
4296 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4297};
4298
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004299static constexpr uint32_t kNoRegNumber = -1;
4300
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004301class HPhi : public HInstruction {
4302 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004303 HPhi(ArenaAllocator* arena,
4304 uint32_t reg_number,
4305 size_t number_of_inputs,
4306 Primitive::Type type,
4307 uint32_t dex_pc = kNoDexPc)
4308 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004309 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004310 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004311 type_(type),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004312 is_live_(false),
Calin Juravle10e244f2015-01-26 18:54:32 +00004313 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004314 }
4315
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004316 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4317 static Primitive::Type ToPhiType(Primitive::Type type) {
4318 switch (type) {
4319 case Primitive::kPrimBoolean:
4320 case Primitive::kPrimByte:
4321 case Primitive::kPrimShort:
4322 case Primitive::kPrimChar:
4323 return Primitive::kPrimInt;
4324 default:
4325 return type;
4326 }
4327 }
4328
David Brazdilffee3d32015-07-06 11:48:53 +01004329 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4330
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004331 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004332
4333 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004334 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004335
Calin Juravle10e244f2015-01-26 18:54:32 +00004336 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004337 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004338
Calin Juravle10e244f2015-01-26 18:54:32 +00004339 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4340 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4341
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004342 uint32_t GetRegNumber() const { return reg_number_; }
4343
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004344 void SetDead() { is_live_ = false; }
4345 void SetLive() { is_live_ = true; }
4346 bool IsDead() const { return !is_live_; }
4347 bool IsLive() const { return is_live_; }
4348
David Brazdil77a48ae2015-09-15 12:34:04 +00004349 bool IsVRegEquivalentOf(HInstruction* other) const {
4350 return other != nullptr
4351 && other->IsPhi()
4352 && other->AsPhi()->GetBlock() == GetBlock()
4353 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4354 }
4355
Calin Juravlea4f88312015-04-16 12:57:19 +01004356 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4357 // An equivalent phi is a phi having the same dex register and type.
4358 // It assumes that phis with the same dex register are adjacent.
4359 HPhi* GetNextEquivalentPhiWithSameType() {
4360 HInstruction* next = GetNext();
4361 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4362 if (next->GetType() == GetType()) {
4363 return next->AsPhi();
4364 }
4365 next = next->GetNext();
4366 }
4367 return nullptr;
4368 }
4369
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004370 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004371
David Brazdil1abb4192015-02-17 18:33:36 +00004372 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004373 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004374 return inputs_[index];
4375 }
David Brazdil1abb4192015-02-17 18:33:36 +00004376
4377 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004378 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004379 }
4380
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004381 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004382 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004383 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004384 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004385 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004386 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004387
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004388 DISALLOW_COPY_AND_ASSIGN(HPhi);
4389};
4390
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004391class HNullCheck : public HExpression<1> {
4392 public:
4393 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004394 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004395 SetRawInputAt(0, value);
4396 }
4397
Calin Juravle10e244f2015-01-26 18:54:32 +00004398 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004399 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004400 return true;
4401 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004402
Calin Juravle10e244f2015-01-26 18:54:32 +00004403 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004404
Calin Juravle10e244f2015-01-26 18:54:32 +00004405 bool CanThrow() const OVERRIDE { return true; }
4406
4407 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004408
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004409
4410 DECLARE_INSTRUCTION(NullCheck);
4411
4412 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004413 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4414};
4415
4416class FieldInfo : public ValueObject {
4417 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004418 FieldInfo(MemberOffset field_offset,
4419 Primitive::Type field_type,
4420 bool is_volatile,
4421 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004422 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004423 const DexFile& dex_file,
4424 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004425 : field_offset_(field_offset),
4426 field_type_(field_type),
4427 is_volatile_(is_volatile),
4428 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004429 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004430 dex_file_(dex_file),
4431 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004432
4433 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004434 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004435 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004436 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004437 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004438 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004439 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004440
4441 private:
4442 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004443 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004444 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004445 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004446 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004447 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004448 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004449};
4450
4451class HInstanceFieldGet : public HExpression<1> {
4452 public:
4453 HInstanceFieldGet(HInstruction* value,
4454 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004455 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004456 bool is_volatile,
4457 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004458 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004459 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004460 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004461 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004462 : HExpression(field_type,
4463 SideEffects::FieldReadOfType(field_type, is_volatile),
4464 dex_pc),
4465 field_info_(field_offset,
4466 field_type,
4467 is_volatile,
4468 field_idx,
4469 declaring_class_def_index,
4470 dex_file,
4471 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004472 SetRawInputAt(0, value);
4473 }
4474
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004475 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004476
4477 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4478 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4479 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004480 }
4481
Calin Juravle641547a2015-04-21 22:08:51 +01004482 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4483 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004484 }
4485
4486 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004487 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4488 }
4489
Calin Juravle52c48962014-12-16 17:02:57 +00004490 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004491 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004492 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004493 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004494
4495 DECLARE_INSTRUCTION(InstanceFieldGet);
4496
4497 private:
4498 const FieldInfo field_info_;
4499
4500 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4501};
4502
4503class HInstanceFieldSet : public HTemplateInstruction<2> {
4504 public:
4505 HInstanceFieldSet(HInstruction* object,
4506 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004507 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004508 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004509 bool is_volatile,
4510 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004511 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004512 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004513 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004514 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004515 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4516 dex_pc),
4517 field_info_(field_offset,
4518 field_type,
4519 is_volatile,
4520 field_idx,
4521 declaring_class_def_index,
4522 dex_file,
4523 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004524 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004525 SetRawInputAt(0, object);
4526 SetRawInputAt(1, value);
4527 }
4528
Calin Juravle641547a2015-04-21 22:08:51 +01004529 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4530 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004531 }
4532
Calin Juravle52c48962014-12-16 17:02:57 +00004533 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004534 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004535 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004536 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004537 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004538 bool GetValueCanBeNull() const { return value_can_be_null_; }
4539 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004540
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004541 DECLARE_INSTRUCTION(InstanceFieldSet);
4542
4543 private:
4544 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004545 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004546
4547 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4548};
4549
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004550class HArrayGet : public HExpression<2> {
4551 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004552 HArrayGet(HInstruction* array,
4553 HInstruction* index,
4554 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004555 uint32_t dex_pc,
4556 SideEffects additional_side_effects = SideEffects::None())
4557 : HExpression(type,
4558 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004559 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004560 SetRawInputAt(0, array);
4561 SetRawInputAt(1, index);
4562 }
4563
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004564 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004565 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004566 return true;
4567 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004568 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004569 // TODO: We can be smarter here.
4570 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4571 // which generates the implicit null check. There are cases when these can be removed
4572 // to produce better code. If we ever add optimizations to do so we should allow an
4573 // implicit check here (as long as the address falls in the first page).
4574 return false;
4575 }
4576
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004577 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004578
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004579 HInstruction* GetArray() const { return InputAt(0); }
4580 HInstruction* GetIndex() const { return InputAt(1); }
4581
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004582 DECLARE_INSTRUCTION(ArrayGet);
4583
4584 private:
4585 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4586};
4587
4588class HArraySet : public HTemplateInstruction<3> {
4589 public:
4590 HArraySet(HInstruction* array,
4591 HInstruction* index,
4592 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004593 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004594 uint32_t dex_pc,
4595 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004596 : HTemplateInstruction(
4597 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004598 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4599 additional_side_effects),
4600 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004601 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004602 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004603 value_can_be_null_(true),
4604 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605 SetRawInputAt(0, array);
4606 SetRawInputAt(1, index);
4607 SetRawInputAt(2, value);
4608 }
4609
Calin Juravle77520bc2015-01-12 18:45:46 +00004610 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004611 // We currently always call a runtime method to catch array store
4612 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004613 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004614 }
4615
Mingyao Yang81014cb2015-06-02 03:16:27 -07004616 // Can throw ArrayStoreException.
4617 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4618
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004619 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004620 // TODO: Same as for ArrayGet.
4621 return false;
4622 }
4623
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004624 void ClearNeedsTypeCheck() {
4625 needs_type_check_ = false;
4626 }
4627
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004628 void ClearValueCanBeNull() {
4629 value_can_be_null_ = false;
4630 }
4631
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004632 void SetStaticTypeOfArrayIsObjectArray() {
4633 static_type_of_array_is_object_array_ = true;
4634 }
4635
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004636 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004637 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004638 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004639
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004640 HInstruction* GetArray() const { return InputAt(0); }
4641 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004642 HInstruction* GetValue() const { return InputAt(2); }
4643
4644 Primitive::Type GetComponentType() const {
4645 // The Dex format does not type floating point index operations. Since the
4646 // `expected_component_type_` is set during building and can therefore not
4647 // be correct, we also check what is the value type. If it is a floating
4648 // point type, we must use that type.
4649 Primitive::Type value_type = GetValue()->GetType();
4650 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4651 ? value_type
4652 : expected_component_type_;
4653 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004654
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004655 Primitive::Type GetRawExpectedComponentType() const {
4656 return expected_component_type_;
4657 }
4658
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004659 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4660 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4661 }
4662
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004663 DECLARE_INSTRUCTION(ArraySet);
4664
4665 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004666 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004667 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004668 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004669 // Cached information for the reference_type_info_ so that codegen
4670 // does not need to inspect the static type.
4671 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004672
4673 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4674};
4675
4676class HArrayLength : public HExpression<1> {
4677 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004678 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004679 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004680 // Note that arrays do not change length, so the instruction does not
4681 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004682 SetRawInputAt(0, array);
4683 }
4684
Calin Juravle77520bc2015-01-12 18:45:46 +00004685 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004686 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004687 return true;
4688 }
Calin Juravle641547a2015-04-21 22:08:51 +01004689 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4690 return obj == InputAt(0);
4691 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004692
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004693 DECLARE_INSTRUCTION(ArrayLength);
4694
4695 private:
4696 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4697};
4698
4699class HBoundsCheck : public HExpression<2> {
4700 public:
4701 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004702 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004703 DCHECK(index->GetType() == Primitive::kPrimInt);
4704 SetRawInputAt(0, index);
4705 SetRawInputAt(1, length);
4706 }
4707
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004708 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004709 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004710 return true;
4711 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004712
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004713 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004714
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004715 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004716
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004717 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004718
4719 DECLARE_INSTRUCTION(BoundsCheck);
4720
4721 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004722 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4723};
4724
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004725/**
4726 * Some DEX instructions are folded into multiple HInstructions that need
4727 * to stay live until the last HInstruction. This class
4728 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004729 * HInstruction stays live. `index` represents the stack location index of the
4730 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004731 */
4732class HTemporary : public HTemplateInstruction<0> {
4733 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004734 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4735 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004736
4737 size_t GetIndex() const { return index_; }
4738
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004739 Primitive::Type GetType() const OVERRIDE {
4740 // The previous instruction is the one that will be stored in the temporary location.
4741 DCHECK(GetPrevious() != nullptr);
4742 return GetPrevious()->GetType();
4743 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004744
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004745 DECLARE_INSTRUCTION(Temporary);
4746
4747 private:
4748 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004749 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4750};
4751
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004752class HSuspendCheck : public HTemplateInstruction<0> {
4753 public:
4754 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004755 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004756
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004757 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004758 return true;
4759 }
4760
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004761 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4762 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004763
4764 DECLARE_INSTRUCTION(SuspendCheck);
4765
4766 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004767 // Only used for code generation, in order to share the same slow path between back edges
4768 // of a same loop.
4769 SlowPathCode* slow_path_;
4770
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004771 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4772};
4773
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004774/**
4775 * Instruction to load a Class object.
4776 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004777class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004778 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004779 HLoadClass(HCurrentMethod* current_method,
4780 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004781 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004782 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004783 uint32_t dex_pc,
4784 bool needs_access_check)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004785 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004786 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004787 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004788 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004789 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004790 needs_access_check_(needs_access_check),
Calin Juravle2e768302015-07-28 14:41:11 +00004791 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004792 // Referrers class should not need access check. We never inline unverified
4793 // methods so we can't possibly end up in this situation.
4794 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004795 SetRawInputAt(0, current_method);
4796 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004797
4798 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004799
4800 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004801 // Note that we don't need to test for generate_clinit_check_.
4802 // Whether or not we need to generate the clinit check is processed in
4803 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004804 return other->AsLoadClass()->type_index_ == type_index_ &&
4805 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004806 }
4807
4808 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4809
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004810 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004811 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004812 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004813
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004814 bool NeedsEnvironment() const OVERRIDE {
4815 // Will call runtime and load the class if the class is not loaded yet.
4816 // TODO: finer grain decision.
Calin Juravle4e2a5572015-10-07 18:55:43 +01004817 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004818 }
4819
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004820 bool MustGenerateClinitCheck() const {
4821 return generate_clinit_check_;
4822 }
Calin Juravle0ba218d2015-05-19 18:46:01 +01004823 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004824 // The entrypoint the code generator is going to call does not do
4825 // clinit of the class.
4826 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004827 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004828 }
4829
4830 bool CanCallRuntime() const {
Calin Juravle98893e12015-10-02 21:05:03 +01004831 return MustGenerateClinitCheck() || !is_referrers_class_ || needs_access_check_;
4832 }
4833
4834 bool NeedsAccessCheck() const {
4835 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004836 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004837
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004838 bool CanThrow() const OVERRIDE {
4839 // May call runtime and and therefore can throw.
4840 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004841 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004842 }
4843
Calin Juravleacf735c2015-02-12 15:25:22 +00004844 ReferenceTypeInfo GetLoadedClassRTI() {
4845 return loaded_class_rti_;
4846 }
4847
4848 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4849 // Make sure we only set exact types (the loaded class should never be merged).
4850 DCHECK(rti.IsExact());
4851 loaded_class_rti_ = rti;
4852 }
4853
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004854 const DexFile& GetDexFile() { return dex_file_; }
4855
Vladimir Markodc151b22015-10-15 18:02:30 +01004856 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004857
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004858 static SideEffects SideEffectsForArchRuntimeCalls() {
4859 return SideEffects::CanTriggerGC();
4860 }
4861
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004862 DECLARE_INSTRUCTION(LoadClass);
4863
4864 private:
4865 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004866 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004867 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004868 // Whether this instruction must generate the initialization check.
4869 // Used for code generation.
4870 bool generate_clinit_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004871 bool needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004872
Calin Juravleacf735c2015-02-12 15:25:22 +00004873 ReferenceTypeInfo loaded_class_rti_;
4874
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004875 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4876};
4877
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004878class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004879 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004880 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004881 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4882 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004883 SetRawInputAt(0, current_method);
4884 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004885
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004886 bool CanBeMoved() const OVERRIDE { return true; }
4887
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004888 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4889 return other->AsLoadString()->string_index_ == string_index_;
4890 }
4891
4892 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4893
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004894 uint32_t GetStringIndex() const { return string_index_; }
4895
4896 // TODO: Can we deopt or debug when we resolve a string?
4897 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004898 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004899 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004900
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004901 static SideEffects SideEffectsForArchRuntimeCalls() {
4902 return SideEffects::CanTriggerGC();
4903 }
4904
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004905 DECLARE_INSTRUCTION(LoadString);
4906
4907 private:
4908 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004909
4910 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4911};
4912
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004913/**
4914 * Performs an initialization check on its Class object input.
4915 */
4916class HClinitCheck : public HExpression<1> {
4917 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004918 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004919 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004920 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004921 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4922 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004923 SetRawInputAt(0, constant);
4924 }
4925
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004926 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004927 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004928 return true;
4929 }
4930
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004931 bool NeedsEnvironment() const OVERRIDE {
4932 // May call runtime to initialize the class.
4933 return true;
4934 }
4935
Nicolas Geoffray729645a2015-11-19 13:29:02 +00004936 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004937
4938 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4939
4940 DECLARE_INSTRUCTION(ClinitCheck);
4941
4942 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004943 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4944};
4945
4946class HStaticFieldGet : public HExpression<1> {
4947 public:
4948 HStaticFieldGet(HInstruction* cls,
4949 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004950 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004951 bool is_volatile,
4952 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004953 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004954 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004955 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004956 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004957 : HExpression(field_type,
4958 SideEffects::FieldReadOfType(field_type, is_volatile),
4959 dex_pc),
4960 field_info_(field_offset,
4961 field_type,
4962 is_volatile,
4963 field_idx,
4964 declaring_class_def_index,
4965 dex_file,
4966 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004967 SetRawInputAt(0, cls);
4968 }
4969
Calin Juravle52c48962014-12-16 17:02:57 +00004970
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004971 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004972
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004973 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004974 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4975 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004976 }
4977
4978 size_t ComputeHashCode() const OVERRIDE {
4979 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4980 }
4981
Calin Juravle52c48962014-12-16 17:02:57 +00004982 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004983 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4984 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004985 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004986
4987 DECLARE_INSTRUCTION(StaticFieldGet);
4988
4989 private:
4990 const FieldInfo field_info_;
4991
4992 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4993};
4994
4995class HStaticFieldSet : public HTemplateInstruction<2> {
4996 public:
4997 HStaticFieldSet(HInstruction* cls,
4998 HInstruction* value,
4999 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005000 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005001 bool is_volatile,
5002 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005003 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005004 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005005 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005006 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005007 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5008 dex_pc),
5009 field_info_(field_offset,
5010 field_type,
5011 is_volatile,
5012 field_idx,
5013 declaring_class_def_index,
5014 dex_file,
5015 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005016 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005017 SetRawInputAt(0, cls);
5018 SetRawInputAt(1, value);
5019 }
5020
Calin Juravle52c48962014-12-16 17:02:57 +00005021 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005022 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5023 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005024 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005025
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005026 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005027 bool GetValueCanBeNull() const { return value_can_be_null_; }
5028 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005029
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005030 DECLARE_INSTRUCTION(StaticFieldSet);
5031
5032 private:
5033 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005034 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005035
5036 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5037};
5038
Calin Juravlee460d1d2015-09-29 04:52:17 +01005039class HUnresolvedInstanceFieldGet : public HExpression<1> {
5040 public:
5041 HUnresolvedInstanceFieldGet(HInstruction* obj,
5042 Primitive::Type field_type,
5043 uint32_t field_index,
5044 uint32_t dex_pc)
5045 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5046 field_index_(field_index) {
5047 SetRawInputAt(0, obj);
5048 }
5049
5050 bool NeedsEnvironment() const OVERRIDE { return true; }
5051 bool CanThrow() const OVERRIDE { return true; }
5052
5053 Primitive::Type GetFieldType() const { return GetType(); }
5054 uint32_t GetFieldIndex() const { return field_index_; }
5055
5056 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5057
5058 private:
5059 const uint32_t field_index_;
5060
5061 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5062};
5063
5064class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5065 public:
5066 HUnresolvedInstanceFieldSet(HInstruction* obj,
5067 HInstruction* value,
5068 Primitive::Type field_type,
5069 uint32_t field_index,
5070 uint32_t dex_pc)
5071 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5072 field_type_(field_type),
5073 field_index_(field_index) {
5074 DCHECK_EQ(field_type, value->GetType());
5075 SetRawInputAt(0, obj);
5076 SetRawInputAt(1, value);
5077 }
5078
5079 bool NeedsEnvironment() const OVERRIDE { return true; }
5080 bool CanThrow() const OVERRIDE { return true; }
5081
5082 Primitive::Type GetFieldType() const { return field_type_; }
5083 uint32_t GetFieldIndex() const { return field_index_; }
5084
5085 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5086
5087 private:
5088 const Primitive::Type field_type_;
5089 const uint32_t field_index_;
5090
5091 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5092};
5093
5094class HUnresolvedStaticFieldGet : public HExpression<0> {
5095 public:
5096 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5097 uint32_t field_index,
5098 uint32_t dex_pc)
5099 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5100 field_index_(field_index) {
5101 }
5102
5103 bool NeedsEnvironment() const OVERRIDE { return true; }
5104 bool CanThrow() const OVERRIDE { return true; }
5105
5106 Primitive::Type GetFieldType() const { return GetType(); }
5107 uint32_t GetFieldIndex() const { return field_index_; }
5108
5109 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5110
5111 private:
5112 const uint32_t field_index_;
5113
5114 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5115};
5116
5117class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5118 public:
5119 HUnresolvedStaticFieldSet(HInstruction* value,
5120 Primitive::Type field_type,
5121 uint32_t field_index,
5122 uint32_t dex_pc)
5123 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5124 field_type_(field_type),
5125 field_index_(field_index) {
5126 DCHECK_EQ(field_type, value->GetType());
5127 SetRawInputAt(0, value);
5128 }
5129
5130 bool NeedsEnvironment() const OVERRIDE { return true; }
5131 bool CanThrow() const OVERRIDE { return true; }
5132
5133 Primitive::Type GetFieldType() const { return field_type_; }
5134 uint32_t GetFieldIndex() const { return field_index_; }
5135
5136 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5137
5138 private:
5139 const Primitive::Type field_type_;
5140 const uint32_t field_index_;
5141
5142 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5143};
5144
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005145// Implement the move-exception DEX instruction.
5146class HLoadException : public HExpression<0> {
5147 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005148 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5149 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005150
David Brazdilbbd733e2015-08-18 17:48:17 +01005151 bool CanBeNull() const OVERRIDE { return false; }
5152
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005153 DECLARE_INSTRUCTION(LoadException);
5154
5155 private:
5156 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5157};
5158
David Brazdilcb1c0552015-08-04 16:22:25 +01005159// Implicit part of move-exception which clears thread-local exception storage.
5160// Must not be removed because the runtime expects the TLS to get cleared.
5161class HClearException : public HTemplateInstruction<0> {
5162 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005163 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5164 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005165
5166 DECLARE_INSTRUCTION(ClearException);
5167
5168 private:
5169 DISALLOW_COPY_AND_ASSIGN(HClearException);
5170};
5171
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005172class HThrow : public HTemplateInstruction<1> {
5173 public:
5174 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005175 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005176 SetRawInputAt(0, exception);
5177 }
5178
5179 bool IsControlFlow() const OVERRIDE { return true; }
5180
5181 bool NeedsEnvironment() const OVERRIDE { return true; }
5182
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005183 bool CanThrow() const OVERRIDE { return true; }
5184
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005185
5186 DECLARE_INSTRUCTION(Throw);
5187
5188 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005189 DISALLOW_COPY_AND_ASSIGN(HThrow);
5190};
5191
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005192/**
5193 * Implementation strategies for the code generator of a HInstanceOf
5194 * or `HCheckCast`.
5195 */
5196enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005197 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005198 kExactCheck, // Can do a single class compare.
5199 kClassHierarchyCheck, // Can just walk the super class chain.
5200 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5201 kInterfaceCheck, // No optimization yet when checking against an interface.
5202 kArrayObjectCheck, // Can just check if the array is not primitive.
5203 kArrayCheck // No optimization yet when checking against a generic array.
5204};
5205
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005206class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005207 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005208 HInstanceOf(HInstruction* object,
5209 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005210 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005211 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005212 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005213 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005214 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005215 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005216 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005217 SetRawInputAt(0, object);
5218 SetRawInputAt(1, constant);
5219 }
5220
5221 bool CanBeMoved() const OVERRIDE { return true; }
5222
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005223 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005224 return true;
5225 }
5226
5227 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005228 return false;
5229 }
5230
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005231 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5232
5233 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005234
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005235 // Used only in code generation.
5236 bool MustDoNullCheck() const { return must_do_null_check_; }
5237 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5238
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005239 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5240 return (check_kind == TypeCheckKind::kExactCheck)
5241 ? SideEffects::None()
5242 // Mips currently does runtime calls for any other checks.
5243 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005244 }
5245
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005246 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005247
5248 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005249 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005250 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005251
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005252 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5253};
5254
Calin Juravleb1498f62015-02-16 13:13:29 +00005255class HBoundType : public HExpression<1> {
5256 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005257 // Constructs an HBoundType with the given upper_bound.
5258 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005259 HBoundType(HInstruction* input,
5260 ReferenceTypeInfo upper_bound,
5261 bool upper_can_be_null,
5262 uint32_t dex_pc = kNoDexPc)
5263 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005264 upper_bound_(upper_bound),
5265 upper_can_be_null_(upper_can_be_null),
5266 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005267 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005268 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005269 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005270 }
5271
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005272 // GetUpper* should only be used in reference type propagation.
5273 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5274 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005275
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005276 void SetCanBeNull(bool can_be_null) {
5277 DCHECK(upper_can_be_null_ || !can_be_null);
5278 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005279 }
5280
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005281 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5282
Calin Juravleb1498f62015-02-16 13:13:29 +00005283 DECLARE_INSTRUCTION(BoundType);
5284
5285 private:
5286 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005287 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5288 // It is used to bound the type in cases like:
5289 // if (x instanceof ClassX) {
5290 // // uper_bound_ will be ClassX
5291 // }
5292 const ReferenceTypeInfo upper_bound_;
5293 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5294 // is false then can_be_null_ cannot be true).
5295 const bool upper_can_be_null_;
5296 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005297
5298 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5299};
5300
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005301class HCheckCast : public HTemplateInstruction<2> {
5302 public:
5303 HCheckCast(HInstruction* object,
5304 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005305 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005306 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005307 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005308 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005309 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005310 SetRawInputAt(0, object);
5311 SetRawInputAt(1, constant);
5312 }
5313
5314 bool CanBeMoved() const OVERRIDE { return true; }
5315
5316 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5317 return true;
5318 }
5319
5320 bool NeedsEnvironment() const OVERRIDE {
5321 // Instruction may throw a CheckCastError.
5322 return true;
5323 }
5324
5325 bool CanThrow() const OVERRIDE { return true; }
5326
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005327 bool MustDoNullCheck() const { return must_do_null_check_; }
5328 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005329 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005330
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005331 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005332
5333 DECLARE_INSTRUCTION(CheckCast);
5334
5335 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005336 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005337 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005338
5339 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005340};
5341
Calin Juravle27df7582015-04-17 19:12:31 +01005342class HMemoryBarrier : public HTemplateInstruction<0> {
5343 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005344 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005345 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005346 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005347 barrier_kind_(barrier_kind) {}
5348
5349 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5350
5351 DECLARE_INSTRUCTION(MemoryBarrier);
5352
5353 private:
5354 const MemBarrierKind barrier_kind_;
5355
5356 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5357};
5358
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005359class HMonitorOperation : public HTemplateInstruction<1> {
5360 public:
5361 enum OperationKind {
5362 kEnter,
5363 kExit,
5364 };
5365
5366 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005367 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005368 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5369 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005370 SetRawInputAt(0, object);
5371 }
5372
5373 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005374 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5375
5376 bool CanThrow() const OVERRIDE {
5377 // Verifier guarantees that monitor-exit cannot throw.
5378 // This is important because it allows the HGraphBuilder to remove
5379 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5380 return IsEnter();
5381 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005382
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005383
5384 bool IsEnter() const { return kind_ == kEnter; }
5385
5386 DECLARE_INSTRUCTION(MonitorOperation);
5387
Calin Juravle52c48962014-12-16 17:02:57 +00005388 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005389 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005390
5391 private:
5392 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5393};
5394
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005395/**
5396 * A HInstruction used as a marker for the replacement of new + <init>
5397 * of a String to a call to a StringFactory. Only baseline will see
5398 * the node at code generation, where it will be be treated as null.
5399 * When compiling non-baseline, `HFakeString` instructions are being removed
5400 * in the instruction simplifier.
5401 */
5402class HFakeString : public HTemplateInstruction<0> {
5403 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005404 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5405 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005406
5407 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5408
5409 DECLARE_INSTRUCTION(FakeString);
5410
5411 private:
5412 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5413};
5414
Vladimir Markof9f64412015-09-02 14:05:49 +01005415class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005416 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005417 MoveOperands(Location source,
5418 Location destination,
5419 Primitive::Type type,
5420 HInstruction* instruction)
5421 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005422
5423 Location GetSource() const { return source_; }
5424 Location GetDestination() const { return destination_; }
5425
5426 void SetSource(Location value) { source_ = value; }
5427 void SetDestination(Location value) { destination_ = value; }
5428
5429 // The parallel move resolver marks moves as "in-progress" by clearing the
5430 // destination (but not the source).
5431 Location MarkPending() {
5432 DCHECK(!IsPending());
5433 Location dest = destination_;
5434 destination_ = Location::NoLocation();
5435 return dest;
5436 }
5437
5438 void ClearPending(Location dest) {
5439 DCHECK(IsPending());
5440 destination_ = dest;
5441 }
5442
5443 bool IsPending() const {
5444 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5445 return destination_.IsInvalid() && !source_.IsInvalid();
5446 }
5447
5448 // True if this blocks a move from the given location.
5449 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005450 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005451 }
5452
5453 // A move is redundant if it's been eliminated, if its source and
5454 // destination are the same, or if its destination is unneeded.
5455 bool IsRedundant() const {
5456 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5457 }
5458
5459 // We clear both operands to indicate move that's been eliminated.
5460 void Eliminate() {
5461 source_ = destination_ = Location::NoLocation();
5462 }
5463
5464 bool IsEliminated() const {
5465 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5466 return source_.IsInvalid();
5467 }
5468
Alexey Frunze4dda3372015-06-01 18:31:49 -07005469 Primitive::Type GetType() const { return type_; }
5470
Nicolas Geoffray90218252015-04-15 11:56:51 +01005471 bool Is64BitMove() const {
5472 return Primitive::Is64BitType(type_);
5473 }
5474
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005475 HInstruction* GetInstruction() const { return instruction_; }
5476
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005477 private:
5478 Location source_;
5479 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005480 // The type this move is for.
5481 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005482 // The instruction this move is assocatied with. Null when this move is
5483 // for moving an input in the expected locations of user (including a phi user).
5484 // This is only used in debug mode, to ensure we do not connect interval siblings
5485 // in the same parallel move.
5486 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005487};
5488
5489static constexpr size_t kDefaultNumberOfMoves = 4;
5490
5491class HParallelMove : public HTemplateInstruction<0> {
5492 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005493 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005494 : HTemplateInstruction(SideEffects::None(), dex_pc),
5495 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5496 moves_.reserve(kDefaultNumberOfMoves);
5497 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005498
Nicolas Geoffray90218252015-04-15 11:56:51 +01005499 void AddMove(Location source,
5500 Location destination,
5501 Primitive::Type type,
5502 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005503 DCHECK(source.IsValid());
5504 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005505 if (kIsDebugBuild) {
5506 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005507 for (const MoveOperands& move : moves_) {
5508 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005509 // Special case the situation where the move is for the spill slot
5510 // of the instruction.
5511 if ((GetPrevious() == instruction)
5512 || ((GetPrevious() == nullptr)
5513 && instruction->IsPhi()
5514 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005515 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005516 << "Doing parallel moves for the same instruction.";
5517 } else {
5518 DCHECK(false) << "Doing parallel moves for the same instruction.";
5519 }
5520 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005521 }
5522 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005523 for (const MoveOperands& move : moves_) {
5524 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005525 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005526 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005527 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005528 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005529 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005530 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005531 }
5532
Vladimir Marko225b6462015-09-28 12:17:40 +01005533 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005534 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005535 }
5536
Vladimir Marko225b6462015-09-28 12:17:40 +01005537 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005538
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005539 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005540
5541 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005542 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005543
5544 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5545};
5546
Mark Mendell0616ae02015-04-17 12:49:27 -04005547} // namespace art
5548
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005549#ifdef ART_ENABLE_CODEGEN_arm64
5550#include "nodes_arm64.h"
5551#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005552#ifdef ART_ENABLE_CODEGEN_x86
5553#include "nodes_x86.h"
5554#endif
5555
5556namespace art {
5557
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005558class HGraphVisitor : public ValueObject {
5559 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005560 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5561 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005562
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005563 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005564 virtual void VisitBasicBlock(HBasicBlock* block);
5565
Roland Levillain633021e2014-10-01 14:12:25 +01005566 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005567 void VisitInsertionOrder();
5568
Roland Levillain633021e2014-10-01 14:12:25 +01005569 // Visit the graph following dominator tree reverse post-order.
5570 void VisitReversePostOrder();
5571
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005572 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005573
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005574 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005575#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005576 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5577
5578 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5579
5580#undef DECLARE_VISIT_INSTRUCTION
5581
5582 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005583 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005584
5585 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5586};
5587
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005588class HGraphDelegateVisitor : public HGraphVisitor {
5589 public:
5590 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5591 virtual ~HGraphDelegateVisitor() {}
5592
5593 // Visit functions that delegate to to super class.
5594#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005595 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005596
5597 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5598
5599#undef DECLARE_VISIT_INSTRUCTION
5600
5601 private:
5602 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5603};
5604
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005605class HInsertionOrderIterator : public ValueObject {
5606 public:
5607 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5608
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005609 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005610 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005611 void Advance() { ++index_; }
5612
5613 private:
5614 const HGraph& graph_;
5615 size_t index_;
5616
5617 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5618};
5619
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005620class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005621 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005622 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5623 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005624 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005625 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005626
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005627 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5628 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005629 void Advance() { ++index_; }
5630
5631 private:
5632 const HGraph& graph_;
5633 size_t index_;
5634
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005635 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005636};
5637
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005638class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005639 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005640 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005641 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005642 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005643 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005644 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005645
5646 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005647 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005648 void Advance() { --index_; }
5649
5650 private:
5651 const HGraph& graph_;
5652 size_t index_;
5653
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005654 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005655};
5656
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005657class HLinearPostOrderIterator : public ValueObject {
5658 public:
5659 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005660 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005661
5662 bool Done() const { return index_ == 0; }
5663
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005664 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005665
5666 void Advance() {
5667 --index_;
5668 DCHECK_GE(index_, 0U);
5669 }
5670
5671 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005672 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005673 size_t index_;
5674
5675 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5676};
5677
5678class HLinearOrderIterator : public ValueObject {
5679 public:
5680 explicit HLinearOrderIterator(const HGraph& graph)
5681 : order_(graph.GetLinearOrder()), index_(0) {}
5682
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005683 bool Done() const { return index_ == order_.size(); }
5684 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005685 void Advance() { ++index_; }
5686
5687 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005688 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005689 size_t index_;
5690
5691 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5692};
5693
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005694// Iterator over the blocks that art part of the loop. Includes blocks part
5695// of an inner loop. The order in which the blocks are iterated is on their
5696// block id.
5697class HBlocksInLoopIterator : public ValueObject {
5698 public:
5699 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5700 : blocks_in_loop_(info.GetBlocks()),
5701 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5702 index_(0) {
5703 if (!blocks_in_loop_.IsBitSet(index_)) {
5704 Advance();
5705 }
5706 }
5707
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005708 bool Done() const { return index_ == blocks_.size(); }
5709 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005710 void Advance() {
5711 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005712 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005713 if (blocks_in_loop_.IsBitSet(index_)) {
5714 break;
5715 }
5716 }
5717 }
5718
5719 private:
5720 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005721 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005722 size_t index_;
5723
5724 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5725};
5726
Mingyao Yang3584bce2015-05-19 16:01:59 -07005727// Iterator over the blocks that art part of the loop. Includes blocks part
5728// of an inner loop. The order in which the blocks are iterated is reverse
5729// post order.
5730class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5731 public:
5732 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5733 : blocks_in_loop_(info.GetBlocks()),
5734 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5735 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005736 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005737 Advance();
5738 }
5739 }
5740
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005741 bool Done() const { return index_ == blocks_.size(); }
5742 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005743 void Advance() {
5744 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005745 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5746 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005747 break;
5748 }
5749 }
5750 }
5751
5752 private:
5753 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005754 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005755 size_t index_;
5756
5757 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5758};
5759
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005760inline int64_t Int64FromConstant(HConstant* constant) {
5761 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5762 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5763 : constant->AsLongConstant()->GetValue();
5764}
5765
Vladimir Marko58155012015-08-19 12:49:41 +00005766inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5767 // For the purposes of the compiler, the dex files must actually be the same object
5768 // if we want to safely treat them as the same. This is especially important for JIT
5769 // as custom class loaders can open the same underlying file (or memory) multiple
5770 // times and provide different class resolution but no two class loaders should ever
5771 // use the same DexFile object - doing so is an unsupported hack that can lead to
5772 // all sorts of weird failures.
5773 return &lhs == &rhs;
5774}
5775
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005776} // namespace art
5777
5778#endif // ART_COMPILER_OPTIMIZING_NODES_H_