blob: ddd215dc5524521b4ed4813e4338007ce6cbee58 [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) \
David Srbecky0cf44932015-12-09 14:09:59 +00001069 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001070 M(Neg, UnaryOperation) \
1071 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001072 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001073 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001074 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001075 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001076 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001077 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001078 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001079 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001080 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001081 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001082 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001083 M(Return, Instruction) \
1084 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001085 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001086 M(Shl, BinaryOperation) \
1087 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001088 M(StaticFieldGet, Instruction) \
1089 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001090 M(UnresolvedInstanceFieldGet, Instruction) \
1091 M(UnresolvedInstanceFieldSet, Instruction) \
1092 M(UnresolvedStaticFieldGet, Instruction) \
1093 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001094 M(StoreLocal, Instruction) \
1095 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001096 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001097 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001098 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001099 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001100 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001101 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001102 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001103
Vladimir Markob4536b72015-11-24 13:45:23 +00001104#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001105#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001106#else
1107#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1108 M(ArmDexCacheArraysBase, Instruction)
1109#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001110
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001111#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001112#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001113#else
1114#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001115 M(Arm64DataProcWithShifterOp, Instruction) \
Alexandre Rames418318f2015-11-20 15:55:47 +00001116 M(Arm64IntermediateAddress, Instruction) \
1117 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001118#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001119
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001120#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1121
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001122#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1123
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001124#ifndef ART_ENABLE_CODEGEN_x86
1125#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1126#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001127#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1128 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001129 M(X86LoadFromConstantTable, Instruction) \
1130 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001131#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001132
1133#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1134
1135#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1136 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1137 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1138 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001139 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001140 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001141 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1142 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1143
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001144#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1145 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001146 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001147 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001148 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001149 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001150
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001151#define FOR_EACH_INSTRUCTION(M) \
1152 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1153 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1154
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001155#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001156FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1157#undef FORWARD_DECLARATION
1158
Roland Levillainccc07a92014-09-16 14:48:16 +01001159#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001160 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001161 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001162 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001163 return other->Is##type(); \
1164 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001165 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001166
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001167#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1168 bool Is##type() const { return As##type() != nullptr; } \
1169 const H##type* As##type() const { return this; } \
1170 H##type* As##type() { return this; }
1171
David Brazdiled596192015-01-23 10:39:45 +00001172template <typename T> class HUseList;
1173
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001174template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001175class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001176 public:
David Brazdiled596192015-01-23 10:39:45 +00001177 HUseListNode* GetPrevious() const { return prev_; }
1178 HUseListNode* GetNext() const { return next_; }
1179 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001180 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001181 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001182
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001183 private:
David Brazdiled596192015-01-23 10:39:45 +00001184 HUseListNode(T user, size_t index)
1185 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1186
1187 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001188 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001189 HUseListNode<T>* prev_;
1190 HUseListNode<T>* next_;
1191
1192 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001193
1194 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1195};
1196
David Brazdiled596192015-01-23 10:39:45 +00001197template <typename T>
1198class HUseList : public ValueObject {
1199 public:
1200 HUseList() : first_(nullptr) {}
1201
1202 void Clear() {
1203 first_ = nullptr;
1204 }
1205
1206 // Adds a new entry at the beginning of the use list and returns
1207 // the newly created node.
1208 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001209 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001210 if (IsEmpty()) {
1211 first_ = new_node;
1212 } else {
1213 first_->prev_ = new_node;
1214 new_node->next_ = first_;
1215 first_ = new_node;
1216 }
1217 return new_node;
1218 }
1219
1220 HUseListNode<T>* GetFirst() const {
1221 return first_;
1222 }
1223
1224 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001225 DCHECK(node != nullptr);
1226 DCHECK(Contains(node));
1227
David Brazdiled596192015-01-23 10:39:45 +00001228 if (node->prev_ != nullptr) {
1229 node->prev_->next_ = node->next_;
1230 }
1231 if (node->next_ != nullptr) {
1232 node->next_->prev_ = node->prev_;
1233 }
1234 if (node == first_) {
1235 first_ = node->next_;
1236 }
1237 }
1238
David Brazdil1abb4192015-02-17 18:33:36 +00001239 bool Contains(const HUseListNode<T>* node) const {
1240 if (node == nullptr) {
1241 return false;
1242 }
1243 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1244 if (current == node) {
1245 return true;
1246 }
1247 }
1248 return false;
1249 }
1250
David Brazdiled596192015-01-23 10:39:45 +00001251 bool IsEmpty() const {
1252 return first_ == nullptr;
1253 }
1254
1255 bool HasOnlyOneUse() const {
1256 return first_ != nullptr && first_->next_ == nullptr;
1257 }
1258
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001259 size_t SizeSlow() const {
1260 size_t count = 0;
1261 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1262 ++count;
1263 }
1264 return count;
1265 }
1266
David Brazdiled596192015-01-23 10:39:45 +00001267 private:
1268 HUseListNode<T>* first_;
1269};
1270
1271template<typename T>
1272class HUseIterator : public ValueObject {
1273 public:
1274 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1275
1276 bool Done() const { return current_ == nullptr; }
1277
1278 void Advance() {
1279 DCHECK(!Done());
1280 current_ = current_->GetNext();
1281 }
1282
1283 HUseListNode<T>* Current() const {
1284 DCHECK(!Done());
1285 return current_;
1286 }
1287
1288 private:
1289 HUseListNode<T>* current_;
1290
1291 friend class HValue;
1292};
1293
David Brazdil1abb4192015-02-17 18:33:36 +00001294// This class is used by HEnvironment and HInstruction classes to record the
1295// instructions they use and pointers to the corresponding HUseListNodes kept
1296// by the used instructions.
1297template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001298class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001299 public:
1300 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1301 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1302
1303 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1304 : instruction_(old_record.instruction_), use_node_(use_node) {
1305 DCHECK(instruction_ != nullptr);
1306 DCHECK(use_node_ != nullptr);
1307 DCHECK(old_record.use_node_ == nullptr);
1308 }
1309
1310 HInstruction* GetInstruction() const { return instruction_; }
1311 HUseListNode<T>* GetUseNode() const { return use_node_; }
1312
1313 private:
1314 // Instruction used by the user.
1315 HInstruction* instruction_;
1316
1317 // Corresponding entry in the use list kept by 'instruction_'.
1318 HUseListNode<T>* use_node_;
1319};
1320
Aart Bik854a02b2015-07-14 16:07:00 -07001321/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001322 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001323 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001324 * For write/read dependences on fields/arrays, the dependence analysis uses
1325 * type disambiguation (e.g. a float field write cannot modify the value of an
1326 * integer field read) and the access type (e.g. a reference array write cannot
1327 * modify the value of a reference field read [although it may modify the
1328 * reference fetch prior to reading the field, which is represented by its own
1329 * write/read dependence]). The analysis makes conservative points-to
1330 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1331 * the same, and any reference read depends on any reference read without
1332 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001333 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001334 * The internal representation uses 38-bit and is described in the table below.
1335 * The first line indicates the side effect, and for field/array accesses the
1336 * second line indicates the type of the access (in the order of the
1337 * Primitive::Type enum).
1338 * The two numbered lines below indicate the bit position in the bitfield (read
1339 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001340 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001341 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1342 * +-------------+---------+---------+--------------+---------+---------+
1343 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1344 * | 3 |333333322|222222221| 1 |111111110|000000000|
1345 * | 7 |654321098|765432109| 8 |765432109|876543210|
1346 *
1347 * Note that, to ease the implementation, 'changes' bits are least significant
1348 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001349 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001350class SideEffects : public ValueObject {
1351 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001352 SideEffects() : flags_(0) {}
1353
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001354 static SideEffects None() {
1355 return SideEffects(0);
1356 }
1357
1358 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001359 return SideEffects(kAllChangeBits | kAllDependOnBits);
1360 }
1361
1362 static SideEffects AllChanges() {
1363 return SideEffects(kAllChangeBits);
1364 }
1365
1366 static SideEffects AllDependencies() {
1367 return SideEffects(kAllDependOnBits);
1368 }
1369
1370 static SideEffects AllExceptGCDependency() {
1371 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1372 }
1373
1374 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001375 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001376 }
1377
Aart Bik34c3ba92015-07-20 14:08:59 -07001378 static SideEffects AllWrites() {
1379 return SideEffects(kAllWrites);
1380 }
1381
1382 static SideEffects AllReads() {
1383 return SideEffects(kAllReads);
1384 }
1385
1386 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1387 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001388 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001389 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001390 }
1391
Aart Bik854a02b2015-07-14 16:07:00 -07001392 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1393 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001394 }
1395
Aart Bik34c3ba92015-07-20 14:08:59 -07001396 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1397 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001398 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001399 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001400 }
1401
1402 static SideEffects ArrayReadOfType(Primitive::Type type) {
1403 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1404 }
1405
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001406 static SideEffects CanTriggerGC() {
1407 return SideEffects(1ULL << kCanTriggerGCBit);
1408 }
1409
1410 static SideEffects DependsOnGC() {
1411 return SideEffects(1ULL << kDependsOnGCBit);
1412 }
1413
Aart Bik854a02b2015-07-14 16:07:00 -07001414 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001415 SideEffects Union(SideEffects other) const {
1416 return SideEffects(flags_ | other.flags_);
1417 }
1418
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001419 SideEffects Exclusion(SideEffects other) const {
1420 return SideEffects(flags_ & ~other.flags_);
1421 }
1422
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001423 void Add(SideEffects other) {
1424 flags_ |= other.flags_;
1425 }
1426
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001427 bool Includes(SideEffects other) const {
1428 return (other.flags_ & flags_) == other.flags_;
1429 }
1430
1431 bool HasSideEffects() const {
1432 return (flags_ & kAllChangeBits);
1433 }
1434
1435 bool HasDependencies() const {
1436 return (flags_ & kAllDependOnBits);
1437 }
1438
1439 // Returns true if there are no side effects or dependencies.
1440 bool DoesNothing() const {
1441 return flags_ == 0;
1442 }
1443
Aart Bik854a02b2015-07-14 16:07:00 -07001444 // Returns true if something is written.
1445 bool DoesAnyWrite() const {
1446 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001447 }
1448
Aart Bik854a02b2015-07-14 16:07:00 -07001449 // Returns true if something is read.
1450 bool DoesAnyRead() const {
1451 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001452 }
1453
Aart Bik854a02b2015-07-14 16:07:00 -07001454 // Returns true if potentially everything is written and read
1455 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001456 bool DoesAllReadWrite() const {
1457 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1458 }
1459
Aart Bik854a02b2015-07-14 16:07:00 -07001460 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001461 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001462 }
1463
Roland Levillain0d5a2812015-11-13 10:07:31 +00001464 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001465 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001466 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1467 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001468 }
1469
1470 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001471 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001472 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001473 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001474 for (int s = kLastBit; s >= 0; s--) {
1475 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1476 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1477 // This is a bit for the GC side effect.
1478 if (current_bit_is_set) {
1479 flags += "GC";
1480 }
Aart Bik854a02b2015-07-14 16:07:00 -07001481 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001482 } else {
1483 // This is a bit for the array/field analysis.
1484 // The underscore character stands for the 'can trigger GC' bit.
1485 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1486 if (current_bit_is_set) {
1487 flags += kDebug[s];
1488 }
1489 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1490 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1491 flags += "|";
1492 }
1493 }
Aart Bik854a02b2015-07-14 16:07:00 -07001494 }
1495 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001496 }
1497
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001498 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001499
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001500 private:
1501 static constexpr int kFieldArrayAnalysisBits = 9;
1502
1503 static constexpr int kFieldWriteOffset = 0;
1504 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1505 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1506 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1507
1508 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1509
1510 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1511 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1512 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1513 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1514
1515 static constexpr int kLastBit = kDependsOnGCBit;
1516 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1517
1518 // Aliases.
1519
1520 static_assert(kChangeBits == kDependOnBits,
1521 "the 'change' bits should match the 'depend on' bits.");
1522
1523 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1524 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1525 static constexpr uint64_t kAllWrites =
1526 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1527 static constexpr uint64_t kAllReads =
1528 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001529
Aart Bik854a02b2015-07-14 16:07:00 -07001530 // Work around the fact that HIR aliases I/F and J/D.
1531 // TODO: remove this interceptor once HIR types are clean
1532 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1533 switch (type) {
1534 case Primitive::kPrimInt:
1535 case Primitive::kPrimFloat:
1536 return TypeFlag(Primitive::kPrimInt, offset) |
1537 TypeFlag(Primitive::kPrimFloat, offset);
1538 case Primitive::kPrimLong:
1539 case Primitive::kPrimDouble:
1540 return TypeFlag(Primitive::kPrimLong, offset) |
1541 TypeFlag(Primitive::kPrimDouble, offset);
1542 default:
1543 return TypeFlag(type, offset);
1544 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001545 }
1546
Aart Bik854a02b2015-07-14 16:07:00 -07001547 // Translates type to bit flag.
1548 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1549 CHECK_NE(type, Primitive::kPrimVoid);
1550 const uint64_t one = 1;
1551 const int shift = type; // 0-based consecutive enum
1552 DCHECK_LE(kFieldWriteOffset, shift);
1553 DCHECK_LT(shift, kArrayWriteOffset);
1554 return one << (type + offset);
1555 }
1556
1557 // Private constructor on direct flags value.
1558 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1559
1560 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001561};
1562
David Brazdiled596192015-01-23 10:39:45 +00001563// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001564class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001565 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001566 HEnvironment(ArenaAllocator* arena,
1567 size_t number_of_vregs,
1568 const DexFile& dex_file,
1569 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001570 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001571 InvokeType invoke_type,
1572 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001573 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1574 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001575 parent_(nullptr),
1576 dex_file_(dex_file),
1577 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001578 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001579 invoke_type_(invoke_type),
1580 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001581 }
1582
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001583 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001584 : HEnvironment(arena,
1585 to_copy.Size(),
1586 to_copy.GetDexFile(),
1587 to_copy.GetMethodIdx(),
1588 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001589 to_copy.GetInvokeType(),
1590 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001591
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001592 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001593 if (parent_ != nullptr) {
1594 parent_->SetAndCopyParentChain(allocator, parent);
1595 } else {
1596 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1597 parent_->CopyFrom(parent);
1598 if (parent->GetParent() != nullptr) {
1599 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1600 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001601 }
David Brazdiled596192015-01-23 10:39:45 +00001602 }
1603
Vladimir Marko71bf8092015-09-15 15:33:14 +01001604 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001605 void CopyFrom(HEnvironment* environment);
1606
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001607 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1608 // input to the loop phi instead. This is for inserting instructions that
1609 // require an environment (like HDeoptimization) in the loop pre-header.
1610 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001611
1612 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001613 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001614 }
1615
1616 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001617 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001618 }
1619
David Brazdil1abb4192015-02-17 18:33:36 +00001620 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001621
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001622 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001623
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001624 HEnvironment* GetParent() const { return parent_; }
1625
1626 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001627 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001628 }
1629
1630 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001631 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001632 }
1633
1634 uint32_t GetDexPc() const {
1635 return dex_pc_;
1636 }
1637
1638 uint32_t GetMethodIdx() const {
1639 return method_idx_;
1640 }
1641
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001642 InvokeType GetInvokeType() const {
1643 return invoke_type_;
1644 }
1645
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001646 const DexFile& GetDexFile() const {
1647 return dex_file_;
1648 }
1649
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001650 HInstruction* GetHolder() const {
1651 return holder_;
1652 }
1653
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001654
1655 bool IsFromInlinedInvoke() const {
1656 return GetParent() != nullptr;
1657 }
1658
David Brazdiled596192015-01-23 10:39:45 +00001659 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001660 // Record instructions' use entries of this environment for constant-time removal.
1661 // It should only be called by HInstruction when a new environment use is added.
1662 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1663 DCHECK(env_use->GetUser() == this);
1664 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001665 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001666 }
David Brazdiled596192015-01-23 10:39:45 +00001667
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001668 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1669 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001670 HEnvironment* parent_;
1671 const DexFile& dex_file_;
1672 const uint32_t method_idx_;
1673 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001674 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001675
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001676 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001677 HInstruction* const holder_;
1678
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001679 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001680
1681 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1682};
1683
Alex Light68289a52015-12-15 17:30:30 -08001684class ReferenceTypeInfo : ValueObject {
1685 public:
1686 typedef Handle<mirror::Class> TypeHandle;
1687
1688 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1689 // The constructor will check that the type_handle is valid.
1690 return ReferenceTypeInfo(type_handle, is_exact);
1691 }
1692
1693 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1694
1695 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1696 return handle.GetReference() != nullptr;
1697 }
1698
1699 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1700 return IsValidHandle(type_handle_);
1701 }
1702
1703 bool IsExact() const { return is_exact_; }
1704
1705 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1706 DCHECK(IsValid());
1707 return GetTypeHandle()->IsObjectClass();
1708 }
1709
1710 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1711 DCHECK(IsValid());
1712 return GetTypeHandle()->IsStringClass();
1713 }
1714
1715 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1716 DCHECK(IsValid());
1717 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1718 }
1719
1720 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
1721 DCHECK(IsValid());
1722 return GetTypeHandle()->IsInterface();
1723 }
1724
1725 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1726 DCHECK(IsValid());
1727 return GetTypeHandle()->IsArrayClass();
1728 }
1729
1730 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1731 DCHECK(IsValid());
1732 return GetTypeHandle()->IsPrimitiveArray();
1733 }
1734
1735 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1736 DCHECK(IsValid());
1737 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1738 }
1739
1740 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1741 DCHECK(IsValid());
1742 if (!IsExact()) return false;
1743 if (!IsArrayClass()) return false;
1744 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1745 }
1746
1747 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1748 DCHECK(IsValid());
1749 if (!IsExact()) return false;
1750 if (!IsArrayClass()) return false;
1751 if (!rti.IsArrayClass()) return false;
1752 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1753 rti.GetTypeHandle()->GetComponentType());
1754 }
1755
1756 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1757
1758 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1759 DCHECK(IsValid());
1760 DCHECK(rti.IsValid());
1761 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1762 }
1763
1764 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1765 DCHECK(IsValid());
1766 DCHECK(rti.IsValid());
1767 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
1768 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1769 }
1770
1771 // Returns true if the type information provide the same amount of details.
1772 // Note that it does not mean that the instructions have the same actual type
1773 // (because the type can be the result of a merge).
1774 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
1775 if (!IsValid() && !rti.IsValid()) {
1776 // Invalid types are equal.
1777 return true;
1778 }
1779 if (!IsValid() || !rti.IsValid()) {
1780 // One is valid, the other not.
1781 return false;
1782 }
1783 return IsExact() == rti.IsExact()
1784 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1785 }
1786
1787 private:
1788 ReferenceTypeInfo();
1789 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
1790
1791 // The class of the object.
1792 TypeHandle type_handle_;
1793 // Whether or not the type is exact or a superclass of the actual type.
1794 // Whether or not we have any information about this type.
1795 bool is_exact_;
1796};
1797
1798std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1799
Vladimir Markof9f64412015-09-02 14:05:49 +01001800class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001801 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001802 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001803 : previous_(nullptr),
1804 next_(nullptr),
1805 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001806 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001807 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001808 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001809 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001810 locations_(nullptr),
1811 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001812 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001813 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001814 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001815
Dave Allison20dfc792014-06-16 20:44:29 -07001816 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001817
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001818#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001819 enum InstructionKind {
1820 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1821 };
1822#undef DECLARE_KIND
1823
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001824 HInstruction* GetNext() const { return next_; }
1825 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001826
Calin Juravle77520bc2015-01-12 18:45:46 +00001827 HInstruction* GetNextDisregardingMoves() const;
1828 HInstruction* GetPreviousDisregardingMoves() const;
1829
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001830 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001831 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001832 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001833 bool IsInBlock() const { return block_ != nullptr; }
1834 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001835 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001836
Roland Levillain6b879dd2014-09-22 17:13:44 +01001837 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001838 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001839
1840 virtual void Accept(HGraphVisitor* visitor) = 0;
1841 virtual const char* DebugName() const = 0;
1842
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001843 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001844 void SetRawInputAt(size_t index, HInstruction* input) {
1845 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1846 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001847
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001848 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001849
1850 uint32_t GetDexPc() const { return dex_pc_; }
1851
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001852 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001853
Roland Levillaine161a2a2014-10-03 12:45:18 +01001854 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001855 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001856
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001857 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001858 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001859
Calin Juravle10e244f2015-01-26 18:54:32 +00001860 // Does not apply for all instructions, but having this at top level greatly
1861 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001862 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001863 virtual bool CanBeNull() const {
1864 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1865 return true;
1866 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001867
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001868 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001869 return false;
1870 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001871
Calin Juravle2e768302015-07-28 14:41:11 +00001872 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001873
Calin Juravle61d544b2015-02-23 16:46:57 +00001874 ReferenceTypeInfo GetReferenceTypeInfo() const {
1875 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1876 return reference_type_info_;
1877 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001878
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001879 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001880 DCHECK(user != nullptr);
1881 HUseListNode<HInstruction*>* use =
1882 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1883 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001884 }
1885
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001886 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001887 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001888 HUseListNode<HEnvironment*>* env_use =
1889 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1890 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001891 }
1892
David Brazdil1abb4192015-02-17 18:33:36 +00001893 void RemoveAsUserOfInput(size_t input) {
1894 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1895 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1896 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001897
David Brazdil1abb4192015-02-17 18:33:36 +00001898 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1899 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001900
David Brazdiled596192015-01-23 10:39:45 +00001901 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1902 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001903 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001904 bool HasOnlyOneNonEnvironmentUse() const {
1905 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1906 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001907
Roland Levillain6c82d402014-10-13 16:10:27 +01001908 // Does this instruction strictly dominate `other_instruction`?
1909 // Returns false if this instruction and `other_instruction` are the same.
1910 // Aborts if this instruction and `other_instruction` are both phis.
1911 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001912
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001913 int GetId() const { return id_; }
1914 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001915
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001916 int GetSsaIndex() const { return ssa_index_; }
1917 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1918 bool HasSsaIndex() const { return ssa_index_ != -1; }
1919
1920 bool HasEnvironment() const { return environment_ != nullptr; }
1921 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001922 // Set the `environment_` field. Raw because this method does not
1923 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001924 void SetRawEnvironment(HEnvironment* environment) {
1925 DCHECK(environment_ == nullptr);
1926 DCHECK_EQ(environment->GetHolder(), this);
1927 environment_ = environment;
1928 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001929
1930 // Set the environment of this instruction, copying it from `environment`. While
1931 // copying, the uses lists are being updated.
1932 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001933 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001934 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001935 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001936 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001937 if (environment->GetParent() != nullptr) {
1938 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1939 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001940 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001941
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001942 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1943 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001944 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001945 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001946 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001947 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001948 if (environment->GetParent() != nullptr) {
1949 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1950 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001951 }
1952
Nicolas Geoffray39468442014-09-02 15:17:15 +01001953 // Returns the number of entries in the environment. Typically, that is the
1954 // number of dex registers in a method. It could be more in case of inlining.
1955 size_t EnvironmentSize() const;
1956
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001957 LocationSummary* GetLocations() const { return locations_; }
1958 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001959
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001960 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001961 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001962
Alexandre Rames188d4312015-04-09 18:30:21 +01001963 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1964 // uses of this instruction by `other` are *not* updated.
1965 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1966 ReplaceWith(other);
1967 other->ReplaceInput(this, use_index);
1968 }
1969
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001970 // Move `this` instruction before `cursor`.
1971 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001972
Vladimir Markofb337ea2015-11-25 15:25:10 +00001973 // Move `this` before its first user and out of any loops. If there is no
1974 // out-of-loop user that dominates all other users, move the instruction
1975 // to the end of the out-of-loop common dominator of the user's blocks.
1976 //
1977 // This can be used only on non-throwing instructions with no side effects that
1978 // have at least one use but no environment uses.
1979 void MoveBeforeFirstUserAndOutOfLoops();
1980
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001981#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001982 bool Is##type() const; \
1983 const H##type* As##type() const; \
1984 H##type* As##type();
1985
1986 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1987#undef INSTRUCTION_TYPE_CHECK
1988
1989#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001990 bool Is##type() const { return (As##type() != nullptr); } \
1991 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001992 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001993 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001994#undef INSTRUCTION_TYPE_CHECK
1995
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001996 // Returns whether the instruction can be moved within the graph.
1997 virtual bool CanBeMoved() const { return false; }
1998
1999 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002000 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002001 return false;
2002 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002003
2004 // Returns whether any data encoded in the two instructions is equal.
2005 // This method does not look at the inputs. Both instructions must be
2006 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002007 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002008 return false;
2009 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002010
2011 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002012 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002013 // 2) Their inputs are identical.
2014 bool Equals(HInstruction* other) const;
2015
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2017 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2018 // the virtual function because the __attribute__((__pure__)) doesn't really
2019 // apply the strong requirement for virtual functions, preventing optimizations.
2020 InstructionKind GetKind() const PURE;
2021 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002022
2023 virtual size_t ComputeHashCode() const {
2024 size_t result = GetKind();
2025 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2026 result = (result * 31) + InputAt(i)->GetId();
2027 }
2028 return result;
2029 }
2030
2031 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002032 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002033
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002034 size_t GetLifetimePosition() const { return lifetime_position_; }
2035 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2036 LiveInterval* GetLiveInterval() const { return live_interval_; }
2037 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2038 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2039
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002040 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2041
2042 // Returns whether the code generation of the instruction will require to have access
2043 // to the current method. Such instructions are:
2044 // (1): Instructions that require an environment, as calling the runtime requires
2045 // to walk the stack and have the current method stored at a specific stack address.
2046 // (2): Object literals like classes and strings, that are loaded from the dex cache
2047 // fields of the current method.
2048 bool NeedsCurrentMethod() const {
2049 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2050 }
2051
Vladimir Markodc151b22015-10-15 18:02:30 +01002052 // Returns whether the code generation of the instruction will require to have access
2053 // to the dex cache of the current method's declaring class via the current method.
2054 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002055
Mark Mendellc4701932015-04-10 13:18:51 -04002056 // Does this instruction have any use in an environment before
2057 // control flow hits 'other'?
2058 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2059
2060 // Remove all references to environment uses of this instruction.
2061 // The caller must ensure that this is safe to do.
2062 void RemoveEnvironmentUsers();
2063
David Brazdil1abb4192015-02-17 18:33:36 +00002064 protected:
2065 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2066 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
Aart Bik5d75afe2015-12-14 11:57:01 -08002067 void SetSideEffects(SideEffects other) { side_effects_ = other; }
David Brazdil1abb4192015-02-17 18:33:36 +00002068
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002069 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002070 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2071
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002072 HInstruction* previous_;
2073 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002074 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002075 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002076
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002077 // An instruction gets an id when it is added to the graph.
2078 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002079 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002080 int id_;
2081
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002082 // When doing liveness analysis, instructions that have uses get an SSA index.
2083 int ssa_index_;
2084
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002085 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002086 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002087
2088 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002089 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002090
Nicolas Geoffray39468442014-09-02 15:17:15 +01002091 // The environment associated with this instruction. Not null if the instruction
2092 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002093 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002094
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002095 // Set by the code generator.
2096 LocationSummary* locations_;
2097
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002098 // Set by the liveness analysis.
2099 LiveInterval* live_interval_;
2100
2101 // Set by the liveness analysis, this is the position in a linear
2102 // order of blocks where this instruction's live interval start.
2103 size_t lifetime_position_;
2104
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002105 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002106
Calin Juravleacf735c2015-02-12 15:25:22 +00002107 // TODO: for primitive types this should be marked as invalid.
2108 ReferenceTypeInfo reference_type_info_;
2109
David Brazdil1abb4192015-02-17 18:33:36 +00002110 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002111 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002112 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002113 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002114 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002115
2116 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2117};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002118std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002119
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002120class HInputIterator : public ValueObject {
2121 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002122 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002123
2124 bool Done() const { return index_ == instruction_->InputCount(); }
2125 HInstruction* Current() const { return instruction_->InputAt(index_); }
2126 void Advance() { index_++; }
2127
2128 private:
2129 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002130 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002131
2132 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2133};
2134
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002135class HInstructionIterator : public ValueObject {
2136 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002137 explicit HInstructionIterator(const HInstructionList& instructions)
2138 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002139 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002140 }
2141
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002142 bool Done() const { return instruction_ == nullptr; }
2143 HInstruction* Current() const { return instruction_; }
2144 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002145 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002146 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002147 }
2148
2149 private:
2150 HInstruction* instruction_;
2151 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002152
2153 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002154};
2155
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002156class HBackwardInstructionIterator : public ValueObject {
2157 public:
2158 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2159 : instruction_(instructions.last_instruction_) {
2160 next_ = Done() ? nullptr : instruction_->GetPrevious();
2161 }
2162
2163 bool Done() const { return instruction_ == nullptr; }
2164 HInstruction* Current() const { return instruction_; }
2165 void Advance() {
2166 instruction_ = next_;
2167 next_ = Done() ? nullptr : instruction_->GetPrevious();
2168 }
2169
2170 private:
2171 HInstruction* instruction_;
2172 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002173
2174 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002175};
2176
Vladimir Markof9f64412015-09-02 14:05:49 +01002177template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002178class HTemplateInstruction: public HInstruction {
2179 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002180 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002181 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002182 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002183
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002184 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002185
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002186 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002187 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2188 DCHECK_LT(i, N);
2189 return inputs_[i];
2190 }
David Brazdil1abb4192015-02-17 18:33:36 +00002191
2192 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002193 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002194 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002195 }
2196
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002197 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002198 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002199
2200 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002201};
2202
Vladimir Markof9f64412015-09-02 14:05:49 +01002203// HTemplateInstruction specialization for N=0.
2204template<>
2205class HTemplateInstruction<0>: public HInstruction {
2206 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002207 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002208 : HInstruction(side_effects, dex_pc) {}
2209
Vladimir Markof9f64412015-09-02 14:05:49 +01002210 virtual ~HTemplateInstruction() {}
2211
2212 size_t InputCount() const OVERRIDE { return 0; }
2213
2214 protected:
2215 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2216 LOG(FATAL) << "Unreachable";
2217 UNREACHABLE();
2218 }
2219
2220 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2221 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2222 LOG(FATAL) << "Unreachable";
2223 UNREACHABLE();
2224 }
2225
2226 private:
2227 friend class SsaBuilder;
2228};
2229
Dave Allison20dfc792014-06-16 20:44:29 -07002230template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002231class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002232 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002233 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002234 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002235 virtual ~HExpression() {}
2236
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002237 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002238
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002239 protected:
2240 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002241};
2242
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002243// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2244// instruction that branches to the exit block.
2245class HReturnVoid : public HTemplateInstruction<0> {
2246 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002247 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2248 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002249
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002250 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002251
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002252 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002253
2254 private:
2255 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2256};
2257
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002258// Represents dex's RETURN opcodes. A HReturn is a control flow
2259// instruction that branches to the exit block.
2260class HReturn : public HTemplateInstruction<1> {
2261 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002262 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2263 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002264 SetRawInputAt(0, value);
2265 }
2266
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002267 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002268
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002269 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002270
2271 private:
2272 DISALLOW_COPY_AND_ASSIGN(HReturn);
2273};
2274
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002275// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002276// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002277// exit block.
2278class HExit : public HTemplateInstruction<0> {
2279 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002280 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002281
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002282 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002283
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002284 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002285
2286 private:
2287 DISALLOW_COPY_AND_ASSIGN(HExit);
2288};
2289
2290// Jumps from one block to another.
2291class HGoto : public HTemplateInstruction<0> {
2292 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002293 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002294
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002295 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002296
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002297 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002298 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002299 }
2300
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002301 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002302
2303 private:
2304 DISALLOW_COPY_AND_ASSIGN(HGoto);
2305};
2306
Roland Levillain9867bc72015-08-05 10:21:34 +01002307class HConstant : public HExpression<0> {
2308 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002309 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2310 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002311
2312 bool CanBeMoved() const OVERRIDE { return true; }
2313
2314 virtual bool IsMinusOne() const { return false; }
2315 virtual bool IsZero() const { return false; }
2316 virtual bool IsOne() const { return false; }
2317
David Brazdil77a48ae2015-09-15 12:34:04 +00002318 virtual uint64_t GetValueAsUint64() const = 0;
2319
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002320 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002321
2322 private:
2323 DISALLOW_COPY_AND_ASSIGN(HConstant);
2324};
2325
2326class HNullConstant : public HConstant {
2327 public:
2328 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2329 return true;
2330 }
2331
David Brazdil77a48ae2015-09-15 12:34:04 +00002332 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2333
Roland Levillain9867bc72015-08-05 10:21:34 +01002334 size_t ComputeHashCode() const OVERRIDE { return 0; }
2335
2336 DECLARE_INSTRUCTION(NullConstant);
2337
2338 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002339 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002340
2341 friend class HGraph;
2342 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2343};
2344
2345// Constants of the type int. Those can be from Dex instructions, or
2346// synthesized (for example with the if-eqz instruction).
2347class HIntConstant : public HConstant {
2348 public:
2349 int32_t GetValue() const { return value_; }
2350
David Brazdil9f389d42015-10-01 14:32:56 +01002351 uint64_t GetValueAsUint64() const OVERRIDE {
2352 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2353 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002354
Roland Levillain9867bc72015-08-05 10:21:34 +01002355 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2356 DCHECK(other->IsIntConstant());
2357 return other->AsIntConstant()->value_ == value_;
2358 }
2359
2360 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2361
2362 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2363 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2364 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2365
2366 DECLARE_INSTRUCTION(IntConstant);
2367
2368 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002369 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2370 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2371 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2372 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002373
2374 const int32_t value_;
2375
2376 friend class HGraph;
2377 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2378 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2379 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2380};
2381
2382class HLongConstant : public HConstant {
2383 public:
2384 int64_t GetValue() const { return value_; }
2385
David Brazdil77a48ae2015-09-15 12:34:04 +00002386 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2387
Roland Levillain9867bc72015-08-05 10:21:34 +01002388 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2389 DCHECK(other->IsLongConstant());
2390 return other->AsLongConstant()->value_ == value_;
2391 }
2392
2393 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2394
2395 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2396 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2397 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2398
2399 DECLARE_INSTRUCTION(LongConstant);
2400
2401 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002402 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2403 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002404
2405 const int64_t value_;
2406
2407 friend class HGraph;
2408 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2409};
Dave Allison20dfc792014-06-16 20:44:29 -07002410
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002411// Conditional branch. A block ending with an HIf instruction must have
2412// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002413class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002414 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002415 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2416 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002417 SetRawInputAt(0, input);
2418 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002419
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002420 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002421
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002422 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002423 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002424 }
2425
2426 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002427 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002428 }
2429
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002430 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002431
2432 private:
2433 DISALLOW_COPY_AND_ASSIGN(HIf);
2434};
2435
David Brazdilfc6a86a2015-06-26 10:33:45 +00002436
2437// Abstract instruction which marks the beginning and/or end of a try block and
2438// links it to the respective exception handlers. Behaves the same as a Goto in
2439// non-exceptional control flow.
2440// Normal-flow successor is stored at index zero, exception handlers under
2441// higher indices in no particular order.
2442class HTryBoundary : public HTemplateInstruction<0> {
2443 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002444 enum BoundaryKind {
2445 kEntry,
2446 kExit,
2447 };
2448
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002449 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2450 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002451
2452 bool IsControlFlow() const OVERRIDE { return true; }
2453
2454 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002455 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002456
David Brazdild26a4112015-11-10 11:07:31 +00002457 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2458 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2459 }
2460
David Brazdilfc6a86a2015-06-26 10:33:45 +00002461 // Returns whether `handler` is among its exception handlers (non-zero index
2462 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002463 bool HasExceptionHandler(const HBasicBlock& handler) const {
2464 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002465 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002466 }
2467
2468 // If not present already, adds `handler` to its block's list of exception
2469 // handlers.
2470 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002471 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002472 GetBlock()->AddSuccessor(handler);
2473 }
2474 }
2475
David Brazdil56e1acc2015-06-30 15:41:36 +01002476 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002477
David Brazdilffee3d32015-07-06 11:48:53 +01002478 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2479
David Brazdilfc6a86a2015-06-26 10:33:45 +00002480 DECLARE_INSTRUCTION(TryBoundary);
2481
2482 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002483 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002484
2485 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2486};
2487
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002488// Deoptimize to interpreter, upon checking a condition.
2489class HDeoptimize : public HTemplateInstruction<1> {
2490 public:
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002491 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002492 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002493 SetRawInputAt(0, cond);
2494 }
2495
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002496 bool CanBeMoved() const OVERRIDE { return true; }
2497 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2498 return true;
2499 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002500 bool NeedsEnvironment() const OVERRIDE { return true; }
2501 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002502
2503 DECLARE_INSTRUCTION(Deoptimize);
2504
2505 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002506 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2507};
2508
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002509// Represents the ArtMethod that was passed as a first argument to
2510// the method. It is used by instructions that depend on it, like
2511// instructions that work with the dex cache.
2512class HCurrentMethod : public HExpression<0> {
2513 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002514 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2515 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002516
2517 DECLARE_INSTRUCTION(CurrentMethod);
2518
2519 private:
2520 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2521};
2522
Mark Mendellfe57faa2015-09-18 09:26:15 -04002523// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2524// have one successor for each entry in the switch table, and the final successor
2525// will be the block containing the next Dex opcode.
2526class HPackedSwitch : public HTemplateInstruction<1> {
2527 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002528 HPackedSwitch(int32_t start_value,
2529 uint32_t num_entries,
2530 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002531 uint32_t dex_pc = kNoDexPc)
2532 : HTemplateInstruction(SideEffects::None(), dex_pc),
2533 start_value_(start_value),
2534 num_entries_(num_entries) {
2535 SetRawInputAt(0, input);
2536 }
2537
2538 bool IsControlFlow() const OVERRIDE { return true; }
2539
2540 int32_t GetStartValue() const { return start_value_; }
2541
Vladimir Marko211c2112015-09-24 16:52:33 +01002542 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002543
2544 HBasicBlock* GetDefaultBlock() const {
2545 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002546 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002547 }
2548 DECLARE_INSTRUCTION(PackedSwitch);
2549
2550 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002551 const int32_t start_value_;
2552 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002553
2554 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2555};
2556
Roland Levillain88cb1752014-10-20 16:36:47 +01002557class HUnaryOperation : public HExpression<1> {
2558 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002559 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2560 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002561 SetRawInputAt(0, input);
2562 }
2563
2564 HInstruction* GetInput() const { return InputAt(0); }
2565 Primitive::Type GetResultType() const { return GetType(); }
2566
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002567 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002568 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002569 return true;
2570 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002571
Roland Levillain9240d6a2014-10-20 16:47:04 +01002572 // Try to statically evaluate `operation` and return a HConstant
2573 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002574 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002575 HConstant* TryStaticEvaluation() const;
2576
2577 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002578 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2579 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002580
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002581 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002582
2583 private:
2584 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2585};
2586
Dave Allison20dfc792014-06-16 20:44:29 -07002587class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002588 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002589 HBinaryOperation(Primitive::Type result_type,
2590 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002591 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002592 SideEffects side_effects = SideEffects::None(),
2593 uint32_t dex_pc = kNoDexPc)
2594 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002595 SetRawInputAt(0, left);
2596 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002597 }
2598
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002599 HInstruction* GetLeft() const { return InputAt(0); }
2600 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002601 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002602
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002603 virtual bool IsCommutative() const { return false; }
2604
2605 // Put constant on the right.
2606 // Returns whether order is changed.
2607 bool OrderInputsWithConstantOnTheRight() {
2608 HInstruction* left = InputAt(0);
2609 HInstruction* right = InputAt(1);
2610 if (left->IsConstant() && !right->IsConstant()) {
2611 ReplaceInput(right, 0);
2612 ReplaceInput(left, 1);
2613 return true;
2614 }
2615 return false;
2616 }
2617
2618 // Order inputs by instruction id, but favor constant on the right side.
2619 // This helps GVN for commutative ops.
2620 void OrderInputs() {
2621 DCHECK(IsCommutative());
2622 HInstruction* left = InputAt(0);
2623 HInstruction* right = InputAt(1);
2624 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2625 return;
2626 }
2627 if (OrderInputsWithConstantOnTheRight()) {
2628 return;
2629 }
2630 // Order according to instruction id.
2631 if (left->GetId() > right->GetId()) {
2632 ReplaceInput(right, 0);
2633 ReplaceInput(left, 1);
2634 }
2635 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002636
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002637 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002638 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002639 return true;
2640 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002641
Roland Levillain9240d6a2014-10-20 16:47:04 +01002642 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002643 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002644 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002645 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002646
2647 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002648 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2649 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2650 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2651 HLongConstant* y ATTRIBUTE_UNUSED) const {
2652 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2653 return nullptr;
2654 }
2655 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2656 HIntConstant* y ATTRIBUTE_UNUSED) const {
2657 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2658 return nullptr;
2659 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002660 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2661 HNullConstant* y ATTRIBUTE_UNUSED) const {
2662 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2663 return nullptr;
2664 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002665
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002666 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002667 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002668 HConstant* GetConstantRight() const;
2669
2670 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002671 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002672 HInstruction* GetLeastConstantLeft() const;
2673
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002674 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002675
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002676 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002677 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2678};
2679
Mark Mendellc4701932015-04-10 13:18:51 -04002680// The comparison bias applies for floating point operations and indicates how NaN
2681// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002682enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002683 kNoBias, // bias is not applicable (i.e. for long operation)
2684 kGtBias, // return 1 for NaN comparisons
2685 kLtBias, // return -1 for NaN comparisons
2686};
2687
Dave Allison20dfc792014-06-16 20:44:29 -07002688class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002689 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002690 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2691 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002692 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002693 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002694
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002695 bool NeedsMaterialization() const { return needs_materialization_; }
2696 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002697
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002698 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002699 // `instruction`, and disregard moves in between.
2700 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002701
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002702 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002703
2704 virtual IfCondition GetCondition() const = 0;
2705
Mark Mendellc4701932015-04-10 13:18:51 -04002706 virtual IfCondition GetOppositeCondition() const = 0;
2707
Roland Levillain4fa13f62015-07-06 18:11:54 +01002708 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002709
2710 void SetBias(ComparisonBias bias) { bias_ = bias; }
2711
2712 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2713 return bias_ == other->AsCondition()->bias_;
2714 }
2715
Roland Levillain4fa13f62015-07-06 18:11:54 +01002716 bool IsFPConditionTrueIfNaN() const {
2717 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2718 IfCondition if_cond = GetCondition();
2719 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2720 }
2721
2722 bool IsFPConditionFalseIfNaN() const {
2723 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2724 IfCondition if_cond = GetCondition();
2725 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2726 }
2727
Dave Allison20dfc792014-06-16 20:44:29 -07002728 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002729 // For register allocation purposes, returns whether this instruction needs to be
2730 // materialized (that is, not just be in the processor flags).
2731 bool needs_materialization_;
2732
Mark Mendellc4701932015-04-10 13:18:51 -04002733 // Needed if we merge a HCompare into a HCondition.
2734 ComparisonBias bias_;
2735
Dave Allison20dfc792014-06-16 20:44:29 -07002736 DISALLOW_COPY_AND_ASSIGN(HCondition);
2737};
2738
2739// Instruction to check if two inputs are equal to each other.
2740class HEqual : public HCondition {
2741 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002742 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2743 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002744
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002745 bool IsCommutative() const OVERRIDE { return true; }
2746
Roland Levillain9867bc72015-08-05 10:21:34 +01002747 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002748 return GetBlock()->GetGraph()->GetIntConstant(
2749 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002750 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002751 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002752 return GetBlock()->GetGraph()->GetIntConstant(
2753 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002754 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002755 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2756 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002757 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002758 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002759
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002760 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002761
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002762 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002763 return kCondEQ;
2764 }
2765
Mark Mendellc4701932015-04-10 13:18:51 -04002766 IfCondition GetOppositeCondition() const OVERRIDE {
2767 return kCondNE;
2768 }
2769
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002770 private:
Aart Bike9f37602015-10-09 11:15:55 -07002771 template <typename T> bool Compute(T x, T y) const { return x == y; }
2772
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002773 DISALLOW_COPY_AND_ASSIGN(HEqual);
2774};
2775
Dave Allison20dfc792014-06-16 20:44:29 -07002776class HNotEqual : public HCondition {
2777 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002778 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2779 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002780
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002781 bool IsCommutative() const OVERRIDE { return true; }
2782
Roland Levillain9867bc72015-08-05 10:21:34 +01002783 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002784 return GetBlock()->GetGraph()->GetIntConstant(
2785 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002786 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002787 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002788 return GetBlock()->GetGraph()->GetIntConstant(
2789 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002790 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002791 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2792 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002793 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002794 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002795
Dave Allison20dfc792014-06-16 20:44:29 -07002796 DECLARE_INSTRUCTION(NotEqual);
2797
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002798 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002799 return kCondNE;
2800 }
2801
Mark Mendellc4701932015-04-10 13:18:51 -04002802 IfCondition GetOppositeCondition() const OVERRIDE {
2803 return kCondEQ;
2804 }
2805
Dave Allison20dfc792014-06-16 20:44:29 -07002806 private:
Aart Bike9f37602015-10-09 11:15:55 -07002807 template <typename T> bool Compute(T x, T y) const { return x != y; }
2808
Dave Allison20dfc792014-06-16 20:44:29 -07002809 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2810};
2811
2812class HLessThan : public HCondition {
2813 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002814 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2815 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002816
Roland Levillain9867bc72015-08-05 10:21:34 +01002817 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002818 return GetBlock()->GetGraph()->GetIntConstant(
2819 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002820 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002821 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002822 return GetBlock()->GetGraph()->GetIntConstant(
2823 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002824 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002825
Dave Allison20dfc792014-06-16 20:44:29 -07002826 DECLARE_INSTRUCTION(LessThan);
2827
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002828 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002829 return kCondLT;
2830 }
2831
Mark Mendellc4701932015-04-10 13:18:51 -04002832 IfCondition GetOppositeCondition() const OVERRIDE {
2833 return kCondGE;
2834 }
2835
Dave Allison20dfc792014-06-16 20:44:29 -07002836 private:
Aart Bike9f37602015-10-09 11:15:55 -07002837 template <typename T> bool Compute(T x, T y) const { return x < y; }
2838
Dave Allison20dfc792014-06-16 20:44:29 -07002839 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2840};
2841
2842class HLessThanOrEqual : public HCondition {
2843 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002844 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2845 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002846
Roland Levillain9867bc72015-08-05 10:21:34 +01002847 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002848 return GetBlock()->GetGraph()->GetIntConstant(
2849 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002850 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002851 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002852 return GetBlock()->GetGraph()->GetIntConstant(
2853 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002854 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002855
Dave Allison20dfc792014-06-16 20:44:29 -07002856 DECLARE_INSTRUCTION(LessThanOrEqual);
2857
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002858 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002859 return kCondLE;
2860 }
2861
Mark Mendellc4701932015-04-10 13:18:51 -04002862 IfCondition GetOppositeCondition() const OVERRIDE {
2863 return kCondGT;
2864 }
2865
Dave Allison20dfc792014-06-16 20:44:29 -07002866 private:
Aart Bike9f37602015-10-09 11:15:55 -07002867 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2868
Dave Allison20dfc792014-06-16 20:44:29 -07002869 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2870};
2871
2872class HGreaterThan : public HCondition {
2873 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002874 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2875 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002876
Roland Levillain9867bc72015-08-05 10:21:34 +01002877 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002878 return GetBlock()->GetGraph()->GetIntConstant(
2879 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002880 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002881 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002882 return GetBlock()->GetGraph()->GetIntConstant(
2883 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002884 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002885
Dave Allison20dfc792014-06-16 20:44:29 -07002886 DECLARE_INSTRUCTION(GreaterThan);
2887
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002888 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002889 return kCondGT;
2890 }
2891
Mark Mendellc4701932015-04-10 13:18:51 -04002892 IfCondition GetOppositeCondition() const OVERRIDE {
2893 return kCondLE;
2894 }
2895
Dave Allison20dfc792014-06-16 20:44:29 -07002896 private:
Aart Bike9f37602015-10-09 11:15:55 -07002897 template <typename T> bool Compute(T x, T y) const { return x > y; }
2898
Dave Allison20dfc792014-06-16 20:44:29 -07002899 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2900};
2901
2902class HGreaterThanOrEqual : public HCondition {
2903 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002904 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2905 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002906
Roland Levillain9867bc72015-08-05 10:21:34 +01002907 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002908 return GetBlock()->GetGraph()->GetIntConstant(
2909 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002910 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002911 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002912 return GetBlock()->GetGraph()->GetIntConstant(
2913 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002914 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002915
Dave Allison20dfc792014-06-16 20:44:29 -07002916 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2917
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002918 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002919 return kCondGE;
2920 }
2921
Mark Mendellc4701932015-04-10 13:18:51 -04002922 IfCondition GetOppositeCondition() const OVERRIDE {
2923 return kCondLT;
2924 }
2925
Dave Allison20dfc792014-06-16 20:44:29 -07002926 private:
Aart Bike9f37602015-10-09 11:15:55 -07002927 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2928
Dave Allison20dfc792014-06-16 20:44:29 -07002929 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2930};
2931
Aart Bike9f37602015-10-09 11:15:55 -07002932class HBelow : public HCondition {
2933 public:
2934 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2935 : HCondition(first, second, dex_pc) {}
2936
2937 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2938 return GetBlock()->GetGraph()->GetIntConstant(
2939 Compute(static_cast<uint32_t>(x->GetValue()),
2940 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2941 }
2942 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2943 return GetBlock()->GetGraph()->GetIntConstant(
2944 Compute(static_cast<uint64_t>(x->GetValue()),
2945 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2946 }
2947
2948 DECLARE_INSTRUCTION(Below);
2949
2950 IfCondition GetCondition() const OVERRIDE {
2951 return kCondB;
2952 }
2953
2954 IfCondition GetOppositeCondition() const OVERRIDE {
2955 return kCondAE;
2956 }
2957
2958 private:
2959 template <typename T> bool Compute(T x, T y) const { return x < y; }
2960
2961 DISALLOW_COPY_AND_ASSIGN(HBelow);
2962};
2963
2964class HBelowOrEqual : public HCondition {
2965 public:
2966 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2967 : HCondition(first, second, dex_pc) {}
2968
2969 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2970 return GetBlock()->GetGraph()->GetIntConstant(
2971 Compute(static_cast<uint32_t>(x->GetValue()),
2972 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2973 }
2974 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2975 return GetBlock()->GetGraph()->GetIntConstant(
2976 Compute(static_cast<uint64_t>(x->GetValue()),
2977 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2978 }
2979
2980 DECLARE_INSTRUCTION(BelowOrEqual);
2981
2982 IfCondition GetCondition() const OVERRIDE {
2983 return kCondBE;
2984 }
2985
2986 IfCondition GetOppositeCondition() const OVERRIDE {
2987 return kCondA;
2988 }
2989
2990 private:
2991 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2992
2993 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2994};
2995
2996class HAbove : public HCondition {
2997 public:
2998 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2999 : HCondition(first, second, dex_pc) {}
3000
3001 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3002 return GetBlock()->GetGraph()->GetIntConstant(
3003 Compute(static_cast<uint32_t>(x->GetValue()),
3004 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3005 }
3006 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3007 return GetBlock()->GetGraph()->GetIntConstant(
3008 Compute(static_cast<uint64_t>(x->GetValue()),
3009 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3010 }
3011
3012 DECLARE_INSTRUCTION(Above);
3013
3014 IfCondition GetCondition() const OVERRIDE {
3015 return kCondA;
3016 }
3017
3018 IfCondition GetOppositeCondition() const OVERRIDE {
3019 return kCondBE;
3020 }
3021
3022 private:
3023 template <typename T> bool Compute(T x, T y) const { return x > y; }
3024
3025 DISALLOW_COPY_AND_ASSIGN(HAbove);
3026};
3027
3028class HAboveOrEqual : public HCondition {
3029 public:
3030 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3031 : HCondition(first, second, dex_pc) {}
3032
3033 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3034 return GetBlock()->GetGraph()->GetIntConstant(
3035 Compute(static_cast<uint32_t>(x->GetValue()),
3036 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3037 }
3038 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3039 return GetBlock()->GetGraph()->GetIntConstant(
3040 Compute(static_cast<uint64_t>(x->GetValue()),
3041 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3042 }
3043
3044 DECLARE_INSTRUCTION(AboveOrEqual);
3045
3046 IfCondition GetCondition() const OVERRIDE {
3047 return kCondAE;
3048 }
3049
3050 IfCondition GetOppositeCondition() const OVERRIDE {
3051 return kCondB;
3052 }
3053
3054 private:
3055 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3056
3057 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3058};
Dave Allison20dfc792014-06-16 20:44:29 -07003059
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003060// Instruction to check how two inputs compare to each other.
3061// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3062class HCompare : public HBinaryOperation {
3063 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003064 HCompare(Primitive::Type type,
3065 HInstruction* first,
3066 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003067 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003068 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003069 : HBinaryOperation(Primitive::kPrimInt,
3070 first,
3071 second,
3072 SideEffectsForArchRuntimeCalls(type),
3073 dex_pc),
3074 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003075 DCHECK_EQ(type, first->GetType());
3076 DCHECK_EQ(type, second->GetType());
3077 }
3078
Roland Levillain9867bc72015-08-05 10:21:34 +01003079 template <typename T>
3080 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003081
Roland Levillain9867bc72015-08-05 10:21:34 +01003082 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003083 return GetBlock()->GetGraph()->GetIntConstant(
3084 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003085 }
3086 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003087 return GetBlock()->GetGraph()->GetIntConstant(
3088 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003089 }
3090
Calin Juravleddb7df22014-11-25 20:56:51 +00003091 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3092 return bias_ == other->AsCompare()->bias_;
3093 }
3094
Mark Mendellc4701932015-04-10 13:18:51 -04003095 ComparisonBias GetBias() const { return bias_; }
3096
Roland Levillain4fa13f62015-07-06 18:11:54 +01003097 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003098
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003099
3100 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3101 // MIPS64 uses a runtime call for FP comparisons.
3102 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3103 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003104
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003105 DECLARE_INSTRUCTION(Compare);
3106
3107 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003108 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003109
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003110 DISALLOW_COPY_AND_ASSIGN(HCompare);
3111};
3112
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003113// A local in the graph. Corresponds to a Dex register.
3114class HLocal : public HTemplateInstruction<0> {
3115 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003116 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003117 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003118
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003119 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003120
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003121 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003122
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003123 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003124 // The Dex register number.
3125 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003126
3127 DISALLOW_COPY_AND_ASSIGN(HLocal);
3128};
3129
3130// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003131class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003132 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003133 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3134 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003135 SetRawInputAt(0, local);
3136 }
3137
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003138 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3139
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003140 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003141
3142 private:
3143 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3144};
3145
3146// Store a value in a given local. This instruction has two inputs: the value
3147// and the local.
3148class HStoreLocal : public HTemplateInstruction<2> {
3149 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003150 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3151 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003152 SetRawInputAt(0, local);
3153 SetRawInputAt(1, value);
3154 }
3155
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003156 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3157
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003158 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003159
3160 private:
3161 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3162};
3163
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003164class HFloatConstant : public HConstant {
3165 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003166 float GetValue() const { return value_; }
3167
David Brazdil77a48ae2015-09-15 12:34:04 +00003168 uint64_t GetValueAsUint64() const OVERRIDE {
3169 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3170 }
3171
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003172 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003173 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003174 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003175 }
3176
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003177 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003178
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003179 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003180 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003181 }
3182 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003183 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003184 }
3185 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003186 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3187 }
3188 bool IsNaN() const {
3189 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003190 }
3191
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003192 DECLARE_INSTRUCTION(FloatConstant);
3193
3194 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003195 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3196 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3197 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3198 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003199
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003200 const float value_;
3201
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003202 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003203 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003204 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003205 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3206};
3207
3208class HDoubleConstant : public HConstant {
3209 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003210 double GetValue() const { return value_; }
3211
David Brazdil77a48ae2015-09-15 12:34:04 +00003212 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3213
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003214 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003215 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003216 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003217 }
3218
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003219 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003220
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003221 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003222 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003223 }
3224 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003225 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003226 }
3227 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003228 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3229 }
3230 bool IsNaN() const {
3231 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003232 }
3233
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003234 DECLARE_INSTRUCTION(DoubleConstant);
3235
3236 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003237 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3238 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3239 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3240 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003241
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003242 const double value_;
3243
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003244 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003245 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003246 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003247 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3248};
3249
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003250enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003251#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3252 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003253#include "intrinsics_list.h"
3254 kNone,
3255 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3256#undef INTRINSICS_LIST
3257#undef OPTIMIZING_INTRINSICS
3258};
3259std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3260
Agi Csaki05f20562015-08-19 14:58:14 -07003261enum IntrinsicNeedsEnvironmentOrCache {
3262 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3263 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003264};
3265
Aart Bik5d75afe2015-12-14 11:57:01 -08003266enum IntrinsicSideEffects {
3267 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3268 kReadSideEffects, // Intrinsic may read heap memory.
3269 kWriteSideEffects, // Intrinsic may write heap memory.
3270 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3271};
3272
3273enum IntrinsicExceptions {
3274 kNoThrow, // Intrinsic does not throw any exceptions.
3275 kCanThrow // Intrinsic may throw exceptions.
3276};
3277
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003278class HInvoke : public HInstruction {
3279 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003280 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003281
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003282 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003283
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003284 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003285 SetRawInputAt(index, argument);
3286 }
3287
Roland Levillain3e3d7332015-04-28 11:00:54 +01003288 // Return the number of arguments. This number can be lower than
3289 // the number of inputs returned by InputCount(), as some invoke
3290 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3291 // inputs at the end of their list of inputs.
3292 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3293
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003294 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003295
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003296 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003297 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003298
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003299 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3300
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003301 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003302 return intrinsic_;
3303 }
3304
Aart Bik5d75afe2015-12-14 11:57:01 -08003305 void SetIntrinsic(Intrinsics intrinsic,
3306 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3307 IntrinsicSideEffects side_effects,
3308 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003309
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003310 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003311 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003312 }
3313
Aart Bik5d75afe2015-12-14 11:57:01 -08003314 bool CanThrow() const OVERRIDE { return can_throw_; }
3315
3316 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3317
3318 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3319 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3320 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003321
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003322 uint32_t* GetIntrinsicOptimizations() {
3323 return &intrinsic_optimizations_;
3324 }
3325
3326 const uint32_t* GetIntrinsicOptimizations() const {
3327 return &intrinsic_optimizations_;
3328 }
3329
3330 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3331
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003332 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003333
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003334 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003335 HInvoke(ArenaAllocator* arena,
3336 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003337 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003338 Primitive::Type return_type,
3339 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003340 uint32_t dex_method_index,
3341 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003342 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003343 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003344 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003345 inputs_(number_of_arguments + number_of_other_inputs,
3346 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003347 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003348 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003349 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003350 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003351 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003352 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003353 }
3354
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003355 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003356 return inputs_[index];
3357 }
3358
David Brazdil1abb4192015-02-17 18:33:36 +00003359 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003360 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003361 }
3362
Aart Bik5d75afe2015-12-14 11:57:01 -08003363 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3364
Roland Levillain3e3d7332015-04-28 11:00:54 +01003365 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003366 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003367 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003368 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003369 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003370 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003371 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003372
3373 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3374 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003375
3376 private:
3377 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3378};
3379
Calin Juravle175dc732015-08-25 15:42:32 +01003380class HInvokeUnresolved : public HInvoke {
3381 public:
3382 HInvokeUnresolved(ArenaAllocator* arena,
3383 uint32_t number_of_arguments,
3384 Primitive::Type return_type,
3385 uint32_t dex_pc,
3386 uint32_t dex_method_index,
3387 InvokeType invoke_type)
3388 : HInvoke(arena,
3389 number_of_arguments,
3390 0u /* number_of_other_inputs */,
3391 return_type,
3392 dex_pc,
3393 dex_method_index,
3394 invoke_type) {
3395 }
3396
3397 DECLARE_INSTRUCTION(InvokeUnresolved);
3398
3399 private:
3400 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3401};
3402
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003403class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003404 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003405 // Requirements of this method call regarding the class
3406 // initialization (clinit) check of its declaring class.
3407 enum class ClinitCheckRequirement {
3408 kNone, // Class already initialized.
3409 kExplicit, // Static call having explicit clinit check as last input.
3410 kImplicit, // Static call implicitly requiring a clinit check.
3411 };
3412
Vladimir Marko58155012015-08-19 12:49:41 +00003413 // Determines how to load the target ArtMethod*.
3414 enum class MethodLoadKind {
3415 // Use a String init ArtMethod* loaded from Thread entrypoints.
3416 kStringInit,
3417
3418 // Use the method's own ArtMethod* loaded by the register allocator.
3419 kRecursive,
3420
3421 // Use ArtMethod* at a known address, embed the direct address in the code.
3422 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3423 kDirectAddress,
3424
3425 // Use ArtMethod* at an address that will be known at link time, embed the direct
3426 // address in the code. If the image is relocatable, emit .patch_oat entry.
3427 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3428 // the image relocatable or not.
3429 kDirectAddressWithFixup,
3430
3431 // Load from resoved methods array in the dex cache using a PC-relative load.
3432 // Used when we need to use the dex cache, for example for invoke-static that
3433 // may cause class initialization (the entry may point to a resolution method),
3434 // and we know that we can access the dex cache arrays using a PC-relative load.
3435 kDexCachePcRelative,
3436
3437 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3438 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3439 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3440 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3441 kDexCacheViaMethod,
3442 };
3443
3444 // Determines the location of the code pointer.
3445 enum class CodePtrLocation {
3446 // Recursive call, use local PC-relative call instruction.
3447 kCallSelf,
3448
3449 // Use PC-relative call instruction patched at link time.
3450 // Used for calls within an oat file, boot->boot or app->app.
3451 kCallPCRelative,
3452
3453 // Call to a known target address, embed the direct address in code.
3454 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3455 kCallDirect,
3456
3457 // Call to a target address that will be known at link time, embed the direct
3458 // address in code. If the image is relocatable, emit .patch_oat entry.
3459 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3460 // the image relocatable or not.
3461 kCallDirectWithFixup,
3462
3463 // Use code pointer from the ArtMethod*.
3464 // Used when we don't know the target code. This is also the last-resort-kind used when
3465 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3466 kCallArtMethod,
3467 };
3468
3469 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003470 MethodLoadKind method_load_kind;
3471 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003472 // The method load data holds
3473 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3474 // Note that there are multiple string init methods, each having its own offset.
3475 // - the method address for kDirectAddress
3476 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003477 uint64_t method_load_data;
3478 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003479 };
3480
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003481 HInvokeStaticOrDirect(ArenaAllocator* arena,
3482 uint32_t number_of_arguments,
3483 Primitive::Type return_type,
3484 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003485 uint32_t method_index,
3486 MethodReference target_method,
3487 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003488 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003489 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003490 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003491 : HInvoke(arena,
3492 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003493 // There is potentially one extra argument for the HCurrentMethod node, and
3494 // potentially one other if the clinit check is explicit, and potentially
3495 // one other if the method is a string factory.
3496 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3497 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3498 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003499 return_type,
3500 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003501 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003502 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003503 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003504 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003505 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003506 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003507
Vladimir Markodc151b22015-10-15 18:02:30 +01003508 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003509 bool had_current_method_input = HasCurrentMethodInput();
3510 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3511
3512 // Using the current method is the default and once we find a better
3513 // method load kind, we should not go back to using the current method.
3514 DCHECK(had_current_method_input || !needs_current_method_input);
3515
3516 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003517 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3518 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003519 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003520 dispatch_info_ = dispatch_info;
3521 }
3522
Vladimir Markoc53c0792015-11-19 15:48:33 +00003523 void AddSpecialInput(HInstruction* input) {
3524 // We allow only one special input.
3525 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3526 DCHECK(InputCount() == GetSpecialInputIndex() ||
3527 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3528 InsertInputAt(GetSpecialInputIndex(), input);
3529 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003530
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003531 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003532 // We access the method via the dex cache so we can't do an implicit null check.
3533 // TODO: for intrinsics we can generate implicit null checks.
3534 return false;
3535 }
3536
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003537 bool CanBeNull() const OVERRIDE {
3538 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3539 }
3540
Vladimir Markoc53c0792015-11-19 15:48:33 +00003541 // Get the index of the special input, if any.
3542 //
3543 // If the invoke IsStringInit(), it initially has a HFakeString special argument
3544 // which is removed by the instruction simplifier; if the invoke HasCurrentMethodInput(),
3545 // the "special input" is the current method pointer; otherwise there may be one
3546 // platform-specific special input, such as PC-relative addressing base.
3547 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3548
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003549 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3550 void SetOptimizedInvokeType(InvokeType invoke_type) {
3551 optimized_invoke_type_ = invoke_type;
3552 }
3553
Vladimir Marko58155012015-08-19 12:49:41 +00003554 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3555 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3556 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003557 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003558 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003559 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003560 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003561 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3562 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003563 bool HasCurrentMethodInput() const {
3564 // This function can be called only after the invoke has been fully initialized by the builder.
3565 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003566 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003567 return true;
3568 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003569 DCHECK(InputCount() == GetSpecialInputIndex() ||
3570 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003571 return false;
3572 }
3573 }
Vladimir Marko58155012015-08-19 12:49:41 +00003574 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3575 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003576 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003577
3578 int32_t GetStringInitOffset() const {
3579 DCHECK(IsStringInit());
3580 return dispatch_info_.method_load_data;
3581 }
3582
3583 uint64_t GetMethodAddress() const {
3584 DCHECK(HasMethodAddress());
3585 return dispatch_info_.method_load_data;
3586 }
3587
3588 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003589 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003590 return dispatch_info_.method_load_data;
3591 }
3592
3593 uint64_t GetDirectCodePtr() const {
3594 DCHECK(HasDirectCodePtr());
3595 return dispatch_info_.direct_code_ptr;
3596 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003597
Calin Juravle68ad6492015-08-18 17:08:12 +01003598 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3599
Roland Levillain4c0eb422015-04-24 16:43:49 +01003600 // Is this instruction a call to a static method?
3601 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003602 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003603 }
3604
Vladimir Markofbb184a2015-11-13 14:47:00 +00003605 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3606 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3607 // instruction; only relevant for static calls with explicit clinit check.
3608 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003609 DCHECK(IsStaticWithExplicitClinitCheck());
3610 size_t last_input_index = InputCount() - 1;
3611 HInstruction* last_input = InputAt(last_input_index);
3612 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003613 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003614 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003615 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003616 clinit_check_requirement_ = new_requirement;
3617 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003618 }
3619
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003620 bool IsStringFactoryFor(HFakeString* str) const {
3621 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003622 DCHECK(!HasCurrentMethodInput());
3623 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003624 return InputAt(InputCount() - 1)->AsFakeString() == str;
3625 }
3626
3627 void RemoveFakeStringArgumentAsLastInput() {
3628 DCHECK(IsStringInit());
3629 size_t last_input_index = InputCount() - 1;
3630 HInstruction* last_input = InputAt(last_input_index);
3631 DCHECK(last_input != nullptr);
3632 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3633 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003634 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003635 }
3636
Roland Levillain4c0eb422015-04-24 16:43:49 +01003637 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003638 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003639 bool IsStaticWithExplicitClinitCheck() const {
3640 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3641 }
3642
3643 // Is this a call to a static method whose declaring class has an
3644 // implicit intialization check requirement?
3645 bool IsStaticWithImplicitClinitCheck() const {
3646 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3647 }
3648
Vladimir Markob554b5a2015-11-06 12:57:55 +00003649 // Does this method load kind need the current method as an input?
3650 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3651 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3652 }
3653
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003654 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003655
Roland Levillain4c0eb422015-04-24 16:43:49 +01003656 protected:
3657 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3658 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3659 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3660 HInstruction* input = input_record.GetInstruction();
3661 // `input` is the last input of a static invoke marked as having
3662 // an explicit clinit check. It must either be:
3663 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3664 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3665 DCHECK(input != nullptr);
3666 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3667 }
3668 return input_record;
3669 }
3670
Vladimir Markoc53c0792015-11-19 15:48:33 +00003671 void InsertInputAt(size_t index, HInstruction* input);
3672 void RemoveInputAt(size_t index);
3673
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003674 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003675 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003676 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003677 // The target method may refer to different dex file or method index than the original
3678 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3679 // in derived class to increase visibility.
3680 MethodReference target_method_;
3681 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003682
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003683 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003684};
Vladimir Markof64242a2015-12-01 14:58:23 +00003685std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003686std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003687
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003688class HInvokeVirtual : public HInvoke {
3689 public:
3690 HInvokeVirtual(ArenaAllocator* arena,
3691 uint32_t number_of_arguments,
3692 Primitive::Type return_type,
3693 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003694 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003695 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003696 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003697 vtable_index_(vtable_index) {}
3698
Calin Juravle641547a2015-04-21 22:08:51 +01003699 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003700 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003701 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003702 }
3703
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003704 uint32_t GetVTableIndex() const { return vtable_index_; }
3705
3706 DECLARE_INSTRUCTION(InvokeVirtual);
3707
3708 private:
3709 const uint32_t vtable_index_;
3710
3711 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3712};
3713
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003714class HInvokeInterface : public HInvoke {
3715 public:
3716 HInvokeInterface(ArenaAllocator* arena,
3717 uint32_t number_of_arguments,
3718 Primitive::Type return_type,
3719 uint32_t dex_pc,
3720 uint32_t dex_method_index,
3721 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003722 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003723 imt_index_(imt_index) {}
3724
Calin Juravle641547a2015-04-21 22:08:51 +01003725 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003726 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003727 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003728 }
3729
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003730 uint32_t GetImtIndex() const { return imt_index_; }
3731 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3732
3733 DECLARE_INSTRUCTION(InvokeInterface);
3734
3735 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003736 const uint32_t imt_index_;
3737
3738 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3739};
3740
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003741class HNewInstance : public HExpression<2> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003742 public:
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003743 HNewInstance(HInstruction* cls,
3744 HCurrentMethod* current_method,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003745 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003746 uint16_t type_index,
3747 const DexFile& dex_file,
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003748 bool can_throw,
3749 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003750 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003751 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003752 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003753 dex_file_(dex_file),
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003754 can_throw_(can_throw),
3755 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003756 entrypoint_(entrypoint) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003757 SetRawInputAt(0, cls);
3758 SetRawInputAt(1, current_method);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003759 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003760
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003761 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003762 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003763
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003764 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003765 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003766
3767 // It may throw when called on type that's not instantiable/accessible.
3768 // It can throw OOME.
3769 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3770 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3771
3772 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003773
Calin Juravle10e244f2015-01-26 18:54:32 +00003774 bool CanBeNull() const OVERRIDE { return false; }
3775
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003776 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3777
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003778 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3779 entrypoint_ = entrypoint;
3780 }
3781
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003782 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003783
3784 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003785 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003786 const DexFile& dex_file_;
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003787 const bool can_throw_;
3788 const bool finalizable_;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003789 QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003790
3791 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3792};
3793
Roland Levillain88cb1752014-10-20 16:36:47 +01003794class HNeg : public HUnaryOperation {
3795 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003796 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3797 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003798
Roland Levillain9867bc72015-08-05 10:21:34 +01003799 template <typename T> T Compute(T x) const { return -x; }
3800
3801 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003802 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003803 }
3804 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003805 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003806 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003807
Roland Levillain88cb1752014-10-20 16:36:47 +01003808 DECLARE_INSTRUCTION(Neg);
3809
3810 private:
3811 DISALLOW_COPY_AND_ASSIGN(HNeg);
3812};
3813
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003814class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003815 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003816 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003817 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003818 uint32_t dex_pc,
3819 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 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003822 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003823 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003824 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003825 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003826 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003827 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003828 }
3829
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003830 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003831 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003832
3833 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003834 bool NeedsEnvironment() const OVERRIDE { return true; }
3835
Mingyao Yang0c365e62015-03-31 15:09:29 -07003836 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3837 bool CanThrow() const OVERRIDE { return true; }
3838
Calin Juravle10e244f2015-01-26 18:54:32 +00003839 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003840
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003841 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3842
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003843 DECLARE_INSTRUCTION(NewArray);
3844
3845 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003846 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003847 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003848 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003849
3850 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3851};
3852
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003853class HAdd : public HBinaryOperation {
3854 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003855 HAdd(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 Geoffrayd8ee7372014-03-28 15:43:40 +00003860
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003861 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003862
Roland Levillain9867bc72015-08-05 10:21:34 +01003863 template <typename T> T Compute(T x, T y) const { return x + y; }
3864
3865 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003866 return GetBlock()->GetGraph()->GetIntConstant(
3867 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003868 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003869 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003870 return GetBlock()->GetGraph()->GetLongConstant(
3871 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003872 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003873
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003874 DECLARE_INSTRUCTION(Add);
3875
3876 private:
3877 DISALLOW_COPY_AND_ASSIGN(HAdd);
3878};
3879
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003880class HSub : public HBinaryOperation {
3881 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003882 HSub(Primitive::Type result_type,
3883 HInstruction* left,
3884 HInstruction* right,
3885 uint32_t dex_pc = kNoDexPc)
3886 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +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 Levillain93445682014-10-06 19:24:02 +01003893 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003894 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 Levillain93445682014-10-06 19:24:02 +01003897 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003898
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003899 DECLARE_INSTRUCTION(Sub);
3900
3901 private:
3902 DISALLOW_COPY_AND_ASSIGN(HSub);
3903};
3904
Calin Juravle34bacdf2014-10-07 20:23:36 +01003905class HMul : public HBinaryOperation {
3906 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003907 HMul(Primitive::Type result_type,
3908 HInstruction* left,
3909 HInstruction* right,
3910 uint32_t dex_pc = kNoDexPc)
3911 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003912
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003913 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003914
Roland Levillain9867bc72015-08-05 10:21:34 +01003915 template <typename T> T Compute(T x, T y) const { return x * y; }
3916
3917 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003918 return GetBlock()->GetGraph()->GetIntConstant(
3919 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003920 }
3921 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003922 return GetBlock()->GetGraph()->GetLongConstant(
3923 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003924 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003925
3926 DECLARE_INSTRUCTION(Mul);
3927
3928 private:
3929 DISALLOW_COPY_AND_ASSIGN(HMul);
3930};
3931
Calin Juravle7c4954d2014-10-28 16:57:40 +00003932class HDiv : public HBinaryOperation {
3933 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003934 HDiv(Primitive::Type result_type,
3935 HInstruction* left,
3936 HInstruction* right,
3937 uint32_t dex_pc)
3938 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003939
Roland Levillain9867bc72015-08-05 10:21:34 +01003940 template <typename T>
3941 T Compute(T x, T y) const {
3942 // Our graph structure ensures we never have 0 for `y` during
3943 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003944 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003945 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003946 return (y == -1) ? -x : x / y;
3947 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003948
Roland Levillain9867bc72015-08-05 10:21:34 +01003949 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003950 return GetBlock()->GetGraph()->GetIntConstant(
3951 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003952 }
3953 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003954 return GetBlock()->GetGraph()->GetLongConstant(
3955 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003956 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003957
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003958 static SideEffects SideEffectsForArchRuntimeCalls() {
3959 // The generated code can use a runtime call.
3960 return SideEffects::CanTriggerGC();
3961 }
3962
Calin Juravle7c4954d2014-10-28 16:57:40 +00003963 DECLARE_INSTRUCTION(Div);
3964
3965 private:
3966 DISALLOW_COPY_AND_ASSIGN(HDiv);
3967};
3968
Calin Juravlebacfec32014-11-14 15:54:36 +00003969class HRem : public HBinaryOperation {
3970 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003971 HRem(Primitive::Type result_type,
3972 HInstruction* left,
3973 HInstruction* right,
3974 uint32_t dex_pc)
3975 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003976
Roland Levillain9867bc72015-08-05 10:21:34 +01003977 template <typename T>
3978 T Compute(T x, T y) const {
3979 // Our graph structure ensures we never have 0 for `y` during
3980 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003981 DCHECK_NE(y, 0);
3982 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3983 return (y == -1) ? 0 : x % y;
3984 }
3985
Roland Levillain9867bc72015-08-05 10:21:34 +01003986 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003987 return GetBlock()->GetGraph()->GetIntConstant(
3988 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003989 }
3990 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003991 return GetBlock()->GetGraph()->GetLongConstant(
3992 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003993 }
3994
Calin Juravlebacfec32014-11-14 15:54:36 +00003995
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003996 static SideEffects SideEffectsForArchRuntimeCalls() {
3997 return SideEffects::CanTriggerGC();
3998 }
3999
Calin Juravlebacfec32014-11-14 15:54:36 +00004000 DECLARE_INSTRUCTION(Rem);
4001
4002 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004003 DISALLOW_COPY_AND_ASSIGN(HRem);
4004};
4005
Calin Juravled0d48522014-11-04 16:40:20 +00004006class HDivZeroCheck : public HExpression<1> {
4007 public:
4008 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004009 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004010 SetRawInputAt(0, value);
4011 }
4012
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004013 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4014
Calin Juravled0d48522014-11-04 16:40:20 +00004015 bool CanBeMoved() const OVERRIDE { return true; }
4016
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004017 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004018 return true;
4019 }
4020
4021 bool NeedsEnvironment() const OVERRIDE { return true; }
4022 bool CanThrow() const OVERRIDE { return true; }
4023
Calin Juravled0d48522014-11-04 16:40:20 +00004024 DECLARE_INSTRUCTION(DivZeroCheck);
4025
4026 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004027 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4028};
4029
Calin Juravle9aec02f2014-11-18 23:06:35 +00004030class HShl : public HBinaryOperation {
4031 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004032 HShl(Primitive::Type result_type,
4033 HInstruction* left,
4034 HInstruction* right,
4035 uint32_t dex_pc = kNoDexPc)
4036 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004037
Roland Levillain9867bc72015-08-05 10:21:34 +01004038 template <typename T, typename U, typename V>
4039 T Compute(T x, U y, V max_shift_value) const {
4040 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4041 "V is not the unsigned integer type corresponding to T");
4042 return x << (y & max_shift_value);
4043 }
4044
4045 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4046 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004047 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004048 }
4049 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4050 // case is handled as `x << static_cast<int>(y)`.
4051 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4052 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004053 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004054 }
4055 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4056 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004057 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004058 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004059
4060 DECLARE_INSTRUCTION(Shl);
4061
4062 private:
4063 DISALLOW_COPY_AND_ASSIGN(HShl);
4064};
4065
4066class HShr : public HBinaryOperation {
4067 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004068 HShr(Primitive::Type result_type,
4069 HInstruction* left,
4070 HInstruction* right,
4071 uint32_t dex_pc = kNoDexPc)
4072 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004073
Roland Levillain9867bc72015-08-05 10:21:34 +01004074 template <typename T, typename U, typename V>
4075 T Compute(T x, U y, V max_shift_value) const {
4076 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4077 "V is not the unsigned integer type corresponding to T");
4078 return x >> (y & max_shift_value);
4079 }
4080
4081 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4082 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004083 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004084 }
4085 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4086 // case is handled as `x >> static_cast<int>(y)`.
4087 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4088 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004089 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004090 }
4091 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4092 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004093 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004094 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004095
4096 DECLARE_INSTRUCTION(Shr);
4097
4098 private:
4099 DISALLOW_COPY_AND_ASSIGN(HShr);
4100};
4101
4102class HUShr : public HBinaryOperation {
4103 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004104 HUShr(Primitive::Type result_type,
4105 HInstruction* left,
4106 HInstruction* right,
4107 uint32_t dex_pc = kNoDexPc)
4108 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004109
Roland Levillain9867bc72015-08-05 10:21:34 +01004110 template <typename T, typename U, typename V>
4111 T Compute(T x, U y, V max_shift_value) const {
4112 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4113 "V is not the unsigned integer type corresponding to T");
4114 V ux = static_cast<V>(x);
4115 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004116 }
4117
Roland Levillain9867bc72015-08-05 10:21:34 +01004118 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4119 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004120 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004121 }
4122 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4123 // case is handled as `x >>> static_cast<int>(y)`.
4124 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4125 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004126 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004127 }
4128 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4129 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004130 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004131 }
4132
4133 DECLARE_INSTRUCTION(UShr);
4134
4135 private:
4136 DISALLOW_COPY_AND_ASSIGN(HUShr);
4137};
4138
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004139class HAnd : public HBinaryOperation {
4140 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004141 HAnd(Primitive::Type result_type,
4142 HInstruction* left,
4143 HInstruction* right,
4144 uint32_t dex_pc = kNoDexPc)
4145 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004146
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004147 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004148
Roland Levillain9867bc72015-08-05 10:21:34 +01004149 template <typename T, typename U>
4150 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4151
4152 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004153 return GetBlock()->GetGraph()->GetIntConstant(
4154 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004155 }
4156 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004157 return GetBlock()->GetGraph()->GetLongConstant(
4158 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004159 }
4160 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004161 return GetBlock()->GetGraph()->GetLongConstant(
4162 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004163 }
4164 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004165 return GetBlock()->GetGraph()->GetLongConstant(
4166 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004167 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004168
4169 DECLARE_INSTRUCTION(And);
4170
4171 private:
4172 DISALLOW_COPY_AND_ASSIGN(HAnd);
4173};
4174
4175class HOr : public HBinaryOperation {
4176 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004177 HOr(Primitive::Type result_type,
4178 HInstruction* left,
4179 HInstruction* right,
4180 uint32_t dex_pc = kNoDexPc)
4181 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004182
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004183 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004184
Roland Levillain9867bc72015-08-05 10:21:34 +01004185 template <typename T, typename U>
4186 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4187
4188 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004189 return GetBlock()->GetGraph()->GetIntConstant(
4190 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004191 }
4192 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004193 return GetBlock()->GetGraph()->GetLongConstant(
4194 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004195 }
4196 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004197 return GetBlock()->GetGraph()->GetLongConstant(
4198 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004199 }
4200 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004201 return GetBlock()->GetGraph()->GetLongConstant(
4202 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004203 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004204
4205 DECLARE_INSTRUCTION(Or);
4206
4207 private:
4208 DISALLOW_COPY_AND_ASSIGN(HOr);
4209};
4210
4211class HXor : public HBinaryOperation {
4212 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004213 HXor(Primitive::Type result_type,
4214 HInstruction* left,
4215 HInstruction* right,
4216 uint32_t dex_pc = kNoDexPc)
4217 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004218
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004219 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004220
Roland Levillain9867bc72015-08-05 10:21:34 +01004221 template <typename T, typename U>
4222 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4223
4224 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004225 return GetBlock()->GetGraph()->GetIntConstant(
4226 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004227 }
4228 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004229 return GetBlock()->GetGraph()->GetLongConstant(
4230 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004231 }
4232 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004233 return GetBlock()->GetGraph()->GetLongConstant(
4234 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004235 }
4236 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004237 return GetBlock()->GetGraph()->GetLongConstant(
4238 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004239 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004240
4241 DECLARE_INSTRUCTION(Xor);
4242
4243 private:
4244 DISALLOW_COPY_AND_ASSIGN(HXor);
4245};
4246
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004247class HRor : public HBinaryOperation {
4248 public:
4249 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4250 : HBinaryOperation(result_type, value, distance) {}
4251
4252 template <typename T, typename U, typename V>
4253 T Compute(T x, U y, V max_shift_value) const {
4254 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4255 "V is not the unsigned integer type corresponding to T");
4256 V ux = static_cast<V>(x);
4257 if ((y & max_shift_value) == 0) {
4258 return static_cast<T>(ux);
4259 } else {
4260 const V reg_bits = sizeof(T) * 8;
4261 return static_cast<T>(ux >> (y & max_shift_value)) |
4262 (x << (reg_bits - (y & max_shift_value)));
4263 }
4264 }
4265
4266 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4267 return GetBlock()->GetGraph()->GetIntConstant(
4268 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4269 }
4270 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4271 return GetBlock()->GetGraph()->GetLongConstant(
4272 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4273 }
4274 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4275 return GetBlock()->GetGraph()->GetLongConstant(
4276 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4277 }
4278
4279 DECLARE_INSTRUCTION(Ror);
4280
4281 private:
4282 DISALLOW_COPY_AND_ASSIGN(HRor);
4283};
4284
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004285// The value of a parameter in this method. Its location depends on
4286// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004287class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004288 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004289 HParameterValue(const DexFile& dex_file,
4290 uint16_t type_index,
4291 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004292 Primitive::Type parameter_type,
4293 bool is_this = false)
4294 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004295 dex_file_(dex_file),
4296 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004297 index_(index),
4298 is_this_(is_this),
4299 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004300
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004301 const DexFile& GetDexFile() const { return dex_file_; }
4302 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004303 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004304 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004305
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004306 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4307 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004308
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004309 DECLARE_INSTRUCTION(ParameterValue);
4310
4311 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004312 const DexFile& dex_file_;
4313 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004314 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004315 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004316 const uint8_t index_;
4317
Calin Juravle10e244f2015-01-26 18:54:32 +00004318 // Whether or not the parameter value corresponds to 'this' argument.
4319 const bool is_this_;
4320
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004321 bool can_be_null_;
4322
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004323 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4324};
4325
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004326class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004327 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004328 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4329 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004330
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004331 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004332 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004333 return true;
4334 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004335
Roland Levillain9867bc72015-08-05 10:21:34 +01004336 template <typename T> T Compute(T x) const { return ~x; }
4337
4338 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004339 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004340 }
4341 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004342 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004343 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004344
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004345 DECLARE_INSTRUCTION(Not);
4346
4347 private:
4348 DISALLOW_COPY_AND_ASSIGN(HNot);
4349};
4350
David Brazdil66d126e2015-04-03 16:02:44 +01004351class HBooleanNot : public HUnaryOperation {
4352 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004353 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4354 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004355
4356 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004357 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004358 return true;
4359 }
4360
Roland Levillain9867bc72015-08-05 10:21:34 +01004361 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004362 DCHECK(IsUint<1>(x));
4363 return !x;
4364 }
4365
Roland Levillain9867bc72015-08-05 10:21:34 +01004366 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004367 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004368 }
4369 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4370 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004371 UNREACHABLE();
4372 }
4373
4374 DECLARE_INSTRUCTION(BooleanNot);
4375
4376 private:
4377 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4378};
4379
Roland Levillaindff1f282014-11-05 14:15:05 +00004380class HTypeConversion : public HExpression<1> {
4381 public:
4382 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004383 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004384 : HExpression(result_type,
4385 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4386 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004387 SetRawInputAt(0, input);
4388 DCHECK_NE(input->GetType(), result_type);
4389 }
4390
4391 HInstruction* GetInput() const { return InputAt(0); }
4392 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4393 Primitive::Type GetResultType() const { return GetType(); }
4394
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004395 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004396 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004397
Roland Levillaindff1f282014-11-05 14:15:05 +00004398 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004399 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004400
Mark Mendelle82549b2015-05-06 10:55:34 -04004401 // Try to statically evaluate the conversion and return a HConstant
4402 // containing the result. If the input cannot be converted, return nullptr.
4403 HConstant* TryStaticEvaluation() const;
4404
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004405 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4406 Primitive::Type result_type) {
4407 // Some architectures may not require the 'GC' side effects, but at this point
4408 // in the compilation process we do not know what architecture we will
4409 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004410 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4411 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004412 return SideEffects::CanTriggerGC();
4413 }
4414 return SideEffects::None();
4415 }
4416
Roland Levillaindff1f282014-11-05 14:15:05 +00004417 DECLARE_INSTRUCTION(TypeConversion);
4418
4419 private:
4420 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4421};
4422
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004423static constexpr uint32_t kNoRegNumber = -1;
4424
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004425class HPhi : public HInstruction {
4426 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004427 HPhi(ArenaAllocator* arena,
4428 uint32_t reg_number,
4429 size_t number_of_inputs,
4430 Primitive::Type type,
4431 uint32_t dex_pc = kNoDexPc)
4432 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004433 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004434 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004435 type_(ToPhiType(type)),
4436 // Phis are constructed live and marked dead if conflicting or unused.
4437 // Individual steps of SsaBuilder should assume that if a phi has been
4438 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4439 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004440 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004441 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004442 }
4443
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004444 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4445 static Primitive::Type ToPhiType(Primitive::Type type) {
4446 switch (type) {
4447 case Primitive::kPrimBoolean:
4448 case Primitive::kPrimByte:
4449 case Primitive::kPrimShort:
4450 case Primitive::kPrimChar:
4451 return Primitive::kPrimInt;
4452 default:
4453 return type;
4454 }
4455 }
4456
David Brazdilffee3d32015-07-06 11:48:53 +01004457 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4458
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004459 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004460
4461 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004462 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004463
Calin Juravle10e244f2015-01-26 18:54:32 +00004464 Primitive::Type GetType() const OVERRIDE { return type_; }
Alex Light68289a52015-12-15 17:30:30 -08004465 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004466
Calin Juravle10e244f2015-01-26 18:54:32 +00004467 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4468 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4469
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004470 uint32_t GetRegNumber() const { return reg_number_; }
4471
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004472 void SetDead() { is_live_ = false; }
4473 void SetLive() { is_live_ = true; }
4474 bool IsDead() const { return !is_live_; }
4475 bool IsLive() const { return is_live_; }
4476
David Brazdil77a48ae2015-09-15 12:34:04 +00004477 bool IsVRegEquivalentOf(HInstruction* other) const {
4478 return other != nullptr
4479 && other->IsPhi()
4480 && other->AsPhi()->GetBlock() == GetBlock()
4481 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4482 }
4483
Calin Juravlea4f88312015-04-16 12:57:19 +01004484 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4485 // An equivalent phi is a phi having the same dex register and type.
4486 // It assumes that phis with the same dex register are adjacent.
4487 HPhi* GetNextEquivalentPhiWithSameType() {
4488 HInstruction* next = GetNext();
4489 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4490 if (next->GetType() == GetType()) {
4491 return next->AsPhi();
4492 }
4493 next = next->GetNext();
4494 }
4495 return nullptr;
4496 }
4497
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004498 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004499
David Brazdil1abb4192015-02-17 18:33:36 +00004500 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004501 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004502 return inputs_[index];
4503 }
David Brazdil1abb4192015-02-17 18:33:36 +00004504
4505 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004506 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004507 }
4508
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004509 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004510 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004511 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004512 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004513 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004514 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004515
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004516 DISALLOW_COPY_AND_ASSIGN(HPhi);
4517};
4518
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004519class HNullCheck : public HExpression<1> {
4520 public:
4521 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004522 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004523 SetRawInputAt(0, value);
4524 }
4525
Calin Juravle10e244f2015-01-26 18:54:32 +00004526 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004527 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004528 return true;
4529 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004530
Calin Juravle10e244f2015-01-26 18:54:32 +00004531 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004532
Calin Juravle10e244f2015-01-26 18:54:32 +00004533 bool CanThrow() const OVERRIDE { return true; }
4534
4535 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004536
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004537
4538 DECLARE_INSTRUCTION(NullCheck);
4539
4540 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004541 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4542};
4543
4544class FieldInfo : public ValueObject {
4545 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004546 FieldInfo(MemberOffset field_offset,
4547 Primitive::Type field_type,
4548 bool is_volatile,
4549 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004550 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004551 const DexFile& dex_file,
4552 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004553 : field_offset_(field_offset),
4554 field_type_(field_type),
4555 is_volatile_(is_volatile),
4556 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004557 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004558 dex_file_(dex_file),
4559 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004560
4561 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004562 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004563 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004564 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004565 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004566 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004567 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004568
4569 private:
4570 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004571 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004572 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004573 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004574 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004575 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004576 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004577};
4578
4579class HInstanceFieldGet : public HExpression<1> {
4580 public:
4581 HInstanceFieldGet(HInstruction* value,
4582 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004583 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004584 bool is_volatile,
4585 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004586 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004587 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004588 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004589 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004590 : HExpression(field_type,
4591 SideEffects::FieldReadOfType(field_type, is_volatile),
4592 dex_pc),
4593 field_info_(field_offset,
4594 field_type,
4595 is_volatile,
4596 field_idx,
4597 declaring_class_def_index,
4598 dex_file,
4599 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004600 SetRawInputAt(0, value);
4601 }
4602
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004603 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004604
4605 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4606 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4607 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004608 }
4609
Calin Juravle641547a2015-04-21 22:08:51 +01004610 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4611 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004612 }
4613
4614 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004615 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4616 }
4617
Calin Juravle52c48962014-12-16 17:02:57 +00004618 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004619 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004620 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004621 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004622
4623 DECLARE_INSTRUCTION(InstanceFieldGet);
4624
4625 private:
4626 const FieldInfo field_info_;
4627
4628 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4629};
4630
4631class HInstanceFieldSet : public HTemplateInstruction<2> {
4632 public:
4633 HInstanceFieldSet(HInstruction* object,
4634 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004635 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004636 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004637 bool is_volatile,
4638 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004639 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004640 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004641 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004642 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004643 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4644 dex_pc),
4645 field_info_(field_offset,
4646 field_type,
4647 is_volatile,
4648 field_idx,
4649 declaring_class_def_index,
4650 dex_file,
4651 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004652 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004653 SetRawInputAt(0, object);
4654 SetRawInputAt(1, value);
4655 }
4656
Calin Juravle641547a2015-04-21 22:08:51 +01004657 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4658 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004659 }
4660
Calin Juravle52c48962014-12-16 17:02:57 +00004661 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004662 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004663 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004664 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004665 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004666 bool GetValueCanBeNull() const { return value_can_be_null_; }
4667 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004668
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004669 DECLARE_INSTRUCTION(InstanceFieldSet);
4670
4671 private:
4672 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004673 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004674
4675 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4676};
4677
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004678class HArrayGet : public HExpression<2> {
4679 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004680 HArrayGet(HInstruction* array,
4681 HInstruction* index,
4682 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004683 uint32_t dex_pc,
4684 SideEffects additional_side_effects = SideEffects::None())
4685 : HExpression(type,
4686 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004687 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004688 SetRawInputAt(0, array);
4689 SetRawInputAt(1, index);
4690 }
4691
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004692 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004693 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004694 return true;
4695 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004696 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004697 // TODO: We can be smarter here.
4698 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4699 // which generates the implicit null check. There are cases when these can be removed
4700 // to produce better code. If we ever add optimizations to do so we should allow an
4701 // implicit check here (as long as the address falls in the first page).
4702 return false;
4703 }
4704
Alex Light68289a52015-12-15 17:30:30 -08004705 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004706
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004707 HInstruction* GetArray() const { return InputAt(0); }
4708 HInstruction* GetIndex() const { return InputAt(1); }
4709
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004710 DECLARE_INSTRUCTION(ArrayGet);
4711
4712 private:
4713 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4714};
4715
4716class HArraySet : public HTemplateInstruction<3> {
4717 public:
4718 HArraySet(HInstruction* array,
4719 HInstruction* index,
4720 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004721 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004722 uint32_t dex_pc,
4723 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004724 : HTemplateInstruction(
4725 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004726 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4727 additional_side_effects),
4728 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004729 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004730 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004731 value_can_be_null_(true),
4732 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004733 SetRawInputAt(0, array);
4734 SetRawInputAt(1, index);
4735 SetRawInputAt(2, value);
4736 }
4737
Calin Juravle77520bc2015-01-12 18:45:46 +00004738 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004739 // We currently always call a runtime method to catch array store
4740 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004741 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004742 }
4743
Mingyao Yang81014cb2015-06-02 03:16:27 -07004744 // Can throw ArrayStoreException.
4745 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4746
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004747 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004748 // TODO: Same as for ArrayGet.
4749 return false;
4750 }
4751
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004752 void ClearNeedsTypeCheck() {
4753 needs_type_check_ = false;
4754 }
4755
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004756 void ClearValueCanBeNull() {
4757 value_can_be_null_ = false;
4758 }
4759
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004760 void SetStaticTypeOfArrayIsObjectArray() {
4761 static_type_of_array_is_object_array_ = true;
4762 }
4763
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004764 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004765 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004766 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004767
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004768 HInstruction* GetArray() const { return InputAt(0); }
4769 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004770 HInstruction* GetValue() const { return InputAt(2); }
4771
4772 Primitive::Type GetComponentType() const {
4773 // The Dex format does not type floating point index operations. Since the
4774 // `expected_component_type_` is set during building and can therefore not
4775 // be correct, we also check what is the value type. If it is a floating
4776 // point type, we must use that type.
4777 Primitive::Type value_type = GetValue()->GetType();
4778 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4779 ? value_type
4780 : expected_component_type_;
4781 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004782
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004783 Primitive::Type GetRawExpectedComponentType() const {
4784 return expected_component_type_;
4785 }
4786
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004787 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4788 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4789 }
4790
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004791 DECLARE_INSTRUCTION(ArraySet);
4792
4793 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004794 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004795 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004796 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004797 // Cached information for the reference_type_info_ so that codegen
4798 // does not need to inspect the static type.
4799 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004800
4801 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4802};
4803
4804class HArrayLength : public HExpression<1> {
4805 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004806 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004807 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004808 // Note that arrays do not change length, so the instruction does not
4809 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004810 SetRawInputAt(0, array);
4811 }
4812
Calin Juravle77520bc2015-01-12 18:45:46 +00004813 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004814 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004815 return true;
4816 }
Calin Juravle641547a2015-04-21 22:08:51 +01004817 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4818 return obj == InputAt(0);
4819 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004820
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004821 DECLARE_INSTRUCTION(ArrayLength);
4822
4823 private:
4824 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4825};
4826
4827class HBoundsCheck : public HExpression<2> {
4828 public:
4829 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004830 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004831 DCHECK(index->GetType() == Primitive::kPrimInt);
4832 SetRawInputAt(0, index);
4833 SetRawInputAt(1, length);
4834 }
4835
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004836 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004837 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004838 return true;
4839 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004840
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004841 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004842
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004843 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004844
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004845 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004846
4847 DECLARE_INSTRUCTION(BoundsCheck);
4848
4849 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004850 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4851};
4852
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004853/**
4854 * Some DEX instructions are folded into multiple HInstructions that need
4855 * to stay live until the last HInstruction. This class
4856 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004857 * HInstruction stays live. `index` represents the stack location index of the
4858 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004859 */
4860class HTemporary : public HTemplateInstruction<0> {
4861 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004862 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4863 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004864
4865 size_t GetIndex() const { return index_; }
4866
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004867 Primitive::Type GetType() const OVERRIDE {
4868 // The previous instruction is the one that will be stored in the temporary location.
4869 DCHECK(GetPrevious() != nullptr);
4870 return GetPrevious()->GetType();
4871 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004872
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004873 DECLARE_INSTRUCTION(Temporary);
4874
4875 private:
4876 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004877 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4878};
4879
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004880class HSuspendCheck : public HTemplateInstruction<0> {
4881 public:
4882 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004883 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004884
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004885 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004886 return true;
4887 }
4888
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004889 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4890 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004891
4892 DECLARE_INSTRUCTION(SuspendCheck);
4893
4894 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004895 // Only used for code generation, in order to share the same slow path between back edges
4896 // of a same loop.
4897 SlowPathCode* slow_path_;
4898
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004899 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4900};
4901
David Srbecky0cf44932015-12-09 14:09:59 +00004902// Pseudo-instruction which provides the native debugger with mapping information.
4903// It ensures that we can generate line number and local variables at this point.
4904class HNativeDebugInfo : public HTemplateInstruction<0> {
4905 public:
4906 explicit HNativeDebugInfo(uint32_t dex_pc)
4907 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
4908
4909 bool NeedsEnvironment() const OVERRIDE {
4910 return true;
4911 }
4912
4913 DECLARE_INSTRUCTION(NativeDebugInfo);
4914
4915 private:
4916 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
4917};
4918
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004919/**
4920 * Instruction to load a Class object.
4921 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004922class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004923 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004924 HLoadClass(HCurrentMethod* current_method,
4925 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004926 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004927 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004928 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004929 bool needs_access_check,
4930 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004931 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004932 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004933 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004934 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004935 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004936 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004937 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004938 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004939 // Referrers class should not need access check. We never inline unverified
4940 // methods so we can't possibly end up in this situation.
4941 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004942 SetRawInputAt(0, current_method);
4943 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004944
4945 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004946
4947 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004948 // Note that we don't need to test for generate_clinit_check_.
4949 // Whether or not we need to generate the clinit check is processed in
4950 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004951 return other->AsLoadClass()->type_index_ == type_index_ &&
4952 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004953 }
4954
4955 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4956
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004957 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004958 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004959 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004960
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004961 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004962 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004963 }
4964
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004965 bool MustGenerateClinitCheck() const {
4966 return generate_clinit_check_;
4967 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004968
Calin Juravle0ba218d2015-05-19 18:46:01 +01004969 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004970 // The entrypoint the code generator is going to call does not do
4971 // clinit of the class.
4972 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004973 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004974 }
4975
4976 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004977 return MustGenerateClinitCheck() ||
4978 (!is_referrers_class_ && !is_in_dex_cache_) ||
4979 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004980 }
4981
4982 bool NeedsAccessCheck() const {
4983 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004984 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004985
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004986 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004987 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004988 }
4989
Calin Juravleacf735c2015-02-12 15:25:22 +00004990 ReferenceTypeInfo GetLoadedClassRTI() {
4991 return loaded_class_rti_;
4992 }
4993
4994 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4995 // Make sure we only set exact types (the loaded class should never be merged).
4996 DCHECK(rti.IsExact());
4997 loaded_class_rti_ = rti;
4998 }
4999
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005000 const DexFile& GetDexFile() { return dex_file_; }
5001
Vladimir Markodc151b22015-10-15 18:02:30 +01005002 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005003
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005004 static SideEffects SideEffectsForArchRuntimeCalls() {
5005 return SideEffects::CanTriggerGC();
5006 }
5007
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005008 bool IsInDexCache() const { return is_in_dex_cache_; }
5009
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005010 DECLARE_INSTRUCTION(LoadClass);
5011
5012 private:
5013 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005014 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005015 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005016 // Whether this instruction must generate the initialization check.
5017 // Used for code generation.
5018 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005019 const bool needs_access_check_;
5020 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005021
Calin Juravleacf735c2015-02-12 15:25:22 +00005022 ReferenceTypeInfo loaded_class_rti_;
5023
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005024 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5025};
5026
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005027class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005028 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005029 HLoadString(HCurrentMethod* current_method,
5030 uint32_t string_index,
5031 uint32_t dex_pc,
5032 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005033 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005034 string_index_(string_index),
5035 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005036 SetRawInputAt(0, current_method);
5037 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005038
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005039 bool CanBeMoved() const OVERRIDE { return true; }
5040
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005041 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5042 return other->AsLoadString()->string_index_ == string_index_;
5043 }
5044
5045 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5046
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005047 uint32_t GetStringIndex() const { return string_index_; }
5048
5049 // TODO: Can we deopt or debug when we resolve a string?
5050 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01005051 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005052 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005053 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005054
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005055 static SideEffects SideEffectsForArchRuntimeCalls() {
5056 return SideEffects::CanTriggerGC();
5057 }
5058
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005059 DECLARE_INSTRUCTION(LoadString);
5060
5061 private:
5062 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005063 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005064
5065 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5066};
5067
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005068/**
5069 * Performs an initialization check on its Class object input.
5070 */
5071class HClinitCheck : public HExpression<1> {
5072 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005073 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005074 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005075 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005076 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5077 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005078 SetRawInputAt(0, constant);
5079 }
5080
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005081 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005082 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005083 return true;
5084 }
5085
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005086 bool NeedsEnvironment() const OVERRIDE {
5087 // May call runtime to initialize the class.
5088 return true;
5089 }
5090
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005091 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005092
5093 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5094
5095 DECLARE_INSTRUCTION(ClinitCheck);
5096
5097 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005098 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5099};
5100
5101class HStaticFieldGet : public HExpression<1> {
5102 public:
5103 HStaticFieldGet(HInstruction* cls,
5104 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005105 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005106 bool is_volatile,
5107 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005108 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005109 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005110 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005111 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005112 : HExpression(field_type,
5113 SideEffects::FieldReadOfType(field_type, is_volatile),
5114 dex_pc),
5115 field_info_(field_offset,
5116 field_type,
5117 is_volatile,
5118 field_idx,
5119 declaring_class_def_index,
5120 dex_file,
5121 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005122 SetRawInputAt(0, cls);
5123 }
5124
Calin Juravle52c48962014-12-16 17:02:57 +00005125
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005126 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005127
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005128 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005129 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5130 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005131 }
5132
5133 size_t ComputeHashCode() const OVERRIDE {
5134 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5135 }
5136
Calin Juravle52c48962014-12-16 17:02:57 +00005137 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005138 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5139 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005140 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005141
5142 DECLARE_INSTRUCTION(StaticFieldGet);
5143
5144 private:
5145 const FieldInfo field_info_;
5146
5147 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5148};
5149
5150class HStaticFieldSet : public HTemplateInstruction<2> {
5151 public:
5152 HStaticFieldSet(HInstruction* cls,
5153 HInstruction* value,
5154 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005155 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005156 bool is_volatile,
5157 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005158 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005159 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005160 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005161 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005162 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5163 dex_pc),
5164 field_info_(field_offset,
5165 field_type,
5166 is_volatile,
5167 field_idx,
5168 declaring_class_def_index,
5169 dex_file,
5170 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005171 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005172 SetRawInputAt(0, cls);
5173 SetRawInputAt(1, value);
5174 }
5175
Calin Juravle52c48962014-12-16 17:02:57 +00005176 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005177 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5178 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005179 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005180
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005181 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005182 bool GetValueCanBeNull() const { return value_can_be_null_; }
5183 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005184
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005185 DECLARE_INSTRUCTION(StaticFieldSet);
5186
5187 private:
5188 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005189 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005190
5191 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5192};
5193
Calin Juravlee460d1d2015-09-29 04:52:17 +01005194class HUnresolvedInstanceFieldGet : public HExpression<1> {
5195 public:
5196 HUnresolvedInstanceFieldGet(HInstruction* obj,
5197 Primitive::Type field_type,
5198 uint32_t field_index,
5199 uint32_t dex_pc)
5200 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5201 field_index_(field_index) {
5202 SetRawInputAt(0, obj);
5203 }
5204
5205 bool NeedsEnvironment() const OVERRIDE { return true; }
5206 bool CanThrow() const OVERRIDE { return true; }
5207
5208 Primitive::Type GetFieldType() const { return GetType(); }
5209 uint32_t GetFieldIndex() const { return field_index_; }
5210
5211 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5212
5213 private:
5214 const uint32_t field_index_;
5215
5216 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5217};
5218
5219class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5220 public:
5221 HUnresolvedInstanceFieldSet(HInstruction* obj,
5222 HInstruction* value,
5223 Primitive::Type field_type,
5224 uint32_t field_index,
5225 uint32_t dex_pc)
5226 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5227 field_type_(field_type),
5228 field_index_(field_index) {
5229 DCHECK_EQ(field_type, value->GetType());
5230 SetRawInputAt(0, obj);
5231 SetRawInputAt(1, value);
5232 }
5233
5234 bool NeedsEnvironment() const OVERRIDE { return true; }
5235 bool CanThrow() const OVERRIDE { return true; }
5236
5237 Primitive::Type GetFieldType() const { return field_type_; }
5238 uint32_t GetFieldIndex() const { return field_index_; }
5239
5240 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5241
5242 private:
5243 const Primitive::Type field_type_;
5244 const uint32_t field_index_;
5245
5246 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5247};
5248
5249class HUnresolvedStaticFieldGet : public HExpression<0> {
5250 public:
5251 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5252 uint32_t field_index,
5253 uint32_t dex_pc)
5254 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5255 field_index_(field_index) {
5256 }
5257
5258 bool NeedsEnvironment() const OVERRIDE { return true; }
5259 bool CanThrow() const OVERRIDE { return true; }
5260
5261 Primitive::Type GetFieldType() const { return GetType(); }
5262 uint32_t GetFieldIndex() const { return field_index_; }
5263
5264 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5265
5266 private:
5267 const uint32_t field_index_;
5268
5269 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5270};
5271
5272class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5273 public:
5274 HUnresolvedStaticFieldSet(HInstruction* value,
5275 Primitive::Type field_type,
5276 uint32_t field_index,
5277 uint32_t dex_pc)
5278 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5279 field_type_(field_type),
5280 field_index_(field_index) {
5281 DCHECK_EQ(field_type, value->GetType());
5282 SetRawInputAt(0, value);
5283 }
5284
5285 bool NeedsEnvironment() const OVERRIDE { return true; }
5286 bool CanThrow() const OVERRIDE { return true; }
5287
5288 Primitive::Type GetFieldType() const { return field_type_; }
5289 uint32_t GetFieldIndex() const { return field_index_; }
5290
5291 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5292
5293 private:
5294 const Primitive::Type field_type_;
5295 const uint32_t field_index_;
5296
5297 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5298};
5299
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005300// Implement the move-exception DEX instruction.
5301class HLoadException : public HExpression<0> {
5302 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005303 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5304 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005305
David Brazdilbbd733e2015-08-18 17:48:17 +01005306 bool CanBeNull() const OVERRIDE { return false; }
5307
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005308 DECLARE_INSTRUCTION(LoadException);
5309
5310 private:
5311 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5312};
5313
David Brazdilcb1c0552015-08-04 16:22:25 +01005314// Implicit part of move-exception which clears thread-local exception storage.
5315// Must not be removed because the runtime expects the TLS to get cleared.
5316class HClearException : public HTemplateInstruction<0> {
5317 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005318 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5319 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005320
5321 DECLARE_INSTRUCTION(ClearException);
5322
5323 private:
5324 DISALLOW_COPY_AND_ASSIGN(HClearException);
5325};
5326
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005327class HThrow : public HTemplateInstruction<1> {
5328 public:
5329 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005330 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005331 SetRawInputAt(0, exception);
5332 }
5333
5334 bool IsControlFlow() const OVERRIDE { return true; }
5335
5336 bool NeedsEnvironment() const OVERRIDE { return true; }
5337
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005338 bool CanThrow() const OVERRIDE { return true; }
5339
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005340
5341 DECLARE_INSTRUCTION(Throw);
5342
5343 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005344 DISALLOW_COPY_AND_ASSIGN(HThrow);
5345};
5346
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005347/**
5348 * Implementation strategies for the code generator of a HInstanceOf
5349 * or `HCheckCast`.
5350 */
5351enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005352 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005353 kExactCheck, // Can do a single class compare.
5354 kClassHierarchyCheck, // Can just walk the super class chain.
5355 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5356 kInterfaceCheck, // No optimization yet when checking against an interface.
5357 kArrayObjectCheck, // Can just check if the array is not primitive.
5358 kArrayCheck // No optimization yet when checking against a generic array.
5359};
5360
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005361class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005362 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005363 HInstanceOf(HInstruction* object,
5364 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005365 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005366 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005367 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005368 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005369 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005370 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005371 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005372 SetRawInputAt(0, object);
5373 SetRawInputAt(1, constant);
5374 }
5375
5376 bool CanBeMoved() const OVERRIDE { return true; }
5377
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005378 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005379 return true;
5380 }
5381
5382 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005383 return false;
5384 }
5385
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005386 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5387
5388 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005389
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005390 // Used only in code generation.
5391 bool MustDoNullCheck() const { return must_do_null_check_; }
5392 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5393
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005394 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5395 return (check_kind == TypeCheckKind::kExactCheck)
5396 ? SideEffects::None()
5397 // Mips currently does runtime calls for any other checks.
5398 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005399 }
5400
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005401 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005402
5403 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005404 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005405 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005406
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005407 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5408};
5409
Calin Juravleb1498f62015-02-16 13:13:29 +00005410class HBoundType : public HExpression<1> {
5411 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005412 // Constructs an HBoundType with the given upper_bound.
5413 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005414 HBoundType(HInstruction* input,
5415 ReferenceTypeInfo upper_bound,
5416 bool upper_can_be_null,
5417 uint32_t dex_pc = kNoDexPc)
5418 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005419 upper_bound_(upper_bound),
5420 upper_can_be_null_(upper_can_be_null),
5421 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005422 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005423 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005424 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005425 }
5426
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005427 // GetUpper* should only be used in reference type propagation.
5428 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5429 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005430
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005431 void SetCanBeNull(bool can_be_null) {
5432 DCHECK(upper_can_be_null_ || !can_be_null);
5433 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005434 }
5435
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005436 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5437
Calin Juravleb1498f62015-02-16 13:13:29 +00005438 DECLARE_INSTRUCTION(BoundType);
5439
5440 private:
5441 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005442 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5443 // It is used to bound the type in cases like:
5444 // if (x instanceof ClassX) {
5445 // // uper_bound_ will be ClassX
5446 // }
5447 const ReferenceTypeInfo upper_bound_;
5448 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5449 // is false then can_be_null_ cannot be true).
5450 const bool upper_can_be_null_;
5451 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005452
5453 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5454};
5455
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005456class HCheckCast : public HTemplateInstruction<2> {
5457 public:
5458 HCheckCast(HInstruction* object,
5459 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005460 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005461 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005462 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005463 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005464 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005465 SetRawInputAt(0, object);
5466 SetRawInputAt(1, constant);
5467 }
5468
5469 bool CanBeMoved() const OVERRIDE { return true; }
5470
5471 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5472 return true;
5473 }
5474
5475 bool NeedsEnvironment() const OVERRIDE {
5476 // Instruction may throw a CheckCastError.
5477 return true;
5478 }
5479
5480 bool CanThrow() const OVERRIDE { return true; }
5481
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005482 bool MustDoNullCheck() const { return must_do_null_check_; }
5483 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005484 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005485
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005486 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005487
5488 DECLARE_INSTRUCTION(CheckCast);
5489
5490 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005491 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005492 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005493
5494 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005495};
5496
Calin Juravle27df7582015-04-17 19:12:31 +01005497class HMemoryBarrier : public HTemplateInstruction<0> {
5498 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005499 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005500 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005501 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005502 barrier_kind_(barrier_kind) {}
5503
5504 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5505
5506 DECLARE_INSTRUCTION(MemoryBarrier);
5507
5508 private:
5509 const MemBarrierKind barrier_kind_;
5510
5511 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5512};
5513
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005514class HMonitorOperation : public HTemplateInstruction<1> {
5515 public:
5516 enum OperationKind {
5517 kEnter,
5518 kExit,
5519 };
5520
5521 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005522 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005523 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5524 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005525 SetRawInputAt(0, object);
5526 }
5527
5528 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005529 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5530
5531 bool CanThrow() const OVERRIDE {
5532 // Verifier guarantees that monitor-exit cannot throw.
5533 // This is important because it allows the HGraphBuilder to remove
5534 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5535 return IsEnter();
5536 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005537
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005538
5539 bool IsEnter() const { return kind_ == kEnter; }
5540
5541 DECLARE_INSTRUCTION(MonitorOperation);
5542
Calin Juravle52c48962014-12-16 17:02:57 +00005543 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005544 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005545
5546 private:
5547 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5548};
5549
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005550/**
5551 * A HInstruction used as a marker for the replacement of new + <init>
5552 * of a String to a call to a StringFactory. Only baseline will see
5553 * the node at code generation, where it will be be treated as null.
5554 * When compiling non-baseline, `HFakeString` instructions are being removed
5555 * in the instruction simplifier.
5556 */
5557class HFakeString : public HTemplateInstruction<0> {
5558 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005559 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5560 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005561
5562 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5563
5564 DECLARE_INSTRUCTION(FakeString);
5565
5566 private:
5567 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5568};
5569
Vladimir Markof9f64412015-09-02 14:05:49 +01005570class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005571 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005572 MoveOperands(Location source,
5573 Location destination,
5574 Primitive::Type type,
5575 HInstruction* instruction)
5576 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005577
5578 Location GetSource() const { return source_; }
5579 Location GetDestination() const { return destination_; }
5580
5581 void SetSource(Location value) { source_ = value; }
5582 void SetDestination(Location value) { destination_ = value; }
5583
5584 // The parallel move resolver marks moves as "in-progress" by clearing the
5585 // destination (but not the source).
5586 Location MarkPending() {
5587 DCHECK(!IsPending());
5588 Location dest = destination_;
5589 destination_ = Location::NoLocation();
5590 return dest;
5591 }
5592
5593 void ClearPending(Location dest) {
5594 DCHECK(IsPending());
5595 destination_ = dest;
5596 }
5597
5598 bool IsPending() const {
5599 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5600 return destination_.IsInvalid() && !source_.IsInvalid();
5601 }
5602
5603 // True if this blocks a move from the given location.
5604 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005605 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005606 }
5607
5608 // A move is redundant if it's been eliminated, if its source and
5609 // destination are the same, or if its destination is unneeded.
5610 bool IsRedundant() const {
5611 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5612 }
5613
5614 // We clear both operands to indicate move that's been eliminated.
5615 void Eliminate() {
5616 source_ = destination_ = Location::NoLocation();
5617 }
5618
5619 bool IsEliminated() const {
5620 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5621 return source_.IsInvalid();
5622 }
5623
Alexey Frunze4dda3372015-06-01 18:31:49 -07005624 Primitive::Type GetType() const { return type_; }
5625
Nicolas Geoffray90218252015-04-15 11:56:51 +01005626 bool Is64BitMove() const {
5627 return Primitive::Is64BitType(type_);
5628 }
5629
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005630 HInstruction* GetInstruction() const { return instruction_; }
5631
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005632 private:
5633 Location source_;
5634 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005635 // The type this move is for.
5636 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005637 // The instruction this move is assocatied with. Null when this move is
5638 // for moving an input in the expected locations of user (including a phi user).
5639 // This is only used in debug mode, to ensure we do not connect interval siblings
5640 // in the same parallel move.
5641 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005642};
5643
5644static constexpr size_t kDefaultNumberOfMoves = 4;
5645
5646class HParallelMove : public HTemplateInstruction<0> {
5647 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005648 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005649 : HTemplateInstruction(SideEffects::None(), dex_pc),
5650 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5651 moves_.reserve(kDefaultNumberOfMoves);
5652 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005653
Nicolas Geoffray90218252015-04-15 11:56:51 +01005654 void AddMove(Location source,
5655 Location destination,
5656 Primitive::Type type,
5657 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005658 DCHECK(source.IsValid());
5659 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005660 if (kIsDebugBuild) {
5661 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005662 for (const MoveOperands& move : moves_) {
5663 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005664 // Special case the situation where the move is for the spill slot
5665 // of the instruction.
5666 if ((GetPrevious() == instruction)
5667 || ((GetPrevious() == nullptr)
5668 && instruction->IsPhi()
5669 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005670 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005671 << "Doing parallel moves for the same instruction.";
5672 } else {
5673 DCHECK(false) << "Doing parallel moves for the same instruction.";
5674 }
5675 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005676 }
5677 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005678 for (const MoveOperands& move : moves_) {
5679 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005680 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005681 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005682 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005683 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005684 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005685 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005686 }
5687
Vladimir Marko225b6462015-09-28 12:17:40 +01005688 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005689 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005690 }
5691
Vladimir Marko225b6462015-09-28 12:17:40 +01005692 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005693
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005694 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005695
5696 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005697 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005698
5699 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5700};
5701
Mark Mendell0616ae02015-04-17 12:49:27 -04005702} // namespace art
5703
Vladimir Markob4536b72015-11-24 13:45:23 +00005704#ifdef ART_ENABLE_CODEGEN_arm
5705#include "nodes_arm.h"
5706#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005707#ifdef ART_ENABLE_CODEGEN_arm64
5708#include "nodes_arm64.h"
5709#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005710#ifdef ART_ENABLE_CODEGEN_x86
5711#include "nodes_x86.h"
5712#endif
5713
5714namespace art {
5715
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005716class HGraphVisitor : public ValueObject {
5717 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005718 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5719 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005720
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005721 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005722 virtual void VisitBasicBlock(HBasicBlock* block);
5723
Roland Levillain633021e2014-10-01 14:12:25 +01005724 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005725 void VisitInsertionOrder();
5726
Roland Levillain633021e2014-10-01 14:12:25 +01005727 // Visit the graph following dominator tree reverse post-order.
5728 void VisitReversePostOrder();
5729
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005730 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005731
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005732 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005733#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005734 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5735
5736 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5737
5738#undef DECLARE_VISIT_INSTRUCTION
5739
5740 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005741 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005742
5743 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5744};
5745
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005746class HGraphDelegateVisitor : public HGraphVisitor {
5747 public:
5748 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5749 virtual ~HGraphDelegateVisitor() {}
5750
5751 // Visit functions that delegate to to super class.
5752#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005753 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005754
5755 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5756
5757#undef DECLARE_VISIT_INSTRUCTION
5758
5759 private:
5760 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5761};
5762
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005763class HInsertionOrderIterator : public ValueObject {
5764 public:
5765 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5766
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005767 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005768 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005769 void Advance() { ++index_; }
5770
5771 private:
5772 const HGraph& graph_;
5773 size_t index_;
5774
5775 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5776};
5777
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005778class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005779 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005780 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5781 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005782 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005783 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005784
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005785 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5786 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005787 void Advance() { ++index_; }
5788
5789 private:
5790 const HGraph& graph_;
5791 size_t index_;
5792
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005793 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005794};
5795
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005796class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005797 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005798 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005799 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005800 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005801 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005802 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005803
5804 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005805 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005806 void Advance() { --index_; }
5807
5808 private:
5809 const HGraph& graph_;
5810 size_t index_;
5811
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005812 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005813};
5814
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005815class HLinearPostOrderIterator : public ValueObject {
5816 public:
5817 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005818 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005819
5820 bool Done() const { return index_ == 0; }
5821
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005822 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005823
5824 void Advance() {
5825 --index_;
5826 DCHECK_GE(index_, 0U);
5827 }
5828
5829 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005830 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005831 size_t index_;
5832
5833 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5834};
5835
5836class HLinearOrderIterator : public ValueObject {
5837 public:
5838 explicit HLinearOrderIterator(const HGraph& graph)
5839 : order_(graph.GetLinearOrder()), index_(0) {}
5840
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005841 bool Done() const { return index_ == order_.size(); }
5842 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005843 void Advance() { ++index_; }
5844
5845 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005846 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005847 size_t index_;
5848
5849 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5850};
5851
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005852// Iterator over the blocks that art part of the loop. Includes blocks part
5853// of an inner loop. The order in which the blocks are iterated is on their
5854// block id.
5855class HBlocksInLoopIterator : public ValueObject {
5856 public:
5857 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5858 : blocks_in_loop_(info.GetBlocks()),
5859 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5860 index_(0) {
5861 if (!blocks_in_loop_.IsBitSet(index_)) {
5862 Advance();
5863 }
5864 }
5865
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005866 bool Done() const { return index_ == blocks_.size(); }
5867 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005868 void Advance() {
5869 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005870 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005871 if (blocks_in_loop_.IsBitSet(index_)) {
5872 break;
5873 }
5874 }
5875 }
5876
5877 private:
5878 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005879 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005880 size_t index_;
5881
5882 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5883};
5884
Mingyao Yang3584bce2015-05-19 16:01:59 -07005885// Iterator over the blocks that art part of the loop. Includes blocks part
5886// of an inner loop. The order in which the blocks are iterated is reverse
5887// post order.
5888class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5889 public:
5890 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5891 : blocks_in_loop_(info.GetBlocks()),
5892 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5893 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005894 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005895 Advance();
5896 }
5897 }
5898
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005899 bool Done() const { return index_ == blocks_.size(); }
5900 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005901 void Advance() {
5902 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005903 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5904 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005905 break;
5906 }
5907 }
5908 }
5909
5910 private:
5911 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005912 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005913 size_t index_;
5914
5915 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5916};
5917
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005918inline int64_t Int64FromConstant(HConstant* constant) {
5919 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5920 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5921 : constant->AsLongConstant()->GetValue();
5922}
5923
Vladimir Marko58155012015-08-19 12:49:41 +00005924inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5925 // For the purposes of the compiler, the dex files must actually be the same object
5926 // if we want to safely treat them as the same. This is especially important for JIT
5927 // as custom class loaders can open the same underlying file (or memory) multiple
5928 // times and provide different class resolution but no two class loaders should ever
5929 // use the same DexFile object - doing so is an unsupported hack that can lead to
5930 // all sorts of weird failures.
5931 return &lhs == &rhs;
5932}
5933
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005934#define INSTRUCTION_TYPE_CHECK(type, super) \
5935 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
5936 inline const H##type* HInstruction::As##type() const { \
5937 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
5938 } \
5939 inline H##type* HInstruction::As##type() { \
5940 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
5941 }
5942
5943 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
5944#undef INSTRUCTION_TYPE_CHECK
5945
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005946} // namespace art
5947
5948#endif // ART_COMPILER_OPTIMIZING_NODES_H_