blob: d5110a7172b698510957cbf39b74187e9cb612ad [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039
40namespace art {
41
David Brazdil1abb4192015-02-17 18:33:36 +000042class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010047class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000049class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000055class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010057class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010058class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010061class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Mathieu Chartier736b5602015-09-02 14:54:11 -070064namespace mirror {
65class DexCache;
66} // namespace mirror
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068static const int kDefaultNumberOfBlocks = 8;
69static const int kDefaultNumberOfSuccessors = 2;
70static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010071static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010072static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
Calin Juravle9aec02f2014-11-18 23:06:35 +000075static constexpr uint32_t kMaxIntShiftValue = 0x1f;
76static constexpr uint64_t kMaxLongShiftValue = 0x3f;
77
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010078static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070079static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010080
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010081static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
82
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060083static constexpr uint32_t kNoDexPc = -1;
84
Dave Allison20dfc792014-06-16 20:44:29 -070085enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070086 // All types.
87 kCondEQ, // ==
88 kCondNE, // !=
89 // Signed integers and floating-point numbers.
90 kCondLT, // <
91 kCondLE, // <=
92 kCondGT, // >
93 kCondGE, // >=
94 // Unsigned integers.
95 kCondB, // <
96 kCondBE, // <=
97 kCondA, // >
98 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070099};
100
Vladimir Markof9f64412015-09-02 14:05:49 +0100101class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100102 public:
103 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
104
105 void AddInstruction(HInstruction* instruction);
106 void RemoveInstruction(HInstruction* instruction);
107
David Brazdilc3d743f2015-04-22 13:40:50 +0100108 // Insert `instruction` before/after an existing instruction `cursor`.
109 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
110 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
111
Roland Levillain6b469232014-09-25 10:10:38 +0100112 // Return true if this list contains `instruction`.
113 bool Contains(HInstruction* instruction) const;
114
Roland Levillainccc07a92014-09-16 14:48:16 +0100115 // Return true if `instruction1` is found before `instruction2` in
116 // this instruction list and false otherwise. Abort if none
117 // of these instructions is found.
118 bool FoundBefore(const HInstruction* instruction1,
119 const HInstruction* instruction2) const;
120
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000121 bool IsEmpty() const { return first_instruction_ == nullptr; }
122 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
123
124 // Update the block of all instructions to be `block`.
125 void SetBlockOfInstructions(HBasicBlock* block) const;
126
127 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
128 void Add(const HInstructionList& instruction_list);
129
David Brazdil2d7352b2015-04-20 14:52:42 +0100130 // Return the number of instructions in the list. This is an expensive operation.
131 size_t CountSize() const;
132
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100133 private:
134 HInstruction* first_instruction_;
135 HInstruction* last_instruction_;
136
137 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000138 friend class HGraph;
139 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100141 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100142
143 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
144};
145
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000146// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100147class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000148 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100149 HGraph(ArenaAllocator* arena,
150 const DexFile& dex_file,
151 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100152 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100154 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100155 bool debuggable = false,
156 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000157 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100158 blocks_(arena->Adapter(kArenaAllocBlockList)),
159 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
160 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700161 entry_block_(nullptr),
162 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100163 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100164 number_of_vregs_(0),
165 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000166 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400167 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000168 has_try_catch_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000169 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000170 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100171 dex_file_(dex_file),
172 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100173 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100174 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100175 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700176 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000177 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100178 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
179 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
180 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
181 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
182 cached_current_method_(nullptr) {
183 blocks_.reserve(kDefaultNumberOfBlocks);
184 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000185
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000186 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100187 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
188
David Brazdil69ba7b72015-06-23 18:27:30 +0100189 bool IsInSsaForm() const { return in_ssa_form_; }
190
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000191 HBasicBlock* GetEntryBlock() const { return entry_block_; }
192 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100193 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000194
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000195 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
196 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000197
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000198 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100199
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000200 // Try building the SSA form of this graph, with dominance computation and loop
201 // recognition. Returns whether it was successful in doing all these steps.
202 bool TryBuildingSsa() {
203 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000204 // The SSA builder requires loops to all be natural. Specifically, the dead phi
205 // elimination phase checks the consistency of the graph when doing a post-order
206 // visit for eliminating dead phis: a dead phi can only have loop header phi
207 // users remaining when being visited.
208 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100209 // Precompute per-block try membership before entering the SSA builder,
210 // which needs the information to build catch block phis from values of
211 // locals at throwing instructions inside try blocks.
212 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100214 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000215 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000216 }
217
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100218 void ComputeDominanceInformation();
219 void ClearDominanceInformation();
220
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000221 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000222 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100223 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100224 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000225
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000226 // Analyze all natural loops in this graph. Returns false if one
227 // loop is not natural, that is the header does not dominate the
228 // back edge.
229 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100230
David Brazdilffee3d32015-07-06 11:48:53 +0100231 // Iterate over blocks to compute try block membership. Needs reverse post
232 // order and loop information.
233 void ComputeTryBlockInformation();
234
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000235 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000236 // Returns the instruction used to replace the invoke expression or null if the
237 // invoke is for a void method.
238 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000239
Mingyao Yang3584bce2015-05-19 16:01:59 -0700240 // Need to add a couple of blocks to test if the loop body is entered and
241 // put deoptimization instructions, etc.
242 void TransformLoopHeaderForBCE(HBasicBlock* header);
243
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000244 // Removes `block` from the graph. Assumes `block` has been disconnected from
245 // other blocks and has no instructions or phis.
246 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000247
David Brazdilfc6a86a2015-06-26 10:33:45 +0000248 // Splits the edge between `block` and `successor` while preserving the
249 // indices in the predecessor/successor lists. If there are multiple edges
250 // between the blocks, the lowest indices are used.
251 // Returns the new block which is empty and has the same dex pc as `successor`.
252 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
253
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100254 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
255 void SimplifyLoop(HBasicBlock* header);
256
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000257 int32_t GetNextInstructionId() {
258 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000259 return current_instruction_id_++;
260 }
261
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000262 int32_t GetCurrentInstructionId() const {
263 return current_instruction_id_;
264 }
265
266 void SetCurrentInstructionId(int32_t id) {
267 current_instruction_id_ = id;
268 }
269
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100270 uint16_t GetMaximumNumberOfOutVRegs() const {
271 return maximum_number_of_out_vregs_;
272 }
273
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000274 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
275 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100276 }
277
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100278 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
279 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
280 }
281
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000282 void UpdateTemporariesVRegSlots(size_t slots) {
283 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100284 }
285
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000286 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100287 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000288 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100289 }
290
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100291 void SetNumberOfVRegs(uint16_t number_of_vregs) {
292 number_of_vregs_ = number_of_vregs;
293 }
294
295 uint16_t GetNumberOfVRegs() const {
296 return number_of_vregs_;
297 }
298
299 void SetNumberOfInVRegs(uint16_t value) {
300 number_of_in_vregs_ = value;
301 }
302
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100303 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100304 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100305 return number_of_vregs_ - number_of_in_vregs_;
306 }
307
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100308 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100309 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100310 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100311
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100312 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100313 return linear_order_;
314 }
315
Mark Mendell1152c922015-04-24 17:06:35 -0400316 bool HasBoundsChecks() const {
317 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800318 }
319
Mark Mendell1152c922015-04-24 17:06:35 -0400320 void SetHasBoundsChecks(bool value) {
321 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800322 }
323
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100324 bool ShouldGenerateConstructorBarrier() const {
325 return should_generate_constructor_barrier_;
326 }
327
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000328 bool IsDebuggable() const { return debuggable_; }
329
David Brazdil8d5b8b22015-03-24 10:51:52 +0000330 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000331 // already, it is created and inserted into the graph. This method is only for
332 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600333 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000334
335 // TODO: This is problematic for the consistency of reference type propagation
336 // because it can be created anytime after the pass and thus it will be left
337 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600338 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000339
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600340 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
341 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600343 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
344 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000345 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600346 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
347 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000348 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600349 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
350 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000351 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000352
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100353 HCurrentMethod* GetCurrentMethod();
354
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100355 const DexFile& GetDexFile() const {
356 return dex_file_;
357 }
358
359 uint32_t GetMethodIdx() const {
360 return method_idx_;
361 }
362
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100363 InvokeType GetInvokeType() const {
364 return invoke_type_;
365 }
366
Mark Mendellc4701932015-04-10 13:18:51 -0400367 InstructionSet GetInstructionSet() const {
368 return instruction_set_;
369 }
370
David Brazdil77a48ae2015-09-15 12:34:04 +0000371 bool HasTryCatch() const { return has_try_catch_; }
372 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100373
Mark Mendellf6529172015-11-17 11:16:56 -0500374 // Returns an instruction with the opposite boolean value from 'cond'.
375 // The instruction has been inserted into the graph, either as a constant, or
376 // before cursor.
377 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
378
David Brazdil2d7352b2015-04-20 14:52:42 +0100379 private:
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100380 void FindBackEdges(ArenaBitVector* visited);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000381 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100382 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000383
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000384 template <class InstructionType, typename ValueType>
385 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600386 ArenaSafeMap<ValueType, InstructionType*>* cache,
387 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000388 // Try to find an existing constant of the given value.
389 InstructionType* constant = nullptr;
390 auto cached_constant = cache->find(value);
391 if (cached_constant != cache->end()) {
392 constant = cached_constant->second;
393 }
394
395 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100396 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000397 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600398 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000399 cache->Overwrite(value, constant);
400 InsertConstant(constant);
401 }
402 return constant;
403 }
404
David Brazdil8d5b8b22015-03-24 10:51:52 +0000405 void InsertConstant(HConstant* instruction);
406
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000407 // Cache a float constant into the graph. This method should only be
408 // called by the SsaBuilder when creating "equivalent" instructions.
409 void CacheFloatConstant(HFloatConstant* constant);
410
411 // See CacheFloatConstant comment.
412 void CacheDoubleConstant(HDoubleConstant* constant);
413
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000414 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000415
416 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100417 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000418
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100420 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000421
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100422 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100423 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100424
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000425 HBasicBlock* entry_block_;
426 HBasicBlock* exit_block_;
427
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100428 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100429 uint16_t maximum_number_of_out_vregs_;
430
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100431 // The number of virtual registers in this method. Contains the parameters.
432 uint16_t number_of_vregs_;
433
434 // The number of virtual registers used by parameters of this method.
435 uint16_t number_of_in_vregs_;
436
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000437 // Number of vreg size slots that the temporaries use (used in baseline compiler).
438 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100439
Mark Mendell1152c922015-04-24 17:06:35 -0400440 // Has bounds checks. We can totally skip BCE if it's false.
441 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800442
David Brazdil77a48ae2015-09-15 12:34:04 +0000443 // Flag whether there are any try/catch blocks in the graph. We will skip
444 // try/catch-related passes if false.
445 bool has_try_catch_;
446
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000447 // Indicates whether the graph should be compiled in a way that
448 // ensures full debuggability. If false, we can apply more
449 // aggressive optimizations that may limit the level of debugging.
450 const bool debuggable_;
451
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000452 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000453 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000454
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100455 // The dex file from which the method is from.
456 const DexFile& dex_file_;
457
458 // The method index in the dex file.
459 const uint32_t method_idx_;
460
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100461 // If inlined, this encodes how the callee is being invoked.
462 const InvokeType invoke_type_;
463
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100464 // Whether the graph has been transformed to SSA form. Only used
465 // in debug mode to ensure we are not using properties only valid
466 // for non-SSA form (like the number of temporaries).
467 bool in_ssa_form_;
468
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100469 const bool should_generate_constructor_barrier_;
470
Mathieu Chartiere401d142015-04-22 13:56:20 -0700471 const InstructionSet instruction_set_;
472
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000473 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000474 HNullConstant* cached_null_constant_;
475 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000476 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000477 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000478 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000479
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100480 HCurrentMethod* cached_current_method_;
481
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000482 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100483 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000484 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000485 DISALLOW_COPY_AND_ASSIGN(HGraph);
486};
487
Vladimir Markof9f64412015-09-02 14:05:49 +0100488class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000489 public:
490 HLoopInformation(HBasicBlock* header, HGraph* graph)
491 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100492 suspend_check_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100493 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100494 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100495 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
496 back_edges_.reserve(kDefaultNumberOfBackEdges);
497 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100498
499 HBasicBlock* GetHeader() const {
500 return header_;
501 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000502
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000503 void SetHeader(HBasicBlock* block) {
504 header_ = block;
505 }
506
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100507 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
508 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
509 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
510
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000511 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100512 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000513 }
514
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100515 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100516 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100517 }
518
David Brazdil46e2a392015-03-16 17:31:52 +0000519 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100520 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100521 }
522
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000523 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100524 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000525 }
526
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100527 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100528
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100529 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100530 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100531 }
532
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100533 // Returns the lifetime position of the back edge that has the
534 // greatest lifetime position.
535 size_t GetLifetimeEnd() const;
536
537 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100538 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100539 }
540
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100541 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100542 // that is the header dominates the back edge.
543 bool Populate();
544
David Brazdila4b8c212015-05-07 09:59:30 +0100545 // Reanalyzes the loop by removing loop info from its blocks and re-running
546 // Populate(). If there are no back edges left, the loop info is completely
547 // removed as well as its SuspendCheck instruction. It must be run on nested
548 // inner loops first.
549 void Update();
550
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100551 // Returns whether this loop information contains `block`.
552 // Note that this loop information *must* be populated before entering this function.
553 bool Contains(const HBasicBlock& block) const;
554
555 // Returns whether this loop information is an inner loop of `other`.
556 // Note that `other` *must* be populated before entering this function.
557 bool IsIn(const HLoopInformation& other) const;
558
Aart Bik73f1f3b2015-10-28 15:28:08 -0700559 // Returns true if instruction is not defined within this loop or any loop nested inside
560 // this loop. If must_dominate is set, only definitions that actually dominate the loop
561 // header can be invariant. Otherwise, any definition outside the loop, including
562 // definitions that appear after the loop, is invariant.
563 bool IsLoopInvariant(HInstruction* instruction, bool must_dominate) const;
564
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100565 const ArenaBitVector& GetBlocks() const { return blocks_; }
566
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000567 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000568 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000569
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000570 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100571 // Internal recursive implementation of `Populate`.
572 void PopulateRecursive(HBasicBlock* block);
573
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000574 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100575 HSuspendCheck* suspend_check_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100576 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100577 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000578
579 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
580};
581
David Brazdilec16f792015-08-19 15:04:01 +0100582// Stores try/catch information for basic blocks.
583// Note that HGraph is constructed so that catch blocks cannot simultaneously
584// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100585class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100586 public:
587 // Try block information constructor.
588 explicit TryCatchInformation(const HTryBoundary& try_entry)
589 : try_entry_(&try_entry),
590 catch_dex_file_(nullptr),
591 catch_type_index_(DexFile::kDexNoIndex16) {
592 DCHECK(try_entry_ != nullptr);
593 }
594
595 // Catch block information constructor.
596 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
597 : try_entry_(nullptr),
598 catch_dex_file_(&dex_file),
599 catch_type_index_(catch_type_index) {}
600
601 bool IsTryBlock() const { return try_entry_ != nullptr; }
602
603 const HTryBoundary& GetTryEntry() const {
604 DCHECK(IsTryBlock());
605 return *try_entry_;
606 }
607
608 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
609
610 bool IsCatchAllTypeIndex() const {
611 DCHECK(IsCatchBlock());
612 return catch_type_index_ == DexFile::kDexNoIndex16;
613 }
614
615 uint16_t GetCatchTypeIndex() const {
616 DCHECK(IsCatchBlock());
617 return catch_type_index_;
618 }
619
620 const DexFile& GetCatchDexFile() const {
621 DCHECK(IsCatchBlock());
622 return *catch_dex_file_;
623 }
624
625 private:
626 // One of possibly several TryBoundary instructions entering the block's try.
627 // Only set for try blocks.
628 const HTryBoundary* try_entry_;
629
630 // Exception type information. Only set for catch blocks.
631 const DexFile* catch_dex_file_;
632 const uint16_t catch_type_index_;
633};
634
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100635static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100636static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100637
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000638// A block in a method. Contains the list of instructions represented
639// as a double linked list. Each block knows its predecessors and
640// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100641
Vladimir Markof9f64412015-09-02 14:05:49 +0100642class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600644 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000645 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000646 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
647 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000648 loop_information_(nullptr),
649 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000650 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100651 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100652 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100653 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000654 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000655 try_catch_information_(nullptr) {
656 predecessors_.reserve(kDefaultNumberOfPredecessors);
657 successors_.reserve(kDefaultNumberOfSuccessors);
658 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
659 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000660
Vladimir Marko60584552015-09-03 13:35:12 +0000661 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100662 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000663 }
664
Vladimir Marko60584552015-09-03 13:35:12 +0000665 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100666 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000667 }
668
David Brazdild26a4112015-11-10 11:07:31 +0000669 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
670 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
671
Vladimir Marko60584552015-09-03 13:35:12 +0000672 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
673 return ContainsElement(successors_, block, start_from);
674 }
675
676 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100677 return dominated_blocks_;
678 }
679
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100680 bool IsEntryBlock() const {
681 return graph_->GetEntryBlock() == this;
682 }
683
684 bool IsExitBlock() const {
685 return graph_->GetExitBlock() == this;
686 }
687
David Brazdil46e2a392015-03-16 17:31:52 +0000688 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000689 bool IsSingleTryBoundary() const;
690
691 // Returns true if this block emits nothing but a jump.
692 bool IsSingleJump() const {
693 HLoopInformation* loop_info = GetLoopInformation();
694 return (IsSingleGoto() || IsSingleTryBoundary())
695 // Back edges generate a suspend check.
696 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
697 }
David Brazdil46e2a392015-03-16 17:31:52 +0000698
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000699 void AddBackEdge(HBasicBlock* back_edge) {
700 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000701 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000702 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100703 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000704 loop_information_->AddBackEdge(back_edge);
705 }
706
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000707 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000708 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000709
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100710 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000711 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600712 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000713
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000714 HBasicBlock* GetDominator() const { return dominator_; }
715 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000716 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
717
718 void RemoveDominatedBlock(HBasicBlock* block) {
719 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100720 }
Vladimir Marko60584552015-09-03 13:35:12 +0000721
722 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
723 ReplaceElement(dominated_blocks_, existing, new_block);
724 }
725
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100726 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000727
728 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100729 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000730 }
731
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100732 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
733 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100734 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100735 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100736 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
737 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000738
739 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000740 successors_.push_back(block);
741 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000742 }
743
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100744 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
745 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100746 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000747 new_block->predecessors_.push_back(this);
748 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000749 }
750
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000751 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
752 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000753 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000754 new_block->successors_.push_back(this);
755 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000756 }
757
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100758 // Insert `this` between `predecessor` and `successor. This method
759 // preserves the indicies, and will update the first edge found between
760 // `predecessor` and `successor`.
761 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
762 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100763 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000764 successor->predecessors_[predecessor_index] = this;
765 predecessor->successors_[successor_index] = this;
766 successors_.push_back(successor);
767 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100768 }
769
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100770 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000771 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100772 }
773
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000774 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000775 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000776 }
777
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100778 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000779 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780 }
781
782 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000783 predecessors_.push_back(block);
784 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100785 }
786
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100787 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000788 DCHECK_EQ(predecessors_.size(), 2u);
789 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100790 }
791
David Brazdil769c9e52015-04-27 13:54:09 +0100792 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000793 DCHECK_EQ(successors_.size(), 2u);
794 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100795 }
796
David Brazdilfc6a86a2015-06-26 10:33:45 +0000797 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000798 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100799 }
800
David Brazdilfc6a86a2015-06-26 10:33:45 +0000801 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000802 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100803 }
804
David Brazdilfc6a86a2015-06-26 10:33:45 +0000805 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000806 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100807 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000808 }
809
810 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000811 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100812 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813 }
814
815 // Returns whether the first occurrence of `predecessor` in the list of
816 // predecessors is at index `idx`.
817 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100818 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000819 return GetPredecessorIndexOf(predecessor) == idx;
820 }
821
David Brazdild7558da2015-09-22 13:04:14 +0100822 // Create a new block between this block and its predecessors. The new block
823 // is added to the graph, all predecessor edges are relinked to it and an edge
824 // is created to `this`. Returns the new empty block. Reverse post order or
825 // loop and try/catch information are not updated.
826 HBasicBlock* CreateImmediateDominator();
827
David Brazdilfc6a86a2015-06-26 10:33:45 +0000828 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100829 // created, latter block. Note that this method will add the block to the
830 // graph, create a Goto at the end of the former block and will create an edge
831 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100832 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000833 HBasicBlock* SplitBefore(HInstruction* cursor);
834
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000835 // Split the block into two blocks just after `cursor`. Returns the newly
836 // created block. Note that this method just updates raw block information,
837 // like predecessors, successors, dominators, and instruction list. It does not
838 // update the graph, reverse post order, loop information, nor make sure the
839 // blocks are consistent (for example ending with a control flow instruction).
840 HBasicBlock* SplitAfter(HInstruction* cursor);
841
David Brazdil9bc43612015-11-05 21:25:24 +0000842 // Split catch block into two blocks after the original move-exception bytecode
843 // instruction, or at the beginning if not present. Returns the newly created,
844 // latter block, or nullptr if such block could not be created (must be dead
845 // in that case). Note that this method just updates raw block information,
846 // like predecessors, successors, dominators, and instruction list. It does not
847 // update the graph, reverse post order, loop information, nor make sure the
848 // blocks are consistent (for example ending with a control flow instruction).
849 HBasicBlock* SplitCatchBlockAfterMoveException();
850
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000851 // Merge `other` at the end of `this`. Successors and dominated blocks of
852 // `other` are changed to be successors and dominated blocks of `this`. Note
853 // that this method does not update the graph, reverse post order, loop
854 // information, nor make sure the blocks are consistent (for example ending
855 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100856 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000857
858 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
859 // of `this` are moved to `other`.
860 // Note that this method does not update the graph, reverse post order, loop
861 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000862 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000863 void ReplaceWith(HBasicBlock* other);
864
David Brazdil2d7352b2015-04-20 14:52:42 +0100865 // Merge `other` at the end of `this`. This method updates loops, reverse post
866 // order, links to predecessors, successors, dominators and deletes the block
867 // from the graph. The two blocks must be successive, i.e. `this` the only
868 // predecessor of `other` and vice versa.
869 void MergeWith(HBasicBlock* other);
870
871 // Disconnects `this` from all its predecessors, successors and dominator,
872 // removes it from all loops it is included in and eventually from the graph.
873 // The block must not dominate any other block. Predecessors and successors
874 // are safely updated.
875 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000876
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000877 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100878 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100879 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100880 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100881 // Replace instruction `initial` with `replacement` within this block.
882 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
883 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100884 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100885 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000886 // RemoveInstruction and RemovePhi delete a given instruction from the respective
887 // instruction list. With 'ensure_safety' set to true, it verifies that the
888 // instruction is not in use and removes it from the use lists of its inputs.
889 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
890 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100891 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100892
893 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100894 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100895 }
896
Roland Levillain6b879dd2014-09-22 17:13:44 +0100897 bool IsLoopPreHeaderFirstPredecessor() const {
898 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100899 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100900 }
901
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100902 HLoopInformation* GetLoopInformation() const {
903 return loop_information_;
904 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000905
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000906 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100907 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000908 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100909 void SetInLoop(HLoopInformation* info) {
910 if (IsLoopHeader()) {
911 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100912 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100913 loop_information_ = info;
914 } else if (loop_information_->Contains(*info->GetHeader())) {
915 // Block is currently part of an outer loop. Make it part of this inner loop.
916 // Note that a non loop header having a loop information means this loop information
917 // has already been populated
918 loop_information_ = info;
919 } else {
920 // Block is part of an inner loop. Do not update the loop information.
921 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
922 // at this point, because this method is being called while populating `info`.
923 }
924 }
925
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000926 // Raw update of the loop information.
927 void SetLoopInformation(HLoopInformation* info) {
928 loop_information_ = info;
929 }
930
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100931 bool IsInLoop() const { return loop_information_ != nullptr; }
932
David Brazdilec16f792015-08-19 15:04:01 +0100933 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
934
935 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
936 try_catch_information_ = try_catch_information;
937 }
938
939 bool IsTryBlock() const {
940 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
941 }
942
943 bool IsCatchBlock() const {
944 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
945 }
David Brazdilffee3d32015-07-06 11:48:53 +0100946
947 // Returns the try entry that this block's successors should have. They will
948 // be in the same try, unless the block ends in a try boundary. In that case,
949 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +0100950 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100951
David Brazdild7558da2015-09-22 13:04:14 +0100952 bool HasThrowingInstructions() const;
953
David Brazdila4b8c212015-05-07 09:59:30 +0100954 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100955 bool Dominates(HBasicBlock* block) const;
956
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100957 size_t GetLifetimeStart() const { return lifetime_start_; }
958 size_t GetLifetimeEnd() const { return lifetime_end_; }
959
960 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
961 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
962
David Brazdil8d5b8b22015-03-24 10:51:52 +0000963 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000964 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100965 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000966 bool HasSinglePhi() const;
967
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000968 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000969 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +0000970 ArenaVector<HBasicBlock*> predecessors_;
971 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100972 HInstructionList instructions_;
973 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000974 HLoopInformation* loop_information_;
975 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +0000976 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100977 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100978 // The dex program counter of the first instruction of this block.
979 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100980 size_t lifetime_start_;
981 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +0100982 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +0100983
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000984 friend class HGraph;
985 friend class HInstruction;
986
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000987 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
988};
989
David Brazdilb2bd1c52015-03-25 11:17:37 +0000990// Iterates over the LoopInformation of all loops which contain 'block'
991// from the innermost to the outermost.
992class HLoopInformationOutwardIterator : public ValueObject {
993 public:
994 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
995 : current_(block.GetLoopInformation()) {}
996
997 bool Done() const { return current_ == nullptr; }
998
999 void Advance() {
1000 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001001 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001002 }
1003
1004 HLoopInformation* Current() const {
1005 DCHECK(!Done());
1006 return current_;
1007 }
1008
1009 private:
1010 HLoopInformation* current_;
1011
1012 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1013};
1014
Alexandre Ramesef20f712015-06-09 10:29:30 +01001015#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001016 M(Above, Condition) \
1017 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001018 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001019 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001020 M(ArrayGet, Instruction) \
1021 M(ArrayLength, Instruction) \
1022 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001023 M(Below, Condition) \
1024 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001025 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001026 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001027 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001028 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001029 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001030 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001031 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001032 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001033 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001034 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001035 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001036 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001037 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001038 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001039 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001040 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001041 M(FloatConstant, Constant) \
1042 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001043 M(GreaterThan, Condition) \
1044 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001045 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001046 M(InstanceFieldGet, Instruction) \
1047 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001048 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001049 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001050 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001051 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001052 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001053 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001054 M(LessThan, Condition) \
1055 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001056 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001057 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001058 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001059 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001060 M(Local, Instruction) \
1061 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001062 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001063 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001064 M(Mul, BinaryOperation) \
1065 M(Neg, UnaryOperation) \
1066 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001067 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001068 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001069 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001070 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001071 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001072 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001073 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001074 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001075 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001076 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001077 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001078 M(Return, Instruction) \
1079 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001080 M(Shl, BinaryOperation) \
1081 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001082 M(StaticFieldGet, Instruction) \
1083 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001084 M(UnresolvedInstanceFieldGet, Instruction) \
1085 M(UnresolvedInstanceFieldSet, Instruction) \
1086 M(UnresolvedStaticFieldGet, Instruction) \
1087 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001088 M(StoreLocal, Instruction) \
1089 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001090 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001091 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001092 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001093 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001094 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001095 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001096 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001097
Alexandre Ramesef20f712015-06-09 10:29:30 +01001098#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1099
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001100#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001101#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001102#else
1103#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001104 M(Arm64DataProcWithShifterOp, Instruction) \
Alexandre Rames418318f2015-11-20 15:55:47 +00001105 M(Arm64IntermediateAddress, Instruction) \
1106 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001107#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001108
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001109#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1110
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001111#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1112
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001113#ifndef ART_ENABLE_CODEGEN_x86
1114#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1115#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001116#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1117 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001118 M(X86LoadFromConstantTable, Instruction) \
1119 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001120#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001121
1122#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1123
1124#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1125 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1126 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1127 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001128 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001129 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001130 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1131 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1132
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001133#define FOR_EACH_INSTRUCTION(M) \
1134 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1135 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001136 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001137 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001138 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001139
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001140#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001141FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1142#undef FORWARD_DECLARATION
1143
Roland Levillainccc07a92014-09-16 14:48:16 +01001144#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001145 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1146 const char* DebugName() const OVERRIDE { return #type; } \
1147 const H##type* As##type() const OVERRIDE { return this; } \
1148 H##type* As##type() OVERRIDE { return this; } \
1149 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001150 return other->Is##type(); \
1151 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001152 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001153
David Brazdiled596192015-01-23 10:39:45 +00001154template <typename T> class HUseList;
1155
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001156template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001157class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001158 public:
David Brazdiled596192015-01-23 10:39:45 +00001159 HUseListNode* GetPrevious() const { return prev_; }
1160 HUseListNode* GetNext() const { return next_; }
1161 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001162 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001163 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001164
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001165 private:
David Brazdiled596192015-01-23 10:39:45 +00001166 HUseListNode(T user, size_t index)
1167 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1168
1169 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001170 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001171 HUseListNode<T>* prev_;
1172 HUseListNode<T>* next_;
1173
1174 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001175
1176 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1177};
1178
David Brazdiled596192015-01-23 10:39:45 +00001179template <typename T>
1180class HUseList : public ValueObject {
1181 public:
1182 HUseList() : first_(nullptr) {}
1183
1184 void Clear() {
1185 first_ = nullptr;
1186 }
1187
1188 // Adds a new entry at the beginning of the use list and returns
1189 // the newly created node.
1190 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001191 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001192 if (IsEmpty()) {
1193 first_ = new_node;
1194 } else {
1195 first_->prev_ = new_node;
1196 new_node->next_ = first_;
1197 first_ = new_node;
1198 }
1199 return new_node;
1200 }
1201
1202 HUseListNode<T>* GetFirst() const {
1203 return first_;
1204 }
1205
1206 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001207 DCHECK(node != nullptr);
1208 DCHECK(Contains(node));
1209
David Brazdiled596192015-01-23 10:39:45 +00001210 if (node->prev_ != nullptr) {
1211 node->prev_->next_ = node->next_;
1212 }
1213 if (node->next_ != nullptr) {
1214 node->next_->prev_ = node->prev_;
1215 }
1216 if (node == first_) {
1217 first_ = node->next_;
1218 }
1219 }
1220
David Brazdil1abb4192015-02-17 18:33:36 +00001221 bool Contains(const HUseListNode<T>* node) const {
1222 if (node == nullptr) {
1223 return false;
1224 }
1225 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1226 if (current == node) {
1227 return true;
1228 }
1229 }
1230 return false;
1231 }
1232
David Brazdiled596192015-01-23 10:39:45 +00001233 bool IsEmpty() const {
1234 return first_ == nullptr;
1235 }
1236
1237 bool HasOnlyOneUse() const {
1238 return first_ != nullptr && first_->next_ == nullptr;
1239 }
1240
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001241 size_t SizeSlow() const {
1242 size_t count = 0;
1243 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1244 ++count;
1245 }
1246 return count;
1247 }
1248
David Brazdiled596192015-01-23 10:39:45 +00001249 private:
1250 HUseListNode<T>* first_;
1251};
1252
1253template<typename T>
1254class HUseIterator : public ValueObject {
1255 public:
1256 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1257
1258 bool Done() const { return current_ == nullptr; }
1259
1260 void Advance() {
1261 DCHECK(!Done());
1262 current_ = current_->GetNext();
1263 }
1264
1265 HUseListNode<T>* Current() const {
1266 DCHECK(!Done());
1267 return current_;
1268 }
1269
1270 private:
1271 HUseListNode<T>* current_;
1272
1273 friend class HValue;
1274};
1275
David Brazdil1abb4192015-02-17 18:33:36 +00001276// This class is used by HEnvironment and HInstruction classes to record the
1277// instructions they use and pointers to the corresponding HUseListNodes kept
1278// by the used instructions.
1279template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001280class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001281 public:
1282 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1283 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1284
1285 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1286 : instruction_(old_record.instruction_), use_node_(use_node) {
1287 DCHECK(instruction_ != nullptr);
1288 DCHECK(use_node_ != nullptr);
1289 DCHECK(old_record.use_node_ == nullptr);
1290 }
1291
1292 HInstruction* GetInstruction() const { return instruction_; }
1293 HUseListNode<T>* GetUseNode() const { return use_node_; }
1294
1295 private:
1296 // Instruction used by the user.
1297 HInstruction* instruction_;
1298
1299 // Corresponding entry in the use list kept by 'instruction_'.
1300 HUseListNode<T>* use_node_;
1301};
1302
Aart Bik854a02b2015-07-14 16:07:00 -07001303/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001304 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001305 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001306 * For write/read dependences on fields/arrays, the dependence analysis uses
1307 * type disambiguation (e.g. a float field write cannot modify the value of an
1308 * integer field read) and the access type (e.g. a reference array write cannot
1309 * modify the value of a reference field read [although it may modify the
1310 * reference fetch prior to reading the field, which is represented by its own
1311 * write/read dependence]). The analysis makes conservative points-to
1312 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1313 * the same, and any reference read depends on any reference read without
1314 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001315 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001316 * The internal representation uses 38-bit and is described in the table below.
1317 * The first line indicates the side effect, and for field/array accesses the
1318 * second line indicates the type of the access (in the order of the
1319 * Primitive::Type enum).
1320 * The two numbered lines below indicate the bit position in the bitfield (read
1321 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001322 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001323 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1324 * +-------------+---------+---------+--------------+---------+---------+
1325 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1326 * | 3 |333333322|222222221| 1 |111111110|000000000|
1327 * | 7 |654321098|765432109| 8 |765432109|876543210|
1328 *
1329 * Note that, to ease the implementation, 'changes' bits are least significant
1330 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001331 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001332class SideEffects : public ValueObject {
1333 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001334 SideEffects() : flags_(0) {}
1335
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001336 static SideEffects None() {
1337 return SideEffects(0);
1338 }
1339
1340 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001341 return SideEffects(kAllChangeBits | kAllDependOnBits);
1342 }
1343
1344 static SideEffects AllChanges() {
1345 return SideEffects(kAllChangeBits);
1346 }
1347
1348 static SideEffects AllDependencies() {
1349 return SideEffects(kAllDependOnBits);
1350 }
1351
1352 static SideEffects AllExceptGCDependency() {
1353 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1354 }
1355
1356 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001357 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001358 }
1359
Aart Bik34c3ba92015-07-20 14:08:59 -07001360 static SideEffects AllWrites() {
1361 return SideEffects(kAllWrites);
1362 }
1363
1364 static SideEffects AllReads() {
1365 return SideEffects(kAllReads);
1366 }
1367
1368 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1369 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001370 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001371 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001372 }
1373
Aart Bik854a02b2015-07-14 16:07:00 -07001374 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1375 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001376 }
1377
Aart Bik34c3ba92015-07-20 14:08:59 -07001378 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1379 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001380 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001381 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001382 }
1383
1384 static SideEffects ArrayReadOfType(Primitive::Type type) {
1385 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1386 }
1387
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001388 static SideEffects CanTriggerGC() {
1389 return SideEffects(1ULL << kCanTriggerGCBit);
1390 }
1391
1392 static SideEffects DependsOnGC() {
1393 return SideEffects(1ULL << kDependsOnGCBit);
1394 }
1395
Aart Bik854a02b2015-07-14 16:07:00 -07001396 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001397 SideEffects Union(SideEffects other) const {
1398 return SideEffects(flags_ | other.flags_);
1399 }
1400
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001401 SideEffects Exclusion(SideEffects other) const {
1402 return SideEffects(flags_ & ~other.flags_);
1403 }
1404
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001405 void Add(SideEffects other) {
1406 flags_ |= other.flags_;
1407 }
1408
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001409 bool Includes(SideEffects other) const {
1410 return (other.flags_ & flags_) == other.flags_;
1411 }
1412
1413 bool HasSideEffects() const {
1414 return (flags_ & kAllChangeBits);
1415 }
1416
1417 bool HasDependencies() const {
1418 return (flags_ & kAllDependOnBits);
1419 }
1420
1421 // Returns true if there are no side effects or dependencies.
1422 bool DoesNothing() const {
1423 return flags_ == 0;
1424 }
1425
Aart Bik854a02b2015-07-14 16:07:00 -07001426 // Returns true if something is written.
1427 bool DoesAnyWrite() const {
1428 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001429 }
1430
Aart Bik854a02b2015-07-14 16:07:00 -07001431 // Returns true if something is read.
1432 bool DoesAnyRead() const {
1433 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001434 }
1435
Aart Bik854a02b2015-07-14 16:07:00 -07001436 // Returns true if potentially everything is written and read
1437 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001438 bool DoesAllReadWrite() const {
1439 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1440 }
1441
Aart Bik854a02b2015-07-14 16:07:00 -07001442 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001443 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001444 }
1445
Roland Levillain0d5a2812015-11-13 10:07:31 +00001446 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001447 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001448 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1449 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001450 }
1451
1452 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001453 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001454 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001455 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001456 for (int s = kLastBit; s >= 0; s--) {
1457 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1458 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1459 // This is a bit for the GC side effect.
1460 if (current_bit_is_set) {
1461 flags += "GC";
1462 }
Aart Bik854a02b2015-07-14 16:07:00 -07001463 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001464 } else {
1465 // This is a bit for the array/field analysis.
1466 // The underscore character stands for the 'can trigger GC' bit.
1467 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1468 if (current_bit_is_set) {
1469 flags += kDebug[s];
1470 }
1471 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1472 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1473 flags += "|";
1474 }
1475 }
Aart Bik854a02b2015-07-14 16:07:00 -07001476 }
1477 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001478 }
1479
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001480 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001481
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001482 private:
1483 static constexpr int kFieldArrayAnalysisBits = 9;
1484
1485 static constexpr int kFieldWriteOffset = 0;
1486 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1487 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1488 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1489
1490 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1491
1492 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1493 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1494 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1495 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1496
1497 static constexpr int kLastBit = kDependsOnGCBit;
1498 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1499
1500 // Aliases.
1501
1502 static_assert(kChangeBits == kDependOnBits,
1503 "the 'change' bits should match the 'depend on' bits.");
1504
1505 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1506 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1507 static constexpr uint64_t kAllWrites =
1508 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1509 static constexpr uint64_t kAllReads =
1510 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001511
Aart Bik854a02b2015-07-14 16:07:00 -07001512 // Work around the fact that HIR aliases I/F and J/D.
1513 // TODO: remove this interceptor once HIR types are clean
1514 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1515 switch (type) {
1516 case Primitive::kPrimInt:
1517 case Primitive::kPrimFloat:
1518 return TypeFlag(Primitive::kPrimInt, offset) |
1519 TypeFlag(Primitive::kPrimFloat, offset);
1520 case Primitive::kPrimLong:
1521 case Primitive::kPrimDouble:
1522 return TypeFlag(Primitive::kPrimLong, offset) |
1523 TypeFlag(Primitive::kPrimDouble, offset);
1524 default:
1525 return TypeFlag(type, offset);
1526 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001527 }
1528
Aart Bik854a02b2015-07-14 16:07:00 -07001529 // Translates type to bit flag.
1530 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1531 CHECK_NE(type, Primitive::kPrimVoid);
1532 const uint64_t one = 1;
1533 const int shift = type; // 0-based consecutive enum
1534 DCHECK_LE(kFieldWriteOffset, shift);
1535 DCHECK_LT(shift, kArrayWriteOffset);
1536 return one << (type + offset);
1537 }
1538
1539 // Private constructor on direct flags value.
1540 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1541
1542 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001543};
1544
David Brazdiled596192015-01-23 10:39:45 +00001545// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001546class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001547 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001548 HEnvironment(ArenaAllocator* arena,
1549 size_t number_of_vregs,
1550 const DexFile& dex_file,
1551 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001552 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001553 InvokeType invoke_type,
1554 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001555 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1556 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001557 parent_(nullptr),
1558 dex_file_(dex_file),
1559 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001560 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001561 invoke_type_(invoke_type),
1562 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001563 }
1564
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001565 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001566 : HEnvironment(arena,
1567 to_copy.Size(),
1568 to_copy.GetDexFile(),
1569 to_copy.GetMethodIdx(),
1570 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001571 to_copy.GetInvokeType(),
1572 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001573
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001574 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001575 if (parent_ != nullptr) {
1576 parent_->SetAndCopyParentChain(allocator, parent);
1577 } else {
1578 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1579 parent_->CopyFrom(parent);
1580 if (parent->GetParent() != nullptr) {
1581 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1582 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001583 }
David Brazdiled596192015-01-23 10:39:45 +00001584 }
1585
Vladimir Marko71bf8092015-09-15 15:33:14 +01001586 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001587 void CopyFrom(HEnvironment* environment);
1588
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001589 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1590 // input to the loop phi instead. This is for inserting instructions that
1591 // require an environment (like HDeoptimization) in the loop pre-header.
1592 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001593
1594 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001595 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001596 }
1597
1598 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001599 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001600 }
1601
David Brazdil1abb4192015-02-17 18:33:36 +00001602 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001603
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001604 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001605
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001606 HEnvironment* GetParent() const { return parent_; }
1607
1608 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001609 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001610 }
1611
1612 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001613 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001614 }
1615
1616 uint32_t GetDexPc() const {
1617 return dex_pc_;
1618 }
1619
1620 uint32_t GetMethodIdx() const {
1621 return method_idx_;
1622 }
1623
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001624 InvokeType GetInvokeType() const {
1625 return invoke_type_;
1626 }
1627
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001628 const DexFile& GetDexFile() const {
1629 return dex_file_;
1630 }
1631
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001632 HInstruction* GetHolder() const {
1633 return holder_;
1634 }
1635
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001636
1637 bool IsFromInlinedInvoke() const {
1638 return GetParent() != nullptr;
1639 }
1640
David Brazdiled596192015-01-23 10:39:45 +00001641 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001642 // Record instructions' use entries of this environment for constant-time removal.
1643 // It should only be called by HInstruction when a new environment use is added.
1644 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1645 DCHECK(env_use->GetUser() == this);
1646 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001647 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001648 }
David Brazdiled596192015-01-23 10:39:45 +00001649
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001650 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1651 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001652 HEnvironment* parent_;
1653 const DexFile& dex_file_;
1654 const uint32_t method_idx_;
1655 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001656 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001657
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001658 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001659 HInstruction* const holder_;
1660
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001661 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001662
1663 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1664};
1665
Calin Juravleacf735c2015-02-12 15:25:22 +00001666class ReferenceTypeInfo : ValueObject {
1667 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001668 typedef Handle<mirror::Class> TypeHandle;
1669
Calin Juravle2e768302015-07-28 14:41:11 +00001670 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1671 // The constructor will check that the type_handle is valid.
1672 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001673 }
1674
Calin Juravle2e768302015-07-28 14:41:11 +00001675 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1676
1677 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1678 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001679 }
1680
Calin Juravle2e768302015-07-28 14:41:11 +00001681 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1682 return IsValidHandle(type_handle_);
1683 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001684
Calin Juravleacf735c2015-02-12 15:25:22 +00001685 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001686
1687 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1688 DCHECK(IsValid());
1689 return GetTypeHandle()->IsObjectClass();
1690 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001691
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001692 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001693 DCHECK(IsValid());
1694 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001695 }
1696
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001697 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1698 DCHECK(IsValid());
1699 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1700 }
1701
Mathieu Chartier90443472015-07-16 20:32:27 -07001702 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001703 DCHECK(IsValid());
1704 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001705 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001706
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001707 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001708 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001709 return GetTypeHandle()->IsArrayClass();
1710 }
1711
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001712 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1713 DCHECK(IsValid());
1714 return GetTypeHandle()->IsPrimitiveArray();
1715 }
1716
1717 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1718 DCHECK(IsValid());
1719 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1720 }
1721
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001722 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001723 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001724 if (!IsExact()) return false;
1725 if (!IsArrayClass()) return false;
1726 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1727 }
1728
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001729 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1730 DCHECK(IsValid());
1731 if (!IsExact()) return false;
1732 if (!IsArrayClass()) return false;
1733 if (!rti.IsArrayClass()) return false;
1734 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1735 rti.GetTypeHandle()->GetComponentType());
1736 }
1737
Calin Juravleacf735c2015-02-12 15:25:22 +00001738 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1739
Mathieu Chartier90443472015-07-16 20:32:27 -07001740 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001741 DCHECK(IsValid());
1742 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001743 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1744 }
1745
Calin Juravlecdfed3d2015-10-26 14:05:01 +00001746 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1747 DCHECK(IsValid());
1748 DCHECK(rti.IsValid());
1749 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
1750 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1751 }
1752
Calin Juravleacf735c2015-02-12 15:25:22 +00001753 // Returns true if the type information provide the same amount of details.
1754 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001755 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001756 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001757 if (!IsValid() && !rti.IsValid()) {
1758 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001759 return true;
1760 }
Calin Juravle2e768302015-07-28 14:41:11 +00001761 if (!IsValid() || !rti.IsValid()) {
1762 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001763 return false;
1764 }
Calin Juravle2e768302015-07-28 14:41:11 +00001765 return IsExact() == rti.IsExact()
1766 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001767 }
1768
1769 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001770 ReferenceTypeInfo();
1771 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001772
Calin Juravleacf735c2015-02-12 15:25:22 +00001773 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001774 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001775 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001776 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001777 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001778};
1779
1780std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1781
Vladimir Markof9f64412015-09-02 14:05:49 +01001782class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001783 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001784 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001785 : previous_(nullptr),
1786 next_(nullptr),
1787 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001788 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001789 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001790 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001791 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001792 locations_(nullptr),
1793 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001794 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001795 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001796 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001797
Dave Allison20dfc792014-06-16 20:44:29 -07001798 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001799
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001800#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001801 enum InstructionKind {
1802 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1803 };
1804#undef DECLARE_KIND
1805
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001806 HInstruction* GetNext() const { return next_; }
1807 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001808
Calin Juravle77520bc2015-01-12 18:45:46 +00001809 HInstruction* GetNextDisregardingMoves() const;
1810 HInstruction* GetPreviousDisregardingMoves() const;
1811
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001812 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001813 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001814 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001815 bool IsInBlock() const { return block_ != nullptr; }
1816 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001817 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001818
Roland Levillain6b879dd2014-09-22 17:13:44 +01001819 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001820 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001821
1822 virtual void Accept(HGraphVisitor* visitor) = 0;
1823 virtual const char* DebugName() const = 0;
1824
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001825 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001826 void SetRawInputAt(size_t index, HInstruction* input) {
1827 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1828 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001829
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001830 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001831
1832 uint32_t GetDexPc() const { return dex_pc_; }
1833
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001834 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001835
Roland Levillaine161a2a2014-10-03 12:45:18 +01001836 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001837 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001838
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001839 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001840 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001841
Calin Juravle10e244f2015-01-26 18:54:32 +00001842 // Does not apply for all instructions, but having this at top level greatly
1843 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001844 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001845 virtual bool CanBeNull() const {
1846 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1847 return true;
1848 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001849
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001850 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001851 return false;
1852 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001853
Calin Juravle2e768302015-07-28 14:41:11 +00001854 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001855
Calin Juravle61d544b2015-02-23 16:46:57 +00001856 ReferenceTypeInfo GetReferenceTypeInfo() const {
1857 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1858 return reference_type_info_;
1859 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001860
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001861 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001862 DCHECK(user != nullptr);
1863 HUseListNode<HInstruction*>* use =
1864 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1865 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001866 }
1867
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001868 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001869 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001870 HUseListNode<HEnvironment*>* env_use =
1871 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1872 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001873 }
1874
David Brazdil1abb4192015-02-17 18:33:36 +00001875 void RemoveAsUserOfInput(size_t input) {
1876 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1877 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1878 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001879
David Brazdil1abb4192015-02-17 18:33:36 +00001880 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1881 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001882
David Brazdiled596192015-01-23 10:39:45 +00001883 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1884 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001885 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001886 bool HasOnlyOneNonEnvironmentUse() const {
1887 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1888 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001889
Roland Levillain6c82d402014-10-13 16:10:27 +01001890 // Does this instruction strictly dominate `other_instruction`?
1891 // Returns false if this instruction and `other_instruction` are the same.
1892 // Aborts if this instruction and `other_instruction` are both phis.
1893 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001894
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001895 int GetId() const { return id_; }
1896 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001897
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001898 int GetSsaIndex() const { return ssa_index_; }
1899 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1900 bool HasSsaIndex() const { return ssa_index_ != -1; }
1901
1902 bool HasEnvironment() const { return environment_ != nullptr; }
1903 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001904 // Set the `environment_` field. Raw because this method does not
1905 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001906 void SetRawEnvironment(HEnvironment* environment) {
1907 DCHECK(environment_ == nullptr);
1908 DCHECK_EQ(environment->GetHolder(), this);
1909 environment_ = environment;
1910 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001911
1912 // Set the environment of this instruction, copying it from `environment`. While
1913 // copying, the uses lists are being updated.
1914 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001915 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001916 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001917 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001918 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001919 if (environment->GetParent() != nullptr) {
1920 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1921 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001922 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001923
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001924 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1925 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001926 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001927 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001928 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001929 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001930 if (environment->GetParent() != nullptr) {
1931 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1932 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001933 }
1934
Nicolas Geoffray39468442014-09-02 15:17:15 +01001935 // Returns the number of entries in the environment. Typically, that is the
1936 // number of dex registers in a method. It could be more in case of inlining.
1937 size_t EnvironmentSize() const;
1938
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001939 LocationSummary* GetLocations() const { return locations_; }
1940 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001941
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001942 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001943 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001944
Alexandre Rames188d4312015-04-09 18:30:21 +01001945 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1946 // uses of this instruction by `other` are *not* updated.
1947 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1948 ReplaceWith(other);
1949 other->ReplaceInput(this, use_index);
1950 }
1951
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001952 // Move `this` instruction before `cursor`.
1953 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001954
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001955#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001956 bool Is##type() const { return (As##type() != nullptr); } \
1957 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001958 virtual H##type* As##type() { return nullptr; }
1959
1960 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1961#undef INSTRUCTION_TYPE_CHECK
1962
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001963 // Returns whether the instruction can be moved within the graph.
1964 virtual bool CanBeMoved() const { return false; }
1965
1966 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001967 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001968 return false;
1969 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001970
1971 // Returns whether any data encoded in the two instructions is equal.
1972 // This method does not look at the inputs. Both instructions must be
1973 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001974 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001975 return false;
1976 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001977
1978 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001979 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001980 // 2) Their inputs are identical.
1981 bool Equals(HInstruction* other) const;
1982
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001983 virtual InstructionKind GetKind() const = 0;
1984
1985 virtual size_t ComputeHashCode() const {
1986 size_t result = GetKind();
1987 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1988 result = (result * 31) + InputAt(i)->GetId();
1989 }
1990 return result;
1991 }
1992
1993 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001994 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001995
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001996 size_t GetLifetimePosition() const { return lifetime_position_; }
1997 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1998 LiveInterval* GetLiveInterval() const { return live_interval_; }
1999 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2000 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2001
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002002 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2003
2004 // Returns whether the code generation of the instruction will require to have access
2005 // to the current method. Such instructions are:
2006 // (1): Instructions that require an environment, as calling the runtime requires
2007 // to walk the stack and have the current method stored at a specific stack address.
2008 // (2): Object literals like classes and strings, that are loaded from the dex cache
2009 // fields of the current method.
2010 bool NeedsCurrentMethod() const {
2011 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2012 }
2013
Vladimir Markodc151b22015-10-15 18:02:30 +01002014 // Returns whether the code generation of the instruction will require to have access
2015 // to the dex cache of the current method's declaring class via the current method.
2016 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002017
Mark Mendellc4701932015-04-10 13:18:51 -04002018 // Does this instruction have any use in an environment before
2019 // control flow hits 'other'?
2020 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2021
2022 // Remove all references to environment uses of this instruction.
2023 // The caller must ensure that this is safe to do.
2024 void RemoveEnvironmentUsers();
2025
David Brazdil1abb4192015-02-17 18:33:36 +00002026 protected:
2027 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2028 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2029
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002030 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002031 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2032
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002033 HInstruction* previous_;
2034 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002035 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002036 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002037
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002038 // An instruction gets an id when it is added to the graph.
2039 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002040 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002041 int id_;
2042
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002043 // When doing liveness analysis, instructions that have uses get an SSA index.
2044 int ssa_index_;
2045
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002046 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002047 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002048
2049 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002050 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002051
Nicolas Geoffray39468442014-09-02 15:17:15 +01002052 // The environment associated with this instruction. Not null if the instruction
2053 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002054 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002055
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002056 // Set by the code generator.
2057 LocationSummary* locations_;
2058
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002059 // Set by the liveness analysis.
2060 LiveInterval* live_interval_;
2061
2062 // Set by the liveness analysis, this is the position in a linear
2063 // order of blocks where this instruction's live interval start.
2064 size_t lifetime_position_;
2065
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002066 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002067
Calin Juravleacf735c2015-02-12 15:25:22 +00002068 // TODO: for primitive types this should be marked as invalid.
2069 ReferenceTypeInfo reference_type_info_;
2070
David Brazdil1abb4192015-02-17 18:33:36 +00002071 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002072 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002073 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002074 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002075 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002076
2077 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2078};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002079std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002080
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002081class HInputIterator : public ValueObject {
2082 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002083 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002084
2085 bool Done() const { return index_ == instruction_->InputCount(); }
2086 HInstruction* Current() const { return instruction_->InputAt(index_); }
2087 void Advance() { index_++; }
2088
2089 private:
2090 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002091 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002092
2093 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2094};
2095
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002096class HInstructionIterator : public ValueObject {
2097 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002098 explicit HInstructionIterator(const HInstructionList& instructions)
2099 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002100 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002101 }
2102
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002103 bool Done() const { return instruction_ == nullptr; }
2104 HInstruction* Current() const { return instruction_; }
2105 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002106 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002107 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002108 }
2109
2110 private:
2111 HInstruction* instruction_;
2112 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002113
2114 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002115};
2116
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002117class HBackwardInstructionIterator : public ValueObject {
2118 public:
2119 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2120 : instruction_(instructions.last_instruction_) {
2121 next_ = Done() ? nullptr : instruction_->GetPrevious();
2122 }
2123
2124 bool Done() const { return instruction_ == nullptr; }
2125 HInstruction* Current() const { return instruction_; }
2126 void Advance() {
2127 instruction_ = next_;
2128 next_ = Done() ? nullptr : instruction_->GetPrevious();
2129 }
2130
2131 private:
2132 HInstruction* instruction_;
2133 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002134
2135 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002136};
2137
Vladimir Markof9f64412015-09-02 14:05:49 +01002138template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002139class HTemplateInstruction: public HInstruction {
2140 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002141 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002142 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002143 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002144
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002145 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002146
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002147 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002148 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2149 DCHECK_LT(i, N);
2150 return inputs_[i];
2151 }
David Brazdil1abb4192015-02-17 18:33:36 +00002152
2153 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002154 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002155 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002156 }
2157
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002158 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002159 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002160
2161 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002162};
2163
Vladimir Markof9f64412015-09-02 14:05:49 +01002164// HTemplateInstruction specialization for N=0.
2165template<>
2166class HTemplateInstruction<0>: public HInstruction {
2167 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002168 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002169 : HInstruction(side_effects, dex_pc) {}
2170
Vladimir Markof9f64412015-09-02 14:05:49 +01002171 virtual ~HTemplateInstruction() {}
2172
2173 size_t InputCount() const OVERRIDE { return 0; }
2174
2175 protected:
2176 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2177 LOG(FATAL) << "Unreachable";
2178 UNREACHABLE();
2179 }
2180
2181 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2182 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2183 LOG(FATAL) << "Unreachable";
2184 UNREACHABLE();
2185 }
2186
2187 private:
2188 friend class SsaBuilder;
2189};
2190
Dave Allison20dfc792014-06-16 20:44:29 -07002191template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002192class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002193 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002194 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002195 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002196 virtual ~HExpression() {}
2197
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002198 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002199
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002200 protected:
2201 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002202};
2203
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002204// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2205// instruction that branches to the exit block.
2206class HReturnVoid : public HTemplateInstruction<0> {
2207 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002208 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2209 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002210
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002211 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002212
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002213 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002214
2215 private:
2216 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2217};
2218
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002219// Represents dex's RETURN opcodes. A HReturn is a control flow
2220// instruction that branches to the exit block.
2221class HReturn : public HTemplateInstruction<1> {
2222 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002223 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2224 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002225 SetRawInputAt(0, value);
2226 }
2227
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002228 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002229
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002230 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002231
2232 private:
2233 DISALLOW_COPY_AND_ASSIGN(HReturn);
2234};
2235
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002236// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002237// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002238// exit block.
2239class HExit : public HTemplateInstruction<0> {
2240 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002241 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002242
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002243 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002244
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002245 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002246
2247 private:
2248 DISALLOW_COPY_AND_ASSIGN(HExit);
2249};
2250
2251// Jumps from one block to another.
2252class HGoto : public HTemplateInstruction<0> {
2253 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002254 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002255
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002256 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002257
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002258 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002259 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002260 }
2261
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002262 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002263
2264 private:
2265 DISALLOW_COPY_AND_ASSIGN(HGoto);
2266};
2267
Roland Levillain9867bc72015-08-05 10:21:34 +01002268class HConstant : public HExpression<0> {
2269 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002270 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2271 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002272
2273 bool CanBeMoved() const OVERRIDE { return true; }
2274
2275 virtual bool IsMinusOne() const { return false; }
2276 virtual bool IsZero() const { return false; }
2277 virtual bool IsOne() const { return false; }
2278
David Brazdil77a48ae2015-09-15 12:34:04 +00002279 virtual uint64_t GetValueAsUint64() const = 0;
2280
Roland Levillain9867bc72015-08-05 10:21:34 +01002281 DECLARE_INSTRUCTION(Constant);
2282
2283 private:
2284 DISALLOW_COPY_AND_ASSIGN(HConstant);
2285};
2286
2287class HNullConstant : public HConstant {
2288 public:
2289 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2290 return true;
2291 }
2292
David Brazdil77a48ae2015-09-15 12:34:04 +00002293 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2294
Roland Levillain9867bc72015-08-05 10:21:34 +01002295 size_t ComputeHashCode() const OVERRIDE { return 0; }
2296
2297 DECLARE_INSTRUCTION(NullConstant);
2298
2299 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002300 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002301
2302 friend class HGraph;
2303 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2304};
2305
2306// Constants of the type int. Those can be from Dex instructions, or
2307// synthesized (for example with the if-eqz instruction).
2308class HIntConstant : public HConstant {
2309 public:
2310 int32_t GetValue() const { return value_; }
2311
David Brazdil9f389d42015-10-01 14:32:56 +01002312 uint64_t GetValueAsUint64() const OVERRIDE {
2313 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2314 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002315
Roland Levillain9867bc72015-08-05 10:21:34 +01002316 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2317 DCHECK(other->IsIntConstant());
2318 return other->AsIntConstant()->value_ == value_;
2319 }
2320
2321 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2322
2323 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2324 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2325 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2326
2327 DECLARE_INSTRUCTION(IntConstant);
2328
2329 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002330 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2331 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2332 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2333 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002334
2335 const int32_t value_;
2336
2337 friend class HGraph;
2338 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2339 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2340 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2341};
2342
2343class HLongConstant : public HConstant {
2344 public:
2345 int64_t GetValue() const { return value_; }
2346
David Brazdil77a48ae2015-09-15 12:34:04 +00002347 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2348
Roland Levillain9867bc72015-08-05 10:21:34 +01002349 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2350 DCHECK(other->IsLongConstant());
2351 return other->AsLongConstant()->value_ == value_;
2352 }
2353
2354 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2355
2356 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2357 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2358 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2359
2360 DECLARE_INSTRUCTION(LongConstant);
2361
2362 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002363 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2364 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002365
2366 const int64_t value_;
2367
2368 friend class HGraph;
2369 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2370};
Dave Allison20dfc792014-06-16 20:44:29 -07002371
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002372// Conditional branch. A block ending with an HIf instruction must have
2373// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002374class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002375 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002376 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2377 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002378 SetRawInputAt(0, input);
2379 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002380
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002381 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002382
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002383 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002384 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002385 }
2386
2387 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002388 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002389 }
2390
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002391 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002392
2393 private:
2394 DISALLOW_COPY_AND_ASSIGN(HIf);
2395};
2396
David Brazdilfc6a86a2015-06-26 10:33:45 +00002397
2398// Abstract instruction which marks the beginning and/or end of a try block and
2399// links it to the respective exception handlers. Behaves the same as a Goto in
2400// non-exceptional control flow.
2401// Normal-flow successor is stored at index zero, exception handlers under
2402// higher indices in no particular order.
2403class HTryBoundary : public HTemplateInstruction<0> {
2404 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002405 enum BoundaryKind {
2406 kEntry,
2407 kExit,
2408 };
2409
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002410 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2411 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002412
2413 bool IsControlFlow() const OVERRIDE { return true; }
2414
2415 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002416 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002417
David Brazdild26a4112015-11-10 11:07:31 +00002418 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2419 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2420 }
2421
David Brazdilfc6a86a2015-06-26 10:33:45 +00002422 // Returns whether `handler` is among its exception handlers (non-zero index
2423 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002424 bool HasExceptionHandler(const HBasicBlock& handler) const {
2425 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002426 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002427 }
2428
2429 // If not present already, adds `handler` to its block's list of exception
2430 // handlers.
2431 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002432 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002433 GetBlock()->AddSuccessor(handler);
2434 }
2435 }
2436
David Brazdil56e1acc2015-06-30 15:41:36 +01002437 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002438
David Brazdilffee3d32015-07-06 11:48:53 +01002439 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2440
David Brazdilfc6a86a2015-06-26 10:33:45 +00002441 DECLARE_INSTRUCTION(TryBoundary);
2442
2443 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002444 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002445
2446 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2447};
2448
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002449// Deoptimize to interpreter, upon checking a condition.
2450class HDeoptimize : public HTemplateInstruction<1> {
2451 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002452 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2453 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002454 SetRawInputAt(0, cond);
2455 }
2456
2457 bool NeedsEnvironment() const OVERRIDE { return true; }
2458 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002459
2460 DECLARE_INSTRUCTION(Deoptimize);
2461
2462 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002463 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2464};
2465
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002466// Represents the ArtMethod that was passed as a first argument to
2467// the method. It is used by instructions that depend on it, like
2468// instructions that work with the dex cache.
2469class HCurrentMethod : public HExpression<0> {
2470 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002471 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2472 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002473
2474 DECLARE_INSTRUCTION(CurrentMethod);
2475
2476 private:
2477 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2478};
2479
Mark Mendellfe57faa2015-09-18 09:26:15 -04002480// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2481// have one successor for each entry in the switch table, and the final successor
2482// will be the block containing the next Dex opcode.
2483class HPackedSwitch : public HTemplateInstruction<1> {
2484 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002485 HPackedSwitch(int32_t start_value,
2486 uint32_t num_entries,
2487 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002488 uint32_t dex_pc = kNoDexPc)
2489 : HTemplateInstruction(SideEffects::None(), dex_pc),
2490 start_value_(start_value),
2491 num_entries_(num_entries) {
2492 SetRawInputAt(0, input);
2493 }
2494
2495 bool IsControlFlow() const OVERRIDE { return true; }
2496
2497 int32_t GetStartValue() const { return start_value_; }
2498
Vladimir Marko211c2112015-09-24 16:52:33 +01002499 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002500
2501 HBasicBlock* GetDefaultBlock() const {
2502 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002503 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002504 }
2505 DECLARE_INSTRUCTION(PackedSwitch);
2506
2507 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002508 const int32_t start_value_;
2509 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002510
2511 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2512};
2513
Roland Levillain88cb1752014-10-20 16:36:47 +01002514class HUnaryOperation : public HExpression<1> {
2515 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002516 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2517 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002518 SetRawInputAt(0, input);
2519 }
2520
2521 HInstruction* GetInput() const { return InputAt(0); }
2522 Primitive::Type GetResultType() const { return GetType(); }
2523
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002524 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002525 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002526 return true;
2527 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002528
Roland Levillain9240d6a2014-10-20 16:47:04 +01002529 // Try to statically evaluate `operation` and return a HConstant
2530 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002531 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002532 HConstant* TryStaticEvaluation() const;
2533
2534 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002535 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2536 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002537
Roland Levillain88cb1752014-10-20 16:36:47 +01002538 DECLARE_INSTRUCTION(UnaryOperation);
2539
2540 private:
2541 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2542};
2543
Dave Allison20dfc792014-06-16 20:44:29 -07002544class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002545 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002546 HBinaryOperation(Primitive::Type result_type,
2547 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002548 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002549 SideEffects side_effects = SideEffects::None(),
2550 uint32_t dex_pc = kNoDexPc)
2551 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002552 SetRawInputAt(0, left);
2553 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002554 }
2555
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002556 HInstruction* GetLeft() const { return InputAt(0); }
2557 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002558 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002559
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002560 virtual bool IsCommutative() const { return false; }
2561
2562 // Put constant on the right.
2563 // Returns whether order is changed.
2564 bool OrderInputsWithConstantOnTheRight() {
2565 HInstruction* left = InputAt(0);
2566 HInstruction* right = InputAt(1);
2567 if (left->IsConstant() && !right->IsConstant()) {
2568 ReplaceInput(right, 0);
2569 ReplaceInput(left, 1);
2570 return true;
2571 }
2572 return false;
2573 }
2574
2575 // Order inputs by instruction id, but favor constant on the right side.
2576 // This helps GVN for commutative ops.
2577 void OrderInputs() {
2578 DCHECK(IsCommutative());
2579 HInstruction* left = InputAt(0);
2580 HInstruction* right = InputAt(1);
2581 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2582 return;
2583 }
2584 if (OrderInputsWithConstantOnTheRight()) {
2585 return;
2586 }
2587 // Order according to instruction id.
2588 if (left->GetId() > right->GetId()) {
2589 ReplaceInput(right, 0);
2590 ReplaceInput(left, 1);
2591 }
2592 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002593
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002594 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002595 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002596 return true;
2597 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002598
Roland Levillain9240d6a2014-10-20 16:47:04 +01002599 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002600 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002601 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002602 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002603
2604 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002605 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2606 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2607 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2608 HLongConstant* y ATTRIBUTE_UNUSED) const {
2609 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2610 return nullptr;
2611 }
2612 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2613 HIntConstant* y ATTRIBUTE_UNUSED) const {
2614 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2615 return nullptr;
2616 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002617 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2618 HNullConstant* y ATTRIBUTE_UNUSED) const {
2619 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2620 return nullptr;
2621 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002622
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002623 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002624 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002625 HConstant* GetConstantRight() const;
2626
2627 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002628 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002629 HInstruction* GetLeastConstantLeft() const;
2630
Roland Levillainccc07a92014-09-16 14:48:16 +01002631 DECLARE_INSTRUCTION(BinaryOperation);
2632
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002633 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002634 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2635};
2636
Mark Mendellc4701932015-04-10 13:18:51 -04002637// The comparison bias applies for floating point operations and indicates how NaN
2638// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002639enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002640 kNoBias, // bias is not applicable (i.e. for long operation)
2641 kGtBias, // return 1 for NaN comparisons
2642 kLtBias, // return -1 for NaN comparisons
2643};
2644
Dave Allison20dfc792014-06-16 20:44:29 -07002645class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002646 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002647 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2648 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002649 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002650 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002651
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002652 bool NeedsMaterialization() const { return needs_materialization_; }
2653 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002654
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002655 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002656 // `instruction`, and disregard moves in between.
2657 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002658
Dave Allison20dfc792014-06-16 20:44:29 -07002659 DECLARE_INSTRUCTION(Condition);
2660
2661 virtual IfCondition GetCondition() const = 0;
2662
Mark Mendellc4701932015-04-10 13:18:51 -04002663 virtual IfCondition GetOppositeCondition() const = 0;
2664
Roland Levillain4fa13f62015-07-06 18:11:54 +01002665 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002666
2667 void SetBias(ComparisonBias bias) { bias_ = bias; }
2668
2669 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2670 return bias_ == other->AsCondition()->bias_;
2671 }
2672
Roland Levillain4fa13f62015-07-06 18:11:54 +01002673 bool IsFPConditionTrueIfNaN() const {
2674 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2675 IfCondition if_cond = GetCondition();
2676 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2677 }
2678
2679 bool IsFPConditionFalseIfNaN() const {
2680 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2681 IfCondition if_cond = GetCondition();
2682 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2683 }
2684
Dave Allison20dfc792014-06-16 20:44:29 -07002685 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002686 // For register allocation purposes, returns whether this instruction needs to be
2687 // materialized (that is, not just be in the processor flags).
2688 bool needs_materialization_;
2689
Mark Mendellc4701932015-04-10 13:18:51 -04002690 // Needed if we merge a HCompare into a HCondition.
2691 ComparisonBias bias_;
2692
Dave Allison20dfc792014-06-16 20:44:29 -07002693 DISALLOW_COPY_AND_ASSIGN(HCondition);
2694};
2695
2696// Instruction to check if two inputs are equal to each other.
2697class HEqual : public HCondition {
2698 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002699 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2700 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002701
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002702 bool IsCommutative() const OVERRIDE { return true; }
2703
Roland Levillain9867bc72015-08-05 10:21:34 +01002704 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002705 return GetBlock()->GetGraph()->GetIntConstant(
2706 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002707 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002708 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002709 return GetBlock()->GetGraph()->GetIntConstant(
2710 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002711 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002712 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2713 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002714 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002715 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002716
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002717 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002718
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002719 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002720 return kCondEQ;
2721 }
2722
Mark Mendellc4701932015-04-10 13:18:51 -04002723 IfCondition GetOppositeCondition() const OVERRIDE {
2724 return kCondNE;
2725 }
2726
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002727 private:
Aart Bike9f37602015-10-09 11:15:55 -07002728 template <typename T> bool Compute(T x, T y) const { return x == y; }
2729
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002730 DISALLOW_COPY_AND_ASSIGN(HEqual);
2731};
2732
Dave Allison20dfc792014-06-16 20:44:29 -07002733class HNotEqual : public HCondition {
2734 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002735 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2736 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002737
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002738 bool IsCommutative() const OVERRIDE { return true; }
2739
Roland Levillain9867bc72015-08-05 10:21:34 +01002740 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002741 return GetBlock()->GetGraph()->GetIntConstant(
2742 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002743 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002744 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002745 return GetBlock()->GetGraph()->GetIntConstant(
2746 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002747 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002748 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2749 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002750 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002751 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002752
Dave Allison20dfc792014-06-16 20:44:29 -07002753 DECLARE_INSTRUCTION(NotEqual);
2754
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002755 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002756 return kCondNE;
2757 }
2758
Mark Mendellc4701932015-04-10 13:18:51 -04002759 IfCondition GetOppositeCondition() const OVERRIDE {
2760 return kCondEQ;
2761 }
2762
Dave Allison20dfc792014-06-16 20:44:29 -07002763 private:
Aart Bike9f37602015-10-09 11:15:55 -07002764 template <typename T> bool Compute(T x, T y) const { return x != y; }
2765
Dave Allison20dfc792014-06-16 20:44:29 -07002766 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2767};
2768
2769class HLessThan : public HCondition {
2770 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002771 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2772 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002773
Roland Levillain9867bc72015-08-05 10:21:34 +01002774 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002775 return GetBlock()->GetGraph()->GetIntConstant(
2776 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002777 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002778 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002779 return GetBlock()->GetGraph()->GetIntConstant(
2780 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002781 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002782
Dave Allison20dfc792014-06-16 20:44:29 -07002783 DECLARE_INSTRUCTION(LessThan);
2784
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002785 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002786 return kCondLT;
2787 }
2788
Mark Mendellc4701932015-04-10 13:18:51 -04002789 IfCondition GetOppositeCondition() const OVERRIDE {
2790 return kCondGE;
2791 }
2792
Dave Allison20dfc792014-06-16 20:44:29 -07002793 private:
Aart Bike9f37602015-10-09 11:15:55 -07002794 template <typename T> bool Compute(T x, T y) const { return x < y; }
2795
Dave Allison20dfc792014-06-16 20:44:29 -07002796 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2797};
2798
2799class HLessThanOrEqual : public HCondition {
2800 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002801 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2802 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002803
Roland Levillain9867bc72015-08-05 10:21:34 +01002804 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002805 return GetBlock()->GetGraph()->GetIntConstant(
2806 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002807 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002808 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002809 return GetBlock()->GetGraph()->GetIntConstant(
2810 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002811 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002812
Dave Allison20dfc792014-06-16 20:44:29 -07002813 DECLARE_INSTRUCTION(LessThanOrEqual);
2814
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002815 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002816 return kCondLE;
2817 }
2818
Mark Mendellc4701932015-04-10 13:18:51 -04002819 IfCondition GetOppositeCondition() const OVERRIDE {
2820 return kCondGT;
2821 }
2822
Dave Allison20dfc792014-06-16 20:44:29 -07002823 private:
Aart Bike9f37602015-10-09 11:15:55 -07002824 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2825
Dave Allison20dfc792014-06-16 20:44:29 -07002826 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2827};
2828
2829class HGreaterThan : public HCondition {
2830 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002831 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2832 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002833
Roland Levillain9867bc72015-08-05 10:21:34 +01002834 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002835 return GetBlock()->GetGraph()->GetIntConstant(
2836 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002837 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002838 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002839 return GetBlock()->GetGraph()->GetIntConstant(
2840 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002841 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002842
Dave Allison20dfc792014-06-16 20:44:29 -07002843 DECLARE_INSTRUCTION(GreaterThan);
2844
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002845 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002846 return kCondGT;
2847 }
2848
Mark Mendellc4701932015-04-10 13:18:51 -04002849 IfCondition GetOppositeCondition() const OVERRIDE {
2850 return kCondLE;
2851 }
2852
Dave Allison20dfc792014-06-16 20:44:29 -07002853 private:
Aart Bike9f37602015-10-09 11:15:55 -07002854 template <typename T> bool Compute(T x, T y) const { return x > y; }
2855
Dave Allison20dfc792014-06-16 20:44:29 -07002856 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2857};
2858
2859class HGreaterThanOrEqual : public HCondition {
2860 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002861 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2862 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002863
Roland Levillain9867bc72015-08-05 10:21:34 +01002864 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002865 return GetBlock()->GetGraph()->GetIntConstant(
2866 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002867 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002868 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002869 return GetBlock()->GetGraph()->GetIntConstant(
2870 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002871 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002872
Dave Allison20dfc792014-06-16 20:44:29 -07002873 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2874
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002875 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002876 return kCondGE;
2877 }
2878
Mark Mendellc4701932015-04-10 13:18:51 -04002879 IfCondition GetOppositeCondition() const OVERRIDE {
2880 return kCondLT;
2881 }
2882
Dave Allison20dfc792014-06-16 20:44:29 -07002883 private:
Aart Bike9f37602015-10-09 11:15:55 -07002884 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2885
Dave Allison20dfc792014-06-16 20:44:29 -07002886 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2887};
2888
Aart Bike9f37602015-10-09 11:15:55 -07002889class HBelow : public HCondition {
2890 public:
2891 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2892 : HCondition(first, second, dex_pc) {}
2893
2894 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2895 return GetBlock()->GetGraph()->GetIntConstant(
2896 Compute(static_cast<uint32_t>(x->GetValue()),
2897 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2898 }
2899 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2900 return GetBlock()->GetGraph()->GetIntConstant(
2901 Compute(static_cast<uint64_t>(x->GetValue()),
2902 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2903 }
2904
2905 DECLARE_INSTRUCTION(Below);
2906
2907 IfCondition GetCondition() const OVERRIDE {
2908 return kCondB;
2909 }
2910
2911 IfCondition GetOppositeCondition() const OVERRIDE {
2912 return kCondAE;
2913 }
2914
2915 private:
2916 template <typename T> bool Compute(T x, T y) const { return x < y; }
2917
2918 DISALLOW_COPY_AND_ASSIGN(HBelow);
2919};
2920
2921class HBelowOrEqual : public HCondition {
2922 public:
2923 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2924 : HCondition(first, second, dex_pc) {}
2925
2926 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2927 return GetBlock()->GetGraph()->GetIntConstant(
2928 Compute(static_cast<uint32_t>(x->GetValue()),
2929 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2930 }
2931 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2932 return GetBlock()->GetGraph()->GetIntConstant(
2933 Compute(static_cast<uint64_t>(x->GetValue()),
2934 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2935 }
2936
2937 DECLARE_INSTRUCTION(BelowOrEqual);
2938
2939 IfCondition GetCondition() const OVERRIDE {
2940 return kCondBE;
2941 }
2942
2943 IfCondition GetOppositeCondition() const OVERRIDE {
2944 return kCondA;
2945 }
2946
2947 private:
2948 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2949
2950 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2951};
2952
2953class HAbove : public HCondition {
2954 public:
2955 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2956 : HCondition(first, second, dex_pc) {}
2957
2958 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2959 return GetBlock()->GetGraph()->GetIntConstant(
2960 Compute(static_cast<uint32_t>(x->GetValue()),
2961 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2962 }
2963 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2964 return GetBlock()->GetGraph()->GetIntConstant(
2965 Compute(static_cast<uint64_t>(x->GetValue()),
2966 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2967 }
2968
2969 DECLARE_INSTRUCTION(Above);
2970
2971 IfCondition GetCondition() const OVERRIDE {
2972 return kCondA;
2973 }
2974
2975 IfCondition GetOppositeCondition() const OVERRIDE {
2976 return kCondBE;
2977 }
2978
2979 private:
2980 template <typename T> bool Compute(T x, T y) const { return x > y; }
2981
2982 DISALLOW_COPY_AND_ASSIGN(HAbove);
2983};
2984
2985class HAboveOrEqual : public HCondition {
2986 public:
2987 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2988 : HCondition(first, second, dex_pc) {}
2989
2990 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2991 return GetBlock()->GetGraph()->GetIntConstant(
2992 Compute(static_cast<uint32_t>(x->GetValue()),
2993 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2994 }
2995 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2996 return GetBlock()->GetGraph()->GetIntConstant(
2997 Compute(static_cast<uint64_t>(x->GetValue()),
2998 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2999 }
3000
3001 DECLARE_INSTRUCTION(AboveOrEqual);
3002
3003 IfCondition GetCondition() const OVERRIDE {
3004 return kCondAE;
3005 }
3006
3007 IfCondition GetOppositeCondition() const OVERRIDE {
3008 return kCondB;
3009 }
3010
3011 private:
3012 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3013
3014 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3015};
Dave Allison20dfc792014-06-16 20:44:29 -07003016
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003017// Instruction to check how two inputs compare to each other.
3018// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3019class HCompare : public HBinaryOperation {
3020 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003021 HCompare(Primitive::Type type,
3022 HInstruction* first,
3023 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003024 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003025 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003026 : HBinaryOperation(Primitive::kPrimInt,
3027 first,
3028 second,
3029 SideEffectsForArchRuntimeCalls(type),
3030 dex_pc),
3031 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003032 DCHECK_EQ(type, first->GetType());
3033 DCHECK_EQ(type, second->GetType());
3034 }
3035
Roland Levillain9867bc72015-08-05 10:21:34 +01003036 template <typename T>
3037 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003038
Roland Levillain9867bc72015-08-05 10:21:34 +01003039 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003040 return GetBlock()->GetGraph()->GetIntConstant(
3041 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003042 }
3043 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003044 return GetBlock()->GetGraph()->GetIntConstant(
3045 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003046 }
3047
Calin Juravleddb7df22014-11-25 20:56:51 +00003048 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3049 return bias_ == other->AsCompare()->bias_;
3050 }
3051
Mark Mendellc4701932015-04-10 13:18:51 -04003052 ComparisonBias GetBias() const { return bias_; }
3053
Roland Levillain4fa13f62015-07-06 18:11:54 +01003054 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003055
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003056
3057 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3058 // MIPS64 uses a runtime call for FP comparisons.
3059 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3060 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003061
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003062 DECLARE_INSTRUCTION(Compare);
3063
3064 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003065 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003066
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003067 DISALLOW_COPY_AND_ASSIGN(HCompare);
3068};
3069
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003070// A local in the graph. Corresponds to a Dex register.
3071class HLocal : public HTemplateInstruction<0> {
3072 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003073 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003074 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003075
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003076 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003077
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003078 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003079
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003080 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003081 // The Dex register number.
3082 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003083
3084 DISALLOW_COPY_AND_ASSIGN(HLocal);
3085};
3086
3087// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003088class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003089 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003090 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3091 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003092 SetRawInputAt(0, local);
3093 }
3094
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003095 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3096
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003097 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003098
3099 private:
3100 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3101};
3102
3103// Store a value in a given local. This instruction has two inputs: the value
3104// and the local.
3105class HStoreLocal : public HTemplateInstruction<2> {
3106 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003107 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3108 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003109 SetRawInputAt(0, local);
3110 SetRawInputAt(1, value);
3111 }
3112
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003113 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3114
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003115 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003116
3117 private:
3118 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3119};
3120
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003121class HFloatConstant : public HConstant {
3122 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003123 float GetValue() const { return value_; }
3124
David Brazdil77a48ae2015-09-15 12:34:04 +00003125 uint64_t GetValueAsUint64() const OVERRIDE {
3126 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3127 }
3128
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003129 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003130 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003131 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003132 }
3133
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003134 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003135
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003136 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003137 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003138 }
3139 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003140 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003141 }
3142 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003143 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3144 }
3145 bool IsNaN() const {
3146 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003147 }
3148
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003149 DECLARE_INSTRUCTION(FloatConstant);
3150
3151 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003152 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3153 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3154 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3155 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003156
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003157 const float value_;
3158
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003159 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003160 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003161 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003162 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3163};
3164
3165class HDoubleConstant : public HConstant {
3166 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003167 double GetValue() const { return value_; }
3168
David Brazdil77a48ae2015-09-15 12:34:04 +00003169 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3170
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003171 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003172 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003173 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003174 }
3175
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003176 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003177
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003178 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003179 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003180 }
3181 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003182 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003183 }
3184 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003185 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3186 }
3187 bool IsNaN() const {
3188 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003189 }
3190
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003191 DECLARE_INSTRUCTION(DoubleConstant);
3192
3193 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003194 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3195 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3196 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3197 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003198
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003199 const double value_;
3200
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003201 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003202 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003203 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003204 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3205};
3206
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003207enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003208#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003209#include "intrinsics_list.h"
3210 kNone,
3211 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3212#undef INTRINSICS_LIST
3213#undef OPTIMIZING_INTRINSICS
3214};
3215std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3216
Agi Csaki05f20562015-08-19 14:58:14 -07003217enum IntrinsicNeedsEnvironmentOrCache {
3218 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3219 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003220};
3221
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003222class HInvoke : public HInstruction {
3223 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003224 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003225
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003226 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003227
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003228 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003229 SetRawInputAt(index, argument);
3230 }
3231
Roland Levillain3e3d7332015-04-28 11:00:54 +01003232 // Return the number of arguments. This number can be lower than
3233 // the number of inputs returned by InputCount(), as some invoke
3234 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3235 // inputs at the end of their list of inputs.
3236 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3237
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003238 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003239
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003240
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003241 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003242 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003243
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003244 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3245
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003246 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003247 return intrinsic_;
3248 }
3249
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003250 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003251
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003252 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003253 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003254 }
3255
3256 bool CanThrow() const OVERRIDE { return true; }
3257
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003258 uint32_t* GetIntrinsicOptimizations() {
3259 return &intrinsic_optimizations_;
3260 }
3261
3262 const uint32_t* GetIntrinsicOptimizations() const {
3263 return &intrinsic_optimizations_;
3264 }
3265
3266 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3267
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003268 DECLARE_INSTRUCTION(Invoke);
3269
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003270 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003271 HInvoke(ArenaAllocator* arena,
3272 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003273 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003274 Primitive::Type return_type,
3275 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003276 uint32_t dex_method_index,
3277 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003278 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003279 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003280 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003281 inputs_(number_of_arguments + number_of_other_inputs,
3282 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003283 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003284 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003285 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003286 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003287 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003288 }
3289
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003290 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003291 return inputs_[index];
3292 }
3293
David Brazdil1abb4192015-02-17 18:33:36 +00003294 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003295 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003296 }
3297
Roland Levillain3e3d7332015-04-28 11:00:54 +01003298 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003299 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003300 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003301 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003302 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003303 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003304
3305 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3306 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003307
3308 private:
3309 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3310};
3311
Calin Juravle175dc732015-08-25 15:42:32 +01003312class HInvokeUnresolved : public HInvoke {
3313 public:
3314 HInvokeUnresolved(ArenaAllocator* arena,
3315 uint32_t number_of_arguments,
3316 Primitive::Type return_type,
3317 uint32_t dex_pc,
3318 uint32_t dex_method_index,
3319 InvokeType invoke_type)
3320 : HInvoke(arena,
3321 number_of_arguments,
3322 0u /* number_of_other_inputs */,
3323 return_type,
3324 dex_pc,
3325 dex_method_index,
3326 invoke_type) {
3327 }
3328
3329 DECLARE_INSTRUCTION(InvokeUnresolved);
3330
3331 private:
3332 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3333};
3334
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003335class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003336 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003337 // Requirements of this method call regarding the class
3338 // initialization (clinit) check of its declaring class.
3339 enum class ClinitCheckRequirement {
3340 kNone, // Class already initialized.
3341 kExplicit, // Static call having explicit clinit check as last input.
3342 kImplicit, // Static call implicitly requiring a clinit check.
3343 };
3344
Vladimir Marko58155012015-08-19 12:49:41 +00003345 // Determines how to load the target ArtMethod*.
3346 enum class MethodLoadKind {
3347 // Use a String init ArtMethod* loaded from Thread entrypoints.
3348 kStringInit,
3349
3350 // Use the method's own ArtMethod* loaded by the register allocator.
3351 kRecursive,
3352
3353 // Use ArtMethod* at a known address, embed the direct address in the code.
3354 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3355 kDirectAddress,
3356
3357 // Use ArtMethod* at an address that will be known at link time, embed the direct
3358 // address in the code. If the image is relocatable, emit .patch_oat entry.
3359 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3360 // the image relocatable or not.
3361 kDirectAddressWithFixup,
3362
3363 // Load from resoved methods array in the dex cache using a PC-relative load.
3364 // Used when we need to use the dex cache, for example for invoke-static that
3365 // may cause class initialization (the entry may point to a resolution method),
3366 // and we know that we can access the dex cache arrays using a PC-relative load.
3367 kDexCachePcRelative,
3368
3369 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3370 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3371 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3372 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3373 kDexCacheViaMethod,
3374 };
3375
3376 // Determines the location of the code pointer.
3377 enum class CodePtrLocation {
3378 // Recursive call, use local PC-relative call instruction.
3379 kCallSelf,
3380
3381 // Use PC-relative call instruction patched at link time.
3382 // Used for calls within an oat file, boot->boot or app->app.
3383 kCallPCRelative,
3384
3385 // Call to a known target address, embed the direct address in code.
3386 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3387 kCallDirect,
3388
3389 // Call to a target address that will be known at link time, embed the direct
3390 // address in code. If the image is relocatable, emit .patch_oat entry.
3391 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3392 // the image relocatable or not.
3393 kCallDirectWithFixup,
3394
3395 // Use code pointer from the ArtMethod*.
3396 // Used when we don't know the target code. This is also the last-resort-kind used when
3397 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3398 kCallArtMethod,
3399 };
3400
3401 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003402 MethodLoadKind method_load_kind;
3403 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003404 // The method load data holds
3405 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3406 // Note that there are multiple string init methods, each having its own offset.
3407 // - the method address for kDirectAddress
3408 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003409 uint64_t method_load_data;
3410 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003411 };
3412
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003413 HInvokeStaticOrDirect(ArenaAllocator* arena,
3414 uint32_t number_of_arguments,
3415 Primitive::Type return_type,
3416 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003417 uint32_t method_index,
3418 MethodReference target_method,
3419 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003420 InvokeType original_invoke_type,
Nicolas Geoffrayc88ef3a2015-12-01 16:28:10 +00003421 InvokeType invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003422 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003423 : HInvoke(arena,
3424 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003425 // There is potentially one extra argument for the HCurrentMethod node, and
3426 // potentially one other if the clinit check is explicit, and potentially
3427 // one other if the method is a string factory.
3428 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3429 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3430 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003431 return_type,
3432 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003433 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003434 original_invoke_type),
Nicolas Geoffrayc88ef3a2015-12-01 16:28:10 +00003435 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003436 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003437 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003438 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003439
Vladimir Markodc151b22015-10-15 18:02:30 +01003440 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003441 bool had_current_method_input = HasCurrentMethodInput();
3442 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3443
3444 // Using the current method is the default and once we find a better
3445 // method load kind, we should not go back to using the current method.
3446 DCHECK(had_current_method_input || !needs_current_method_input);
3447
3448 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003449 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3450 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003451 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003452 dispatch_info_ = dispatch_info;
3453 }
3454
Vladimir Markoc53c0792015-11-19 15:48:33 +00003455 void AddSpecialInput(HInstruction* input) {
3456 // We allow only one special input.
3457 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3458 DCHECK(InputCount() == GetSpecialInputIndex() ||
3459 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3460 InsertInputAt(GetSpecialInputIndex(), input);
3461 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003462
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003463 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003464 // We access the method via the dex cache so we can't do an implicit null check.
3465 // TODO: for intrinsics we can generate implicit null checks.
3466 return false;
3467 }
3468
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003469 bool CanBeNull() const OVERRIDE {
3470 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3471 }
3472
Vladimir Markoc53c0792015-11-19 15:48:33 +00003473 // Get the index of the special input, if any.
3474 //
3475 // If the invoke IsStringInit(), it initially has a HFakeString special argument
3476 // which is removed by the instruction simplifier; if the invoke HasCurrentMethodInput(),
3477 // the "special input" is the current method pointer; otherwise there may be one
3478 // platform-specific special input, such as PC-relative addressing base.
3479 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3480
Nicolas Geoffrayc88ef3a2015-12-01 16:28:10 +00003481 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003482 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3483 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3484 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003485 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003486 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003487 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003488 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003489 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3490 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003491 bool HasCurrentMethodInput() const {
3492 // This function can be called only after the invoke has been fully initialized by the builder.
3493 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003494 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003495 return true;
3496 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003497 DCHECK(InputCount() == GetSpecialInputIndex() ||
3498 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003499 return false;
3500 }
3501 }
Vladimir Marko58155012015-08-19 12:49:41 +00003502 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3503 MethodReference GetTargetMethod() const { return target_method_; }
3504
3505 int32_t GetStringInitOffset() const {
3506 DCHECK(IsStringInit());
3507 return dispatch_info_.method_load_data;
3508 }
3509
3510 uint64_t GetMethodAddress() const {
3511 DCHECK(HasMethodAddress());
3512 return dispatch_info_.method_load_data;
3513 }
3514
3515 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003516 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003517 return dispatch_info_.method_load_data;
3518 }
3519
3520 uint64_t GetDirectCodePtr() const {
3521 DCHECK(HasDirectCodePtr());
3522 return dispatch_info_.direct_code_ptr;
3523 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003524
Calin Juravle68ad6492015-08-18 17:08:12 +01003525 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3526
Roland Levillain4c0eb422015-04-24 16:43:49 +01003527 // Is this instruction a call to a static method?
3528 bool IsStatic() const {
Nicolas Geoffrayc88ef3a2015-12-01 16:28:10 +00003529 return GetInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003530 }
3531
Vladimir Markofbb184a2015-11-13 14:47:00 +00003532 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3533 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3534 // instruction; only relevant for static calls with explicit clinit check.
3535 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003536 DCHECK(IsStaticWithExplicitClinitCheck());
3537 size_t last_input_index = InputCount() - 1;
3538 HInstruction* last_input = InputAt(last_input_index);
3539 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003540 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003541 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003542 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003543 clinit_check_requirement_ = new_requirement;
3544 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003545 }
3546
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003547 bool IsStringFactoryFor(HFakeString* str) const {
3548 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003549 DCHECK(!HasCurrentMethodInput());
3550 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003551 return InputAt(InputCount() - 1)->AsFakeString() == str;
3552 }
3553
3554 void RemoveFakeStringArgumentAsLastInput() {
3555 DCHECK(IsStringInit());
3556 size_t last_input_index = InputCount() - 1;
3557 HInstruction* last_input = InputAt(last_input_index);
3558 DCHECK(last_input != nullptr);
3559 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3560 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003561 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003562 }
3563
Roland Levillain4c0eb422015-04-24 16:43:49 +01003564 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003565 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003566 bool IsStaticWithExplicitClinitCheck() const {
3567 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3568 }
3569
3570 // Is this a call to a static method whose declaring class has an
3571 // implicit intialization check requirement?
3572 bool IsStaticWithImplicitClinitCheck() const {
3573 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3574 }
3575
Vladimir Markob554b5a2015-11-06 12:57:55 +00003576 // Does this method load kind need the current method as an input?
3577 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3578 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3579 }
3580
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003581 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003582
Roland Levillain4c0eb422015-04-24 16:43:49 +01003583 protected:
3584 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3585 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3586 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3587 HInstruction* input = input_record.GetInstruction();
3588 // `input` is the last input of a static invoke marked as having
3589 // an explicit clinit check. It must either be:
3590 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3591 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3592 DCHECK(input != nullptr);
3593 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3594 }
3595 return input_record;
3596 }
3597
Vladimir Markoc53c0792015-11-19 15:48:33 +00003598 void InsertInputAt(size_t index, HInstruction* input);
3599 void RemoveInputAt(size_t index);
3600
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003601 private:
Nicolas Geoffrayc88ef3a2015-12-01 16:28:10 +00003602 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003603 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003604 // The target method may refer to different dex file or method index than the original
3605 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3606 // in derived class to increase visibility.
3607 MethodReference target_method_;
3608 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003609
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003610 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003611};
Vladimir Markofbb184a2015-11-13 14:47:00 +00003612std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003613
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003614class HInvokeVirtual : public HInvoke {
3615 public:
3616 HInvokeVirtual(ArenaAllocator* arena,
3617 uint32_t number_of_arguments,
3618 Primitive::Type return_type,
3619 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003620 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003621 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003622 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003623 vtable_index_(vtable_index) {}
3624
Calin Juravle641547a2015-04-21 22:08:51 +01003625 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003626 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003627 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003628 }
3629
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003630 uint32_t GetVTableIndex() const { return vtable_index_; }
3631
3632 DECLARE_INSTRUCTION(InvokeVirtual);
3633
3634 private:
3635 const uint32_t vtable_index_;
3636
3637 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3638};
3639
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003640class HInvokeInterface : public HInvoke {
3641 public:
3642 HInvokeInterface(ArenaAllocator* arena,
3643 uint32_t number_of_arguments,
3644 Primitive::Type return_type,
3645 uint32_t dex_pc,
3646 uint32_t dex_method_index,
3647 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003648 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003649 imt_index_(imt_index) {}
3650
Calin Juravle641547a2015-04-21 22:08:51 +01003651 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003652 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003653 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003654 }
3655
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003656 uint32_t GetImtIndex() const { return imt_index_; }
3657 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3658
3659 DECLARE_INSTRUCTION(InvokeInterface);
3660
3661 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003662 const uint32_t imt_index_;
3663
3664 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3665};
3666
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003667class HNewInstance : public HExpression<2> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003668 public:
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003669 HNewInstance(HInstruction* cls,
3670 HCurrentMethod* current_method,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003671 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003672 uint16_t type_index,
3673 const DexFile& dex_file,
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003674 bool can_throw,
3675 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003676 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003677 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003678 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003679 dex_file_(dex_file),
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003680 can_throw_(can_throw),
3681 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003682 entrypoint_(entrypoint) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003683 SetRawInputAt(0, cls);
3684 SetRawInputAt(1, current_method);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003685 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003686
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003687 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003688 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003689
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003690 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003691 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003692
3693 // It may throw when called on type that's not instantiable/accessible.
3694 // It can throw OOME.
3695 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3696 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3697
3698 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003699
Calin Juravle10e244f2015-01-26 18:54:32 +00003700 bool CanBeNull() const OVERRIDE { return false; }
3701
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003702 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3703
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003704 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3705 entrypoint_ = entrypoint;
3706 }
3707
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003708 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003709
3710 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003711 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003712 const DexFile& dex_file_;
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003713 const bool can_throw_;
3714 const bool finalizable_;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003715 QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003716
3717 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3718};
3719
Roland Levillain88cb1752014-10-20 16:36:47 +01003720class HNeg : public HUnaryOperation {
3721 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003722 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3723 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003724
Roland Levillain9867bc72015-08-05 10:21:34 +01003725 template <typename T> T Compute(T x) const { return -x; }
3726
3727 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003728 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003729 }
3730 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003731 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003732 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003733
Roland Levillain88cb1752014-10-20 16:36:47 +01003734 DECLARE_INSTRUCTION(Neg);
3735
3736 private:
3737 DISALLOW_COPY_AND_ASSIGN(HNeg);
3738};
3739
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003740class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003741 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003742 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003743 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003744 uint32_t dex_pc,
3745 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003746 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003747 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003748 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003749 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003750 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003751 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003752 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003753 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003754 }
3755
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003756 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003757 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003758
3759 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003760 bool NeedsEnvironment() const OVERRIDE { return true; }
3761
Mingyao Yang0c365e62015-03-31 15:09:29 -07003762 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3763 bool CanThrow() const OVERRIDE { return true; }
3764
Calin Juravle10e244f2015-01-26 18:54:32 +00003765 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003766
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003767 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3768
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003769 DECLARE_INSTRUCTION(NewArray);
3770
3771 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003772 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003773 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003774 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003775
3776 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3777};
3778
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003779class HAdd : public HBinaryOperation {
3780 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003781 HAdd(Primitive::Type result_type,
3782 HInstruction* left,
3783 HInstruction* right,
3784 uint32_t dex_pc = kNoDexPc)
3785 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003786
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003787 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003788
Roland Levillain9867bc72015-08-05 10:21:34 +01003789 template <typename T> T Compute(T x, T y) const { return x + y; }
3790
3791 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003792 return GetBlock()->GetGraph()->GetIntConstant(
3793 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003794 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003795 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003796 return GetBlock()->GetGraph()->GetLongConstant(
3797 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003798 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003799
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003800 DECLARE_INSTRUCTION(Add);
3801
3802 private:
3803 DISALLOW_COPY_AND_ASSIGN(HAdd);
3804};
3805
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003806class HSub : public HBinaryOperation {
3807 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003808 HSub(Primitive::Type result_type,
3809 HInstruction* left,
3810 HInstruction* right,
3811 uint32_t dex_pc = kNoDexPc)
3812 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003813
Roland Levillain9867bc72015-08-05 10:21:34 +01003814 template <typename T> T Compute(T x, T y) const { return x - y; }
3815
3816 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003817 return GetBlock()->GetGraph()->GetIntConstant(
3818 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003819 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003820 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003821 return GetBlock()->GetGraph()->GetLongConstant(
3822 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003823 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003824
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003825 DECLARE_INSTRUCTION(Sub);
3826
3827 private:
3828 DISALLOW_COPY_AND_ASSIGN(HSub);
3829};
3830
Calin Juravle34bacdf2014-10-07 20:23:36 +01003831class HMul : public HBinaryOperation {
3832 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003833 HMul(Primitive::Type result_type,
3834 HInstruction* left,
3835 HInstruction* right,
3836 uint32_t dex_pc = kNoDexPc)
3837 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003838
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003839 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003840
Roland Levillain9867bc72015-08-05 10:21:34 +01003841 template <typename T> T Compute(T x, T y) const { return x * y; }
3842
3843 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003844 return GetBlock()->GetGraph()->GetIntConstant(
3845 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003846 }
3847 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003848 return GetBlock()->GetGraph()->GetLongConstant(
3849 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003850 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003851
3852 DECLARE_INSTRUCTION(Mul);
3853
3854 private:
3855 DISALLOW_COPY_AND_ASSIGN(HMul);
3856};
3857
Calin Juravle7c4954d2014-10-28 16:57:40 +00003858class HDiv : public HBinaryOperation {
3859 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003860 HDiv(Primitive::Type result_type,
3861 HInstruction* left,
3862 HInstruction* right,
3863 uint32_t dex_pc)
3864 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003865
Roland Levillain9867bc72015-08-05 10:21:34 +01003866 template <typename T>
3867 T Compute(T x, T y) const {
3868 // Our graph structure ensures we never have 0 for `y` during
3869 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003870 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003871 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003872 return (y == -1) ? -x : x / y;
3873 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003874
Roland Levillain9867bc72015-08-05 10:21:34 +01003875 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003876 return GetBlock()->GetGraph()->GetIntConstant(
3877 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003878 }
3879 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003880 return GetBlock()->GetGraph()->GetLongConstant(
3881 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003882 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003883
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003884 static SideEffects SideEffectsForArchRuntimeCalls() {
3885 // The generated code can use a runtime call.
3886 return SideEffects::CanTriggerGC();
3887 }
3888
Calin Juravle7c4954d2014-10-28 16:57:40 +00003889 DECLARE_INSTRUCTION(Div);
3890
3891 private:
3892 DISALLOW_COPY_AND_ASSIGN(HDiv);
3893};
3894
Calin Juravlebacfec32014-11-14 15:54:36 +00003895class HRem : public HBinaryOperation {
3896 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003897 HRem(Primitive::Type result_type,
3898 HInstruction* left,
3899 HInstruction* right,
3900 uint32_t dex_pc)
3901 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003902
Roland Levillain9867bc72015-08-05 10:21:34 +01003903 template <typename T>
3904 T Compute(T x, T y) const {
3905 // Our graph structure ensures we never have 0 for `y` during
3906 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003907 DCHECK_NE(y, 0);
3908 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3909 return (y == -1) ? 0 : x % y;
3910 }
3911
Roland Levillain9867bc72015-08-05 10:21:34 +01003912 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003913 return GetBlock()->GetGraph()->GetIntConstant(
3914 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003915 }
3916 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003917 return GetBlock()->GetGraph()->GetLongConstant(
3918 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003919 }
3920
Calin Juravlebacfec32014-11-14 15:54:36 +00003921
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003922 static SideEffects SideEffectsForArchRuntimeCalls() {
3923 return SideEffects::CanTriggerGC();
3924 }
3925
Calin Juravlebacfec32014-11-14 15:54:36 +00003926 DECLARE_INSTRUCTION(Rem);
3927
3928 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003929 DISALLOW_COPY_AND_ASSIGN(HRem);
3930};
3931
Calin Juravled0d48522014-11-04 16:40:20 +00003932class HDivZeroCheck : public HExpression<1> {
3933 public:
3934 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003935 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003936 SetRawInputAt(0, value);
3937 }
3938
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003939 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3940
Calin Juravled0d48522014-11-04 16:40:20 +00003941 bool CanBeMoved() const OVERRIDE { return true; }
3942
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003943 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003944 return true;
3945 }
3946
3947 bool NeedsEnvironment() const OVERRIDE { return true; }
3948 bool CanThrow() const OVERRIDE { return true; }
3949
Calin Juravled0d48522014-11-04 16:40:20 +00003950 DECLARE_INSTRUCTION(DivZeroCheck);
3951
3952 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003953 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3954};
3955
Calin Juravle9aec02f2014-11-18 23:06:35 +00003956class HShl : public HBinaryOperation {
3957 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003958 HShl(Primitive::Type result_type,
3959 HInstruction* left,
3960 HInstruction* right,
3961 uint32_t dex_pc = kNoDexPc)
3962 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003963
Roland Levillain9867bc72015-08-05 10:21:34 +01003964 template <typename T, typename U, typename V>
3965 T Compute(T x, U y, V max_shift_value) const {
3966 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3967 "V is not the unsigned integer type corresponding to T");
3968 return x << (y & max_shift_value);
3969 }
3970
3971 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3972 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003973 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003974 }
3975 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3976 // case is handled as `x << static_cast<int>(y)`.
3977 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3978 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003979 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003980 }
3981 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3982 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003983 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003984 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003985
3986 DECLARE_INSTRUCTION(Shl);
3987
3988 private:
3989 DISALLOW_COPY_AND_ASSIGN(HShl);
3990};
3991
3992class HShr : public HBinaryOperation {
3993 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003994 HShr(Primitive::Type result_type,
3995 HInstruction* left,
3996 HInstruction* right,
3997 uint32_t dex_pc = kNoDexPc)
3998 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003999
Roland Levillain9867bc72015-08-05 10:21:34 +01004000 template <typename T, typename U, typename V>
4001 T Compute(T x, U y, V max_shift_value) const {
4002 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4003 "V is not the unsigned integer type corresponding to T");
4004 return x >> (y & max_shift_value);
4005 }
4006
4007 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4008 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004009 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004010 }
4011 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4012 // case is handled as `x >> static_cast<int>(y)`.
4013 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4014 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004015 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004016 }
4017 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4018 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004019 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004020 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004021
4022 DECLARE_INSTRUCTION(Shr);
4023
4024 private:
4025 DISALLOW_COPY_AND_ASSIGN(HShr);
4026};
4027
4028class HUShr : public HBinaryOperation {
4029 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004030 HUShr(Primitive::Type result_type,
4031 HInstruction* left,
4032 HInstruction* right,
4033 uint32_t dex_pc = kNoDexPc)
4034 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004035
Roland Levillain9867bc72015-08-05 10:21:34 +01004036 template <typename T, typename U, typename V>
4037 T Compute(T x, U y, V max_shift_value) const {
4038 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4039 "V is not the unsigned integer type corresponding to T");
4040 V ux = static_cast<V>(x);
4041 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004042 }
4043
Roland Levillain9867bc72015-08-05 10:21:34 +01004044 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4045 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004046 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004047 }
4048 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4049 // case is handled as `x >>> static_cast<int>(y)`.
4050 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4051 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004052 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004053 }
4054 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4055 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004056 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004057 }
4058
4059 DECLARE_INSTRUCTION(UShr);
4060
4061 private:
4062 DISALLOW_COPY_AND_ASSIGN(HUShr);
4063};
4064
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004065class HAnd : public HBinaryOperation {
4066 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004067 HAnd(Primitive::Type result_type,
4068 HInstruction* left,
4069 HInstruction* right,
4070 uint32_t dex_pc = kNoDexPc)
4071 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004072
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004073 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004074
Roland Levillain9867bc72015-08-05 10:21:34 +01004075 template <typename T, typename U>
4076 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4077
4078 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004079 return GetBlock()->GetGraph()->GetIntConstant(
4080 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004081 }
4082 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004083 return GetBlock()->GetGraph()->GetLongConstant(
4084 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004085 }
4086 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004087 return GetBlock()->GetGraph()->GetLongConstant(
4088 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004089 }
4090 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004091 return GetBlock()->GetGraph()->GetLongConstant(
4092 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004093 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004094
4095 DECLARE_INSTRUCTION(And);
4096
4097 private:
4098 DISALLOW_COPY_AND_ASSIGN(HAnd);
4099};
4100
4101class HOr : public HBinaryOperation {
4102 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004103 HOr(Primitive::Type result_type,
4104 HInstruction* left,
4105 HInstruction* right,
4106 uint32_t dex_pc = kNoDexPc)
4107 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004108
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004109 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004110
Roland Levillain9867bc72015-08-05 10:21:34 +01004111 template <typename T, typename U>
4112 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4113
4114 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004115 return GetBlock()->GetGraph()->GetIntConstant(
4116 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004117 }
4118 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004119 return GetBlock()->GetGraph()->GetLongConstant(
4120 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004121 }
4122 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004123 return GetBlock()->GetGraph()->GetLongConstant(
4124 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004125 }
4126 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004127 return GetBlock()->GetGraph()->GetLongConstant(
4128 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004129 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004130
4131 DECLARE_INSTRUCTION(Or);
4132
4133 private:
4134 DISALLOW_COPY_AND_ASSIGN(HOr);
4135};
4136
4137class HXor : public HBinaryOperation {
4138 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004139 HXor(Primitive::Type result_type,
4140 HInstruction* left,
4141 HInstruction* right,
4142 uint32_t dex_pc = kNoDexPc)
4143 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004144
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004145 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004146
Roland Levillain9867bc72015-08-05 10:21:34 +01004147 template <typename T, typename U>
4148 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4149
4150 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004151 return GetBlock()->GetGraph()->GetIntConstant(
4152 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004153 }
4154 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004155 return GetBlock()->GetGraph()->GetLongConstant(
4156 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004157 }
4158 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004159 return GetBlock()->GetGraph()->GetLongConstant(
4160 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004161 }
4162 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004163 return GetBlock()->GetGraph()->GetLongConstant(
4164 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004165 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004166
4167 DECLARE_INSTRUCTION(Xor);
4168
4169 private:
4170 DISALLOW_COPY_AND_ASSIGN(HXor);
4171};
4172
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004173// The value of a parameter in this method. Its location depends on
4174// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004175class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004176 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004177 HParameterValue(const DexFile& dex_file,
4178 uint16_t type_index,
4179 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004180 Primitive::Type parameter_type,
4181 bool is_this = false)
4182 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004183 dex_file_(dex_file),
4184 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004185 index_(index),
4186 is_this_(is_this),
4187 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004188
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004189 const DexFile& GetDexFile() const { return dex_file_; }
4190 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004191 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004192 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004193
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004194 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4195 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004196
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004197 DECLARE_INSTRUCTION(ParameterValue);
4198
4199 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004200 const DexFile& dex_file_;
4201 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004202 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004203 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004204 const uint8_t index_;
4205
Calin Juravle10e244f2015-01-26 18:54:32 +00004206 // Whether or not the parameter value corresponds to 'this' argument.
4207 const bool is_this_;
4208
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004209 bool can_be_null_;
4210
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004211 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4212};
4213
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004214class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004215 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004216 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4217 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004218
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004219 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004220 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004221 return true;
4222 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004223
Roland Levillain9867bc72015-08-05 10:21:34 +01004224 template <typename T> T Compute(T x) const { return ~x; }
4225
4226 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004227 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004228 }
4229 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004230 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004231 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004232
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004233 DECLARE_INSTRUCTION(Not);
4234
4235 private:
4236 DISALLOW_COPY_AND_ASSIGN(HNot);
4237};
4238
David Brazdil66d126e2015-04-03 16:02:44 +01004239class HBooleanNot : public HUnaryOperation {
4240 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004241 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4242 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004243
4244 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004245 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004246 return true;
4247 }
4248
Roland Levillain9867bc72015-08-05 10:21:34 +01004249 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004250 DCHECK(IsUint<1>(x));
4251 return !x;
4252 }
4253
Roland Levillain9867bc72015-08-05 10:21:34 +01004254 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004255 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004256 }
4257 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4258 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004259 UNREACHABLE();
4260 }
4261
4262 DECLARE_INSTRUCTION(BooleanNot);
4263
4264 private:
4265 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4266};
4267
Roland Levillaindff1f282014-11-05 14:15:05 +00004268class HTypeConversion : public HExpression<1> {
4269 public:
4270 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004271 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004272 : HExpression(result_type,
4273 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4274 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004275 SetRawInputAt(0, input);
4276 DCHECK_NE(input->GetType(), result_type);
4277 }
4278
4279 HInstruction* GetInput() const { return InputAt(0); }
4280 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4281 Primitive::Type GetResultType() const { return GetType(); }
4282
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004283 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004284 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004285
Roland Levillaindff1f282014-11-05 14:15:05 +00004286 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004287 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004288
Mark Mendelle82549b2015-05-06 10:55:34 -04004289 // Try to statically evaluate the conversion and return a HConstant
4290 // containing the result. If the input cannot be converted, return nullptr.
4291 HConstant* TryStaticEvaluation() const;
4292
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004293 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4294 Primitive::Type result_type) {
4295 // Some architectures may not require the 'GC' side effects, but at this point
4296 // in the compilation process we do not know what architecture we will
4297 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004298 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4299 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004300 return SideEffects::CanTriggerGC();
4301 }
4302 return SideEffects::None();
4303 }
4304
Roland Levillaindff1f282014-11-05 14:15:05 +00004305 DECLARE_INSTRUCTION(TypeConversion);
4306
4307 private:
4308 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4309};
4310
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004311static constexpr uint32_t kNoRegNumber = -1;
4312
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004313class HPhi : public HInstruction {
4314 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004315 HPhi(ArenaAllocator* arena,
4316 uint32_t reg_number,
4317 size_t number_of_inputs,
4318 Primitive::Type type,
4319 uint32_t dex_pc = kNoDexPc)
4320 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004321 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004322 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004323 type_(ToPhiType(type)),
4324 // Phis are constructed live and marked dead if conflicting or unused.
4325 // Individual steps of SsaBuilder should assume that if a phi has been
4326 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4327 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004328 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004329 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004330 }
4331
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004332 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4333 static Primitive::Type ToPhiType(Primitive::Type type) {
4334 switch (type) {
4335 case Primitive::kPrimBoolean:
4336 case Primitive::kPrimByte:
4337 case Primitive::kPrimShort:
4338 case Primitive::kPrimChar:
4339 return Primitive::kPrimInt;
4340 default:
4341 return type;
4342 }
4343 }
4344
David Brazdilffee3d32015-07-06 11:48:53 +01004345 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4346
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004347 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004348
4349 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004350 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004351
Calin Juravle10e244f2015-01-26 18:54:32 +00004352 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004353 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004354
Calin Juravle10e244f2015-01-26 18:54:32 +00004355 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4356 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4357
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004358 uint32_t GetRegNumber() const { return reg_number_; }
4359
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004360 void SetDead() { is_live_ = false; }
4361 void SetLive() { is_live_ = true; }
4362 bool IsDead() const { return !is_live_; }
4363 bool IsLive() const { return is_live_; }
4364
David Brazdil77a48ae2015-09-15 12:34:04 +00004365 bool IsVRegEquivalentOf(HInstruction* other) const {
4366 return other != nullptr
4367 && other->IsPhi()
4368 && other->AsPhi()->GetBlock() == GetBlock()
4369 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4370 }
4371
Calin Juravlea4f88312015-04-16 12:57:19 +01004372 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4373 // An equivalent phi is a phi having the same dex register and type.
4374 // It assumes that phis with the same dex register are adjacent.
4375 HPhi* GetNextEquivalentPhiWithSameType() {
4376 HInstruction* next = GetNext();
4377 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4378 if (next->GetType() == GetType()) {
4379 return next->AsPhi();
4380 }
4381 next = next->GetNext();
4382 }
4383 return nullptr;
4384 }
4385
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004386 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004387
David Brazdil1abb4192015-02-17 18:33:36 +00004388 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004389 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004390 return inputs_[index];
4391 }
David Brazdil1abb4192015-02-17 18:33:36 +00004392
4393 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004394 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004395 }
4396
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004397 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004398 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004399 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004400 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004401 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004402 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004403
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004404 DISALLOW_COPY_AND_ASSIGN(HPhi);
4405};
4406
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004407class HNullCheck : public HExpression<1> {
4408 public:
4409 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004410 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004411 SetRawInputAt(0, value);
4412 }
4413
Calin Juravle10e244f2015-01-26 18:54:32 +00004414 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004415 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004416 return true;
4417 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004418
Calin Juravle10e244f2015-01-26 18:54:32 +00004419 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004420
Calin Juravle10e244f2015-01-26 18:54:32 +00004421 bool CanThrow() const OVERRIDE { return true; }
4422
4423 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004424
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004425
4426 DECLARE_INSTRUCTION(NullCheck);
4427
4428 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004429 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4430};
4431
4432class FieldInfo : public ValueObject {
4433 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004434 FieldInfo(MemberOffset field_offset,
4435 Primitive::Type field_type,
4436 bool is_volatile,
4437 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004438 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004439 const DexFile& dex_file,
4440 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004441 : field_offset_(field_offset),
4442 field_type_(field_type),
4443 is_volatile_(is_volatile),
4444 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004445 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004446 dex_file_(dex_file),
4447 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004448
4449 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004450 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004451 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004452 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004453 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004454 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004455 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004456
4457 private:
4458 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004459 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004460 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004461 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004462 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004463 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004464 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004465};
4466
4467class HInstanceFieldGet : public HExpression<1> {
4468 public:
4469 HInstanceFieldGet(HInstruction* value,
4470 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004471 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004472 bool is_volatile,
4473 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004474 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004475 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004476 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004477 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004478 : HExpression(field_type,
4479 SideEffects::FieldReadOfType(field_type, is_volatile),
4480 dex_pc),
4481 field_info_(field_offset,
4482 field_type,
4483 is_volatile,
4484 field_idx,
4485 declaring_class_def_index,
4486 dex_file,
4487 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004488 SetRawInputAt(0, value);
4489 }
4490
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004491 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004492
4493 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4494 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4495 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004496 }
4497
Calin Juravle641547a2015-04-21 22:08:51 +01004498 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4499 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004500 }
4501
4502 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004503 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4504 }
4505
Calin Juravle52c48962014-12-16 17:02:57 +00004506 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004507 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004508 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004509 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004510
4511 DECLARE_INSTRUCTION(InstanceFieldGet);
4512
4513 private:
4514 const FieldInfo field_info_;
4515
4516 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4517};
4518
4519class HInstanceFieldSet : public HTemplateInstruction<2> {
4520 public:
4521 HInstanceFieldSet(HInstruction* object,
4522 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004523 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004524 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004525 bool is_volatile,
4526 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004527 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004528 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004529 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004530 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004531 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4532 dex_pc),
4533 field_info_(field_offset,
4534 field_type,
4535 is_volatile,
4536 field_idx,
4537 declaring_class_def_index,
4538 dex_file,
4539 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004540 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004541 SetRawInputAt(0, object);
4542 SetRawInputAt(1, value);
4543 }
4544
Calin Juravle641547a2015-04-21 22:08:51 +01004545 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4546 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004547 }
4548
Calin Juravle52c48962014-12-16 17:02:57 +00004549 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004550 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004551 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004552 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004553 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004554 bool GetValueCanBeNull() const { return value_can_be_null_; }
4555 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004556
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004557 DECLARE_INSTRUCTION(InstanceFieldSet);
4558
4559 private:
4560 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004561 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004562
4563 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4564};
4565
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004566class HArrayGet : public HExpression<2> {
4567 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004568 HArrayGet(HInstruction* array,
4569 HInstruction* index,
4570 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004571 uint32_t dex_pc,
4572 SideEffects additional_side_effects = SideEffects::None())
4573 : HExpression(type,
4574 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004575 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004576 SetRawInputAt(0, array);
4577 SetRawInputAt(1, index);
4578 }
4579
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004580 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004581 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004582 return true;
4583 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004584 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004585 // TODO: We can be smarter here.
4586 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4587 // which generates the implicit null check. There are cases when these can be removed
4588 // to produce better code. If we ever add optimizations to do so we should allow an
4589 // implicit check here (as long as the address falls in the first page).
4590 return false;
4591 }
4592
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004593 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004594
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004595 HInstruction* GetArray() const { return InputAt(0); }
4596 HInstruction* GetIndex() const { return InputAt(1); }
4597
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004598 DECLARE_INSTRUCTION(ArrayGet);
4599
4600 private:
4601 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4602};
4603
4604class HArraySet : public HTemplateInstruction<3> {
4605 public:
4606 HArraySet(HInstruction* array,
4607 HInstruction* index,
4608 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004609 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004610 uint32_t dex_pc,
4611 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004612 : HTemplateInstruction(
4613 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004614 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4615 additional_side_effects),
4616 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004617 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004618 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004619 value_can_be_null_(true),
4620 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004621 SetRawInputAt(0, array);
4622 SetRawInputAt(1, index);
4623 SetRawInputAt(2, value);
4624 }
4625
Calin Juravle77520bc2015-01-12 18:45:46 +00004626 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004627 // We currently always call a runtime method to catch array store
4628 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004629 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004630 }
4631
Mingyao Yang81014cb2015-06-02 03:16:27 -07004632 // Can throw ArrayStoreException.
4633 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4634
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004635 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004636 // TODO: Same as for ArrayGet.
4637 return false;
4638 }
4639
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004640 void ClearNeedsTypeCheck() {
4641 needs_type_check_ = false;
4642 }
4643
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004644 void ClearValueCanBeNull() {
4645 value_can_be_null_ = false;
4646 }
4647
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004648 void SetStaticTypeOfArrayIsObjectArray() {
4649 static_type_of_array_is_object_array_ = true;
4650 }
4651
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004652 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004653 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004654 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004655
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004656 HInstruction* GetArray() const { return InputAt(0); }
4657 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004658 HInstruction* GetValue() const { return InputAt(2); }
4659
4660 Primitive::Type GetComponentType() const {
4661 // The Dex format does not type floating point index operations. Since the
4662 // `expected_component_type_` is set during building and can therefore not
4663 // be correct, we also check what is the value type. If it is a floating
4664 // point type, we must use that type.
4665 Primitive::Type value_type = GetValue()->GetType();
4666 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4667 ? value_type
4668 : expected_component_type_;
4669 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004670
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004671 Primitive::Type GetRawExpectedComponentType() const {
4672 return expected_component_type_;
4673 }
4674
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004675 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4676 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4677 }
4678
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004679 DECLARE_INSTRUCTION(ArraySet);
4680
4681 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004682 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004683 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004684 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004685 // Cached information for the reference_type_info_ so that codegen
4686 // does not need to inspect the static type.
4687 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004688
4689 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4690};
4691
4692class HArrayLength : public HExpression<1> {
4693 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004694 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004695 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004696 // Note that arrays do not change length, so the instruction does not
4697 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004698 SetRawInputAt(0, array);
4699 }
4700
Calin Juravle77520bc2015-01-12 18:45:46 +00004701 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004702 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004703 return true;
4704 }
Calin Juravle641547a2015-04-21 22:08:51 +01004705 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4706 return obj == InputAt(0);
4707 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004708
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004709 DECLARE_INSTRUCTION(ArrayLength);
4710
4711 private:
4712 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4713};
4714
4715class HBoundsCheck : public HExpression<2> {
4716 public:
4717 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004718 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004719 DCHECK(index->GetType() == Primitive::kPrimInt);
4720 SetRawInputAt(0, index);
4721 SetRawInputAt(1, length);
4722 }
4723
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004724 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004725 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004726 return true;
4727 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004728
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004729 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004730
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004731 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004732
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004733 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004734
4735 DECLARE_INSTRUCTION(BoundsCheck);
4736
4737 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004738 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4739};
4740
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004741/**
4742 * Some DEX instructions are folded into multiple HInstructions that need
4743 * to stay live until the last HInstruction. This class
4744 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004745 * HInstruction stays live. `index` represents the stack location index of the
4746 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004747 */
4748class HTemporary : public HTemplateInstruction<0> {
4749 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004750 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4751 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004752
4753 size_t GetIndex() const { return index_; }
4754
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004755 Primitive::Type GetType() const OVERRIDE {
4756 // The previous instruction is the one that will be stored in the temporary location.
4757 DCHECK(GetPrevious() != nullptr);
4758 return GetPrevious()->GetType();
4759 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004760
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004761 DECLARE_INSTRUCTION(Temporary);
4762
4763 private:
4764 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004765 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4766};
4767
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004768class HSuspendCheck : public HTemplateInstruction<0> {
4769 public:
4770 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004771 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004772
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004773 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004774 return true;
4775 }
4776
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004777 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4778 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004779
4780 DECLARE_INSTRUCTION(SuspendCheck);
4781
4782 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004783 // Only used for code generation, in order to share the same slow path between back edges
4784 // of a same loop.
4785 SlowPathCode* slow_path_;
4786
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004787 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4788};
4789
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004790/**
4791 * Instruction to load a Class object.
4792 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004793class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004794 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004795 HLoadClass(HCurrentMethod* current_method,
4796 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004797 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004798 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004799 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004800 bool needs_access_check,
4801 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004803 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004804 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004805 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004806 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004807 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004808 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004809 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004810 // Referrers class should not need access check. We never inline unverified
4811 // methods so we can't possibly end up in this situation.
4812 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004813 SetRawInputAt(0, current_method);
4814 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004815
4816 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004817
4818 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004819 // Note that we don't need to test for generate_clinit_check_.
4820 // Whether or not we need to generate the clinit check is processed in
4821 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004822 return other->AsLoadClass()->type_index_ == type_index_ &&
4823 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004824 }
4825
4826 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4827
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004828 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004829 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004830 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004831
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004832 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004833 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004834 }
4835
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004836 bool MustGenerateClinitCheck() const {
4837 return generate_clinit_check_;
4838 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004839
Calin Juravle0ba218d2015-05-19 18:46:01 +01004840 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004841 // The entrypoint the code generator is going to call does not do
4842 // clinit of the class.
4843 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004844 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004845 }
4846
4847 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004848 return MustGenerateClinitCheck() ||
4849 (!is_referrers_class_ && !is_in_dex_cache_) ||
4850 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004851 }
4852
4853 bool NeedsAccessCheck() const {
4854 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004855 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004856
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004857 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004858 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004859 }
4860
Calin Juravleacf735c2015-02-12 15:25:22 +00004861 ReferenceTypeInfo GetLoadedClassRTI() {
4862 return loaded_class_rti_;
4863 }
4864
4865 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4866 // Make sure we only set exact types (the loaded class should never be merged).
4867 DCHECK(rti.IsExact());
4868 loaded_class_rti_ = rti;
4869 }
4870
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004871 const DexFile& GetDexFile() { return dex_file_; }
4872
Vladimir Markodc151b22015-10-15 18:02:30 +01004873 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004874
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004875 static SideEffects SideEffectsForArchRuntimeCalls() {
4876 return SideEffects::CanTriggerGC();
4877 }
4878
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004879 bool IsInDexCache() const { return is_in_dex_cache_; }
4880
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004881 DECLARE_INSTRUCTION(LoadClass);
4882
4883 private:
4884 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004885 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004886 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004887 // Whether this instruction must generate the initialization check.
4888 // Used for code generation.
4889 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004890 const bool needs_access_check_;
4891 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004892
Calin Juravleacf735c2015-02-12 15:25:22 +00004893 ReferenceTypeInfo loaded_class_rti_;
4894
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004895 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4896};
4897
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004898class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004899 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004900 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004901 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4902 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004903 SetRawInputAt(0, current_method);
4904 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004905
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004906 bool CanBeMoved() const OVERRIDE { return true; }
4907
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004908 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4909 return other->AsLoadString()->string_index_ == string_index_;
4910 }
4911
4912 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4913
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004914 uint32_t GetStringIndex() const { return string_index_; }
4915
4916 // TODO: Can we deopt or debug when we resolve a string?
4917 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004918 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004919 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004920
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004921 static SideEffects SideEffectsForArchRuntimeCalls() {
4922 return SideEffects::CanTriggerGC();
4923 }
4924
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004925 DECLARE_INSTRUCTION(LoadString);
4926
4927 private:
4928 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004929
4930 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4931};
4932
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004933/**
4934 * Performs an initialization check on its Class object input.
4935 */
4936class HClinitCheck : public HExpression<1> {
4937 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004938 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004939 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004940 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004941 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4942 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004943 SetRawInputAt(0, constant);
4944 }
4945
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004946 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004947 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004948 return true;
4949 }
4950
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004951 bool NeedsEnvironment() const OVERRIDE {
4952 // May call runtime to initialize the class.
4953 return true;
4954 }
4955
Nicolas Geoffray729645a2015-11-19 13:29:02 +00004956 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004957
4958 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4959
4960 DECLARE_INSTRUCTION(ClinitCheck);
4961
4962 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004963 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4964};
4965
4966class HStaticFieldGet : public HExpression<1> {
4967 public:
4968 HStaticFieldGet(HInstruction* cls,
4969 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004970 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004971 bool is_volatile,
4972 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004973 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004974 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004975 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004976 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004977 : HExpression(field_type,
4978 SideEffects::FieldReadOfType(field_type, is_volatile),
4979 dex_pc),
4980 field_info_(field_offset,
4981 field_type,
4982 is_volatile,
4983 field_idx,
4984 declaring_class_def_index,
4985 dex_file,
4986 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004987 SetRawInputAt(0, cls);
4988 }
4989
Calin Juravle52c48962014-12-16 17:02:57 +00004990
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004991 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004992
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004993 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004994 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4995 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004996 }
4997
4998 size_t ComputeHashCode() const OVERRIDE {
4999 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5000 }
5001
Calin Juravle52c48962014-12-16 17:02:57 +00005002 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005003 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5004 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005005 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005006
5007 DECLARE_INSTRUCTION(StaticFieldGet);
5008
5009 private:
5010 const FieldInfo field_info_;
5011
5012 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5013};
5014
5015class HStaticFieldSet : public HTemplateInstruction<2> {
5016 public:
5017 HStaticFieldSet(HInstruction* cls,
5018 HInstruction* value,
5019 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005020 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005021 bool is_volatile,
5022 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005023 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005024 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005025 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005026 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005027 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5028 dex_pc),
5029 field_info_(field_offset,
5030 field_type,
5031 is_volatile,
5032 field_idx,
5033 declaring_class_def_index,
5034 dex_file,
5035 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005036 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005037 SetRawInputAt(0, cls);
5038 SetRawInputAt(1, value);
5039 }
5040
Calin Juravle52c48962014-12-16 17:02:57 +00005041 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005042 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5043 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005044 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005045
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005046 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005047 bool GetValueCanBeNull() const { return value_can_be_null_; }
5048 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005049
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005050 DECLARE_INSTRUCTION(StaticFieldSet);
5051
5052 private:
5053 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005054 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005055
5056 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5057};
5058
Calin Juravlee460d1d2015-09-29 04:52:17 +01005059class HUnresolvedInstanceFieldGet : public HExpression<1> {
5060 public:
5061 HUnresolvedInstanceFieldGet(HInstruction* obj,
5062 Primitive::Type field_type,
5063 uint32_t field_index,
5064 uint32_t dex_pc)
5065 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5066 field_index_(field_index) {
5067 SetRawInputAt(0, obj);
5068 }
5069
5070 bool NeedsEnvironment() const OVERRIDE { return true; }
5071 bool CanThrow() const OVERRIDE { return true; }
5072
5073 Primitive::Type GetFieldType() const { return GetType(); }
5074 uint32_t GetFieldIndex() const { return field_index_; }
5075
5076 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5077
5078 private:
5079 const uint32_t field_index_;
5080
5081 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5082};
5083
5084class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5085 public:
5086 HUnresolvedInstanceFieldSet(HInstruction* obj,
5087 HInstruction* value,
5088 Primitive::Type field_type,
5089 uint32_t field_index,
5090 uint32_t dex_pc)
5091 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5092 field_type_(field_type),
5093 field_index_(field_index) {
5094 DCHECK_EQ(field_type, value->GetType());
5095 SetRawInputAt(0, obj);
5096 SetRawInputAt(1, value);
5097 }
5098
5099 bool NeedsEnvironment() const OVERRIDE { return true; }
5100 bool CanThrow() const OVERRIDE { return true; }
5101
5102 Primitive::Type GetFieldType() const { return field_type_; }
5103 uint32_t GetFieldIndex() const { return field_index_; }
5104
5105 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5106
5107 private:
5108 const Primitive::Type field_type_;
5109 const uint32_t field_index_;
5110
5111 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5112};
5113
5114class HUnresolvedStaticFieldGet : public HExpression<0> {
5115 public:
5116 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5117 uint32_t field_index,
5118 uint32_t dex_pc)
5119 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5120 field_index_(field_index) {
5121 }
5122
5123 bool NeedsEnvironment() const OVERRIDE { return true; }
5124 bool CanThrow() const OVERRIDE { return true; }
5125
5126 Primitive::Type GetFieldType() const { return GetType(); }
5127 uint32_t GetFieldIndex() const { return field_index_; }
5128
5129 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5130
5131 private:
5132 const uint32_t field_index_;
5133
5134 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5135};
5136
5137class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5138 public:
5139 HUnresolvedStaticFieldSet(HInstruction* value,
5140 Primitive::Type field_type,
5141 uint32_t field_index,
5142 uint32_t dex_pc)
5143 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5144 field_type_(field_type),
5145 field_index_(field_index) {
5146 DCHECK_EQ(field_type, value->GetType());
5147 SetRawInputAt(0, value);
5148 }
5149
5150 bool NeedsEnvironment() const OVERRIDE { return true; }
5151 bool CanThrow() const OVERRIDE { return true; }
5152
5153 Primitive::Type GetFieldType() const { return field_type_; }
5154 uint32_t GetFieldIndex() const { return field_index_; }
5155
5156 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5157
5158 private:
5159 const Primitive::Type field_type_;
5160 const uint32_t field_index_;
5161
5162 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5163};
5164
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005165// Implement the move-exception DEX instruction.
5166class HLoadException : public HExpression<0> {
5167 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005168 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5169 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005170
David Brazdilbbd733e2015-08-18 17:48:17 +01005171 bool CanBeNull() const OVERRIDE { return false; }
5172
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005173 DECLARE_INSTRUCTION(LoadException);
5174
5175 private:
5176 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5177};
5178
David Brazdilcb1c0552015-08-04 16:22:25 +01005179// Implicit part of move-exception which clears thread-local exception storage.
5180// Must not be removed because the runtime expects the TLS to get cleared.
5181class HClearException : public HTemplateInstruction<0> {
5182 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005183 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5184 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005185
5186 DECLARE_INSTRUCTION(ClearException);
5187
5188 private:
5189 DISALLOW_COPY_AND_ASSIGN(HClearException);
5190};
5191
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005192class HThrow : public HTemplateInstruction<1> {
5193 public:
5194 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005195 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005196 SetRawInputAt(0, exception);
5197 }
5198
5199 bool IsControlFlow() const OVERRIDE { return true; }
5200
5201 bool NeedsEnvironment() const OVERRIDE { return true; }
5202
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005203 bool CanThrow() const OVERRIDE { return true; }
5204
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005205
5206 DECLARE_INSTRUCTION(Throw);
5207
5208 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005209 DISALLOW_COPY_AND_ASSIGN(HThrow);
5210};
5211
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005212/**
5213 * Implementation strategies for the code generator of a HInstanceOf
5214 * or `HCheckCast`.
5215 */
5216enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005217 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005218 kExactCheck, // Can do a single class compare.
5219 kClassHierarchyCheck, // Can just walk the super class chain.
5220 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5221 kInterfaceCheck, // No optimization yet when checking against an interface.
5222 kArrayObjectCheck, // Can just check if the array is not primitive.
5223 kArrayCheck // No optimization yet when checking against a generic array.
5224};
5225
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005226class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005227 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005228 HInstanceOf(HInstruction* object,
5229 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005230 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005231 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005232 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005233 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005234 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005235 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005236 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005237 SetRawInputAt(0, object);
5238 SetRawInputAt(1, constant);
5239 }
5240
5241 bool CanBeMoved() const OVERRIDE { return true; }
5242
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005243 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005244 return true;
5245 }
5246
5247 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005248 return false;
5249 }
5250
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005251 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5252
5253 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005254
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005255 // Used only in code generation.
5256 bool MustDoNullCheck() const { return must_do_null_check_; }
5257 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5258
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005259 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5260 return (check_kind == TypeCheckKind::kExactCheck)
5261 ? SideEffects::None()
5262 // Mips currently does runtime calls for any other checks.
5263 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005264 }
5265
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005266 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005267
5268 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005269 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005270 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005271
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005272 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5273};
5274
Calin Juravleb1498f62015-02-16 13:13:29 +00005275class HBoundType : public HExpression<1> {
5276 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005277 // Constructs an HBoundType with the given upper_bound.
5278 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005279 HBoundType(HInstruction* input,
5280 ReferenceTypeInfo upper_bound,
5281 bool upper_can_be_null,
5282 uint32_t dex_pc = kNoDexPc)
5283 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005284 upper_bound_(upper_bound),
5285 upper_can_be_null_(upper_can_be_null),
5286 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005287 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005288 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005289 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005290 }
5291
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005292 // GetUpper* should only be used in reference type propagation.
5293 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5294 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005295
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005296 void SetCanBeNull(bool can_be_null) {
5297 DCHECK(upper_can_be_null_ || !can_be_null);
5298 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005299 }
5300
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005301 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5302
Calin Juravleb1498f62015-02-16 13:13:29 +00005303 DECLARE_INSTRUCTION(BoundType);
5304
5305 private:
5306 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005307 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5308 // It is used to bound the type in cases like:
5309 // if (x instanceof ClassX) {
5310 // // uper_bound_ will be ClassX
5311 // }
5312 const ReferenceTypeInfo upper_bound_;
5313 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5314 // is false then can_be_null_ cannot be true).
5315 const bool upper_can_be_null_;
5316 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005317
5318 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5319};
5320
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005321class HCheckCast : public HTemplateInstruction<2> {
5322 public:
5323 HCheckCast(HInstruction* object,
5324 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005325 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005326 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005327 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005328 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005329 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005330 SetRawInputAt(0, object);
5331 SetRawInputAt(1, constant);
5332 }
5333
5334 bool CanBeMoved() const OVERRIDE { return true; }
5335
5336 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5337 return true;
5338 }
5339
5340 bool NeedsEnvironment() const OVERRIDE {
5341 // Instruction may throw a CheckCastError.
5342 return true;
5343 }
5344
5345 bool CanThrow() const OVERRIDE { return true; }
5346
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005347 bool MustDoNullCheck() const { return must_do_null_check_; }
5348 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005349 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005350
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005351 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005352
5353 DECLARE_INSTRUCTION(CheckCast);
5354
5355 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005356 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005357 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005358
5359 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005360};
5361
Calin Juravle27df7582015-04-17 19:12:31 +01005362class HMemoryBarrier : public HTemplateInstruction<0> {
5363 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005364 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005365 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005366 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005367 barrier_kind_(barrier_kind) {}
5368
5369 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5370
5371 DECLARE_INSTRUCTION(MemoryBarrier);
5372
5373 private:
5374 const MemBarrierKind barrier_kind_;
5375
5376 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5377};
5378
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005379class HMonitorOperation : public HTemplateInstruction<1> {
5380 public:
5381 enum OperationKind {
5382 kEnter,
5383 kExit,
5384 };
5385
5386 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005387 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005388 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5389 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005390 SetRawInputAt(0, object);
5391 }
5392
5393 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005394 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5395
5396 bool CanThrow() const OVERRIDE {
5397 // Verifier guarantees that monitor-exit cannot throw.
5398 // This is important because it allows the HGraphBuilder to remove
5399 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5400 return IsEnter();
5401 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005402
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005403
5404 bool IsEnter() const { return kind_ == kEnter; }
5405
5406 DECLARE_INSTRUCTION(MonitorOperation);
5407
Calin Juravle52c48962014-12-16 17:02:57 +00005408 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005409 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005410
5411 private:
5412 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5413};
5414
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005415/**
5416 * A HInstruction used as a marker for the replacement of new + <init>
5417 * of a String to a call to a StringFactory. Only baseline will see
5418 * the node at code generation, where it will be be treated as null.
5419 * When compiling non-baseline, `HFakeString` instructions are being removed
5420 * in the instruction simplifier.
5421 */
5422class HFakeString : public HTemplateInstruction<0> {
5423 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005424 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5425 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005426
5427 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5428
5429 DECLARE_INSTRUCTION(FakeString);
5430
5431 private:
5432 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5433};
5434
Vladimir Markof9f64412015-09-02 14:05:49 +01005435class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005436 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005437 MoveOperands(Location source,
5438 Location destination,
5439 Primitive::Type type,
5440 HInstruction* instruction)
5441 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005442
5443 Location GetSource() const { return source_; }
5444 Location GetDestination() const { return destination_; }
5445
5446 void SetSource(Location value) { source_ = value; }
5447 void SetDestination(Location value) { destination_ = value; }
5448
5449 // The parallel move resolver marks moves as "in-progress" by clearing the
5450 // destination (but not the source).
5451 Location MarkPending() {
5452 DCHECK(!IsPending());
5453 Location dest = destination_;
5454 destination_ = Location::NoLocation();
5455 return dest;
5456 }
5457
5458 void ClearPending(Location dest) {
5459 DCHECK(IsPending());
5460 destination_ = dest;
5461 }
5462
5463 bool IsPending() const {
5464 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5465 return destination_.IsInvalid() && !source_.IsInvalid();
5466 }
5467
5468 // True if this blocks a move from the given location.
5469 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005470 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005471 }
5472
5473 // A move is redundant if it's been eliminated, if its source and
5474 // destination are the same, or if its destination is unneeded.
5475 bool IsRedundant() const {
5476 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5477 }
5478
5479 // We clear both operands to indicate move that's been eliminated.
5480 void Eliminate() {
5481 source_ = destination_ = Location::NoLocation();
5482 }
5483
5484 bool IsEliminated() const {
5485 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5486 return source_.IsInvalid();
5487 }
5488
Alexey Frunze4dda3372015-06-01 18:31:49 -07005489 Primitive::Type GetType() const { return type_; }
5490
Nicolas Geoffray90218252015-04-15 11:56:51 +01005491 bool Is64BitMove() const {
5492 return Primitive::Is64BitType(type_);
5493 }
5494
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005495 HInstruction* GetInstruction() const { return instruction_; }
5496
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005497 private:
5498 Location source_;
5499 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005500 // The type this move is for.
5501 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005502 // The instruction this move is assocatied with. Null when this move is
5503 // for moving an input in the expected locations of user (including a phi user).
5504 // This is only used in debug mode, to ensure we do not connect interval siblings
5505 // in the same parallel move.
5506 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005507};
5508
5509static constexpr size_t kDefaultNumberOfMoves = 4;
5510
5511class HParallelMove : public HTemplateInstruction<0> {
5512 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005513 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005514 : HTemplateInstruction(SideEffects::None(), dex_pc),
5515 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5516 moves_.reserve(kDefaultNumberOfMoves);
5517 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005518
Nicolas Geoffray90218252015-04-15 11:56:51 +01005519 void AddMove(Location source,
5520 Location destination,
5521 Primitive::Type type,
5522 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005523 DCHECK(source.IsValid());
5524 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005525 if (kIsDebugBuild) {
5526 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005527 for (const MoveOperands& move : moves_) {
5528 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005529 // Special case the situation where the move is for the spill slot
5530 // of the instruction.
5531 if ((GetPrevious() == instruction)
5532 || ((GetPrevious() == nullptr)
5533 && instruction->IsPhi()
5534 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005535 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005536 << "Doing parallel moves for the same instruction.";
5537 } else {
5538 DCHECK(false) << "Doing parallel moves for the same instruction.";
5539 }
5540 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005541 }
5542 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005543 for (const MoveOperands& move : moves_) {
5544 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005545 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005546 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005547 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005548 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005549 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005550 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005551 }
5552
Vladimir Marko225b6462015-09-28 12:17:40 +01005553 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005554 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005555 }
5556
Vladimir Marko225b6462015-09-28 12:17:40 +01005557 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005558
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005559 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005560
5561 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005562 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005563
5564 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5565};
5566
Mark Mendell0616ae02015-04-17 12:49:27 -04005567} // namespace art
5568
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005569#ifdef ART_ENABLE_CODEGEN_arm64
5570#include "nodes_arm64.h"
5571#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005572#ifdef ART_ENABLE_CODEGEN_x86
5573#include "nodes_x86.h"
5574#endif
5575
5576namespace art {
5577
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005578class HGraphVisitor : public ValueObject {
5579 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005580 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5581 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005582
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005583 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005584 virtual void VisitBasicBlock(HBasicBlock* block);
5585
Roland Levillain633021e2014-10-01 14:12:25 +01005586 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005587 void VisitInsertionOrder();
5588
Roland Levillain633021e2014-10-01 14:12:25 +01005589 // Visit the graph following dominator tree reverse post-order.
5590 void VisitReversePostOrder();
5591
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005592 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005593
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005594 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005595#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005596 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5597
5598 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5599
5600#undef DECLARE_VISIT_INSTRUCTION
5601
5602 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005603 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005604
5605 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5606};
5607
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005608class HGraphDelegateVisitor : public HGraphVisitor {
5609 public:
5610 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5611 virtual ~HGraphDelegateVisitor() {}
5612
5613 // Visit functions that delegate to to super class.
5614#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005615 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005616
5617 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5618
5619#undef DECLARE_VISIT_INSTRUCTION
5620
5621 private:
5622 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5623};
5624
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005625class HInsertionOrderIterator : public ValueObject {
5626 public:
5627 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5628
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005629 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005630 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005631 void Advance() { ++index_; }
5632
5633 private:
5634 const HGraph& graph_;
5635 size_t index_;
5636
5637 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5638};
5639
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005640class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005641 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005642 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5643 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005644 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005645 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005646
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005647 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5648 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005649 void Advance() { ++index_; }
5650
5651 private:
5652 const HGraph& graph_;
5653 size_t index_;
5654
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005655 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005656};
5657
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005658class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005659 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005660 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005661 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005662 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005663 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005664 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005665
5666 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005667 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005668 void Advance() { --index_; }
5669
5670 private:
5671 const HGraph& graph_;
5672 size_t index_;
5673
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005674 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005675};
5676
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005677class HLinearPostOrderIterator : public ValueObject {
5678 public:
5679 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005680 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005681
5682 bool Done() const { return index_ == 0; }
5683
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005684 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005685
5686 void Advance() {
5687 --index_;
5688 DCHECK_GE(index_, 0U);
5689 }
5690
5691 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005692 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005693 size_t index_;
5694
5695 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5696};
5697
5698class HLinearOrderIterator : public ValueObject {
5699 public:
5700 explicit HLinearOrderIterator(const HGraph& graph)
5701 : order_(graph.GetLinearOrder()), index_(0) {}
5702
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005703 bool Done() const { return index_ == order_.size(); }
5704 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005705 void Advance() { ++index_; }
5706
5707 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005708 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005709 size_t index_;
5710
5711 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5712};
5713
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005714// Iterator over the blocks that art part of the loop. Includes blocks part
5715// of an inner loop. The order in which the blocks are iterated is on their
5716// block id.
5717class HBlocksInLoopIterator : public ValueObject {
5718 public:
5719 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5720 : blocks_in_loop_(info.GetBlocks()),
5721 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5722 index_(0) {
5723 if (!blocks_in_loop_.IsBitSet(index_)) {
5724 Advance();
5725 }
5726 }
5727
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005728 bool Done() const { return index_ == blocks_.size(); }
5729 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005730 void Advance() {
5731 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005732 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005733 if (blocks_in_loop_.IsBitSet(index_)) {
5734 break;
5735 }
5736 }
5737 }
5738
5739 private:
5740 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005741 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005742 size_t index_;
5743
5744 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5745};
5746
Mingyao Yang3584bce2015-05-19 16:01:59 -07005747// Iterator over the blocks that art part of the loop. Includes blocks part
5748// of an inner loop. The order in which the blocks are iterated is reverse
5749// post order.
5750class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5751 public:
5752 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5753 : blocks_in_loop_(info.GetBlocks()),
5754 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5755 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005756 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005757 Advance();
5758 }
5759 }
5760
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005761 bool Done() const { return index_ == blocks_.size(); }
5762 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005763 void Advance() {
5764 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005765 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5766 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005767 break;
5768 }
5769 }
5770 }
5771
5772 private:
5773 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005774 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005775 size_t index_;
5776
5777 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5778};
5779
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005780inline int64_t Int64FromConstant(HConstant* constant) {
5781 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5782 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5783 : constant->AsLongConstant()->GetValue();
5784}
5785
Vladimir Marko58155012015-08-19 12:49:41 +00005786inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5787 // For the purposes of the compiler, the dex files must actually be the same object
5788 // if we want to safely treat them as the same. This is especially important for JIT
5789 // as custom class loaders can open the same underlying file (or memory) multiple
5790 // times and provide different class resolution but no two class loaders should ever
5791 // use the same DexFile object - doing so is an unsupported hack that can lead to
5792 // all sorts of weird failures.
5793 return &lhs == &rhs;
5794}
5795
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005796} // namespace art
5797
5798#endif // ART_COMPILER_OPTIMIZING_NODES_H_