blob: 00820a66e7f3ae5c28e4f86ed5d5599d28c3e7b1 [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)),
Alex Light68289a52015-12-15 17:30:30 -0800182 cached_current_method_(nullptr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100183 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
Alex Light68289a52015-12-15 17:30:30 -0800200 // 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();
204 // 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;
209 // 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();
213 TransformToSsa();
214 in_ssa_form_ = true;
215 return true;
216 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000217
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100218 void ComputeDominanceInformation();
219 void ClearDominanceInformation();
220
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000221 void BuildDominatorTree();
Alex Light68289a52015-12-15 17:30:30 -0800222 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
Alex Light68289a52015-12-15 17:30:30 -0800226 // 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
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100374 ArtMethod* GetArtMethod() const { return art_method_; }
375 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
376
Mark Mendellf6529172015-11-17 11:16:56 -0500377 // Returns an instruction with the opposite boolean value from 'cond'.
378 // The instruction has been inserted into the graph, either as a constant, or
379 // before cursor.
380 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
381
David Brazdil2d7352b2015-04-20 14:52:42 +0100382 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100383 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000384 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100385 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000386
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000387 template <class InstructionType, typename ValueType>
388 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600389 ArenaSafeMap<ValueType, InstructionType*>* cache,
390 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000391 // Try to find an existing constant of the given value.
392 InstructionType* constant = nullptr;
393 auto cached_constant = cache->find(value);
394 if (cached_constant != cache->end()) {
395 constant = cached_constant->second;
396 }
397
398 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100399 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000400 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600401 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000402 cache->Overwrite(value, constant);
403 InsertConstant(constant);
404 }
405 return constant;
406 }
407
David Brazdil8d5b8b22015-03-24 10:51:52 +0000408 void InsertConstant(HConstant* instruction);
409
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000410 // Cache a float constant into the graph. This method should only be
411 // called by the SsaBuilder when creating "equivalent" instructions.
412 void CacheFloatConstant(HFloatConstant* constant);
413
414 // See CacheFloatConstant comment.
415 void CacheDoubleConstant(HDoubleConstant* constant);
416
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000417 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000418
419 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100420 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000421
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100423 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000424
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100425 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100426 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100427
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000428 HBasicBlock* entry_block_;
429 HBasicBlock* exit_block_;
430
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100431 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100432 uint16_t maximum_number_of_out_vregs_;
433
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100434 // The number of virtual registers in this method. Contains the parameters.
435 uint16_t number_of_vregs_;
436
437 // The number of virtual registers used by parameters of this method.
438 uint16_t number_of_in_vregs_;
439
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000440 // Number of vreg size slots that the temporaries use (used in baseline compiler).
441 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100442
Mark Mendell1152c922015-04-24 17:06:35 -0400443 // Has bounds checks. We can totally skip BCE if it's false.
444 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800445
David Brazdil77a48ae2015-09-15 12:34:04 +0000446 // Flag whether there are any try/catch blocks in the graph. We will skip
447 // try/catch-related passes if false.
448 bool has_try_catch_;
449
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000450 // Indicates whether the graph should be compiled in a way that
451 // ensures full debuggability. If false, we can apply more
452 // aggressive optimizations that may limit the level of debugging.
453 const bool debuggable_;
454
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000455 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000456 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000457
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100458 // The dex file from which the method is from.
459 const DexFile& dex_file_;
460
461 // The method index in the dex file.
462 const uint32_t method_idx_;
463
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100464 // If inlined, this encodes how the callee is being invoked.
465 const InvokeType invoke_type_;
466
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100467 // Whether the graph has been transformed to SSA form. Only used
468 // in debug mode to ensure we are not using properties only valid
469 // for non-SSA form (like the number of temporaries).
470 bool in_ssa_form_;
471
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100472 const bool should_generate_constructor_barrier_;
473
Mathieu Chartiere401d142015-04-22 13:56:20 -0700474 const InstructionSet instruction_set_;
475
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000476 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000477 HNullConstant* cached_null_constant_;
478 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000479 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000480 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000481 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000482
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100483 HCurrentMethod* cached_current_method_;
484
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100485 // The ArtMethod this graph is for. Note that for AOT, it may be null,
486 // for example for methods whose declaring class could not be resolved
487 // (such as when the superclass could not be found).
488 ArtMethod* art_method_;
489
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000490 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100491 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000492 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000493 DISALLOW_COPY_AND_ASSIGN(HGraph);
494};
495
Vladimir Markof9f64412015-09-02 14:05:49 +0100496class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000497 public:
498 HLoopInformation(HBasicBlock* header, HGraph* graph)
499 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100500 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100501 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100502 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100503 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
504 back_edges_.reserve(kDefaultNumberOfBackEdges);
505 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100506
507 HBasicBlock* GetHeader() const {
508 return header_;
509 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000510
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000511 void SetHeader(HBasicBlock* block) {
512 header_ = block;
513 }
514
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100515 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
516 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
517 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
518
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000519 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100520 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000521 }
522
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100523 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100524 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100525 }
526
David Brazdil46e2a392015-03-16 17:31:52 +0000527 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100528 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100529 }
530
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000531 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100532 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000533 }
534
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100535 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100536
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100537 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100538 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100539 }
540
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100541 // Returns the lifetime position of the back edge that has the
542 // greatest lifetime position.
543 size_t GetLifetimeEnd() const;
544
545 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100546 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100547 }
548
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100549 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100550 // that is the header dominates the back edge.
551 bool Populate();
552
David Brazdila4b8c212015-05-07 09:59:30 +0100553 // Reanalyzes the loop by removing loop info from its blocks and re-running
554 // Populate(). If there are no back edges left, the loop info is completely
555 // removed as well as its SuspendCheck instruction. It must be run on nested
556 // inner loops first.
557 void Update();
558
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100559 // Returns whether this loop information contains `block`.
560 // Note that this loop information *must* be populated before entering this function.
561 bool Contains(const HBasicBlock& block) const;
562
563 // Returns whether this loop information is an inner loop of `other`.
564 // Note that `other` *must* be populated before entering this function.
565 bool IsIn(const HLoopInformation& other) const;
566
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800567 // Returns true if instruction is not defined within this loop.
568 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700569
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100570 const ArenaBitVector& GetBlocks() const { return blocks_; }
571
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000572 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000573 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000574
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000575 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100576 // Internal recursive implementation of `Populate`.
577 void PopulateRecursive(HBasicBlock* block);
578
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000579 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100580 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100581 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100582 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000583
584 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
585};
586
David Brazdilec16f792015-08-19 15:04:01 +0100587// Stores try/catch information for basic blocks.
588// Note that HGraph is constructed so that catch blocks cannot simultaneously
589// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100590class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100591 public:
592 // Try block information constructor.
593 explicit TryCatchInformation(const HTryBoundary& try_entry)
594 : try_entry_(&try_entry),
595 catch_dex_file_(nullptr),
596 catch_type_index_(DexFile::kDexNoIndex16) {
597 DCHECK(try_entry_ != nullptr);
598 }
599
600 // Catch block information constructor.
601 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
602 : try_entry_(nullptr),
603 catch_dex_file_(&dex_file),
604 catch_type_index_(catch_type_index) {}
605
606 bool IsTryBlock() const { return try_entry_ != nullptr; }
607
608 const HTryBoundary& GetTryEntry() const {
609 DCHECK(IsTryBlock());
610 return *try_entry_;
611 }
612
613 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
614
615 bool IsCatchAllTypeIndex() const {
616 DCHECK(IsCatchBlock());
617 return catch_type_index_ == DexFile::kDexNoIndex16;
618 }
619
620 uint16_t GetCatchTypeIndex() const {
621 DCHECK(IsCatchBlock());
622 return catch_type_index_;
623 }
624
625 const DexFile& GetCatchDexFile() const {
626 DCHECK(IsCatchBlock());
627 return *catch_dex_file_;
628 }
629
630 private:
631 // One of possibly several TryBoundary instructions entering the block's try.
632 // Only set for try blocks.
633 const HTryBoundary* try_entry_;
634
635 // Exception type information. Only set for catch blocks.
636 const DexFile* catch_dex_file_;
637 const uint16_t catch_type_index_;
638};
639
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100640static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100641static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100642
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643// A block in a method. Contains the list of instructions represented
644// as a double linked list. Each block knows its predecessors and
645// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100646
Vladimir Markof9f64412015-09-02 14:05:49 +0100647class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000648 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600649 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000650 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000651 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
652 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000653 loop_information_(nullptr),
654 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000655 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100657 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100658 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000659 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000660 try_catch_information_(nullptr) {
661 predecessors_.reserve(kDefaultNumberOfPredecessors);
662 successors_.reserve(kDefaultNumberOfSuccessors);
663 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
664 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000665
Vladimir Marko60584552015-09-03 13:35:12 +0000666 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100667 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000668 }
669
Vladimir Marko60584552015-09-03 13:35:12 +0000670 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100671 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000672 }
673
David Brazdild26a4112015-11-10 11:07:31 +0000674 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
675 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
676
Vladimir Marko60584552015-09-03 13:35:12 +0000677 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
678 return ContainsElement(successors_, block, start_from);
679 }
680
681 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100682 return dominated_blocks_;
683 }
684
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100685 bool IsEntryBlock() const {
686 return graph_->GetEntryBlock() == this;
687 }
688
689 bool IsExitBlock() const {
690 return graph_->GetExitBlock() == this;
691 }
692
David Brazdil46e2a392015-03-16 17:31:52 +0000693 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000694 bool IsSingleTryBoundary() const;
695
696 // Returns true if this block emits nothing but a jump.
697 bool IsSingleJump() const {
698 HLoopInformation* loop_info = GetLoopInformation();
699 return (IsSingleGoto() || IsSingleTryBoundary())
700 // Back edges generate a suspend check.
701 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
702 }
David Brazdil46e2a392015-03-16 17:31:52 +0000703
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000704 void AddBackEdge(HBasicBlock* back_edge) {
705 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000706 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000707 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100708 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000709 loop_information_->AddBackEdge(back_edge);
710 }
711
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000712 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000713 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000714
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100715 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000716 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600717 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000718
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000719 HBasicBlock* GetDominator() const { return dominator_; }
720 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000721 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
722
723 void RemoveDominatedBlock(HBasicBlock* block) {
724 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100725 }
Vladimir Marko60584552015-09-03 13:35:12 +0000726
727 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
728 ReplaceElement(dominated_blocks_, existing, new_block);
729 }
730
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100731 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000732
733 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100734 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000735 }
736
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100737 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
738 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100739 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100740 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100741 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
742 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000743
744 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000745 successors_.push_back(block);
746 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000747 }
748
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100749 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
750 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100751 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000752 new_block->predecessors_.push_back(this);
753 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000754 }
755
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000756 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
757 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000758 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000759 new_block->successors_.push_back(this);
760 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000761 }
762
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100763 // Insert `this` between `predecessor` and `successor. This method
764 // preserves the indicies, and will update the first edge found between
765 // `predecessor` and `successor`.
766 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
767 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100768 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000769 successor->predecessors_[predecessor_index] = this;
770 predecessor->successors_[successor_index] = this;
771 successors_.push_back(successor);
772 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100773 }
774
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100775 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000776 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100777 }
778
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000779 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000780 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000781 }
782
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100783 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000784 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100785 }
786
787 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000788 predecessors_.push_back(block);
789 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100790 }
791
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100792 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000793 DCHECK_EQ(predecessors_.size(), 2u);
794 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100795 }
796
David Brazdil769c9e52015-04-27 13:54:09 +0100797 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000798 DCHECK_EQ(successors_.size(), 2u);
799 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100800 }
801
David Brazdilfc6a86a2015-06-26 10:33:45 +0000802 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000803 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100804 }
805
David Brazdilfc6a86a2015-06-26 10:33:45 +0000806 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000807 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100808 }
809
David Brazdilfc6a86a2015-06-26 10:33:45 +0000810 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000811 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100812 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813 }
814
815 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000816 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100817 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000818 }
819
820 // Returns whether the first occurrence of `predecessor` in the list of
821 // predecessors is at index `idx`.
822 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100823 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000824 return GetPredecessorIndexOf(predecessor) == idx;
825 }
826
David Brazdild7558da2015-09-22 13:04:14 +0100827 // Create a new block between this block and its predecessors. The new block
828 // is added to the graph, all predecessor edges are relinked to it and an edge
829 // is created to `this`. Returns the new empty block. Reverse post order or
830 // loop and try/catch information are not updated.
831 HBasicBlock* CreateImmediateDominator();
832
David Brazdilfc6a86a2015-06-26 10:33:45 +0000833 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100834 // created, latter block. Note that this method will add the block to the
835 // graph, create a Goto at the end of the former block and will create an edge
836 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100837 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000838 HBasicBlock* SplitBefore(HInstruction* cursor);
839
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000840 // Split the block into two blocks just after `cursor`. Returns the newly
841 // created block. Note that this method just updates raw block information,
842 // like predecessors, successors, dominators, and instruction list. It does not
843 // update the graph, reverse post order, loop information, nor make sure the
844 // blocks are consistent (for example ending with a control flow instruction).
845 HBasicBlock* SplitAfter(HInstruction* cursor);
846
David Brazdil9bc43612015-11-05 21:25:24 +0000847 // Split catch block into two blocks after the original move-exception bytecode
848 // instruction, or at the beginning if not present. Returns the newly created,
849 // latter block, or nullptr if such block could not be created (must be dead
850 // in that case). Note that this method just updates raw block information,
851 // like predecessors, successors, dominators, and instruction list. It does not
852 // update the graph, reverse post order, loop information, nor make sure the
853 // blocks are consistent (for example ending with a control flow instruction).
854 HBasicBlock* SplitCatchBlockAfterMoveException();
855
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000856 // Merge `other` at the end of `this`. Successors and dominated blocks of
857 // `other` are changed to be successors and dominated blocks of `this`. Note
858 // that this method does not update the graph, reverse post order, loop
859 // information, nor make sure the blocks are consistent (for example ending
860 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100861 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000862
863 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
864 // of `this` are moved to `other`.
865 // Note that this method does not update the graph, reverse post order, loop
866 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000867 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000868 void ReplaceWith(HBasicBlock* other);
869
David Brazdil2d7352b2015-04-20 14:52:42 +0100870 // Merge `other` at the end of `this`. This method updates loops, reverse post
871 // order, links to predecessors, successors, dominators and deletes the block
872 // from the graph. The two blocks must be successive, i.e. `this` the only
873 // predecessor of `other` and vice versa.
874 void MergeWith(HBasicBlock* other);
875
876 // Disconnects `this` from all its predecessors, successors and dominator,
877 // removes it from all loops it is included in and eventually from the graph.
878 // The block must not dominate any other block. Predecessors and successors
879 // are safely updated.
880 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000881
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000882 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100883 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100884 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100885 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100886 // Replace instruction `initial` with `replacement` within this block.
887 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
888 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100889 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100890 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000891 // RemoveInstruction and RemovePhi delete a given instruction from the respective
892 // instruction list. With 'ensure_safety' set to true, it verifies that the
893 // instruction is not in use and removes it from the use lists of its inputs.
894 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
895 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100896 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100897
898 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100899 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100900 }
901
Roland Levillain6b879dd2014-09-22 17:13:44 +0100902 bool IsLoopPreHeaderFirstPredecessor() const {
903 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100904 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100905 }
906
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100907 HLoopInformation* GetLoopInformation() const {
908 return loop_information_;
909 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000911 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100912 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000913 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100914 void SetInLoop(HLoopInformation* info) {
915 if (IsLoopHeader()) {
916 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100917 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100918 loop_information_ = info;
919 } else if (loop_information_->Contains(*info->GetHeader())) {
920 // Block is currently part of an outer loop. Make it part of this inner loop.
921 // Note that a non loop header having a loop information means this loop information
922 // has already been populated
923 loop_information_ = info;
924 } else {
925 // Block is part of an inner loop. Do not update the loop information.
926 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
927 // at this point, because this method is being called while populating `info`.
928 }
929 }
930
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000931 // Raw update of the loop information.
932 void SetLoopInformation(HLoopInformation* info) {
933 loop_information_ = info;
934 }
935
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100936 bool IsInLoop() const { return loop_information_ != nullptr; }
937
David Brazdilec16f792015-08-19 15:04:01 +0100938 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
939
940 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
941 try_catch_information_ = try_catch_information;
942 }
943
944 bool IsTryBlock() const {
945 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
946 }
947
948 bool IsCatchBlock() const {
949 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
950 }
David Brazdilffee3d32015-07-06 11:48:53 +0100951
952 // Returns the try entry that this block's successors should have. They will
953 // be in the same try, unless the block ends in a try boundary. In that case,
954 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100955 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100956
David Brazdild7558da2015-09-22 13:04:14 +0100957 bool HasThrowingInstructions() const;
958
David Brazdila4b8c212015-05-07 09:59:30 +0100959 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100960 bool Dominates(HBasicBlock* block) const;
961
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100962 size_t GetLifetimeStart() const { return lifetime_start_; }
963 size_t GetLifetimeEnd() const { return lifetime_end_; }
964
965 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
966 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
967
David Brazdil8d5b8b22015-03-24 10:51:52 +0000968 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000969 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100970 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000971 bool HasSinglePhi() const;
972
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000973 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000974 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000975 ArenaVector<HBasicBlock*> predecessors_;
976 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100977 HInstructionList instructions_;
978 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000979 HLoopInformation* loop_information_;
980 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000981 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100982 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100983 // The dex program counter of the first instruction of this block.
984 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100985 size_t lifetime_start_;
986 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100987 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100988
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000989 friend class HGraph;
990 friend class HInstruction;
991
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000992 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
993};
994
David Brazdilb2bd1c52015-03-25 11:17:37 +0000995// Iterates over the LoopInformation of all loops which contain 'block'
996// from the innermost to the outermost.
997class HLoopInformationOutwardIterator : public ValueObject {
998 public:
999 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1000 : current_(block.GetLoopInformation()) {}
1001
1002 bool Done() const { return current_ == nullptr; }
1003
1004 void Advance() {
1005 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001006 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001007 }
1008
1009 HLoopInformation* Current() const {
1010 DCHECK(!Done());
1011 return current_;
1012 }
1013
1014 private:
1015 HLoopInformation* current_;
1016
1017 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1018};
1019
Alexandre Ramesef20f712015-06-09 10:29:30 +01001020#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001021 M(Above, Condition) \
1022 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001023 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001024 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001025 M(ArrayGet, Instruction) \
1026 M(ArrayLength, Instruction) \
1027 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001028 M(Below, Condition) \
1029 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001030 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001031 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001032 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001033 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001034 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001035 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001036 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001037 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001038 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001039 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001040 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001041 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001042 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001043 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001044 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001045 M(FloatConstant, Constant) \
1046 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001047 M(GreaterThan, Condition) \
1048 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001049 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001050 M(InstanceFieldGet, Instruction) \
1051 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001052 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001053 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001054 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001055 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001056 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001057 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001058 M(LessThan, Condition) \
1059 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001060 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001061 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001062 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001063 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001064 M(Local, Instruction) \
1065 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001066 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001067 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001068 M(Mul, BinaryOperation) \
1069 M(Neg, UnaryOperation) \
1070 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001071 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001072 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001073 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001074 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001075 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001076 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001077 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001078 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001079 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001080 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001081 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001082 M(Return, Instruction) \
1083 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001084 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001085 M(Shl, BinaryOperation) \
1086 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001087 M(StaticFieldGet, Instruction) \
1088 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001089 M(UnresolvedInstanceFieldGet, Instruction) \
1090 M(UnresolvedInstanceFieldSet, Instruction) \
1091 M(UnresolvedStaticFieldGet, Instruction) \
1092 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001093 M(StoreLocal, Instruction) \
1094 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001095 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001096 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001097 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001098 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001099 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001100 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001101 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001102
Vladimir Markob4536b72015-11-24 13:45:23 +00001103#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001104#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001105#else
1106#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1107 M(ArmDexCacheArraysBase, Instruction)
1108#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001109
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001110#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001111#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001112#else
1113#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001114 M(Arm64DataProcWithShifterOp, Instruction) \
Alexandre Rames418318f2015-11-20 15:55:47 +00001115 M(Arm64IntermediateAddress, Instruction) \
1116 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001117#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001118
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001119#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1120
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001121#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1122
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001123#ifndef ART_ENABLE_CODEGEN_x86
1124#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1125#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001126#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1127 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001128 M(X86LoadFromConstantTable, Instruction) \
1129 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001130#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001131
1132#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1133
1134#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1135 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1136 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1137 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001138 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001139 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001140 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1141 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1142
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001143#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1144 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001145 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001146 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001147 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001148 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001149
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001150#define FOR_EACH_INSTRUCTION(M) \
1151 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1152 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1153
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001154#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001155FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1156#undef FORWARD_DECLARATION
1157
Roland Levillainccc07a92014-09-16 14:48:16 +01001158#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001159 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001160 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001161 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001162 return other->Is##type(); \
1163 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001164 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001165
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001166#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1167 bool Is##type() const { return As##type() != nullptr; } \
1168 const H##type* As##type() const { return this; } \
1169 H##type* As##type() { return this; }
1170
David Brazdiled596192015-01-23 10:39:45 +00001171template <typename T> class HUseList;
1172
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001173template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001174class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001175 public:
David Brazdiled596192015-01-23 10:39:45 +00001176 HUseListNode* GetPrevious() const { return prev_; }
1177 HUseListNode* GetNext() const { return next_; }
1178 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001179 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001180 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001181
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001182 private:
David Brazdiled596192015-01-23 10:39:45 +00001183 HUseListNode(T user, size_t index)
1184 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1185
1186 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001187 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001188 HUseListNode<T>* prev_;
1189 HUseListNode<T>* next_;
1190
1191 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001192
1193 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1194};
1195
David Brazdiled596192015-01-23 10:39:45 +00001196template <typename T>
1197class HUseList : public ValueObject {
1198 public:
1199 HUseList() : first_(nullptr) {}
1200
1201 void Clear() {
1202 first_ = nullptr;
1203 }
1204
1205 // Adds a new entry at the beginning of the use list and returns
1206 // the newly created node.
1207 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001208 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001209 if (IsEmpty()) {
1210 first_ = new_node;
1211 } else {
1212 first_->prev_ = new_node;
1213 new_node->next_ = first_;
1214 first_ = new_node;
1215 }
1216 return new_node;
1217 }
1218
1219 HUseListNode<T>* GetFirst() const {
1220 return first_;
1221 }
1222
1223 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001224 DCHECK(node != nullptr);
1225 DCHECK(Contains(node));
1226
David Brazdiled596192015-01-23 10:39:45 +00001227 if (node->prev_ != nullptr) {
1228 node->prev_->next_ = node->next_;
1229 }
1230 if (node->next_ != nullptr) {
1231 node->next_->prev_ = node->prev_;
1232 }
1233 if (node == first_) {
1234 first_ = node->next_;
1235 }
1236 }
1237
David Brazdil1abb4192015-02-17 18:33:36 +00001238 bool Contains(const HUseListNode<T>* node) const {
1239 if (node == nullptr) {
1240 return false;
1241 }
1242 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1243 if (current == node) {
1244 return true;
1245 }
1246 }
1247 return false;
1248 }
1249
David Brazdiled596192015-01-23 10:39:45 +00001250 bool IsEmpty() const {
1251 return first_ == nullptr;
1252 }
1253
1254 bool HasOnlyOneUse() const {
1255 return first_ != nullptr && first_->next_ == nullptr;
1256 }
1257
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001258 size_t SizeSlow() const {
1259 size_t count = 0;
1260 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1261 ++count;
1262 }
1263 return count;
1264 }
1265
David Brazdiled596192015-01-23 10:39:45 +00001266 private:
1267 HUseListNode<T>* first_;
1268};
1269
1270template<typename T>
1271class HUseIterator : public ValueObject {
1272 public:
1273 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1274
1275 bool Done() const { return current_ == nullptr; }
1276
1277 void Advance() {
1278 DCHECK(!Done());
1279 current_ = current_->GetNext();
1280 }
1281
1282 HUseListNode<T>* Current() const {
1283 DCHECK(!Done());
1284 return current_;
1285 }
1286
1287 private:
1288 HUseListNode<T>* current_;
1289
1290 friend class HValue;
1291};
1292
David Brazdil1abb4192015-02-17 18:33:36 +00001293// This class is used by HEnvironment and HInstruction classes to record the
1294// instructions they use and pointers to the corresponding HUseListNodes kept
1295// by the used instructions.
1296template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001297class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001298 public:
1299 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1300 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1301
1302 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1303 : instruction_(old_record.instruction_), use_node_(use_node) {
1304 DCHECK(instruction_ != nullptr);
1305 DCHECK(use_node_ != nullptr);
1306 DCHECK(old_record.use_node_ == nullptr);
1307 }
1308
1309 HInstruction* GetInstruction() const { return instruction_; }
1310 HUseListNode<T>* GetUseNode() const { return use_node_; }
1311
1312 private:
1313 // Instruction used by the user.
1314 HInstruction* instruction_;
1315
1316 // Corresponding entry in the use list kept by 'instruction_'.
1317 HUseListNode<T>* use_node_;
1318};
1319
Aart Bik854a02b2015-07-14 16:07:00 -07001320/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001321 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001322 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001323 * For write/read dependences on fields/arrays, the dependence analysis uses
1324 * type disambiguation (e.g. a float field write cannot modify the value of an
1325 * integer field read) and the access type (e.g. a reference array write cannot
1326 * modify the value of a reference field read [although it may modify the
1327 * reference fetch prior to reading the field, which is represented by its own
1328 * write/read dependence]). The analysis makes conservative points-to
1329 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1330 * the same, and any reference read depends on any reference read without
1331 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001332 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001333 * The internal representation uses 38-bit and is described in the table below.
1334 * The first line indicates the side effect, and for field/array accesses the
1335 * second line indicates the type of the access (in the order of the
1336 * Primitive::Type enum).
1337 * The two numbered lines below indicate the bit position in the bitfield (read
1338 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001339 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001340 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1341 * +-------------+---------+---------+--------------+---------+---------+
1342 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1343 * | 3 |333333322|222222221| 1 |111111110|000000000|
1344 * | 7 |654321098|765432109| 8 |765432109|876543210|
1345 *
1346 * Note that, to ease the implementation, 'changes' bits are least significant
1347 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001348 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001349class SideEffects : public ValueObject {
1350 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001351 SideEffects() : flags_(0) {}
1352
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001353 static SideEffects None() {
1354 return SideEffects(0);
1355 }
1356
1357 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001358 return SideEffects(kAllChangeBits | kAllDependOnBits);
1359 }
1360
1361 static SideEffects AllChanges() {
1362 return SideEffects(kAllChangeBits);
1363 }
1364
1365 static SideEffects AllDependencies() {
1366 return SideEffects(kAllDependOnBits);
1367 }
1368
1369 static SideEffects AllExceptGCDependency() {
1370 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1371 }
1372
1373 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001374 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001375 }
1376
Aart Bik34c3ba92015-07-20 14:08:59 -07001377 static SideEffects AllWrites() {
1378 return SideEffects(kAllWrites);
1379 }
1380
1381 static SideEffects AllReads() {
1382 return SideEffects(kAllReads);
1383 }
1384
1385 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1386 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001387 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001388 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001389 }
1390
Aart Bik854a02b2015-07-14 16:07:00 -07001391 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1392 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001393 }
1394
Aart Bik34c3ba92015-07-20 14:08:59 -07001395 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1396 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001397 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001398 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001399 }
1400
1401 static SideEffects ArrayReadOfType(Primitive::Type type) {
1402 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1403 }
1404
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001405 static SideEffects CanTriggerGC() {
1406 return SideEffects(1ULL << kCanTriggerGCBit);
1407 }
1408
1409 static SideEffects DependsOnGC() {
1410 return SideEffects(1ULL << kDependsOnGCBit);
1411 }
1412
Aart Bik854a02b2015-07-14 16:07:00 -07001413 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001414 SideEffects Union(SideEffects other) const {
1415 return SideEffects(flags_ | other.flags_);
1416 }
1417
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001418 SideEffects Exclusion(SideEffects other) const {
1419 return SideEffects(flags_ & ~other.flags_);
1420 }
1421
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001422 void Add(SideEffects other) {
1423 flags_ |= other.flags_;
1424 }
1425
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001426 bool Includes(SideEffects other) const {
1427 return (other.flags_ & flags_) == other.flags_;
1428 }
1429
1430 bool HasSideEffects() const {
1431 return (flags_ & kAllChangeBits);
1432 }
1433
1434 bool HasDependencies() const {
1435 return (flags_ & kAllDependOnBits);
1436 }
1437
1438 // Returns true if there are no side effects or dependencies.
1439 bool DoesNothing() const {
1440 return flags_ == 0;
1441 }
1442
Aart Bik854a02b2015-07-14 16:07:00 -07001443 // Returns true if something is written.
1444 bool DoesAnyWrite() const {
1445 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001446 }
1447
Aart Bik854a02b2015-07-14 16:07:00 -07001448 // Returns true if something is read.
1449 bool DoesAnyRead() const {
1450 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001451 }
1452
Aart Bik854a02b2015-07-14 16:07:00 -07001453 // Returns true if potentially everything is written and read
1454 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001455 bool DoesAllReadWrite() const {
1456 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1457 }
1458
Aart Bik854a02b2015-07-14 16:07:00 -07001459 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001460 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001461 }
1462
Roland Levillain0d5a2812015-11-13 10:07:31 +00001463 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001464 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001465 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1466 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001467 }
1468
1469 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001470 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001471 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001472 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001473 for (int s = kLastBit; s >= 0; s--) {
1474 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1475 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1476 // This is a bit for the GC side effect.
1477 if (current_bit_is_set) {
1478 flags += "GC";
1479 }
Aart Bik854a02b2015-07-14 16:07:00 -07001480 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001481 } else {
1482 // This is a bit for the array/field analysis.
1483 // The underscore character stands for the 'can trigger GC' bit.
1484 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1485 if (current_bit_is_set) {
1486 flags += kDebug[s];
1487 }
1488 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1489 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1490 flags += "|";
1491 }
1492 }
Aart Bik854a02b2015-07-14 16:07:00 -07001493 }
1494 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001495 }
1496
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001497 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001498
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001499 private:
1500 static constexpr int kFieldArrayAnalysisBits = 9;
1501
1502 static constexpr int kFieldWriteOffset = 0;
1503 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1504 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1505 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1506
1507 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1508
1509 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1510 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1511 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1512 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1513
1514 static constexpr int kLastBit = kDependsOnGCBit;
1515 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1516
1517 // Aliases.
1518
1519 static_assert(kChangeBits == kDependOnBits,
1520 "the 'change' bits should match the 'depend on' bits.");
1521
1522 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1523 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1524 static constexpr uint64_t kAllWrites =
1525 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1526 static constexpr uint64_t kAllReads =
1527 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001528
Aart Bik854a02b2015-07-14 16:07:00 -07001529 // Work around the fact that HIR aliases I/F and J/D.
1530 // TODO: remove this interceptor once HIR types are clean
1531 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1532 switch (type) {
1533 case Primitive::kPrimInt:
1534 case Primitive::kPrimFloat:
1535 return TypeFlag(Primitive::kPrimInt, offset) |
1536 TypeFlag(Primitive::kPrimFloat, offset);
1537 case Primitive::kPrimLong:
1538 case Primitive::kPrimDouble:
1539 return TypeFlag(Primitive::kPrimLong, offset) |
1540 TypeFlag(Primitive::kPrimDouble, offset);
1541 default:
1542 return TypeFlag(type, offset);
1543 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001544 }
1545
Aart Bik854a02b2015-07-14 16:07:00 -07001546 // Translates type to bit flag.
1547 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1548 CHECK_NE(type, Primitive::kPrimVoid);
1549 const uint64_t one = 1;
1550 const int shift = type; // 0-based consecutive enum
1551 DCHECK_LE(kFieldWriteOffset, shift);
1552 DCHECK_LT(shift, kArrayWriteOffset);
1553 return one << (type + offset);
1554 }
1555
1556 // Private constructor on direct flags value.
1557 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1558
1559 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001560};
1561
David Brazdiled596192015-01-23 10:39:45 +00001562// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001563class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001564 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001565 HEnvironment(ArenaAllocator* arena,
1566 size_t number_of_vregs,
1567 const DexFile& dex_file,
1568 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001569 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001570 InvokeType invoke_type,
1571 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001572 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1573 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001574 parent_(nullptr),
1575 dex_file_(dex_file),
1576 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001577 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001578 invoke_type_(invoke_type),
1579 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001580 }
1581
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001582 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001583 : HEnvironment(arena,
1584 to_copy.Size(),
1585 to_copy.GetDexFile(),
1586 to_copy.GetMethodIdx(),
1587 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001588 to_copy.GetInvokeType(),
1589 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001590
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001591 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001592 if (parent_ != nullptr) {
1593 parent_->SetAndCopyParentChain(allocator, parent);
1594 } else {
1595 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1596 parent_->CopyFrom(parent);
1597 if (parent->GetParent() != nullptr) {
1598 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1599 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001600 }
David Brazdiled596192015-01-23 10:39:45 +00001601 }
1602
Vladimir Marko71bf8092015-09-15 15:33:14 +01001603 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001604 void CopyFrom(HEnvironment* environment);
1605
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001606 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1607 // input to the loop phi instead. This is for inserting instructions that
1608 // require an environment (like HDeoptimization) in the loop pre-header.
1609 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001610
1611 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001612 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001613 }
1614
1615 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001616 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001617 }
1618
David Brazdil1abb4192015-02-17 18:33:36 +00001619 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001620
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001621 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001622
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001623 HEnvironment* GetParent() const { return parent_; }
1624
1625 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001626 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001627 }
1628
1629 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001630 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001631 }
1632
1633 uint32_t GetDexPc() const {
1634 return dex_pc_;
1635 }
1636
1637 uint32_t GetMethodIdx() const {
1638 return method_idx_;
1639 }
1640
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001641 InvokeType GetInvokeType() const {
1642 return invoke_type_;
1643 }
1644
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001645 const DexFile& GetDexFile() const {
1646 return dex_file_;
1647 }
1648
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001649 HInstruction* GetHolder() const {
1650 return holder_;
1651 }
1652
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001653
1654 bool IsFromInlinedInvoke() const {
1655 return GetParent() != nullptr;
1656 }
1657
David Brazdiled596192015-01-23 10:39:45 +00001658 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001659 // Record instructions' use entries of this environment for constant-time removal.
1660 // It should only be called by HInstruction when a new environment use is added.
1661 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1662 DCHECK(env_use->GetUser() == this);
1663 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001664 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001665 }
David Brazdiled596192015-01-23 10:39:45 +00001666
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001667 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1668 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001669 HEnvironment* parent_;
1670 const DexFile& dex_file_;
1671 const uint32_t method_idx_;
1672 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001673 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001674
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001675 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001676 HInstruction* const holder_;
1677
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001678 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001679
1680 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1681};
1682
Alex Light68289a52015-12-15 17:30:30 -08001683class ReferenceTypeInfo : ValueObject {
1684 public:
1685 typedef Handle<mirror::Class> TypeHandle;
1686
1687 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1688 // The constructor will check that the type_handle is valid.
1689 return ReferenceTypeInfo(type_handle, is_exact);
1690 }
1691
1692 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1693
1694 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1695 return handle.GetReference() != nullptr;
1696 }
1697
1698 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1699 return IsValidHandle(type_handle_);
1700 }
1701
1702 bool IsExact() const { return is_exact_; }
1703
1704 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1705 DCHECK(IsValid());
1706 return GetTypeHandle()->IsObjectClass();
1707 }
1708
1709 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1710 DCHECK(IsValid());
1711 return GetTypeHandle()->IsStringClass();
1712 }
1713
1714 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1715 DCHECK(IsValid());
1716 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1717 }
1718
1719 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
1720 DCHECK(IsValid());
1721 return GetTypeHandle()->IsInterface();
1722 }
1723
1724 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1725 DCHECK(IsValid());
1726 return GetTypeHandle()->IsArrayClass();
1727 }
1728
1729 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1730 DCHECK(IsValid());
1731 return GetTypeHandle()->IsPrimitiveArray();
1732 }
1733
1734 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1735 DCHECK(IsValid());
1736 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1737 }
1738
1739 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1740 DCHECK(IsValid());
1741 if (!IsExact()) return false;
1742 if (!IsArrayClass()) return false;
1743 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1744 }
1745
1746 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1747 DCHECK(IsValid());
1748 if (!IsExact()) return false;
1749 if (!IsArrayClass()) return false;
1750 if (!rti.IsArrayClass()) return false;
1751 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1752 rti.GetTypeHandle()->GetComponentType());
1753 }
1754
1755 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1756
1757 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1758 DCHECK(IsValid());
1759 DCHECK(rti.IsValid());
1760 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1761 }
1762
1763 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1764 DCHECK(IsValid());
1765 DCHECK(rti.IsValid());
1766 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
1767 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1768 }
1769
1770 // Returns true if the type information provide the same amount of details.
1771 // Note that it does not mean that the instructions have the same actual type
1772 // (because the type can be the result of a merge).
1773 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
1774 if (!IsValid() && !rti.IsValid()) {
1775 // Invalid types are equal.
1776 return true;
1777 }
1778 if (!IsValid() || !rti.IsValid()) {
1779 // One is valid, the other not.
1780 return false;
1781 }
1782 return IsExact() == rti.IsExact()
1783 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1784 }
1785
1786 private:
1787 ReferenceTypeInfo();
1788 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
1789
1790 // The class of the object.
1791 TypeHandle type_handle_;
1792 // Whether or not the type is exact or a superclass of the actual type.
1793 // Whether or not we have any information about this type.
1794 bool is_exact_;
1795};
1796
1797std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1798
Vladimir Markof9f64412015-09-02 14:05:49 +01001799class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001800 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001801 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001802 : previous_(nullptr),
1803 next_(nullptr),
1804 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001805 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001806 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001807 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001808 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001809 locations_(nullptr),
1810 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001811 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001812 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001813 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001814
Dave Allison20dfc792014-06-16 20:44:29 -07001815 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001816
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001817#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001818 enum InstructionKind {
1819 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1820 };
1821#undef DECLARE_KIND
1822
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001823 HInstruction* GetNext() const { return next_; }
1824 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001825
Calin Juravle77520bc2015-01-12 18:45:46 +00001826 HInstruction* GetNextDisregardingMoves() const;
1827 HInstruction* GetPreviousDisregardingMoves() const;
1828
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001829 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001830 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001831 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001832 bool IsInBlock() const { return block_ != nullptr; }
1833 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001834 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001835
Roland Levillain6b879dd2014-09-22 17:13:44 +01001836 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001837 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001838
1839 virtual void Accept(HGraphVisitor* visitor) = 0;
1840 virtual const char* DebugName() const = 0;
1841
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001842 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001843 void SetRawInputAt(size_t index, HInstruction* input) {
1844 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1845 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001846
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001847 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001848
1849 uint32_t GetDexPc() const { return dex_pc_; }
1850
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001851 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001852
Roland Levillaine161a2a2014-10-03 12:45:18 +01001853 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001854 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001855
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001856 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001857 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001858
Calin Juravle10e244f2015-01-26 18:54:32 +00001859 // Does not apply for all instructions, but having this at top level greatly
1860 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001861 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001862 virtual bool CanBeNull() const {
1863 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1864 return true;
1865 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001866
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001867 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001868 return false;
1869 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001870
Calin Juravle2e768302015-07-28 14:41:11 +00001871 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001872
Calin Juravle61d544b2015-02-23 16:46:57 +00001873 ReferenceTypeInfo GetReferenceTypeInfo() const {
1874 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1875 return reference_type_info_;
1876 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001877
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001878 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001879 DCHECK(user != nullptr);
1880 HUseListNode<HInstruction*>* use =
1881 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1882 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001883 }
1884
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001885 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001886 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001887 HUseListNode<HEnvironment*>* env_use =
1888 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1889 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001890 }
1891
David Brazdil1abb4192015-02-17 18:33:36 +00001892 void RemoveAsUserOfInput(size_t input) {
1893 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1894 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1895 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001896
David Brazdil1abb4192015-02-17 18:33:36 +00001897 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1898 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001899
David Brazdiled596192015-01-23 10:39:45 +00001900 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1901 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001902 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001903 bool HasOnlyOneNonEnvironmentUse() const {
1904 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1905 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001906
Roland Levillain6c82d402014-10-13 16:10:27 +01001907 // Does this instruction strictly dominate `other_instruction`?
1908 // Returns false if this instruction and `other_instruction` are the same.
1909 // Aborts if this instruction and `other_instruction` are both phis.
1910 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001911
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001912 int GetId() const { return id_; }
1913 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001914
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001915 int GetSsaIndex() const { return ssa_index_; }
1916 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1917 bool HasSsaIndex() const { return ssa_index_ != -1; }
1918
1919 bool HasEnvironment() const { return environment_ != nullptr; }
1920 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001921 // Set the `environment_` field. Raw because this method does not
1922 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001923 void SetRawEnvironment(HEnvironment* environment) {
1924 DCHECK(environment_ == nullptr);
1925 DCHECK_EQ(environment->GetHolder(), this);
1926 environment_ = environment;
1927 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001928
1929 // Set the environment of this instruction, copying it from `environment`. While
1930 // copying, the uses lists are being updated.
1931 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001932 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001933 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001934 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001935 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001936 if (environment->GetParent() != nullptr) {
1937 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1938 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001939 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001940
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001941 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1942 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001943 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001944 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001945 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001946 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001947 if (environment->GetParent() != nullptr) {
1948 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1949 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001950 }
1951
Nicolas Geoffray39468442014-09-02 15:17:15 +01001952 // Returns the number of entries in the environment. Typically, that is the
1953 // number of dex registers in a method. It could be more in case of inlining.
1954 size_t EnvironmentSize() const;
1955
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001956 LocationSummary* GetLocations() const { return locations_; }
1957 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001958
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001959 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001960 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001961
Alexandre Rames188d4312015-04-09 18:30:21 +01001962 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1963 // uses of this instruction by `other` are *not* updated.
1964 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1965 ReplaceWith(other);
1966 other->ReplaceInput(this, use_index);
1967 }
1968
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001969 // Move `this` instruction before `cursor`.
1970 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001971
Vladimir Markofb337ea2015-11-25 15:25:10 +00001972 // Move `this` before its first user and out of any loops. If there is no
1973 // out-of-loop user that dominates all other users, move the instruction
1974 // to the end of the out-of-loop common dominator of the user's blocks.
1975 //
1976 // This can be used only on non-throwing instructions with no side effects that
1977 // have at least one use but no environment uses.
1978 void MoveBeforeFirstUserAndOutOfLoops();
1979
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001980#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001981 bool Is##type() const; \
1982 const H##type* As##type() const; \
1983 H##type* As##type();
1984
1985 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1986#undef INSTRUCTION_TYPE_CHECK
1987
1988#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001989 bool Is##type() const { return (As##type() != nullptr); } \
1990 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001991 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001992 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001993#undef INSTRUCTION_TYPE_CHECK
1994
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001995 // Returns whether the instruction can be moved within the graph.
1996 virtual bool CanBeMoved() const { return false; }
1997
1998 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001999 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002000 return false;
2001 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002002
2003 // Returns whether any data encoded in the two instructions is equal.
2004 // This method does not look at the inputs. Both instructions must be
2005 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002006 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002007 return false;
2008 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002009
2010 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002011 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002012 // 2) Their inputs are identical.
2013 bool Equals(HInstruction* other) const;
2014
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002015 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2016 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2017 // the virtual function because the __attribute__((__pure__)) doesn't really
2018 // apply the strong requirement for virtual functions, preventing optimizations.
2019 InstructionKind GetKind() const PURE;
2020 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002021
2022 virtual size_t ComputeHashCode() const {
2023 size_t result = GetKind();
2024 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2025 result = (result * 31) + InputAt(i)->GetId();
2026 }
2027 return result;
2028 }
2029
2030 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002031 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002032
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002033 size_t GetLifetimePosition() const { return lifetime_position_; }
2034 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2035 LiveInterval* GetLiveInterval() const { return live_interval_; }
2036 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2037 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2038
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002039 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2040
2041 // Returns whether the code generation of the instruction will require to have access
2042 // to the current method. Such instructions are:
2043 // (1): Instructions that require an environment, as calling the runtime requires
2044 // to walk the stack and have the current method stored at a specific stack address.
2045 // (2): Object literals like classes and strings, that are loaded from the dex cache
2046 // fields of the current method.
2047 bool NeedsCurrentMethod() const {
2048 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2049 }
2050
Vladimir Markodc151b22015-10-15 18:02:30 +01002051 // Returns whether the code generation of the instruction will require to have access
2052 // to the dex cache of the current method's declaring class via the current method.
2053 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002054
Mark Mendellc4701932015-04-10 13:18:51 -04002055 // Does this instruction have any use in an environment before
2056 // control flow hits 'other'?
2057 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2058
2059 // Remove all references to environment uses of this instruction.
2060 // The caller must ensure that this is safe to do.
2061 void RemoveEnvironmentUsers();
2062
David Brazdil1abb4192015-02-17 18:33:36 +00002063 protected:
2064 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2065 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2066
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002067 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002068 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2069
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002070 HInstruction* previous_;
2071 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002072 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002073 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002074
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002075 // An instruction gets an id when it is added to the graph.
2076 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002077 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002078 int id_;
2079
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002080 // When doing liveness analysis, instructions that have uses get an SSA index.
2081 int ssa_index_;
2082
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002083 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002084 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002085
2086 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002087 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002088
Nicolas Geoffray39468442014-09-02 15:17:15 +01002089 // The environment associated with this instruction. Not null if the instruction
2090 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002091 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002092
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002093 // Set by the code generator.
2094 LocationSummary* locations_;
2095
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002096 // Set by the liveness analysis.
2097 LiveInterval* live_interval_;
2098
2099 // Set by the liveness analysis, this is the position in a linear
2100 // order of blocks where this instruction's live interval start.
2101 size_t lifetime_position_;
2102
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002103 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002104
Calin Juravleacf735c2015-02-12 15:25:22 +00002105 // TODO: for primitive types this should be marked as invalid.
2106 ReferenceTypeInfo reference_type_info_;
2107
David Brazdil1abb4192015-02-17 18:33:36 +00002108 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002109 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002110 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002111 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002112 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002113
2114 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2115};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002116std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002117
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002118class HInputIterator : public ValueObject {
2119 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002120 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002121
2122 bool Done() const { return index_ == instruction_->InputCount(); }
2123 HInstruction* Current() const { return instruction_->InputAt(index_); }
2124 void Advance() { index_++; }
2125
2126 private:
2127 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002128 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002129
2130 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2131};
2132
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002133class HInstructionIterator : public ValueObject {
2134 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002135 explicit HInstructionIterator(const HInstructionList& instructions)
2136 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002137 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002138 }
2139
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002140 bool Done() const { return instruction_ == nullptr; }
2141 HInstruction* Current() const { return instruction_; }
2142 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002143 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002144 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002145 }
2146
2147 private:
2148 HInstruction* instruction_;
2149 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002150
2151 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002152};
2153
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002154class HBackwardInstructionIterator : public ValueObject {
2155 public:
2156 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2157 : instruction_(instructions.last_instruction_) {
2158 next_ = Done() ? nullptr : instruction_->GetPrevious();
2159 }
2160
2161 bool Done() const { return instruction_ == nullptr; }
2162 HInstruction* Current() const { return instruction_; }
2163 void Advance() {
2164 instruction_ = next_;
2165 next_ = Done() ? nullptr : instruction_->GetPrevious();
2166 }
2167
2168 private:
2169 HInstruction* instruction_;
2170 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002171
2172 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002173};
2174
Vladimir Markof9f64412015-09-02 14:05:49 +01002175template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002176class HTemplateInstruction: public HInstruction {
2177 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002178 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002179 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002180 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002181
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002182 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002183
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002184 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002185 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2186 DCHECK_LT(i, N);
2187 return inputs_[i];
2188 }
David Brazdil1abb4192015-02-17 18:33:36 +00002189
2190 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002191 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002192 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002193 }
2194
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002195 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002196 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002197
2198 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002199};
2200
Vladimir Markof9f64412015-09-02 14:05:49 +01002201// HTemplateInstruction specialization for N=0.
2202template<>
2203class HTemplateInstruction<0>: public HInstruction {
2204 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002205 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002206 : HInstruction(side_effects, dex_pc) {}
2207
Vladimir Markof9f64412015-09-02 14:05:49 +01002208 virtual ~HTemplateInstruction() {}
2209
2210 size_t InputCount() const OVERRIDE { return 0; }
2211
2212 protected:
2213 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2214 LOG(FATAL) << "Unreachable";
2215 UNREACHABLE();
2216 }
2217
2218 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2219 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2220 LOG(FATAL) << "Unreachable";
2221 UNREACHABLE();
2222 }
2223
2224 private:
2225 friend class SsaBuilder;
2226};
2227
Dave Allison20dfc792014-06-16 20:44:29 -07002228template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002229class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002230 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002231 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002232 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002233 virtual ~HExpression() {}
2234
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002235 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002236
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002237 protected:
2238 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002239};
2240
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002241// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2242// instruction that branches to the exit block.
2243class HReturnVoid : public HTemplateInstruction<0> {
2244 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002245 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2246 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002247
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002248 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002249
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002250 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002251
2252 private:
2253 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2254};
2255
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002256// Represents dex's RETURN opcodes. A HReturn is a control flow
2257// instruction that branches to the exit block.
2258class HReturn : public HTemplateInstruction<1> {
2259 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002260 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2261 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002262 SetRawInputAt(0, value);
2263 }
2264
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002265 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002266
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002267 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002268
2269 private:
2270 DISALLOW_COPY_AND_ASSIGN(HReturn);
2271};
2272
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002273// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002274// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002275// exit block.
2276class HExit : public HTemplateInstruction<0> {
2277 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002278 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002279
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002280 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002281
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002282 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002283
2284 private:
2285 DISALLOW_COPY_AND_ASSIGN(HExit);
2286};
2287
2288// Jumps from one block to another.
2289class HGoto : public HTemplateInstruction<0> {
2290 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002291 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002292
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002293 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002294
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002295 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002296 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002297 }
2298
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002299 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002300
2301 private:
2302 DISALLOW_COPY_AND_ASSIGN(HGoto);
2303};
2304
Roland Levillain9867bc72015-08-05 10:21:34 +01002305class HConstant : public HExpression<0> {
2306 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002307 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2308 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002309
2310 bool CanBeMoved() const OVERRIDE { return true; }
2311
2312 virtual bool IsMinusOne() const { return false; }
2313 virtual bool IsZero() const { return false; }
2314 virtual bool IsOne() const { return false; }
2315
David Brazdil77a48ae2015-09-15 12:34:04 +00002316 virtual uint64_t GetValueAsUint64() const = 0;
2317
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002318 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002319
2320 private:
2321 DISALLOW_COPY_AND_ASSIGN(HConstant);
2322};
2323
2324class HNullConstant : public HConstant {
2325 public:
2326 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2327 return true;
2328 }
2329
David Brazdil77a48ae2015-09-15 12:34:04 +00002330 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2331
Roland Levillain9867bc72015-08-05 10:21:34 +01002332 size_t ComputeHashCode() const OVERRIDE { return 0; }
2333
2334 DECLARE_INSTRUCTION(NullConstant);
2335
2336 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002337 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002338
2339 friend class HGraph;
2340 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2341};
2342
2343// Constants of the type int. Those can be from Dex instructions, or
2344// synthesized (for example with the if-eqz instruction).
2345class HIntConstant : public HConstant {
2346 public:
2347 int32_t GetValue() const { return value_; }
2348
David Brazdil9f389d42015-10-01 14:32:56 +01002349 uint64_t GetValueAsUint64() const OVERRIDE {
2350 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2351 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002352
Roland Levillain9867bc72015-08-05 10:21:34 +01002353 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2354 DCHECK(other->IsIntConstant());
2355 return other->AsIntConstant()->value_ == value_;
2356 }
2357
2358 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2359
2360 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2361 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2362 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2363
2364 DECLARE_INSTRUCTION(IntConstant);
2365
2366 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002367 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2368 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2369 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2370 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002371
2372 const int32_t value_;
2373
2374 friend class HGraph;
2375 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2376 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2377 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2378};
2379
2380class HLongConstant : public HConstant {
2381 public:
2382 int64_t GetValue() const { return value_; }
2383
David Brazdil77a48ae2015-09-15 12:34:04 +00002384 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2385
Roland Levillain9867bc72015-08-05 10:21:34 +01002386 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2387 DCHECK(other->IsLongConstant());
2388 return other->AsLongConstant()->value_ == value_;
2389 }
2390
2391 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2392
2393 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2394 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2395 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2396
2397 DECLARE_INSTRUCTION(LongConstant);
2398
2399 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002400 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2401 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002402
2403 const int64_t value_;
2404
2405 friend class HGraph;
2406 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2407};
Dave Allison20dfc792014-06-16 20:44:29 -07002408
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002409// Conditional branch. A block ending with an HIf instruction must have
2410// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002411class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002412 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002413 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2414 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002415 SetRawInputAt(0, input);
2416 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002417
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002418 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002419
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002420 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002421 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002422 }
2423
2424 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002425 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002426 }
2427
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002428 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002429
2430 private:
2431 DISALLOW_COPY_AND_ASSIGN(HIf);
2432};
2433
David Brazdilfc6a86a2015-06-26 10:33:45 +00002434
2435// Abstract instruction which marks the beginning and/or end of a try block and
2436// links it to the respective exception handlers. Behaves the same as a Goto in
2437// non-exceptional control flow.
2438// Normal-flow successor is stored at index zero, exception handlers under
2439// higher indices in no particular order.
2440class HTryBoundary : public HTemplateInstruction<0> {
2441 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002442 enum BoundaryKind {
2443 kEntry,
2444 kExit,
2445 };
2446
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002447 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2448 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002449
2450 bool IsControlFlow() const OVERRIDE { return true; }
2451
2452 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002453 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002454
David Brazdild26a4112015-11-10 11:07:31 +00002455 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2456 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2457 }
2458
David Brazdilfc6a86a2015-06-26 10:33:45 +00002459 // Returns whether `handler` is among its exception handlers (non-zero index
2460 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002461 bool HasExceptionHandler(const HBasicBlock& handler) const {
2462 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002463 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002464 }
2465
2466 // If not present already, adds `handler` to its block's list of exception
2467 // handlers.
2468 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002469 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002470 GetBlock()->AddSuccessor(handler);
2471 }
2472 }
2473
David Brazdil56e1acc2015-06-30 15:41:36 +01002474 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002475
David Brazdilffee3d32015-07-06 11:48:53 +01002476 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2477
David Brazdilfc6a86a2015-06-26 10:33:45 +00002478 DECLARE_INSTRUCTION(TryBoundary);
2479
2480 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002481 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002482
2483 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2484};
2485
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002486// Deoptimize to interpreter, upon checking a condition.
2487class HDeoptimize : public HTemplateInstruction<1> {
2488 public:
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002489 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002490 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002491 SetRawInputAt(0, cond);
2492 }
2493
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002494 bool CanBeMoved() const OVERRIDE { return true; }
2495 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2496 return true;
2497 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002498 bool NeedsEnvironment() const OVERRIDE { return true; }
2499 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002500
2501 DECLARE_INSTRUCTION(Deoptimize);
2502
2503 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002504 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2505};
2506
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002507// Represents the ArtMethod that was passed as a first argument to
2508// the method. It is used by instructions that depend on it, like
2509// instructions that work with the dex cache.
2510class HCurrentMethod : public HExpression<0> {
2511 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002512 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2513 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002514
2515 DECLARE_INSTRUCTION(CurrentMethod);
2516
2517 private:
2518 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2519};
2520
Mark Mendellfe57faa2015-09-18 09:26:15 -04002521// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2522// have one successor for each entry in the switch table, and the final successor
2523// will be the block containing the next Dex opcode.
2524class HPackedSwitch : public HTemplateInstruction<1> {
2525 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002526 HPackedSwitch(int32_t start_value,
2527 uint32_t num_entries,
2528 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002529 uint32_t dex_pc = kNoDexPc)
2530 : HTemplateInstruction(SideEffects::None(), dex_pc),
2531 start_value_(start_value),
2532 num_entries_(num_entries) {
2533 SetRawInputAt(0, input);
2534 }
2535
2536 bool IsControlFlow() const OVERRIDE { return true; }
2537
2538 int32_t GetStartValue() const { return start_value_; }
2539
Vladimir Marko211c2112015-09-24 16:52:33 +01002540 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002541
2542 HBasicBlock* GetDefaultBlock() const {
2543 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002544 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002545 }
2546 DECLARE_INSTRUCTION(PackedSwitch);
2547
2548 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002549 const int32_t start_value_;
2550 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002551
2552 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2553};
2554
Roland Levillain88cb1752014-10-20 16:36:47 +01002555class HUnaryOperation : public HExpression<1> {
2556 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002557 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2558 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002559 SetRawInputAt(0, input);
2560 }
2561
2562 HInstruction* GetInput() const { return InputAt(0); }
2563 Primitive::Type GetResultType() const { return GetType(); }
2564
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002565 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002566 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002567 return true;
2568 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002569
Roland Levillain9240d6a2014-10-20 16:47:04 +01002570 // Try to statically evaluate `operation` and return a HConstant
2571 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002572 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002573 HConstant* TryStaticEvaluation() const;
2574
2575 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002576 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2577 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002578
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002579 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002580
2581 private:
2582 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2583};
2584
Dave Allison20dfc792014-06-16 20:44:29 -07002585class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002586 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002587 HBinaryOperation(Primitive::Type result_type,
2588 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002589 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002590 SideEffects side_effects = SideEffects::None(),
2591 uint32_t dex_pc = kNoDexPc)
2592 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002593 SetRawInputAt(0, left);
2594 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002595 }
2596
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002597 HInstruction* GetLeft() const { return InputAt(0); }
2598 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002599 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002600
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002601 virtual bool IsCommutative() const { return false; }
2602
2603 // Put constant on the right.
2604 // Returns whether order is changed.
2605 bool OrderInputsWithConstantOnTheRight() {
2606 HInstruction* left = InputAt(0);
2607 HInstruction* right = InputAt(1);
2608 if (left->IsConstant() && !right->IsConstant()) {
2609 ReplaceInput(right, 0);
2610 ReplaceInput(left, 1);
2611 return true;
2612 }
2613 return false;
2614 }
2615
2616 // Order inputs by instruction id, but favor constant on the right side.
2617 // This helps GVN for commutative ops.
2618 void OrderInputs() {
2619 DCHECK(IsCommutative());
2620 HInstruction* left = InputAt(0);
2621 HInstruction* right = InputAt(1);
2622 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2623 return;
2624 }
2625 if (OrderInputsWithConstantOnTheRight()) {
2626 return;
2627 }
2628 // Order according to instruction id.
2629 if (left->GetId() > right->GetId()) {
2630 ReplaceInput(right, 0);
2631 ReplaceInput(left, 1);
2632 }
2633 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002634
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002635 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002636 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002637 return true;
2638 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002639
Roland Levillain9240d6a2014-10-20 16:47:04 +01002640 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002641 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002642 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002643 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002644
2645 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002646 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2647 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2648 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2649 HLongConstant* y ATTRIBUTE_UNUSED) const {
2650 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2651 return nullptr;
2652 }
2653 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2654 HIntConstant* y ATTRIBUTE_UNUSED) const {
2655 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2656 return nullptr;
2657 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002658 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2659 HNullConstant* y ATTRIBUTE_UNUSED) const {
2660 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2661 return nullptr;
2662 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002663
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002664 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002665 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002666 HConstant* GetConstantRight() const;
2667
2668 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002669 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002670 HInstruction* GetLeastConstantLeft() const;
2671
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002672 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002673
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002674 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002675 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2676};
2677
Mark Mendellc4701932015-04-10 13:18:51 -04002678// The comparison bias applies for floating point operations and indicates how NaN
2679// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002680enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002681 kNoBias, // bias is not applicable (i.e. for long operation)
2682 kGtBias, // return 1 for NaN comparisons
2683 kLtBias, // return -1 for NaN comparisons
2684};
2685
Dave Allison20dfc792014-06-16 20:44:29 -07002686class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002687 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002688 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2689 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002690 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002691 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002692
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002693 bool NeedsMaterialization() const { return needs_materialization_; }
2694 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002695
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002696 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002697 // `instruction`, and disregard moves in between.
2698 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002699
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002700 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002701
2702 virtual IfCondition GetCondition() const = 0;
2703
Mark Mendellc4701932015-04-10 13:18:51 -04002704 virtual IfCondition GetOppositeCondition() const = 0;
2705
Roland Levillain4fa13f62015-07-06 18:11:54 +01002706 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002707
2708 void SetBias(ComparisonBias bias) { bias_ = bias; }
2709
2710 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2711 return bias_ == other->AsCondition()->bias_;
2712 }
2713
Roland Levillain4fa13f62015-07-06 18:11:54 +01002714 bool IsFPConditionTrueIfNaN() const {
2715 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2716 IfCondition if_cond = GetCondition();
2717 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2718 }
2719
2720 bool IsFPConditionFalseIfNaN() const {
2721 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2722 IfCondition if_cond = GetCondition();
2723 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2724 }
2725
Dave Allison20dfc792014-06-16 20:44:29 -07002726 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002727 // For register allocation purposes, returns whether this instruction needs to be
2728 // materialized (that is, not just be in the processor flags).
2729 bool needs_materialization_;
2730
Mark Mendellc4701932015-04-10 13:18:51 -04002731 // Needed if we merge a HCompare into a HCondition.
2732 ComparisonBias bias_;
2733
Dave Allison20dfc792014-06-16 20:44:29 -07002734 DISALLOW_COPY_AND_ASSIGN(HCondition);
2735};
2736
2737// Instruction to check if two inputs are equal to each other.
2738class HEqual : public HCondition {
2739 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002740 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2741 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002742
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002743 bool IsCommutative() const OVERRIDE { return true; }
2744
Roland Levillain9867bc72015-08-05 10:21:34 +01002745 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002746 return GetBlock()->GetGraph()->GetIntConstant(
2747 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002748 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002749 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002750 return GetBlock()->GetGraph()->GetIntConstant(
2751 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002752 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002753 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2754 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002755 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002756 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002757
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002758 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002759
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002760 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002761 return kCondEQ;
2762 }
2763
Mark Mendellc4701932015-04-10 13:18:51 -04002764 IfCondition GetOppositeCondition() const OVERRIDE {
2765 return kCondNE;
2766 }
2767
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002768 private:
Aart Bike9f37602015-10-09 11:15:55 -07002769 template <typename T> bool Compute(T x, T y) const { return x == y; }
2770
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002771 DISALLOW_COPY_AND_ASSIGN(HEqual);
2772};
2773
Dave Allison20dfc792014-06-16 20:44:29 -07002774class HNotEqual : public HCondition {
2775 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002776 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2777 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002778
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002779 bool IsCommutative() const OVERRIDE { return true; }
2780
Roland Levillain9867bc72015-08-05 10:21:34 +01002781 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002782 return GetBlock()->GetGraph()->GetIntConstant(
2783 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002784 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002785 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002786 return GetBlock()->GetGraph()->GetIntConstant(
2787 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002788 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002789 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2790 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002791 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002792 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002793
Dave Allison20dfc792014-06-16 20:44:29 -07002794 DECLARE_INSTRUCTION(NotEqual);
2795
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002796 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002797 return kCondNE;
2798 }
2799
Mark Mendellc4701932015-04-10 13:18:51 -04002800 IfCondition GetOppositeCondition() const OVERRIDE {
2801 return kCondEQ;
2802 }
2803
Dave Allison20dfc792014-06-16 20:44:29 -07002804 private:
Aart Bike9f37602015-10-09 11:15:55 -07002805 template <typename T> bool Compute(T x, T y) const { return x != y; }
2806
Dave Allison20dfc792014-06-16 20:44:29 -07002807 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2808};
2809
2810class HLessThan : public HCondition {
2811 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002812 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2813 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002814
Roland Levillain9867bc72015-08-05 10:21:34 +01002815 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002816 return GetBlock()->GetGraph()->GetIntConstant(
2817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002818 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002819 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002820 return GetBlock()->GetGraph()->GetIntConstant(
2821 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002822 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002823
Dave Allison20dfc792014-06-16 20:44:29 -07002824 DECLARE_INSTRUCTION(LessThan);
2825
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002826 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002827 return kCondLT;
2828 }
2829
Mark Mendellc4701932015-04-10 13:18:51 -04002830 IfCondition GetOppositeCondition() const OVERRIDE {
2831 return kCondGE;
2832 }
2833
Dave Allison20dfc792014-06-16 20:44:29 -07002834 private:
Aart Bike9f37602015-10-09 11:15:55 -07002835 template <typename T> bool Compute(T x, T y) const { return x < y; }
2836
Dave Allison20dfc792014-06-16 20:44:29 -07002837 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2838};
2839
2840class HLessThanOrEqual : public HCondition {
2841 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002842 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2843 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002844
Roland Levillain9867bc72015-08-05 10:21:34 +01002845 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002846 return GetBlock()->GetGraph()->GetIntConstant(
2847 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002848 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002849 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002850 return GetBlock()->GetGraph()->GetIntConstant(
2851 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002852 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002853
Dave Allison20dfc792014-06-16 20:44:29 -07002854 DECLARE_INSTRUCTION(LessThanOrEqual);
2855
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002856 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002857 return kCondLE;
2858 }
2859
Mark Mendellc4701932015-04-10 13:18:51 -04002860 IfCondition GetOppositeCondition() const OVERRIDE {
2861 return kCondGT;
2862 }
2863
Dave Allison20dfc792014-06-16 20:44:29 -07002864 private:
Aart Bike9f37602015-10-09 11:15:55 -07002865 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2866
Dave Allison20dfc792014-06-16 20:44:29 -07002867 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2868};
2869
2870class HGreaterThan : public HCondition {
2871 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002872 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2873 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002874
Roland Levillain9867bc72015-08-05 10:21:34 +01002875 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002876 return GetBlock()->GetGraph()->GetIntConstant(
2877 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002878 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002879 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002880 return GetBlock()->GetGraph()->GetIntConstant(
2881 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002882 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002883
Dave Allison20dfc792014-06-16 20:44:29 -07002884 DECLARE_INSTRUCTION(GreaterThan);
2885
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002886 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002887 return kCondGT;
2888 }
2889
Mark Mendellc4701932015-04-10 13:18:51 -04002890 IfCondition GetOppositeCondition() const OVERRIDE {
2891 return kCondLE;
2892 }
2893
Dave Allison20dfc792014-06-16 20:44:29 -07002894 private:
Aart Bike9f37602015-10-09 11:15:55 -07002895 template <typename T> bool Compute(T x, T y) const { return x > y; }
2896
Dave Allison20dfc792014-06-16 20:44:29 -07002897 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2898};
2899
2900class HGreaterThanOrEqual : public HCondition {
2901 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002902 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2903 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002904
Roland Levillain9867bc72015-08-05 10:21:34 +01002905 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002906 return GetBlock()->GetGraph()->GetIntConstant(
2907 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002908 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002909 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002910 return GetBlock()->GetGraph()->GetIntConstant(
2911 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002912 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002913
Dave Allison20dfc792014-06-16 20:44:29 -07002914 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2915
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002916 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002917 return kCondGE;
2918 }
2919
Mark Mendellc4701932015-04-10 13:18:51 -04002920 IfCondition GetOppositeCondition() const OVERRIDE {
2921 return kCondLT;
2922 }
2923
Dave Allison20dfc792014-06-16 20:44:29 -07002924 private:
Aart Bike9f37602015-10-09 11:15:55 -07002925 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2926
Dave Allison20dfc792014-06-16 20:44:29 -07002927 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2928};
2929
Aart Bike9f37602015-10-09 11:15:55 -07002930class HBelow : public HCondition {
2931 public:
2932 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2933 : HCondition(first, second, dex_pc) {}
2934
2935 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2936 return GetBlock()->GetGraph()->GetIntConstant(
2937 Compute(static_cast<uint32_t>(x->GetValue()),
2938 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2939 }
2940 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2941 return GetBlock()->GetGraph()->GetIntConstant(
2942 Compute(static_cast<uint64_t>(x->GetValue()),
2943 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2944 }
2945
2946 DECLARE_INSTRUCTION(Below);
2947
2948 IfCondition GetCondition() const OVERRIDE {
2949 return kCondB;
2950 }
2951
2952 IfCondition GetOppositeCondition() const OVERRIDE {
2953 return kCondAE;
2954 }
2955
2956 private:
2957 template <typename T> bool Compute(T x, T y) const { return x < y; }
2958
2959 DISALLOW_COPY_AND_ASSIGN(HBelow);
2960};
2961
2962class HBelowOrEqual : public HCondition {
2963 public:
2964 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2965 : HCondition(first, second, dex_pc) {}
2966
2967 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2968 return GetBlock()->GetGraph()->GetIntConstant(
2969 Compute(static_cast<uint32_t>(x->GetValue()),
2970 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2971 }
2972 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2973 return GetBlock()->GetGraph()->GetIntConstant(
2974 Compute(static_cast<uint64_t>(x->GetValue()),
2975 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2976 }
2977
2978 DECLARE_INSTRUCTION(BelowOrEqual);
2979
2980 IfCondition GetCondition() const OVERRIDE {
2981 return kCondBE;
2982 }
2983
2984 IfCondition GetOppositeCondition() const OVERRIDE {
2985 return kCondA;
2986 }
2987
2988 private:
2989 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2990
2991 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2992};
2993
2994class HAbove : public HCondition {
2995 public:
2996 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2997 : HCondition(first, second, dex_pc) {}
2998
2999 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3000 return GetBlock()->GetGraph()->GetIntConstant(
3001 Compute(static_cast<uint32_t>(x->GetValue()),
3002 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3003 }
3004 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3005 return GetBlock()->GetGraph()->GetIntConstant(
3006 Compute(static_cast<uint64_t>(x->GetValue()),
3007 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3008 }
3009
3010 DECLARE_INSTRUCTION(Above);
3011
3012 IfCondition GetCondition() const OVERRIDE {
3013 return kCondA;
3014 }
3015
3016 IfCondition GetOppositeCondition() const OVERRIDE {
3017 return kCondBE;
3018 }
3019
3020 private:
3021 template <typename T> bool Compute(T x, T y) const { return x > y; }
3022
3023 DISALLOW_COPY_AND_ASSIGN(HAbove);
3024};
3025
3026class HAboveOrEqual : public HCondition {
3027 public:
3028 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3029 : HCondition(first, second, dex_pc) {}
3030
3031 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3032 return GetBlock()->GetGraph()->GetIntConstant(
3033 Compute(static_cast<uint32_t>(x->GetValue()),
3034 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3035 }
3036 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3037 return GetBlock()->GetGraph()->GetIntConstant(
3038 Compute(static_cast<uint64_t>(x->GetValue()),
3039 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3040 }
3041
3042 DECLARE_INSTRUCTION(AboveOrEqual);
3043
3044 IfCondition GetCondition() const OVERRIDE {
3045 return kCondAE;
3046 }
3047
3048 IfCondition GetOppositeCondition() const OVERRIDE {
3049 return kCondB;
3050 }
3051
3052 private:
3053 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3054
3055 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3056};
Dave Allison20dfc792014-06-16 20:44:29 -07003057
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003058// Instruction to check how two inputs compare to each other.
3059// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3060class HCompare : public HBinaryOperation {
3061 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003062 HCompare(Primitive::Type type,
3063 HInstruction* first,
3064 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003065 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003066 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003067 : HBinaryOperation(Primitive::kPrimInt,
3068 first,
3069 second,
3070 SideEffectsForArchRuntimeCalls(type),
3071 dex_pc),
3072 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003073 DCHECK_EQ(type, first->GetType());
3074 DCHECK_EQ(type, second->GetType());
3075 }
3076
Roland Levillain9867bc72015-08-05 10:21:34 +01003077 template <typename T>
3078 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003079
Roland Levillain9867bc72015-08-05 10:21:34 +01003080 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003081 return GetBlock()->GetGraph()->GetIntConstant(
3082 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003083 }
3084 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003085 return GetBlock()->GetGraph()->GetIntConstant(
3086 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003087 }
3088
Calin Juravleddb7df22014-11-25 20:56:51 +00003089 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3090 return bias_ == other->AsCompare()->bias_;
3091 }
3092
Mark Mendellc4701932015-04-10 13:18:51 -04003093 ComparisonBias GetBias() const { return bias_; }
3094
Roland Levillain4fa13f62015-07-06 18:11:54 +01003095 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003096
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003097
3098 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3099 // MIPS64 uses a runtime call for FP comparisons.
3100 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3101 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003102
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003103 DECLARE_INSTRUCTION(Compare);
3104
3105 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003106 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003107
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003108 DISALLOW_COPY_AND_ASSIGN(HCompare);
3109};
3110
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003111// A local in the graph. Corresponds to a Dex register.
3112class HLocal : public HTemplateInstruction<0> {
3113 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003114 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003115 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003116
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003117 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003118
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003119 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003120
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003121 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003122 // The Dex register number.
3123 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003124
3125 DISALLOW_COPY_AND_ASSIGN(HLocal);
3126};
3127
3128// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003129class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003130 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003131 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3132 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003133 SetRawInputAt(0, local);
3134 }
3135
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003136 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3137
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003138 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003139
3140 private:
3141 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3142};
3143
3144// Store a value in a given local. This instruction has two inputs: the value
3145// and the local.
3146class HStoreLocal : public HTemplateInstruction<2> {
3147 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003148 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3149 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003150 SetRawInputAt(0, local);
3151 SetRawInputAt(1, value);
3152 }
3153
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003154 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3155
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003156 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003157
3158 private:
3159 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3160};
3161
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003162class HFloatConstant : public HConstant {
3163 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003164 float GetValue() const { return value_; }
3165
David Brazdil77a48ae2015-09-15 12:34:04 +00003166 uint64_t GetValueAsUint64() const OVERRIDE {
3167 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3168 }
3169
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003170 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003171 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003172 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003173 }
3174
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003175 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003176
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003177 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003178 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003179 }
3180 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003181 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003182 }
3183 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003184 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3185 }
3186 bool IsNaN() const {
3187 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003188 }
3189
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003190 DECLARE_INSTRUCTION(FloatConstant);
3191
3192 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003193 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3194 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3195 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3196 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003197
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003198 const float value_;
3199
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003200 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003201 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003202 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003203 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3204};
3205
3206class HDoubleConstant : public HConstant {
3207 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003208 double GetValue() const { return value_; }
3209
David Brazdil77a48ae2015-09-15 12:34:04 +00003210 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3211
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003212 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003213 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003214 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003215 }
3216
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003217 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003218
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003219 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003220 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003221 }
3222 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003223 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003224 }
3225 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003226 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3227 }
3228 bool IsNaN() const {
3229 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003230 }
3231
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003232 DECLARE_INSTRUCTION(DoubleConstant);
3233
3234 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003235 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3236 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3237 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3238 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003239
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003240 const double value_;
3241
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003242 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003243 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003244 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003245 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3246};
3247
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003248enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003249#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003250#include "intrinsics_list.h"
3251 kNone,
3252 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3253#undef INTRINSICS_LIST
3254#undef OPTIMIZING_INTRINSICS
3255};
3256std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3257
Agi Csaki05f20562015-08-19 14:58:14 -07003258enum IntrinsicNeedsEnvironmentOrCache {
3259 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3260 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003261};
3262
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003263class HInvoke : public HInstruction {
3264 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003265 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003266
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003267 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003268
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003269 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003270 SetRawInputAt(index, argument);
3271 }
3272
Roland Levillain3e3d7332015-04-28 11:00:54 +01003273 // Return the number of arguments. This number can be lower than
3274 // the number of inputs returned by InputCount(), as some invoke
3275 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3276 // inputs at the end of their list of inputs.
3277 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3278
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003279 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003280
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003281
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003282 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003283 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003284
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003285 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3286
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003287 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003288 return intrinsic_;
3289 }
3290
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003291 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003292
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003293 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003294 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003295 }
3296
3297 bool CanThrow() const OVERRIDE { return true; }
3298
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003299 uint32_t* GetIntrinsicOptimizations() {
3300 return &intrinsic_optimizations_;
3301 }
3302
3303 const uint32_t* GetIntrinsicOptimizations() const {
3304 return &intrinsic_optimizations_;
3305 }
3306
3307 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3308
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003309 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003310
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003311 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003312 HInvoke(ArenaAllocator* arena,
3313 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003314 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003315 Primitive::Type return_type,
3316 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003317 uint32_t dex_method_index,
3318 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003319 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003320 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003321 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003322 inputs_(number_of_arguments + number_of_other_inputs,
3323 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003324 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003325 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003326 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003327 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003328 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003329 }
3330
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003331 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003332 return inputs_[index];
3333 }
3334
David Brazdil1abb4192015-02-17 18:33:36 +00003335 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003336 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003337 }
3338
Roland Levillain3e3d7332015-04-28 11:00:54 +01003339 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003340 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003341 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003342 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003343 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003344 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003345
3346 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3347 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003348
3349 private:
3350 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3351};
3352
Calin Juravle175dc732015-08-25 15:42:32 +01003353class HInvokeUnresolved : public HInvoke {
3354 public:
3355 HInvokeUnresolved(ArenaAllocator* arena,
3356 uint32_t number_of_arguments,
3357 Primitive::Type return_type,
3358 uint32_t dex_pc,
3359 uint32_t dex_method_index,
3360 InvokeType invoke_type)
3361 : HInvoke(arena,
3362 number_of_arguments,
3363 0u /* number_of_other_inputs */,
3364 return_type,
3365 dex_pc,
3366 dex_method_index,
3367 invoke_type) {
3368 }
3369
3370 DECLARE_INSTRUCTION(InvokeUnresolved);
3371
3372 private:
3373 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3374};
3375
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003376class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003377 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003378 // Requirements of this method call regarding the class
3379 // initialization (clinit) check of its declaring class.
3380 enum class ClinitCheckRequirement {
3381 kNone, // Class already initialized.
3382 kExplicit, // Static call having explicit clinit check as last input.
3383 kImplicit, // Static call implicitly requiring a clinit check.
3384 };
3385
Vladimir Marko58155012015-08-19 12:49:41 +00003386 // Determines how to load the target ArtMethod*.
3387 enum class MethodLoadKind {
3388 // Use a String init ArtMethod* loaded from Thread entrypoints.
3389 kStringInit,
3390
3391 // Use the method's own ArtMethod* loaded by the register allocator.
3392 kRecursive,
3393
3394 // Use ArtMethod* at a known address, embed the direct address in the code.
3395 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3396 kDirectAddress,
3397
3398 // Use ArtMethod* at an address that will be known at link time, embed the direct
3399 // address in the code. If the image is relocatable, emit .patch_oat entry.
3400 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3401 // the image relocatable or not.
3402 kDirectAddressWithFixup,
3403
3404 // Load from resoved methods array in the dex cache using a PC-relative load.
3405 // Used when we need to use the dex cache, for example for invoke-static that
3406 // may cause class initialization (the entry may point to a resolution method),
3407 // and we know that we can access the dex cache arrays using a PC-relative load.
3408 kDexCachePcRelative,
3409
3410 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3411 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3412 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3413 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3414 kDexCacheViaMethod,
3415 };
3416
3417 // Determines the location of the code pointer.
3418 enum class CodePtrLocation {
3419 // Recursive call, use local PC-relative call instruction.
3420 kCallSelf,
3421
3422 // Use PC-relative call instruction patched at link time.
3423 // Used for calls within an oat file, boot->boot or app->app.
3424 kCallPCRelative,
3425
3426 // Call to a known target address, embed the direct address in code.
3427 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3428 kCallDirect,
3429
3430 // Call to a target address that will be known at link time, embed the direct
3431 // address in code. If the image is relocatable, emit .patch_oat entry.
3432 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3433 // the image relocatable or not.
3434 kCallDirectWithFixup,
3435
3436 // Use code pointer from the ArtMethod*.
3437 // Used when we don't know the target code. This is also the last-resort-kind used when
3438 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3439 kCallArtMethod,
3440 };
3441
3442 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003443 MethodLoadKind method_load_kind;
3444 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003445 // The method load data holds
3446 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3447 // Note that there are multiple string init methods, each having its own offset.
3448 // - the method address for kDirectAddress
3449 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003450 uint64_t method_load_data;
3451 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003452 };
3453
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003454 HInvokeStaticOrDirect(ArenaAllocator* arena,
3455 uint32_t number_of_arguments,
3456 Primitive::Type return_type,
3457 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003458 uint32_t method_index,
3459 MethodReference target_method,
3460 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003461 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003462 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003463 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003464 : HInvoke(arena,
3465 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003466 // There is potentially one extra argument for the HCurrentMethod node, and
3467 // potentially one other if the clinit check is explicit, and potentially
3468 // one other if the method is a string factory.
3469 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3470 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3471 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003472 return_type,
3473 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003474 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003475 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003476 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003477 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003478 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003479 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003480
Vladimir Markodc151b22015-10-15 18:02:30 +01003481 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003482 bool had_current_method_input = HasCurrentMethodInput();
3483 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3484
3485 // Using the current method is the default and once we find a better
3486 // method load kind, we should not go back to using the current method.
3487 DCHECK(had_current_method_input || !needs_current_method_input);
3488
3489 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003490 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3491 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003492 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003493 dispatch_info_ = dispatch_info;
3494 }
3495
Vladimir Markoc53c0792015-11-19 15:48:33 +00003496 void AddSpecialInput(HInstruction* input) {
3497 // We allow only one special input.
3498 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3499 DCHECK(InputCount() == GetSpecialInputIndex() ||
3500 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3501 InsertInputAt(GetSpecialInputIndex(), input);
3502 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003503
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003504 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003505 // We access the method via the dex cache so we can't do an implicit null check.
3506 // TODO: for intrinsics we can generate implicit null checks.
3507 return false;
3508 }
3509
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003510 bool CanBeNull() const OVERRIDE {
3511 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3512 }
3513
Vladimir Markoc53c0792015-11-19 15:48:33 +00003514 // Get the index of the special input, if any.
3515 //
3516 // If the invoke IsStringInit(), it initially has a HFakeString special argument
3517 // which is removed by the instruction simplifier; if the invoke HasCurrentMethodInput(),
3518 // the "special input" is the current method pointer; otherwise there may be one
3519 // platform-specific special input, such as PC-relative addressing base.
3520 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3521
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003522 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3523 void SetOptimizedInvokeType(InvokeType invoke_type) {
3524 optimized_invoke_type_ = invoke_type;
3525 }
3526
Vladimir Marko58155012015-08-19 12:49:41 +00003527 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3528 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3529 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003530 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003531 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003532 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003533 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003534 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3535 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003536 bool HasCurrentMethodInput() const {
3537 // This function can be called only after the invoke has been fully initialized by the builder.
3538 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003539 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003540 return true;
3541 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003542 DCHECK(InputCount() == GetSpecialInputIndex() ||
3543 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003544 return false;
3545 }
3546 }
Vladimir Marko58155012015-08-19 12:49:41 +00003547 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3548 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003549 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003550
3551 int32_t GetStringInitOffset() const {
3552 DCHECK(IsStringInit());
3553 return dispatch_info_.method_load_data;
3554 }
3555
3556 uint64_t GetMethodAddress() const {
3557 DCHECK(HasMethodAddress());
3558 return dispatch_info_.method_load_data;
3559 }
3560
3561 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003562 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003563 return dispatch_info_.method_load_data;
3564 }
3565
3566 uint64_t GetDirectCodePtr() const {
3567 DCHECK(HasDirectCodePtr());
3568 return dispatch_info_.direct_code_ptr;
3569 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003570
Calin Juravle68ad6492015-08-18 17:08:12 +01003571 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3572
Roland Levillain4c0eb422015-04-24 16:43:49 +01003573 // Is this instruction a call to a static method?
3574 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003575 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003576 }
3577
Vladimir Markofbb184a2015-11-13 14:47:00 +00003578 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3579 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3580 // instruction; only relevant for static calls with explicit clinit check.
3581 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003582 DCHECK(IsStaticWithExplicitClinitCheck());
3583 size_t last_input_index = InputCount() - 1;
3584 HInstruction* last_input = InputAt(last_input_index);
3585 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003586 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003587 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003588 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003589 clinit_check_requirement_ = new_requirement;
3590 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003591 }
3592
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003593 bool IsStringFactoryFor(HFakeString* str) const {
3594 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003595 DCHECK(!HasCurrentMethodInput());
3596 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003597 return InputAt(InputCount() - 1)->AsFakeString() == str;
3598 }
3599
3600 void RemoveFakeStringArgumentAsLastInput() {
3601 DCHECK(IsStringInit());
3602 size_t last_input_index = InputCount() - 1;
3603 HInstruction* last_input = InputAt(last_input_index);
3604 DCHECK(last_input != nullptr);
3605 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3606 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003607 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003608 }
3609
Roland Levillain4c0eb422015-04-24 16:43:49 +01003610 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003611 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003612 bool IsStaticWithExplicitClinitCheck() const {
3613 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3614 }
3615
3616 // Is this a call to a static method whose declaring class has an
3617 // implicit intialization check requirement?
3618 bool IsStaticWithImplicitClinitCheck() const {
3619 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3620 }
3621
Vladimir Markob554b5a2015-11-06 12:57:55 +00003622 // Does this method load kind need the current method as an input?
3623 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3624 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3625 }
3626
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003627 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003628
Roland Levillain4c0eb422015-04-24 16:43:49 +01003629 protected:
3630 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3631 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3632 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3633 HInstruction* input = input_record.GetInstruction();
3634 // `input` is the last input of a static invoke marked as having
3635 // an explicit clinit check. It must either be:
3636 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3637 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3638 DCHECK(input != nullptr);
3639 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3640 }
3641 return input_record;
3642 }
3643
Vladimir Markoc53c0792015-11-19 15:48:33 +00003644 void InsertInputAt(size_t index, HInstruction* input);
3645 void RemoveInputAt(size_t index);
3646
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003647 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003648 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003649 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003650 // The target method may refer to different dex file or method index than the original
3651 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3652 // in derived class to increase visibility.
3653 MethodReference target_method_;
3654 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003655
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003656 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003657};
Vladimir Markof64242a2015-12-01 14:58:23 +00003658std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003659std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003660
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003661class HInvokeVirtual : public HInvoke {
3662 public:
3663 HInvokeVirtual(ArenaAllocator* arena,
3664 uint32_t number_of_arguments,
3665 Primitive::Type return_type,
3666 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003667 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003668 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003669 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003670 vtable_index_(vtable_index) {}
3671
Calin Juravle641547a2015-04-21 22:08:51 +01003672 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003673 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003674 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003675 }
3676
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003677 uint32_t GetVTableIndex() const { return vtable_index_; }
3678
3679 DECLARE_INSTRUCTION(InvokeVirtual);
3680
3681 private:
3682 const uint32_t vtable_index_;
3683
3684 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3685};
3686
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003687class HInvokeInterface : public HInvoke {
3688 public:
3689 HInvokeInterface(ArenaAllocator* arena,
3690 uint32_t number_of_arguments,
3691 Primitive::Type return_type,
3692 uint32_t dex_pc,
3693 uint32_t dex_method_index,
3694 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003695 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003696 imt_index_(imt_index) {}
3697
Calin Juravle641547a2015-04-21 22:08:51 +01003698 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003699 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003700 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003701 }
3702
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003703 uint32_t GetImtIndex() const { return imt_index_; }
3704 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3705
3706 DECLARE_INSTRUCTION(InvokeInterface);
3707
3708 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003709 const uint32_t imt_index_;
3710
3711 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3712};
3713
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003714class HNewInstance : public HExpression<2> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003715 public:
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003716 HNewInstance(HInstruction* cls,
3717 HCurrentMethod* current_method,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003718 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003719 uint16_t type_index,
3720 const DexFile& dex_file,
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003721 bool can_throw,
3722 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003723 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003724 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003725 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003726 dex_file_(dex_file),
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003727 can_throw_(can_throw),
3728 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003729 entrypoint_(entrypoint) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003730 SetRawInputAt(0, cls);
3731 SetRawInputAt(1, current_method);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003732 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003733
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003734 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003735 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003736
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003737 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003738 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003739
3740 // It may throw when called on type that's not instantiable/accessible.
3741 // It can throw OOME.
3742 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3743 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3744
3745 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003746
Calin Juravle10e244f2015-01-26 18:54:32 +00003747 bool CanBeNull() const OVERRIDE { return false; }
3748
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003749 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3750
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003751 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3752 entrypoint_ = entrypoint;
3753 }
3754
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003755 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003756
3757 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003758 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003759 const DexFile& dex_file_;
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003760 const bool can_throw_;
3761 const bool finalizable_;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003762 QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003763
3764 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3765};
3766
Roland Levillain88cb1752014-10-20 16:36:47 +01003767class HNeg : public HUnaryOperation {
3768 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003769 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3770 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003771
Roland Levillain9867bc72015-08-05 10:21:34 +01003772 template <typename T> T Compute(T x) const { return -x; }
3773
3774 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003775 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003776 }
3777 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003778 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003779 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003780
Roland Levillain88cb1752014-10-20 16:36:47 +01003781 DECLARE_INSTRUCTION(Neg);
3782
3783 private:
3784 DISALLOW_COPY_AND_ASSIGN(HNeg);
3785};
3786
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003787class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003788 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003789 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003790 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003791 uint32_t dex_pc,
3792 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003793 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003794 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003795 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003796 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003797 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003798 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003799 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003800 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003801 }
3802
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003803 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003804 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003805
3806 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003807 bool NeedsEnvironment() const OVERRIDE { return true; }
3808
Mingyao Yang0c365e62015-03-31 15:09:29 -07003809 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3810 bool CanThrow() const OVERRIDE { return true; }
3811
Calin Juravle10e244f2015-01-26 18:54:32 +00003812 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003813
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003814 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3815
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003816 DECLARE_INSTRUCTION(NewArray);
3817
3818 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003819 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003820 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003821 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003822
3823 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3824};
3825
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003826class HAdd : public HBinaryOperation {
3827 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003828 HAdd(Primitive::Type result_type,
3829 HInstruction* left,
3830 HInstruction* right,
3831 uint32_t dex_pc = kNoDexPc)
3832 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003833
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003834 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003835
Roland Levillain9867bc72015-08-05 10:21:34 +01003836 template <typename T> T Compute(T x, T y) const { return x + y; }
3837
3838 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003839 return GetBlock()->GetGraph()->GetIntConstant(
3840 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003841 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003842 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003843 return GetBlock()->GetGraph()->GetLongConstant(
3844 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003845 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003846
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003847 DECLARE_INSTRUCTION(Add);
3848
3849 private:
3850 DISALLOW_COPY_AND_ASSIGN(HAdd);
3851};
3852
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003853class HSub : public HBinaryOperation {
3854 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003855 HSub(Primitive::Type result_type,
3856 HInstruction* left,
3857 HInstruction* right,
3858 uint32_t dex_pc = kNoDexPc)
3859 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003860
Roland Levillain9867bc72015-08-05 10:21:34 +01003861 template <typename T> T Compute(T x, T y) const { return x - y; }
3862
3863 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 Levillain93445682014-10-06 19:24:02 +01003866 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003867 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());
Roland Levillain93445682014-10-06 19:24:02 +01003870 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003871
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003872 DECLARE_INSTRUCTION(Sub);
3873
3874 private:
3875 DISALLOW_COPY_AND_ASSIGN(HSub);
3876};
3877
Calin Juravle34bacdf2014-10-07 20:23:36 +01003878class HMul : public HBinaryOperation {
3879 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003880 HMul(Primitive::Type result_type,
3881 HInstruction* left,
3882 HInstruction* right,
3883 uint32_t dex_pc = kNoDexPc)
3884 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003885
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003886 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003887
Roland Levillain9867bc72015-08-05 10:21:34 +01003888 template <typename T> T Compute(T x, T y) const { return x * y; }
3889
3890 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003891 return GetBlock()->GetGraph()->GetIntConstant(
3892 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003893 }
3894 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003895 return GetBlock()->GetGraph()->GetLongConstant(
3896 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003897 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003898
3899 DECLARE_INSTRUCTION(Mul);
3900
3901 private:
3902 DISALLOW_COPY_AND_ASSIGN(HMul);
3903};
3904
Calin Juravle7c4954d2014-10-28 16:57:40 +00003905class HDiv : public HBinaryOperation {
3906 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003907 HDiv(Primitive::Type result_type,
3908 HInstruction* left,
3909 HInstruction* right,
3910 uint32_t dex_pc)
3911 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003912
Roland Levillain9867bc72015-08-05 10:21:34 +01003913 template <typename T>
3914 T Compute(T x, T y) const {
3915 // Our graph structure ensures we never have 0 for `y` during
3916 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003917 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003918 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003919 return (y == -1) ? -x : x / y;
3920 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003921
Roland Levillain9867bc72015-08-05 10:21:34 +01003922 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003923 return GetBlock()->GetGraph()->GetIntConstant(
3924 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003925 }
3926 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003927 return GetBlock()->GetGraph()->GetLongConstant(
3928 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003929 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003930
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003931 static SideEffects SideEffectsForArchRuntimeCalls() {
3932 // The generated code can use a runtime call.
3933 return SideEffects::CanTriggerGC();
3934 }
3935
Calin Juravle7c4954d2014-10-28 16:57:40 +00003936 DECLARE_INSTRUCTION(Div);
3937
3938 private:
3939 DISALLOW_COPY_AND_ASSIGN(HDiv);
3940};
3941
Calin Juravlebacfec32014-11-14 15:54:36 +00003942class HRem : public HBinaryOperation {
3943 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003944 HRem(Primitive::Type result_type,
3945 HInstruction* left,
3946 HInstruction* right,
3947 uint32_t dex_pc)
3948 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003949
Roland Levillain9867bc72015-08-05 10:21:34 +01003950 template <typename T>
3951 T Compute(T x, T y) const {
3952 // Our graph structure ensures we never have 0 for `y` during
3953 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003954 DCHECK_NE(y, 0);
3955 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3956 return (y == -1) ? 0 : x % y;
3957 }
3958
Roland Levillain9867bc72015-08-05 10:21:34 +01003959 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003960 return GetBlock()->GetGraph()->GetIntConstant(
3961 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003962 }
3963 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003964 return GetBlock()->GetGraph()->GetLongConstant(
3965 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003966 }
3967
Calin Juravlebacfec32014-11-14 15:54:36 +00003968
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003969 static SideEffects SideEffectsForArchRuntimeCalls() {
3970 return SideEffects::CanTriggerGC();
3971 }
3972
Calin Juravlebacfec32014-11-14 15:54:36 +00003973 DECLARE_INSTRUCTION(Rem);
3974
3975 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003976 DISALLOW_COPY_AND_ASSIGN(HRem);
3977};
3978
Calin Juravled0d48522014-11-04 16:40:20 +00003979class HDivZeroCheck : public HExpression<1> {
3980 public:
3981 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003982 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003983 SetRawInputAt(0, value);
3984 }
3985
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003986 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3987
Calin Juravled0d48522014-11-04 16:40:20 +00003988 bool CanBeMoved() const OVERRIDE { return true; }
3989
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003990 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003991 return true;
3992 }
3993
3994 bool NeedsEnvironment() const OVERRIDE { return true; }
3995 bool CanThrow() const OVERRIDE { return true; }
3996
Calin Juravled0d48522014-11-04 16:40:20 +00003997 DECLARE_INSTRUCTION(DivZeroCheck);
3998
3999 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004000 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4001};
4002
Calin Juravle9aec02f2014-11-18 23:06:35 +00004003class HShl : public HBinaryOperation {
4004 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004005 HShl(Primitive::Type result_type,
4006 HInstruction* left,
4007 HInstruction* right,
4008 uint32_t dex_pc = kNoDexPc)
4009 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004010
Roland Levillain9867bc72015-08-05 10:21:34 +01004011 template <typename T, typename U, typename V>
4012 T Compute(T x, U y, V max_shift_value) const {
4013 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4014 "V is not the unsigned integer type corresponding to T");
4015 return x << (y & max_shift_value);
4016 }
4017
4018 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4019 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004020 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004021 }
4022 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4023 // case is handled as `x << static_cast<int>(y)`.
4024 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4025 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004026 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004027 }
4028 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4029 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004030 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004031 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004032
4033 DECLARE_INSTRUCTION(Shl);
4034
4035 private:
4036 DISALLOW_COPY_AND_ASSIGN(HShl);
4037};
4038
4039class HShr : public HBinaryOperation {
4040 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004041 HShr(Primitive::Type result_type,
4042 HInstruction* left,
4043 HInstruction* right,
4044 uint32_t dex_pc = kNoDexPc)
4045 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004046
Roland Levillain9867bc72015-08-05 10:21:34 +01004047 template <typename T, typename U, typename V>
4048 T Compute(T x, U y, V max_shift_value) const {
4049 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4050 "V is not the unsigned integer type corresponding to T");
4051 return x >> (y & max_shift_value);
4052 }
4053
4054 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4055 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004056 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004057 }
4058 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4059 // case is handled as `x >> static_cast<int>(y)`.
4060 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4061 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004062 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004063 }
4064 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4065 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004066 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004067 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004068
4069 DECLARE_INSTRUCTION(Shr);
4070
4071 private:
4072 DISALLOW_COPY_AND_ASSIGN(HShr);
4073};
4074
4075class HUShr : public HBinaryOperation {
4076 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004077 HUShr(Primitive::Type result_type,
4078 HInstruction* left,
4079 HInstruction* right,
4080 uint32_t dex_pc = kNoDexPc)
4081 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004082
Roland Levillain9867bc72015-08-05 10:21:34 +01004083 template <typename T, typename U, typename V>
4084 T Compute(T x, U y, V max_shift_value) const {
4085 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4086 "V is not the unsigned integer type corresponding to T");
4087 V ux = static_cast<V>(x);
4088 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004089 }
4090
Roland Levillain9867bc72015-08-05 10:21:34 +01004091 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4092 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004093 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004094 }
4095 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4096 // case is handled as `x >>> static_cast<int>(y)`.
4097 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4098 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004099 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004100 }
4101 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4102 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004103 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004104 }
4105
4106 DECLARE_INSTRUCTION(UShr);
4107
4108 private:
4109 DISALLOW_COPY_AND_ASSIGN(HUShr);
4110};
4111
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004112class HAnd : public HBinaryOperation {
4113 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004114 HAnd(Primitive::Type result_type,
4115 HInstruction* left,
4116 HInstruction* right,
4117 uint32_t dex_pc = kNoDexPc)
4118 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004119
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004120 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004121
Roland Levillain9867bc72015-08-05 10:21:34 +01004122 template <typename T, typename U>
4123 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4124
4125 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004126 return GetBlock()->GetGraph()->GetIntConstant(
4127 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004128 }
4129 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004130 return GetBlock()->GetGraph()->GetLongConstant(
4131 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004132 }
4133 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004134 return GetBlock()->GetGraph()->GetLongConstant(
4135 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004136 }
4137 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004138 return GetBlock()->GetGraph()->GetLongConstant(
4139 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004140 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004141
4142 DECLARE_INSTRUCTION(And);
4143
4144 private:
4145 DISALLOW_COPY_AND_ASSIGN(HAnd);
4146};
4147
4148class HOr : public HBinaryOperation {
4149 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004150 HOr(Primitive::Type result_type,
4151 HInstruction* left,
4152 HInstruction* right,
4153 uint32_t dex_pc = kNoDexPc)
4154 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004155
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004156 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004157
Roland Levillain9867bc72015-08-05 10:21:34 +01004158 template <typename T, typename U>
4159 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4160
4161 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004162 return GetBlock()->GetGraph()->GetIntConstant(
4163 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004164 }
4165 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004166 return GetBlock()->GetGraph()->GetLongConstant(
4167 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004168 }
4169 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004170 return GetBlock()->GetGraph()->GetLongConstant(
4171 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004172 }
4173 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004174 return GetBlock()->GetGraph()->GetLongConstant(
4175 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004176 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004177
4178 DECLARE_INSTRUCTION(Or);
4179
4180 private:
4181 DISALLOW_COPY_AND_ASSIGN(HOr);
4182};
4183
4184class HXor : public HBinaryOperation {
4185 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004186 HXor(Primitive::Type result_type,
4187 HInstruction* left,
4188 HInstruction* right,
4189 uint32_t dex_pc = kNoDexPc)
4190 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004191
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004192 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004193
Roland Levillain9867bc72015-08-05 10:21:34 +01004194 template <typename T, typename U>
4195 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4196
4197 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004198 return GetBlock()->GetGraph()->GetIntConstant(
4199 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004200 }
4201 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004202 return GetBlock()->GetGraph()->GetLongConstant(
4203 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004204 }
4205 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004206 return GetBlock()->GetGraph()->GetLongConstant(
4207 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004208 }
4209 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004210 return GetBlock()->GetGraph()->GetLongConstant(
4211 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004212 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004213
4214 DECLARE_INSTRUCTION(Xor);
4215
4216 private:
4217 DISALLOW_COPY_AND_ASSIGN(HXor);
4218};
4219
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004220class HRor : public HBinaryOperation {
4221 public:
4222 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4223 : HBinaryOperation(result_type, value, distance) {}
4224
4225 template <typename T, typename U, typename V>
4226 T Compute(T x, U y, V max_shift_value) const {
4227 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4228 "V is not the unsigned integer type corresponding to T");
4229 V ux = static_cast<V>(x);
4230 if ((y & max_shift_value) == 0) {
4231 return static_cast<T>(ux);
4232 } else {
4233 const V reg_bits = sizeof(T) * 8;
4234 return static_cast<T>(ux >> (y & max_shift_value)) |
4235 (x << (reg_bits - (y & max_shift_value)));
4236 }
4237 }
4238
4239 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4240 return GetBlock()->GetGraph()->GetIntConstant(
4241 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4242 }
4243 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4244 return GetBlock()->GetGraph()->GetLongConstant(
4245 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4246 }
4247 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4248 return GetBlock()->GetGraph()->GetLongConstant(
4249 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4250 }
4251
4252 DECLARE_INSTRUCTION(Ror);
4253
4254 private:
4255 DISALLOW_COPY_AND_ASSIGN(HRor);
4256};
4257
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004258// The value of a parameter in this method. Its location depends on
4259// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004260class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004261 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004262 HParameterValue(const DexFile& dex_file,
4263 uint16_t type_index,
4264 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004265 Primitive::Type parameter_type,
4266 bool is_this = false)
4267 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004268 dex_file_(dex_file),
4269 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004270 index_(index),
4271 is_this_(is_this),
4272 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004273
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004274 const DexFile& GetDexFile() const { return dex_file_; }
4275 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004276 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004277 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004278
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004279 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4280 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004281
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004282 DECLARE_INSTRUCTION(ParameterValue);
4283
4284 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004285 const DexFile& dex_file_;
4286 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004287 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004288 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004289 const uint8_t index_;
4290
Calin Juravle10e244f2015-01-26 18:54:32 +00004291 // Whether or not the parameter value corresponds to 'this' argument.
4292 const bool is_this_;
4293
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004294 bool can_be_null_;
4295
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004296 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4297};
4298
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004299class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004300 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004301 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4302 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004303
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004304 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004305 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004306 return true;
4307 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004308
Roland Levillain9867bc72015-08-05 10:21:34 +01004309 template <typename T> T Compute(T x) const { return ~x; }
4310
4311 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004312 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004313 }
4314 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004315 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004316 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004317
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004318 DECLARE_INSTRUCTION(Not);
4319
4320 private:
4321 DISALLOW_COPY_AND_ASSIGN(HNot);
4322};
4323
David Brazdil66d126e2015-04-03 16:02:44 +01004324class HBooleanNot : public HUnaryOperation {
4325 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004326 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4327 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004328
4329 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004330 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004331 return true;
4332 }
4333
Roland Levillain9867bc72015-08-05 10:21:34 +01004334 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004335 DCHECK(IsUint<1>(x));
4336 return !x;
4337 }
4338
Roland Levillain9867bc72015-08-05 10:21:34 +01004339 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004340 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004341 }
4342 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4343 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004344 UNREACHABLE();
4345 }
4346
4347 DECLARE_INSTRUCTION(BooleanNot);
4348
4349 private:
4350 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4351};
4352
Roland Levillaindff1f282014-11-05 14:15:05 +00004353class HTypeConversion : public HExpression<1> {
4354 public:
4355 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004356 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004357 : HExpression(result_type,
4358 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4359 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004360 SetRawInputAt(0, input);
4361 DCHECK_NE(input->GetType(), result_type);
4362 }
4363
4364 HInstruction* GetInput() const { return InputAt(0); }
4365 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4366 Primitive::Type GetResultType() const { return GetType(); }
4367
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004368 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004369 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004370
Roland Levillaindff1f282014-11-05 14:15:05 +00004371 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004372 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004373
Mark Mendelle82549b2015-05-06 10:55:34 -04004374 // Try to statically evaluate the conversion and return a HConstant
4375 // containing the result. If the input cannot be converted, return nullptr.
4376 HConstant* TryStaticEvaluation() const;
4377
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004378 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4379 Primitive::Type result_type) {
4380 // Some architectures may not require the 'GC' side effects, but at this point
4381 // in the compilation process we do not know what architecture we will
4382 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004383 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4384 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004385 return SideEffects::CanTriggerGC();
4386 }
4387 return SideEffects::None();
4388 }
4389
Roland Levillaindff1f282014-11-05 14:15:05 +00004390 DECLARE_INSTRUCTION(TypeConversion);
4391
4392 private:
4393 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4394};
4395
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004396static constexpr uint32_t kNoRegNumber = -1;
4397
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004398class HPhi : public HInstruction {
4399 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004400 HPhi(ArenaAllocator* arena,
4401 uint32_t reg_number,
4402 size_t number_of_inputs,
4403 Primitive::Type type,
4404 uint32_t dex_pc = kNoDexPc)
4405 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004406 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004407 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004408 type_(ToPhiType(type)),
4409 // Phis are constructed live and marked dead if conflicting or unused.
4410 // Individual steps of SsaBuilder should assume that if a phi has been
4411 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4412 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004413 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004414 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004415 }
4416
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004417 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4418 static Primitive::Type ToPhiType(Primitive::Type type) {
4419 switch (type) {
4420 case Primitive::kPrimBoolean:
4421 case Primitive::kPrimByte:
4422 case Primitive::kPrimShort:
4423 case Primitive::kPrimChar:
4424 return Primitive::kPrimInt;
4425 default:
4426 return type;
4427 }
4428 }
4429
David Brazdilffee3d32015-07-06 11:48:53 +01004430 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4431
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004432 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004433
4434 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004435 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004436
Calin Juravle10e244f2015-01-26 18:54:32 +00004437 Primitive::Type GetType() const OVERRIDE { return type_; }
Alex Light68289a52015-12-15 17:30:30 -08004438 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004439
Calin Juravle10e244f2015-01-26 18:54:32 +00004440 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4441 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4442
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004443 uint32_t GetRegNumber() const { return reg_number_; }
4444
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004445 void SetDead() { is_live_ = false; }
4446 void SetLive() { is_live_ = true; }
4447 bool IsDead() const { return !is_live_; }
4448 bool IsLive() const { return is_live_; }
4449
David Brazdil77a48ae2015-09-15 12:34:04 +00004450 bool IsVRegEquivalentOf(HInstruction* other) const {
4451 return other != nullptr
4452 && other->IsPhi()
4453 && other->AsPhi()->GetBlock() == GetBlock()
4454 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4455 }
4456
Calin Juravlea4f88312015-04-16 12:57:19 +01004457 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4458 // An equivalent phi is a phi having the same dex register and type.
4459 // It assumes that phis with the same dex register are adjacent.
4460 HPhi* GetNextEquivalentPhiWithSameType() {
4461 HInstruction* next = GetNext();
4462 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4463 if (next->GetType() == GetType()) {
4464 return next->AsPhi();
4465 }
4466 next = next->GetNext();
4467 }
4468 return nullptr;
4469 }
4470
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004471 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004472
David Brazdil1abb4192015-02-17 18:33:36 +00004473 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004474 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004475 return inputs_[index];
4476 }
David Brazdil1abb4192015-02-17 18:33:36 +00004477
4478 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004479 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004480 }
4481
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004482 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004483 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004484 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004485 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004486 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004487 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004488
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004489 DISALLOW_COPY_AND_ASSIGN(HPhi);
4490};
4491
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004492class HNullCheck : public HExpression<1> {
4493 public:
4494 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004495 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004496 SetRawInputAt(0, value);
4497 }
4498
Calin Juravle10e244f2015-01-26 18:54:32 +00004499 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004500 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004501 return true;
4502 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004503
Calin Juravle10e244f2015-01-26 18:54:32 +00004504 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004505
Calin Juravle10e244f2015-01-26 18:54:32 +00004506 bool CanThrow() const OVERRIDE { return true; }
4507
4508 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004509
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004510
4511 DECLARE_INSTRUCTION(NullCheck);
4512
4513 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004514 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4515};
4516
4517class FieldInfo : public ValueObject {
4518 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004519 FieldInfo(MemberOffset field_offset,
4520 Primitive::Type field_type,
4521 bool is_volatile,
4522 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004523 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004524 const DexFile& dex_file,
4525 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004526 : field_offset_(field_offset),
4527 field_type_(field_type),
4528 is_volatile_(is_volatile),
4529 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004530 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004531 dex_file_(dex_file),
4532 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004533
4534 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004535 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004536 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004537 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004538 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004539 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004540 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004541
4542 private:
4543 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004544 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004545 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004546 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004547 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004548 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004549 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004550};
4551
4552class HInstanceFieldGet : public HExpression<1> {
4553 public:
4554 HInstanceFieldGet(HInstruction* value,
4555 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004556 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004557 bool is_volatile,
4558 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004559 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004560 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004561 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004562 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004563 : HExpression(field_type,
4564 SideEffects::FieldReadOfType(field_type, is_volatile),
4565 dex_pc),
4566 field_info_(field_offset,
4567 field_type,
4568 is_volatile,
4569 field_idx,
4570 declaring_class_def_index,
4571 dex_file,
4572 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004573 SetRawInputAt(0, value);
4574 }
4575
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004576 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004577
4578 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4579 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4580 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004581 }
4582
Calin Juravle641547a2015-04-21 22:08:51 +01004583 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4584 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004585 }
4586
4587 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004588 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4589 }
4590
Calin Juravle52c48962014-12-16 17:02:57 +00004591 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004592 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004593 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004594 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004595
4596 DECLARE_INSTRUCTION(InstanceFieldGet);
4597
4598 private:
4599 const FieldInfo field_info_;
4600
4601 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4602};
4603
4604class HInstanceFieldSet : public HTemplateInstruction<2> {
4605 public:
4606 HInstanceFieldSet(HInstruction* object,
4607 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004608 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004609 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004610 bool is_volatile,
4611 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004612 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004613 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004614 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004615 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004616 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4617 dex_pc),
4618 field_info_(field_offset,
4619 field_type,
4620 is_volatile,
4621 field_idx,
4622 declaring_class_def_index,
4623 dex_file,
4624 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004625 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004626 SetRawInputAt(0, object);
4627 SetRawInputAt(1, value);
4628 }
4629
Calin Juravle641547a2015-04-21 22:08:51 +01004630 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4631 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004632 }
4633
Calin Juravle52c48962014-12-16 17:02:57 +00004634 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004635 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004636 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004637 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004638 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004639 bool GetValueCanBeNull() const { return value_can_be_null_; }
4640 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004641
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004642 DECLARE_INSTRUCTION(InstanceFieldSet);
4643
4644 private:
4645 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004646 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004647
4648 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4649};
4650
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004651class HArrayGet : public HExpression<2> {
4652 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004653 HArrayGet(HInstruction* array,
4654 HInstruction* index,
4655 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004656 uint32_t dex_pc,
4657 SideEffects additional_side_effects = SideEffects::None())
4658 : HExpression(type,
4659 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004660 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004661 SetRawInputAt(0, array);
4662 SetRawInputAt(1, index);
4663 }
4664
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004665 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004666 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004667 return true;
4668 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004669 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004670 // TODO: We can be smarter here.
4671 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4672 // which generates the implicit null check. There are cases when these can be removed
4673 // to produce better code. If we ever add optimizations to do so we should allow an
4674 // implicit check here (as long as the address falls in the first page).
4675 return false;
4676 }
4677
Alex Light68289a52015-12-15 17:30:30 -08004678 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004679
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004680 HInstruction* GetArray() const { return InputAt(0); }
4681 HInstruction* GetIndex() const { return InputAt(1); }
4682
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004683 DECLARE_INSTRUCTION(ArrayGet);
4684
4685 private:
4686 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4687};
4688
4689class HArraySet : public HTemplateInstruction<3> {
4690 public:
4691 HArraySet(HInstruction* array,
4692 HInstruction* index,
4693 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004694 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004695 uint32_t dex_pc,
4696 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004697 : HTemplateInstruction(
4698 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004699 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4700 additional_side_effects),
4701 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004702 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004703 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004704 value_can_be_null_(true),
4705 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004706 SetRawInputAt(0, array);
4707 SetRawInputAt(1, index);
4708 SetRawInputAt(2, value);
4709 }
4710
Calin Juravle77520bc2015-01-12 18:45:46 +00004711 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004712 // We currently always call a runtime method to catch array store
4713 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004714 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004715 }
4716
Mingyao Yang81014cb2015-06-02 03:16:27 -07004717 // Can throw ArrayStoreException.
4718 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4719
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004720 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004721 // TODO: Same as for ArrayGet.
4722 return false;
4723 }
4724
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004725 void ClearNeedsTypeCheck() {
4726 needs_type_check_ = false;
4727 }
4728
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004729 void ClearValueCanBeNull() {
4730 value_can_be_null_ = false;
4731 }
4732
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004733 void SetStaticTypeOfArrayIsObjectArray() {
4734 static_type_of_array_is_object_array_ = true;
4735 }
4736
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004737 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004738 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004739 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004740
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004741 HInstruction* GetArray() const { return InputAt(0); }
4742 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004743 HInstruction* GetValue() const { return InputAt(2); }
4744
4745 Primitive::Type GetComponentType() const {
4746 // The Dex format does not type floating point index operations. Since the
4747 // `expected_component_type_` is set during building and can therefore not
4748 // be correct, we also check what is the value type. If it is a floating
4749 // point type, we must use that type.
4750 Primitive::Type value_type = GetValue()->GetType();
4751 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4752 ? value_type
4753 : expected_component_type_;
4754 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004755
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004756 Primitive::Type GetRawExpectedComponentType() const {
4757 return expected_component_type_;
4758 }
4759
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004760 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4761 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4762 }
4763
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004764 DECLARE_INSTRUCTION(ArraySet);
4765
4766 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004767 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004768 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004769 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004770 // Cached information for the reference_type_info_ so that codegen
4771 // does not need to inspect the static type.
4772 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004773
4774 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4775};
4776
4777class HArrayLength : public HExpression<1> {
4778 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004779 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004780 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004781 // Note that arrays do not change length, so the instruction does not
4782 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004783 SetRawInputAt(0, array);
4784 }
4785
Calin Juravle77520bc2015-01-12 18:45:46 +00004786 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004787 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004788 return true;
4789 }
Calin Juravle641547a2015-04-21 22:08:51 +01004790 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4791 return obj == InputAt(0);
4792 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004793
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004794 DECLARE_INSTRUCTION(ArrayLength);
4795
4796 private:
4797 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4798};
4799
4800class HBoundsCheck : public HExpression<2> {
4801 public:
4802 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004803 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004804 DCHECK(index->GetType() == Primitive::kPrimInt);
4805 SetRawInputAt(0, index);
4806 SetRawInputAt(1, length);
4807 }
4808
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004809 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004810 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004811 return true;
4812 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004813
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004814 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004815
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004816 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004817
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004818 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004819
4820 DECLARE_INSTRUCTION(BoundsCheck);
4821
4822 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004823 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4824};
4825
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004826/**
4827 * Some DEX instructions are folded into multiple HInstructions that need
4828 * to stay live until the last HInstruction. This class
4829 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004830 * HInstruction stays live. `index` represents the stack location index of the
4831 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004832 */
4833class HTemporary : public HTemplateInstruction<0> {
4834 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004835 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4836 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004837
4838 size_t GetIndex() const { return index_; }
4839
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004840 Primitive::Type GetType() const OVERRIDE {
4841 // The previous instruction is the one that will be stored in the temporary location.
4842 DCHECK(GetPrevious() != nullptr);
4843 return GetPrevious()->GetType();
4844 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004845
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004846 DECLARE_INSTRUCTION(Temporary);
4847
4848 private:
4849 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004850 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4851};
4852
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004853class HSuspendCheck : public HTemplateInstruction<0> {
4854 public:
4855 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004856 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004857
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004858 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004859 return true;
4860 }
4861
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004862 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4863 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004864
4865 DECLARE_INSTRUCTION(SuspendCheck);
4866
4867 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004868 // Only used for code generation, in order to share the same slow path between back edges
4869 // of a same loop.
4870 SlowPathCode* slow_path_;
4871
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004872 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4873};
4874
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004875/**
4876 * Instruction to load a Class object.
4877 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004878class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004879 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004880 HLoadClass(HCurrentMethod* current_method,
4881 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004882 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004883 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004884 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004885 bool needs_access_check,
4886 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004887 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004888 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004889 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004890 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004891 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004892 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004893 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004894 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004895 // Referrers class should not need access check. We never inline unverified
4896 // methods so we can't possibly end up in this situation.
4897 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004898 SetRawInputAt(0, current_method);
4899 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004900
4901 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004902
4903 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004904 // Note that we don't need to test for generate_clinit_check_.
4905 // Whether or not we need to generate the clinit check is processed in
4906 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004907 return other->AsLoadClass()->type_index_ == type_index_ &&
4908 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004909 }
4910
4911 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4912
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004913 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004914 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004915 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004916
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004917 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004918 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004919 }
4920
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004921 bool MustGenerateClinitCheck() const {
4922 return generate_clinit_check_;
4923 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004924
Calin Juravle0ba218d2015-05-19 18:46:01 +01004925 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004926 // The entrypoint the code generator is going to call does not do
4927 // clinit of the class.
4928 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004929 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004930 }
4931
4932 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004933 return MustGenerateClinitCheck() ||
4934 (!is_referrers_class_ && !is_in_dex_cache_) ||
4935 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004936 }
4937
4938 bool NeedsAccessCheck() const {
4939 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004940 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004941
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004942 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004943 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004944 }
4945
Calin Juravleacf735c2015-02-12 15:25:22 +00004946 ReferenceTypeInfo GetLoadedClassRTI() {
4947 return loaded_class_rti_;
4948 }
4949
4950 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4951 // Make sure we only set exact types (the loaded class should never be merged).
4952 DCHECK(rti.IsExact());
4953 loaded_class_rti_ = rti;
4954 }
4955
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004956 const DexFile& GetDexFile() { return dex_file_; }
4957
Vladimir Markodc151b22015-10-15 18:02:30 +01004958 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004959
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004960 static SideEffects SideEffectsForArchRuntimeCalls() {
4961 return SideEffects::CanTriggerGC();
4962 }
4963
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004964 bool IsInDexCache() const { return is_in_dex_cache_; }
4965
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004966 DECLARE_INSTRUCTION(LoadClass);
4967
4968 private:
4969 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004970 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004971 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004972 // Whether this instruction must generate the initialization check.
4973 // Used for code generation.
4974 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004975 const bool needs_access_check_;
4976 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004977
Calin Juravleacf735c2015-02-12 15:25:22 +00004978 ReferenceTypeInfo loaded_class_rti_;
4979
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004980 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4981};
4982
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004983class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004984 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004985 HLoadString(HCurrentMethod* current_method,
4986 uint32_t string_index,
4987 uint32_t dex_pc,
4988 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004989 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004990 string_index_(string_index),
4991 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004992 SetRawInputAt(0, current_method);
4993 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004994
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004995 bool CanBeMoved() const OVERRIDE { return true; }
4996
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004997 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4998 return other->AsLoadString()->string_index_ == string_index_;
4999 }
5000
5001 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5002
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005003 uint32_t GetStringIndex() const { return string_index_; }
5004
5005 // TODO: Can we deopt or debug when we resolve a string?
5006 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01005007 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005008 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005009 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005010
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005011 static SideEffects SideEffectsForArchRuntimeCalls() {
5012 return SideEffects::CanTriggerGC();
5013 }
5014
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005015 DECLARE_INSTRUCTION(LoadString);
5016
5017 private:
5018 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005019 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005020
5021 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5022};
5023
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005024/**
5025 * Performs an initialization check on its Class object input.
5026 */
5027class HClinitCheck : public HExpression<1> {
5028 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005029 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005030 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005031 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005032 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5033 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005034 SetRawInputAt(0, constant);
5035 }
5036
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005037 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005038 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005039 return true;
5040 }
5041
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005042 bool NeedsEnvironment() const OVERRIDE {
5043 // May call runtime to initialize the class.
5044 return true;
5045 }
5046
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005047 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005048
5049 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5050
5051 DECLARE_INSTRUCTION(ClinitCheck);
5052
5053 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005054 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5055};
5056
5057class HStaticFieldGet : public HExpression<1> {
5058 public:
5059 HStaticFieldGet(HInstruction* cls,
5060 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005061 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005062 bool is_volatile,
5063 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005064 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005065 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005066 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005067 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005068 : HExpression(field_type,
5069 SideEffects::FieldReadOfType(field_type, is_volatile),
5070 dex_pc),
5071 field_info_(field_offset,
5072 field_type,
5073 is_volatile,
5074 field_idx,
5075 declaring_class_def_index,
5076 dex_file,
5077 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005078 SetRawInputAt(0, cls);
5079 }
5080
Calin Juravle52c48962014-12-16 17:02:57 +00005081
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005082 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005083
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005084 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005085 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5086 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005087 }
5088
5089 size_t ComputeHashCode() const OVERRIDE {
5090 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5091 }
5092
Calin Juravle52c48962014-12-16 17:02:57 +00005093 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005094 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5095 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005096 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005097
5098 DECLARE_INSTRUCTION(StaticFieldGet);
5099
5100 private:
5101 const FieldInfo field_info_;
5102
5103 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5104};
5105
5106class HStaticFieldSet : public HTemplateInstruction<2> {
5107 public:
5108 HStaticFieldSet(HInstruction* cls,
5109 HInstruction* value,
5110 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005111 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005112 bool is_volatile,
5113 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005114 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005115 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005116 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005117 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005118 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5119 dex_pc),
5120 field_info_(field_offset,
5121 field_type,
5122 is_volatile,
5123 field_idx,
5124 declaring_class_def_index,
5125 dex_file,
5126 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005127 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005128 SetRawInputAt(0, cls);
5129 SetRawInputAt(1, value);
5130 }
5131
Calin Juravle52c48962014-12-16 17:02:57 +00005132 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005133 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5134 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005135 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005136
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005137 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005138 bool GetValueCanBeNull() const { return value_can_be_null_; }
5139 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005140
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005141 DECLARE_INSTRUCTION(StaticFieldSet);
5142
5143 private:
5144 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005145 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005146
5147 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5148};
5149
Calin Juravlee460d1d2015-09-29 04:52:17 +01005150class HUnresolvedInstanceFieldGet : public HExpression<1> {
5151 public:
5152 HUnresolvedInstanceFieldGet(HInstruction* obj,
5153 Primitive::Type field_type,
5154 uint32_t field_index,
5155 uint32_t dex_pc)
5156 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5157 field_index_(field_index) {
5158 SetRawInputAt(0, obj);
5159 }
5160
5161 bool NeedsEnvironment() const OVERRIDE { return true; }
5162 bool CanThrow() const OVERRIDE { return true; }
5163
5164 Primitive::Type GetFieldType() const { return GetType(); }
5165 uint32_t GetFieldIndex() const { return field_index_; }
5166
5167 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5168
5169 private:
5170 const uint32_t field_index_;
5171
5172 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5173};
5174
5175class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5176 public:
5177 HUnresolvedInstanceFieldSet(HInstruction* obj,
5178 HInstruction* value,
5179 Primitive::Type field_type,
5180 uint32_t field_index,
5181 uint32_t dex_pc)
5182 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5183 field_type_(field_type),
5184 field_index_(field_index) {
5185 DCHECK_EQ(field_type, value->GetType());
5186 SetRawInputAt(0, obj);
5187 SetRawInputAt(1, value);
5188 }
5189
5190 bool NeedsEnvironment() const OVERRIDE { return true; }
5191 bool CanThrow() const OVERRIDE { return true; }
5192
5193 Primitive::Type GetFieldType() const { return field_type_; }
5194 uint32_t GetFieldIndex() const { return field_index_; }
5195
5196 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5197
5198 private:
5199 const Primitive::Type field_type_;
5200 const uint32_t field_index_;
5201
5202 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5203};
5204
5205class HUnresolvedStaticFieldGet : public HExpression<0> {
5206 public:
5207 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5208 uint32_t field_index,
5209 uint32_t dex_pc)
5210 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5211 field_index_(field_index) {
5212 }
5213
5214 bool NeedsEnvironment() const OVERRIDE { return true; }
5215 bool CanThrow() const OVERRIDE { return true; }
5216
5217 Primitive::Type GetFieldType() const { return GetType(); }
5218 uint32_t GetFieldIndex() const { return field_index_; }
5219
5220 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5221
5222 private:
5223 const uint32_t field_index_;
5224
5225 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5226};
5227
5228class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5229 public:
5230 HUnresolvedStaticFieldSet(HInstruction* value,
5231 Primitive::Type field_type,
5232 uint32_t field_index,
5233 uint32_t dex_pc)
5234 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5235 field_type_(field_type),
5236 field_index_(field_index) {
5237 DCHECK_EQ(field_type, value->GetType());
5238 SetRawInputAt(0, value);
5239 }
5240
5241 bool NeedsEnvironment() const OVERRIDE { return true; }
5242 bool CanThrow() const OVERRIDE { return true; }
5243
5244 Primitive::Type GetFieldType() const { return field_type_; }
5245 uint32_t GetFieldIndex() const { return field_index_; }
5246
5247 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5248
5249 private:
5250 const Primitive::Type field_type_;
5251 const uint32_t field_index_;
5252
5253 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5254};
5255
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005256// Implement the move-exception DEX instruction.
5257class HLoadException : public HExpression<0> {
5258 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005259 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5260 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005261
David Brazdilbbd733e2015-08-18 17:48:17 +01005262 bool CanBeNull() const OVERRIDE { return false; }
5263
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005264 DECLARE_INSTRUCTION(LoadException);
5265
5266 private:
5267 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5268};
5269
David Brazdilcb1c0552015-08-04 16:22:25 +01005270// Implicit part of move-exception which clears thread-local exception storage.
5271// Must not be removed because the runtime expects the TLS to get cleared.
5272class HClearException : public HTemplateInstruction<0> {
5273 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005274 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5275 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005276
5277 DECLARE_INSTRUCTION(ClearException);
5278
5279 private:
5280 DISALLOW_COPY_AND_ASSIGN(HClearException);
5281};
5282
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005283class HThrow : public HTemplateInstruction<1> {
5284 public:
5285 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005286 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005287 SetRawInputAt(0, exception);
5288 }
5289
5290 bool IsControlFlow() const OVERRIDE { return true; }
5291
5292 bool NeedsEnvironment() const OVERRIDE { return true; }
5293
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005294 bool CanThrow() const OVERRIDE { return true; }
5295
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005296
5297 DECLARE_INSTRUCTION(Throw);
5298
5299 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005300 DISALLOW_COPY_AND_ASSIGN(HThrow);
5301};
5302
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005303/**
5304 * Implementation strategies for the code generator of a HInstanceOf
5305 * or `HCheckCast`.
5306 */
5307enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005308 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005309 kExactCheck, // Can do a single class compare.
5310 kClassHierarchyCheck, // Can just walk the super class chain.
5311 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5312 kInterfaceCheck, // No optimization yet when checking against an interface.
5313 kArrayObjectCheck, // Can just check if the array is not primitive.
5314 kArrayCheck // No optimization yet when checking against a generic array.
5315};
5316
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005317class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005318 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005319 HInstanceOf(HInstruction* object,
5320 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005321 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005322 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005323 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005324 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005325 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005326 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005327 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005328 SetRawInputAt(0, object);
5329 SetRawInputAt(1, constant);
5330 }
5331
5332 bool CanBeMoved() const OVERRIDE { return true; }
5333
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005334 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005335 return true;
5336 }
5337
5338 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005339 return false;
5340 }
5341
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005342 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5343
5344 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005345
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005346 // Used only in code generation.
5347 bool MustDoNullCheck() const { return must_do_null_check_; }
5348 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5349
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005350 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5351 return (check_kind == TypeCheckKind::kExactCheck)
5352 ? SideEffects::None()
5353 // Mips currently does runtime calls for any other checks.
5354 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005355 }
5356
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005357 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005358
5359 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005360 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005361 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005362
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005363 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5364};
5365
Calin Juravleb1498f62015-02-16 13:13:29 +00005366class HBoundType : public HExpression<1> {
5367 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005368 // Constructs an HBoundType with the given upper_bound.
5369 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005370 HBoundType(HInstruction* input,
5371 ReferenceTypeInfo upper_bound,
5372 bool upper_can_be_null,
5373 uint32_t dex_pc = kNoDexPc)
5374 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005375 upper_bound_(upper_bound),
5376 upper_can_be_null_(upper_can_be_null),
5377 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005378 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005379 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005380 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005381 }
5382
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005383 // GetUpper* should only be used in reference type propagation.
5384 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5385 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005386
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005387 void SetCanBeNull(bool can_be_null) {
5388 DCHECK(upper_can_be_null_ || !can_be_null);
5389 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005390 }
5391
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005392 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5393
Calin Juravleb1498f62015-02-16 13:13:29 +00005394 DECLARE_INSTRUCTION(BoundType);
5395
5396 private:
5397 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005398 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5399 // It is used to bound the type in cases like:
5400 // if (x instanceof ClassX) {
5401 // // uper_bound_ will be ClassX
5402 // }
5403 const ReferenceTypeInfo upper_bound_;
5404 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5405 // is false then can_be_null_ cannot be true).
5406 const bool upper_can_be_null_;
5407 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005408
5409 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5410};
5411
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005412class HCheckCast : public HTemplateInstruction<2> {
5413 public:
5414 HCheckCast(HInstruction* object,
5415 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005416 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005417 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005418 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005419 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005420 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005421 SetRawInputAt(0, object);
5422 SetRawInputAt(1, constant);
5423 }
5424
5425 bool CanBeMoved() const OVERRIDE { return true; }
5426
5427 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5428 return true;
5429 }
5430
5431 bool NeedsEnvironment() const OVERRIDE {
5432 // Instruction may throw a CheckCastError.
5433 return true;
5434 }
5435
5436 bool CanThrow() const OVERRIDE { return true; }
5437
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005438 bool MustDoNullCheck() const { return must_do_null_check_; }
5439 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005440 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005441
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005442 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005443
5444 DECLARE_INSTRUCTION(CheckCast);
5445
5446 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005447 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005448 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005449
5450 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005451};
5452
Calin Juravle27df7582015-04-17 19:12:31 +01005453class HMemoryBarrier : public HTemplateInstruction<0> {
5454 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005455 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005456 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005457 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005458 barrier_kind_(barrier_kind) {}
5459
5460 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5461
5462 DECLARE_INSTRUCTION(MemoryBarrier);
5463
5464 private:
5465 const MemBarrierKind barrier_kind_;
5466
5467 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5468};
5469
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005470class HMonitorOperation : public HTemplateInstruction<1> {
5471 public:
5472 enum OperationKind {
5473 kEnter,
5474 kExit,
5475 };
5476
5477 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005478 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005479 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5480 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005481 SetRawInputAt(0, object);
5482 }
5483
5484 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005485 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5486
5487 bool CanThrow() const OVERRIDE {
5488 // Verifier guarantees that monitor-exit cannot throw.
5489 // This is important because it allows the HGraphBuilder to remove
5490 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5491 return IsEnter();
5492 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005493
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005494
5495 bool IsEnter() const { return kind_ == kEnter; }
5496
5497 DECLARE_INSTRUCTION(MonitorOperation);
5498
Calin Juravle52c48962014-12-16 17:02:57 +00005499 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005500 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005501
5502 private:
5503 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5504};
5505
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005506/**
5507 * A HInstruction used as a marker for the replacement of new + <init>
5508 * of a String to a call to a StringFactory. Only baseline will see
5509 * the node at code generation, where it will be be treated as null.
5510 * When compiling non-baseline, `HFakeString` instructions are being removed
5511 * in the instruction simplifier.
5512 */
5513class HFakeString : public HTemplateInstruction<0> {
5514 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005515 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5516 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005517
5518 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5519
5520 DECLARE_INSTRUCTION(FakeString);
5521
5522 private:
5523 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5524};
5525
Vladimir Markof9f64412015-09-02 14:05:49 +01005526class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005527 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005528 MoveOperands(Location source,
5529 Location destination,
5530 Primitive::Type type,
5531 HInstruction* instruction)
5532 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005533
5534 Location GetSource() const { return source_; }
5535 Location GetDestination() const { return destination_; }
5536
5537 void SetSource(Location value) { source_ = value; }
5538 void SetDestination(Location value) { destination_ = value; }
5539
5540 // The parallel move resolver marks moves as "in-progress" by clearing the
5541 // destination (but not the source).
5542 Location MarkPending() {
5543 DCHECK(!IsPending());
5544 Location dest = destination_;
5545 destination_ = Location::NoLocation();
5546 return dest;
5547 }
5548
5549 void ClearPending(Location dest) {
5550 DCHECK(IsPending());
5551 destination_ = dest;
5552 }
5553
5554 bool IsPending() const {
5555 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5556 return destination_.IsInvalid() && !source_.IsInvalid();
5557 }
5558
5559 // True if this blocks a move from the given location.
5560 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005561 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005562 }
5563
5564 // A move is redundant if it's been eliminated, if its source and
5565 // destination are the same, or if its destination is unneeded.
5566 bool IsRedundant() const {
5567 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5568 }
5569
5570 // We clear both operands to indicate move that's been eliminated.
5571 void Eliminate() {
5572 source_ = destination_ = Location::NoLocation();
5573 }
5574
5575 bool IsEliminated() const {
5576 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5577 return source_.IsInvalid();
5578 }
5579
Alexey Frunze4dda3372015-06-01 18:31:49 -07005580 Primitive::Type GetType() const { return type_; }
5581
Nicolas Geoffray90218252015-04-15 11:56:51 +01005582 bool Is64BitMove() const {
5583 return Primitive::Is64BitType(type_);
5584 }
5585
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005586 HInstruction* GetInstruction() const { return instruction_; }
5587
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005588 private:
5589 Location source_;
5590 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005591 // The type this move is for.
5592 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005593 // The instruction this move is assocatied with. Null when this move is
5594 // for moving an input in the expected locations of user (including a phi user).
5595 // This is only used in debug mode, to ensure we do not connect interval siblings
5596 // in the same parallel move.
5597 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005598};
5599
5600static constexpr size_t kDefaultNumberOfMoves = 4;
5601
5602class HParallelMove : public HTemplateInstruction<0> {
5603 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005604 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005605 : HTemplateInstruction(SideEffects::None(), dex_pc),
5606 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5607 moves_.reserve(kDefaultNumberOfMoves);
5608 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005609
Nicolas Geoffray90218252015-04-15 11:56:51 +01005610 void AddMove(Location source,
5611 Location destination,
5612 Primitive::Type type,
5613 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005614 DCHECK(source.IsValid());
5615 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005616 if (kIsDebugBuild) {
5617 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005618 for (const MoveOperands& move : moves_) {
5619 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005620 // Special case the situation where the move is for the spill slot
5621 // of the instruction.
5622 if ((GetPrevious() == instruction)
5623 || ((GetPrevious() == nullptr)
5624 && instruction->IsPhi()
5625 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005626 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005627 << "Doing parallel moves for the same instruction.";
5628 } else {
5629 DCHECK(false) << "Doing parallel moves for the same instruction.";
5630 }
5631 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005632 }
5633 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005634 for (const MoveOperands& move : moves_) {
5635 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005636 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005637 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005638 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005639 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005640 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005641 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005642 }
5643
Vladimir Marko225b6462015-09-28 12:17:40 +01005644 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005645 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005646 }
5647
Vladimir Marko225b6462015-09-28 12:17:40 +01005648 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005649
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005650 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005651
5652 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005653 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005654
5655 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5656};
5657
Mark Mendell0616ae02015-04-17 12:49:27 -04005658} // namespace art
5659
Vladimir Markob4536b72015-11-24 13:45:23 +00005660#ifdef ART_ENABLE_CODEGEN_arm
5661#include "nodes_arm.h"
5662#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005663#ifdef ART_ENABLE_CODEGEN_arm64
5664#include "nodes_arm64.h"
5665#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005666#ifdef ART_ENABLE_CODEGEN_x86
5667#include "nodes_x86.h"
5668#endif
5669
5670namespace art {
5671
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005672class HGraphVisitor : public ValueObject {
5673 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005674 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5675 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005676
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005677 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005678 virtual void VisitBasicBlock(HBasicBlock* block);
5679
Roland Levillain633021e2014-10-01 14:12:25 +01005680 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005681 void VisitInsertionOrder();
5682
Roland Levillain633021e2014-10-01 14:12:25 +01005683 // Visit the graph following dominator tree reverse post-order.
5684 void VisitReversePostOrder();
5685
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005686 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005687
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005688 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005689#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005690 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5691
5692 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5693
5694#undef DECLARE_VISIT_INSTRUCTION
5695
5696 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005697 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005698
5699 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5700};
5701
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005702class HGraphDelegateVisitor : public HGraphVisitor {
5703 public:
5704 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5705 virtual ~HGraphDelegateVisitor() {}
5706
5707 // Visit functions that delegate to to super class.
5708#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005709 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005710
5711 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5712
5713#undef DECLARE_VISIT_INSTRUCTION
5714
5715 private:
5716 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5717};
5718
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005719class HInsertionOrderIterator : public ValueObject {
5720 public:
5721 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5722
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005723 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005724 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005725 void Advance() { ++index_; }
5726
5727 private:
5728 const HGraph& graph_;
5729 size_t index_;
5730
5731 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5732};
5733
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005734class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005735 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005736 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5737 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005738 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005739 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005740
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005741 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5742 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005743 void Advance() { ++index_; }
5744
5745 private:
5746 const HGraph& graph_;
5747 size_t index_;
5748
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005749 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005750};
5751
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005752class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005753 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005754 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005755 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005756 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005757 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005758 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005759
5760 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005761 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005762 void Advance() { --index_; }
5763
5764 private:
5765 const HGraph& graph_;
5766 size_t index_;
5767
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005768 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005769};
5770
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005771class HLinearPostOrderIterator : public ValueObject {
5772 public:
5773 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005774 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005775
5776 bool Done() const { return index_ == 0; }
5777
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005778 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005779
5780 void Advance() {
5781 --index_;
5782 DCHECK_GE(index_, 0U);
5783 }
5784
5785 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005786 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005787 size_t index_;
5788
5789 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5790};
5791
5792class HLinearOrderIterator : public ValueObject {
5793 public:
5794 explicit HLinearOrderIterator(const HGraph& graph)
5795 : order_(graph.GetLinearOrder()), index_(0) {}
5796
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005797 bool Done() const { return index_ == order_.size(); }
5798 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005799 void Advance() { ++index_; }
5800
5801 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005802 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005803 size_t index_;
5804
5805 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5806};
5807
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005808// Iterator over the blocks that art part of the loop. Includes blocks part
5809// of an inner loop. The order in which the blocks are iterated is on their
5810// block id.
5811class HBlocksInLoopIterator : public ValueObject {
5812 public:
5813 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5814 : blocks_in_loop_(info.GetBlocks()),
5815 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5816 index_(0) {
5817 if (!blocks_in_loop_.IsBitSet(index_)) {
5818 Advance();
5819 }
5820 }
5821
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005822 bool Done() const { return index_ == blocks_.size(); }
5823 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005824 void Advance() {
5825 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005826 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005827 if (blocks_in_loop_.IsBitSet(index_)) {
5828 break;
5829 }
5830 }
5831 }
5832
5833 private:
5834 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005835 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005836 size_t index_;
5837
5838 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5839};
5840
Mingyao Yang3584bce2015-05-19 16:01:59 -07005841// Iterator over the blocks that art part of the loop. Includes blocks part
5842// of an inner loop. The order in which the blocks are iterated is reverse
5843// post order.
5844class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5845 public:
5846 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5847 : blocks_in_loop_(info.GetBlocks()),
5848 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5849 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005850 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005851 Advance();
5852 }
5853 }
5854
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005855 bool Done() const { return index_ == blocks_.size(); }
5856 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005857 void Advance() {
5858 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005859 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5860 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005861 break;
5862 }
5863 }
5864 }
5865
5866 private:
5867 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005868 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005869 size_t index_;
5870
5871 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5872};
5873
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005874inline int64_t Int64FromConstant(HConstant* constant) {
5875 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5876 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5877 : constant->AsLongConstant()->GetValue();
5878}
5879
Vladimir Marko58155012015-08-19 12:49:41 +00005880inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5881 // For the purposes of the compiler, the dex files must actually be the same object
5882 // if we want to safely treat them as the same. This is especially important for JIT
5883 // as custom class loaders can open the same underlying file (or memory) multiple
5884 // times and provide different class resolution but no two class loaders should ever
5885 // use the same DexFile object - doing so is an unsupported hack that can lead to
5886 // all sorts of weird failures.
5887 return &lhs == &rhs;
5888}
5889
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005890#define INSTRUCTION_TYPE_CHECK(type, super) \
5891 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
5892 inline const H##type* HInstruction::As##type() const { \
5893 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
5894 } \
5895 inline H##type* HInstruction::As##type() { \
5896 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
5897 }
5898
5899 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
5900#undef INSTRUCTION_TYPE_CHECK
5901
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005902} // namespace art
5903
5904#endif // ART_COMPILER_OPTIMIZING_NODES_H_