blob: d3f30cb01b6995ce6d372266fbfd28d7d688970e [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 Rames418318f2015-11-20 15:55:47 +00001104 M(Arm64IntermediateAddress, Instruction) \
1105 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001106#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001107
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001108#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1109
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001110#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1111
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001112#ifndef ART_ENABLE_CODEGEN_x86
1113#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1114#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001115#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1116 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001117 M(X86LoadFromConstantTable, Instruction) \
1118 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001119#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001120
1121#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1122
1123#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1124 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1125 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1126 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001127 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001128 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001129 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1130 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1131
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001132#define FOR_EACH_INSTRUCTION(M) \
1133 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1134 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001135 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001136 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001137 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001138
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001139#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001140FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1141#undef FORWARD_DECLARATION
1142
Roland Levillainccc07a92014-09-16 14:48:16 +01001143#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001144 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1145 const char* DebugName() const OVERRIDE { return #type; } \
1146 const H##type* As##type() const OVERRIDE { return this; } \
1147 H##type* As##type() OVERRIDE { return this; } \
1148 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001149 return other->Is##type(); \
1150 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001151 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001152
David Brazdiled596192015-01-23 10:39:45 +00001153template <typename T> class HUseList;
1154
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001155template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001156class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001157 public:
David Brazdiled596192015-01-23 10:39:45 +00001158 HUseListNode* GetPrevious() const { return prev_; }
1159 HUseListNode* GetNext() const { return next_; }
1160 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001161 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001162 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001163
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001164 private:
David Brazdiled596192015-01-23 10:39:45 +00001165 HUseListNode(T user, size_t index)
1166 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1167
1168 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001169 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001170 HUseListNode<T>* prev_;
1171 HUseListNode<T>* next_;
1172
1173 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001174
1175 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1176};
1177
David Brazdiled596192015-01-23 10:39:45 +00001178template <typename T>
1179class HUseList : public ValueObject {
1180 public:
1181 HUseList() : first_(nullptr) {}
1182
1183 void Clear() {
1184 first_ = nullptr;
1185 }
1186
1187 // Adds a new entry at the beginning of the use list and returns
1188 // the newly created node.
1189 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001190 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001191 if (IsEmpty()) {
1192 first_ = new_node;
1193 } else {
1194 first_->prev_ = new_node;
1195 new_node->next_ = first_;
1196 first_ = new_node;
1197 }
1198 return new_node;
1199 }
1200
1201 HUseListNode<T>* GetFirst() const {
1202 return first_;
1203 }
1204
1205 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001206 DCHECK(node != nullptr);
1207 DCHECK(Contains(node));
1208
David Brazdiled596192015-01-23 10:39:45 +00001209 if (node->prev_ != nullptr) {
1210 node->prev_->next_ = node->next_;
1211 }
1212 if (node->next_ != nullptr) {
1213 node->next_->prev_ = node->prev_;
1214 }
1215 if (node == first_) {
1216 first_ = node->next_;
1217 }
1218 }
1219
David Brazdil1abb4192015-02-17 18:33:36 +00001220 bool Contains(const HUseListNode<T>* node) const {
1221 if (node == nullptr) {
1222 return false;
1223 }
1224 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1225 if (current == node) {
1226 return true;
1227 }
1228 }
1229 return false;
1230 }
1231
David Brazdiled596192015-01-23 10:39:45 +00001232 bool IsEmpty() const {
1233 return first_ == nullptr;
1234 }
1235
1236 bool HasOnlyOneUse() const {
1237 return first_ != nullptr && first_->next_ == nullptr;
1238 }
1239
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001240 size_t SizeSlow() const {
1241 size_t count = 0;
1242 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1243 ++count;
1244 }
1245 return count;
1246 }
1247
David Brazdiled596192015-01-23 10:39:45 +00001248 private:
1249 HUseListNode<T>* first_;
1250};
1251
1252template<typename T>
1253class HUseIterator : public ValueObject {
1254 public:
1255 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1256
1257 bool Done() const { return current_ == nullptr; }
1258
1259 void Advance() {
1260 DCHECK(!Done());
1261 current_ = current_->GetNext();
1262 }
1263
1264 HUseListNode<T>* Current() const {
1265 DCHECK(!Done());
1266 return current_;
1267 }
1268
1269 private:
1270 HUseListNode<T>* current_;
1271
1272 friend class HValue;
1273};
1274
David Brazdil1abb4192015-02-17 18:33:36 +00001275// This class is used by HEnvironment and HInstruction classes to record the
1276// instructions they use and pointers to the corresponding HUseListNodes kept
1277// by the used instructions.
1278template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001279class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001280 public:
1281 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1282 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1283
1284 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1285 : instruction_(old_record.instruction_), use_node_(use_node) {
1286 DCHECK(instruction_ != nullptr);
1287 DCHECK(use_node_ != nullptr);
1288 DCHECK(old_record.use_node_ == nullptr);
1289 }
1290
1291 HInstruction* GetInstruction() const { return instruction_; }
1292 HUseListNode<T>* GetUseNode() const { return use_node_; }
1293
1294 private:
1295 // Instruction used by the user.
1296 HInstruction* instruction_;
1297
1298 // Corresponding entry in the use list kept by 'instruction_'.
1299 HUseListNode<T>* use_node_;
1300};
1301
Aart Bik854a02b2015-07-14 16:07:00 -07001302/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001303 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001304 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001305 * For write/read dependences on fields/arrays, the dependence analysis uses
1306 * type disambiguation (e.g. a float field write cannot modify the value of an
1307 * integer field read) and the access type (e.g. a reference array write cannot
1308 * modify the value of a reference field read [although it may modify the
1309 * reference fetch prior to reading the field, which is represented by its own
1310 * write/read dependence]). The analysis makes conservative points-to
1311 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1312 * the same, and any reference read depends on any reference read without
1313 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001314 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001315 * The internal representation uses 38-bit and is described in the table below.
1316 * The first line indicates the side effect, and for field/array accesses the
1317 * second line indicates the type of the access (in the order of the
1318 * Primitive::Type enum).
1319 * The two numbered lines below indicate the bit position in the bitfield (read
1320 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001321 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001322 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1323 * +-------------+---------+---------+--------------+---------+---------+
1324 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1325 * | 3 |333333322|222222221| 1 |111111110|000000000|
1326 * | 7 |654321098|765432109| 8 |765432109|876543210|
1327 *
1328 * Note that, to ease the implementation, 'changes' bits are least significant
1329 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001330 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001331class SideEffects : public ValueObject {
1332 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001333 SideEffects() : flags_(0) {}
1334
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001335 static SideEffects None() {
1336 return SideEffects(0);
1337 }
1338
1339 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001340 return SideEffects(kAllChangeBits | kAllDependOnBits);
1341 }
1342
1343 static SideEffects AllChanges() {
1344 return SideEffects(kAllChangeBits);
1345 }
1346
1347 static SideEffects AllDependencies() {
1348 return SideEffects(kAllDependOnBits);
1349 }
1350
1351 static SideEffects AllExceptGCDependency() {
1352 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1353 }
1354
1355 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001356 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001357 }
1358
Aart Bik34c3ba92015-07-20 14:08:59 -07001359 static SideEffects AllWrites() {
1360 return SideEffects(kAllWrites);
1361 }
1362
1363 static SideEffects AllReads() {
1364 return SideEffects(kAllReads);
1365 }
1366
1367 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1368 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001369 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001370 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001371 }
1372
Aart Bik854a02b2015-07-14 16:07:00 -07001373 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1374 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001375 }
1376
Aart Bik34c3ba92015-07-20 14:08:59 -07001377 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1378 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001379 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001380 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001381 }
1382
1383 static SideEffects ArrayReadOfType(Primitive::Type type) {
1384 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1385 }
1386
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001387 static SideEffects CanTriggerGC() {
1388 return SideEffects(1ULL << kCanTriggerGCBit);
1389 }
1390
1391 static SideEffects DependsOnGC() {
1392 return SideEffects(1ULL << kDependsOnGCBit);
1393 }
1394
Aart Bik854a02b2015-07-14 16:07:00 -07001395 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001396 SideEffects Union(SideEffects other) const {
1397 return SideEffects(flags_ | other.flags_);
1398 }
1399
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001400 SideEffects Exclusion(SideEffects other) const {
1401 return SideEffects(flags_ & ~other.flags_);
1402 }
1403
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001404 void Add(SideEffects other) {
1405 flags_ |= other.flags_;
1406 }
1407
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001408 bool Includes(SideEffects other) const {
1409 return (other.flags_ & flags_) == other.flags_;
1410 }
1411
1412 bool HasSideEffects() const {
1413 return (flags_ & kAllChangeBits);
1414 }
1415
1416 bool HasDependencies() const {
1417 return (flags_ & kAllDependOnBits);
1418 }
1419
1420 // Returns true if there are no side effects or dependencies.
1421 bool DoesNothing() const {
1422 return flags_ == 0;
1423 }
1424
Aart Bik854a02b2015-07-14 16:07:00 -07001425 // Returns true if something is written.
1426 bool DoesAnyWrite() const {
1427 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001428 }
1429
Aart Bik854a02b2015-07-14 16:07:00 -07001430 // Returns true if something is read.
1431 bool DoesAnyRead() const {
1432 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001433 }
1434
Aart Bik854a02b2015-07-14 16:07:00 -07001435 // Returns true if potentially everything is written and read
1436 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001437 bool DoesAllReadWrite() const {
1438 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1439 }
1440
Aart Bik854a02b2015-07-14 16:07:00 -07001441 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001442 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001443 }
1444
Roland Levillain0d5a2812015-11-13 10:07:31 +00001445 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001446 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001447 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1448 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001449 }
1450
1451 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001452 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001453 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001454 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001455 for (int s = kLastBit; s >= 0; s--) {
1456 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1457 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1458 // This is a bit for the GC side effect.
1459 if (current_bit_is_set) {
1460 flags += "GC";
1461 }
Aart Bik854a02b2015-07-14 16:07:00 -07001462 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001463 } else {
1464 // This is a bit for the array/field analysis.
1465 // The underscore character stands for the 'can trigger GC' bit.
1466 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1467 if (current_bit_is_set) {
1468 flags += kDebug[s];
1469 }
1470 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1471 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1472 flags += "|";
1473 }
1474 }
Aart Bik854a02b2015-07-14 16:07:00 -07001475 }
1476 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001477 }
1478
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001479 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001480
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001481 private:
1482 static constexpr int kFieldArrayAnalysisBits = 9;
1483
1484 static constexpr int kFieldWriteOffset = 0;
1485 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1486 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1487 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1488
1489 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1490
1491 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1492 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1493 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1494 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1495
1496 static constexpr int kLastBit = kDependsOnGCBit;
1497 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1498
1499 // Aliases.
1500
1501 static_assert(kChangeBits == kDependOnBits,
1502 "the 'change' bits should match the 'depend on' bits.");
1503
1504 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1505 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1506 static constexpr uint64_t kAllWrites =
1507 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1508 static constexpr uint64_t kAllReads =
1509 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001510
Aart Bik854a02b2015-07-14 16:07:00 -07001511 // Work around the fact that HIR aliases I/F and J/D.
1512 // TODO: remove this interceptor once HIR types are clean
1513 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1514 switch (type) {
1515 case Primitive::kPrimInt:
1516 case Primitive::kPrimFloat:
1517 return TypeFlag(Primitive::kPrimInt, offset) |
1518 TypeFlag(Primitive::kPrimFloat, offset);
1519 case Primitive::kPrimLong:
1520 case Primitive::kPrimDouble:
1521 return TypeFlag(Primitive::kPrimLong, offset) |
1522 TypeFlag(Primitive::kPrimDouble, offset);
1523 default:
1524 return TypeFlag(type, offset);
1525 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001526 }
1527
Aart Bik854a02b2015-07-14 16:07:00 -07001528 // Translates type to bit flag.
1529 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1530 CHECK_NE(type, Primitive::kPrimVoid);
1531 const uint64_t one = 1;
1532 const int shift = type; // 0-based consecutive enum
1533 DCHECK_LE(kFieldWriteOffset, shift);
1534 DCHECK_LT(shift, kArrayWriteOffset);
1535 return one << (type + offset);
1536 }
1537
1538 // Private constructor on direct flags value.
1539 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1540
1541 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001542};
1543
David Brazdiled596192015-01-23 10:39:45 +00001544// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001545class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001546 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001547 HEnvironment(ArenaAllocator* arena,
1548 size_t number_of_vregs,
1549 const DexFile& dex_file,
1550 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001551 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001552 InvokeType invoke_type,
1553 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001554 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1555 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001556 parent_(nullptr),
1557 dex_file_(dex_file),
1558 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001559 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001560 invoke_type_(invoke_type),
1561 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001562 }
1563
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001564 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001565 : HEnvironment(arena,
1566 to_copy.Size(),
1567 to_copy.GetDexFile(),
1568 to_copy.GetMethodIdx(),
1569 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001570 to_copy.GetInvokeType(),
1571 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001572
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001573 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001574 if (parent_ != nullptr) {
1575 parent_->SetAndCopyParentChain(allocator, parent);
1576 } else {
1577 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1578 parent_->CopyFrom(parent);
1579 if (parent->GetParent() != nullptr) {
1580 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1581 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001582 }
David Brazdiled596192015-01-23 10:39:45 +00001583 }
1584
Vladimir Marko71bf8092015-09-15 15:33:14 +01001585 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001586 void CopyFrom(HEnvironment* environment);
1587
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001588 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1589 // input to the loop phi instead. This is for inserting instructions that
1590 // require an environment (like HDeoptimization) in the loop pre-header.
1591 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001592
1593 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001594 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001595 }
1596
1597 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001598 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001599 }
1600
David Brazdil1abb4192015-02-17 18:33:36 +00001601 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001602
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001603 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001604
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001605 HEnvironment* GetParent() const { return parent_; }
1606
1607 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001608 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001609 }
1610
1611 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001612 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001613 }
1614
1615 uint32_t GetDexPc() const {
1616 return dex_pc_;
1617 }
1618
1619 uint32_t GetMethodIdx() const {
1620 return method_idx_;
1621 }
1622
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001623 InvokeType GetInvokeType() const {
1624 return invoke_type_;
1625 }
1626
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001627 const DexFile& GetDexFile() const {
1628 return dex_file_;
1629 }
1630
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001631 HInstruction* GetHolder() const {
1632 return holder_;
1633 }
1634
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001635
1636 bool IsFromInlinedInvoke() const {
1637 return GetParent() != nullptr;
1638 }
1639
David Brazdiled596192015-01-23 10:39:45 +00001640 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001641 // Record instructions' use entries of this environment for constant-time removal.
1642 // It should only be called by HInstruction when a new environment use is added.
1643 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1644 DCHECK(env_use->GetUser() == this);
1645 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001646 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001647 }
David Brazdiled596192015-01-23 10:39:45 +00001648
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001649 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1650 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001651 HEnvironment* parent_;
1652 const DexFile& dex_file_;
1653 const uint32_t method_idx_;
1654 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001655 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001656
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001657 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001658 HInstruction* const holder_;
1659
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001660 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001661
1662 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1663};
1664
Calin Juravleacf735c2015-02-12 15:25:22 +00001665class ReferenceTypeInfo : ValueObject {
1666 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001667 typedef Handle<mirror::Class> TypeHandle;
1668
Calin Juravle2e768302015-07-28 14:41:11 +00001669 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1670 // The constructor will check that the type_handle is valid.
1671 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001672 }
1673
Calin Juravle2e768302015-07-28 14:41:11 +00001674 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1675
1676 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1677 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001678 }
1679
Calin Juravle2e768302015-07-28 14:41:11 +00001680 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1681 return IsValidHandle(type_handle_);
1682 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001683
Calin Juravleacf735c2015-02-12 15:25:22 +00001684 bool IsExact() const { return is_exact_; }
Calin Juravle2e768302015-07-28 14:41:11 +00001685
1686 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1687 DCHECK(IsValid());
1688 return GetTypeHandle()->IsObjectClass();
1689 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001690
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001691 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001692 DCHECK(IsValid());
1693 return GetTypeHandle()->IsStringClass();
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001694 }
1695
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001696 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
1697 DCHECK(IsValid());
1698 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
1699 }
1700
Mathieu Chartier90443472015-07-16 20:32:27 -07001701 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001702 DCHECK(IsValid());
1703 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001704 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001705
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001706 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001707 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001708 return GetTypeHandle()->IsArrayClass();
1709 }
1710
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001711 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1712 DCHECK(IsValid());
1713 return GetTypeHandle()->IsPrimitiveArray();
1714 }
1715
1716 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1717 DCHECK(IsValid());
1718 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
1719 }
1720
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001721 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001722 DCHECK(IsValid());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001723 if (!IsExact()) return false;
1724 if (!IsArrayClass()) return false;
1725 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
1726 }
1727
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001728 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1729 DCHECK(IsValid());
1730 if (!IsExact()) return false;
1731 if (!IsArrayClass()) return false;
1732 if (!rti.IsArrayClass()) return false;
1733 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
1734 rti.GetTypeHandle()->GetComponentType());
1735 }
1736
Calin Juravleacf735c2015-02-12 15:25:22 +00001737 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1738
Mathieu Chartier90443472015-07-16 20:32:27 -07001739 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001740 DCHECK(IsValid());
1741 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001742 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1743 }
1744
Calin Juravlecdfed3d2015-10-26 14:05:01 +00001745 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
1746 DCHECK(IsValid());
1747 DCHECK(rti.IsValid());
1748 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
1749 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1750 }
1751
Calin Juravleacf735c2015-02-12 15:25:22 +00001752 // Returns true if the type information provide the same amount of details.
1753 // Note that it does not mean that the instructions have the same actual type
Calin Juravle2e768302015-07-28 14:41:11 +00001754 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001755 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle2e768302015-07-28 14:41:11 +00001756 if (!IsValid() && !rti.IsValid()) {
1757 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001758 return true;
1759 }
Calin Juravle2e768302015-07-28 14:41:11 +00001760 if (!IsValid() || !rti.IsValid()) {
1761 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001762 return false;
1763 }
Calin Juravle2e768302015-07-28 14:41:11 +00001764 return IsExact() == rti.IsExact()
1765 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001766 }
1767
1768 private:
Calin Juravle2e768302015-07-28 14:41:11 +00001769 ReferenceTypeInfo();
1770 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001771
Calin Juravleacf735c2015-02-12 15:25:22 +00001772 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001773 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001774 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001775 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001776 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001777};
1778
1779std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1780
Vladimir Markof9f64412015-09-02 14:05:49 +01001781class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001782 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001783 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001784 : previous_(nullptr),
1785 next_(nullptr),
1786 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001787 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001788 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001789 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001790 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001791 locations_(nullptr),
1792 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001793 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001794 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001795 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001796
Dave Allison20dfc792014-06-16 20:44:29 -07001797 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001798
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001799#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001800 enum InstructionKind {
1801 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1802 };
1803#undef DECLARE_KIND
1804
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001805 HInstruction* GetNext() const { return next_; }
1806 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001807
Calin Juravle77520bc2015-01-12 18:45:46 +00001808 HInstruction* GetNextDisregardingMoves() const;
1809 HInstruction* GetPreviousDisregardingMoves() const;
1810
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001811 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001812 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001813 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001814 bool IsInBlock() const { return block_ != nullptr; }
1815 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001816 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001817
Roland Levillain6b879dd2014-09-22 17:13:44 +01001818 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001819 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001820
1821 virtual void Accept(HGraphVisitor* visitor) = 0;
1822 virtual const char* DebugName() const = 0;
1823
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001824 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001825 void SetRawInputAt(size_t index, HInstruction* input) {
1826 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1827 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001828
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001829 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001830
1831 uint32_t GetDexPc() const { return dex_pc_; }
1832
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001833 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001834
Roland Levillaine161a2a2014-10-03 12:45:18 +01001835 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001836 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001837
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001838 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001839 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001840
Calin Juravle10e244f2015-01-26 18:54:32 +00001841 // Does not apply for all instructions, but having this at top level greatly
1842 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001843 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001844 virtual bool CanBeNull() const {
1845 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1846 return true;
1847 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001848
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001849 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001850 return false;
1851 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001852
Calin Juravle2e768302015-07-28 14:41:11 +00001853 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001854
Calin Juravle61d544b2015-02-23 16:46:57 +00001855 ReferenceTypeInfo GetReferenceTypeInfo() const {
1856 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1857 return reference_type_info_;
1858 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001859
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001860 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001861 DCHECK(user != nullptr);
1862 HUseListNode<HInstruction*>* use =
1863 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1864 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001865 }
1866
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001867 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001868 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001869 HUseListNode<HEnvironment*>* env_use =
1870 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1871 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001872 }
1873
David Brazdil1abb4192015-02-17 18:33:36 +00001874 void RemoveAsUserOfInput(size_t input) {
1875 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1876 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1877 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001878
David Brazdil1abb4192015-02-17 18:33:36 +00001879 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1880 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001881
David Brazdiled596192015-01-23 10:39:45 +00001882 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1883 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001884 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001885 bool HasOnlyOneNonEnvironmentUse() const {
1886 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1887 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001888
Roland Levillain6c82d402014-10-13 16:10:27 +01001889 // Does this instruction strictly dominate `other_instruction`?
1890 // Returns false if this instruction and `other_instruction` are the same.
1891 // Aborts if this instruction and `other_instruction` are both phis.
1892 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001893
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001894 int GetId() const { return id_; }
1895 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001896
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001897 int GetSsaIndex() const { return ssa_index_; }
1898 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1899 bool HasSsaIndex() const { return ssa_index_ != -1; }
1900
1901 bool HasEnvironment() const { return environment_ != nullptr; }
1902 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001903 // Set the `environment_` field. Raw because this method does not
1904 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001905 void SetRawEnvironment(HEnvironment* environment) {
1906 DCHECK(environment_ == nullptr);
1907 DCHECK_EQ(environment->GetHolder(), this);
1908 environment_ = environment;
1909 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001910
1911 // Set the environment of this instruction, copying it from `environment`. While
1912 // copying, the uses lists are being updated.
1913 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001914 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001915 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001916 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001917 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001918 if (environment->GetParent() != nullptr) {
1919 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1920 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001921 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001922
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001923 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1924 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001925 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001926 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001927 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001928 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001929 if (environment->GetParent() != nullptr) {
1930 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1931 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001932 }
1933
Nicolas Geoffray39468442014-09-02 15:17:15 +01001934 // Returns the number of entries in the environment. Typically, that is the
1935 // number of dex registers in a method. It could be more in case of inlining.
1936 size_t EnvironmentSize() const;
1937
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001938 LocationSummary* GetLocations() const { return locations_; }
1939 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001940
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001941 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001942 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001943
Alexandre Rames188d4312015-04-09 18:30:21 +01001944 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1945 // uses of this instruction by `other` are *not* updated.
1946 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1947 ReplaceWith(other);
1948 other->ReplaceInput(this, use_index);
1949 }
1950
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001951 // Move `this` instruction before `cursor`.
1952 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001953
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001954#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001955 bool Is##type() const { return (As##type() != nullptr); } \
1956 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001957 virtual H##type* As##type() { return nullptr; }
1958
1959 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1960#undef INSTRUCTION_TYPE_CHECK
1961
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001962 // Returns whether the instruction can be moved within the graph.
1963 virtual bool CanBeMoved() const { return false; }
1964
1965 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001966 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001967 return false;
1968 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001969
1970 // Returns whether any data encoded in the two instructions is equal.
1971 // This method does not look at the inputs. Both instructions must be
1972 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001973 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001974 return false;
1975 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001976
1977 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001978 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001979 // 2) Their inputs are identical.
1980 bool Equals(HInstruction* other) const;
1981
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001982 virtual InstructionKind GetKind() const = 0;
1983
1984 virtual size_t ComputeHashCode() const {
1985 size_t result = GetKind();
1986 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1987 result = (result * 31) + InputAt(i)->GetId();
1988 }
1989 return result;
1990 }
1991
1992 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001993 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001994
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001995 size_t GetLifetimePosition() const { return lifetime_position_; }
1996 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1997 LiveInterval* GetLiveInterval() const { return live_interval_; }
1998 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1999 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2000
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002001 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2002
2003 // Returns whether the code generation of the instruction will require to have access
2004 // to the current method. Such instructions are:
2005 // (1): Instructions that require an environment, as calling the runtime requires
2006 // to walk the stack and have the current method stored at a specific stack address.
2007 // (2): Object literals like classes and strings, that are loaded from the dex cache
2008 // fields of the current method.
2009 bool NeedsCurrentMethod() const {
2010 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2011 }
2012
Vladimir Markodc151b22015-10-15 18:02:30 +01002013 // Returns whether the code generation of the instruction will require to have access
2014 // to the dex cache of the current method's declaring class via the current method.
2015 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002016
Mark Mendellc4701932015-04-10 13:18:51 -04002017 // Does this instruction have any use in an environment before
2018 // control flow hits 'other'?
2019 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2020
2021 // Remove all references to environment uses of this instruction.
2022 // The caller must ensure that this is safe to do.
2023 void RemoveEnvironmentUsers();
2024
David Brazdil1abb4192015-02-17 18:33:36 +00002025 protected:
2026 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2027 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2028
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002029 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002030 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2031
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002032 HInstruction* previous_;
2033 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002034 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002035 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002036
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002037 // An instruction gets an id when it is added to the graph.
2038 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002039 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002040 int id_;
2041
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002042 // When doing liveness analysis, instructions that have uses get an SSA index.
2043 int ssa_index_;
2044
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002045 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002046 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002047
2048 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002049 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002050
Nicolas Geoffray39468442014-09-02 15:17:15 +01002051 // The environment associated with this instruction. Not null if the instruction
2052 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002053 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002054
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002055 // Set by the code generator.
2056 LocationSummary* locations_;
2057
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002058 // Set by the liveness analysis.
2059 LiveInterval* live_interval_;
2060
2061 // Set by the liveness analysis, this is the position in a linear
2062 // order of blocks where this instruction's live interval start.
2063 size_t lifetime_position_;
2064
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002065 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002066
Calin Juravleacf735c2015-02-12 15:25:22 +00002067 // TODO: for primitive types this should be marked as invalid.
2068 ReferenceTypeInfo reference_type_info_;
2069
David Brazdil1abb4192015-02-17 18:33:36 +00002070 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002071 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002072 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002073 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002074 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002075
2076 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2077};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002078std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002079
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002080class HInputIterator : public ValueObject {
2081 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002082 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002083
2084 bool Done() const { return index_ == instruction_->InputCount(); }
2085 HInstruction* Current() const { return instruction_->InputAt(index_); }
2086 void Advance() { index_++; }
2087
2088 private:
2089 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002090 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002091
2092 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2093};
2094
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002095class HInstructionIterator : public ValueObject {
2096 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002097 explicit HInstructionIterator(const HInstructionList& instructions)
2098 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002099 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002100 }
2101
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002102 bool Done() const { return instruction_ == nullptr; }
2103 HInstruction* Current() const { return instruction_; }
2104 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002105 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002106 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002107 }
2108
2109 private:
2110 HInstruction* instruction_;
2111 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002112
2113 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114};
2115
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002116class HBackwardInstructionIterator : public ValueObject {
2117 public:
2118 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2119 : instruction_(instructions.last_instruction_) {
2120 next_ = Done() ? nullptr : instruction_->GetPrevious();
2121 }
2122
2123 bool Done() const { return instruction_ == nullptr; }
2124 HInstruction* Current() const { return instruction_; }
2125 void Advance() {
2126 instruction_ = next_;
2127 next_ = Done() ? nullptr : instruction_->GetPrevious();
2128 }
2129
2130 private:
2131 HInstruction* instruction_;
2132 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002133
2134 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002135};
2136
Vladimir Markof9f64412015-09-02 14:05:49 +01002137template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002138class HTemplateInstruction: public HInstruction {
2139 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002140 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002141 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002142 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002143
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002144 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002145
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002146 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002147 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2148 DCHECK_LT(i, N);
2149 return inputs_[i];
2150 }
David Brazdil1abb4192015-02-17 18:33:36 +00002151
2152 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002153 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002154 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002155 }
2156
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002157 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002158 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002159
2160 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002161};
2162
Vladimir Markof9f64412015-09-02 14:05:49 +01002163// HTemplateInstruction specialization for N=0.
2164template<>
2165class HTemplateInstruction<0>: public HInstruction {
2166 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002167 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002168 : HInstruction(side_effects, dex_pc) {}
2169
Vladimir Markof9f64412015-09-02 14:05:49 +01002170 virtual ~HTemplateInstruction() {}
2171
2172 size_t InputCount() const OVERRIDE { return 0; }
2173
2174 protected:
2175 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2176 LOG(FATAL) << "Unreachable";
2177 UNREACHABLE();
2178 }
2179
2180 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2181 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2182 LOG(FATAL) << "Unreachable";
2183 UNREACHABLE();
2184 }
2185
2186 private:
2187 friend class SsaBuilder;
2188};
2189
Dave Allison20dfc792014-06-16 20:44:29 -07002190template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002191class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002192 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002193 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002194 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002195 virtual ~HExpression() {}
2196
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002197 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002198
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002199 protected:
2200 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002201};
2202
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002203// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2204// instruction that branches to the exit block.
2205class HReturnVoid : public HTemplateInstruction<0> {
2206 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002207 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2208 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002209
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002210 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002211
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002212 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213
2214 private:
2215 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2216};
2217
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002218// Represents dex's RETURN opcodes. A HReturn is a control flow
2219// instruction that branches to the exit block.
2220class HReturn : public HTemplateInstruction<1> {
2221 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002222 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2223 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002224 SetRawInputAt(0, value);
2225 }
2226
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002227 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002228
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002229 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002230
2231 private:
2232 DISALLOW_COPY_AND_ASSIGN(HReturn);
2233};
2234
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002235// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002236// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002237// exit block.
2238class HExit : public HTemplateInstruction<0> {
2239 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002240 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002241
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002242 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002243
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002244 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002245
2246 private:
2247 DISALLOW_COPY_AND_ASSIGN(HExit);
2248};
2249
2250// Jumps from one block to another.
2251class HGoto : public HTemplateInstruction<0> {
2252 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002253 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002254
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002255 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002256
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002257 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002258 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002259 }
2260
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002261 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002262
2263 private:
2264 DISALLOW_COPY_AND_ASSIGN(HGoto);
2265};
2266
Roland Levillain9867bc72015-08-05 10:21:34 +01002267class HConstant : public HExpression<0> {
2268 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002269 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2270 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002271
2272 bool CanBeMoved() const OVERRIDE { return true; }
2273
2274 virtual bool IsMinusOne() const { return false; }
2275 virtual bool IsZero() const { return false; }
2276 virtual bool IsOne() const { return false; }
2277
David Brazdil77a48ae2015-09-15 12:34:04 +00002278 virtual uint64_t GetValueAsUint64() const = 0;
2279
Roland Levillain9867bc72015-08-05 10:21:34 +01002280 DECLARE_INSTRUCTION(Constant);
2281
2282 private:
2283 DISALLOW_COPY_AND_ASSIGN(HConstant);
2284};
2285
2286class HNullConstant : public HConstant {
2287 public:
2288 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2289 return true;
2290 }
2291
David Brazdil77a48ae2015-09-15 12:34:04 +00002292 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2293
Roland Levillain9867bc72015-08-05 10:21:34 +01002294 size_t ComputeHashCode() const OVERRIDE { return 0; }
2295
2296 DECLARE_INSTRUCTION(NullConstant);
2297
2298 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002299 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002300
2301 friend class HGraph;
2302 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2303};
2304
2305// Constants of the type int. Those can be from Dex instructions, or
2306// synthesized (for example with the if-eqz instruction).
2307class HIntConstant : public HConstant {
2308 public:
2309 int32_t GetValue() const { return value_; }
2310
David Brazdil9f389d42015-10-01 14:32:56 +01002311 uint64_t GetValueAsUint64() const OVERRIDE {
2312 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2313 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002314
Roland Levillain9867bc72015-08-05 10:21:34 +01002315 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2316 DCHECK(other->IsIntConstant());
2317 return other->AsIntConstant()->value_ == value_;
2318 }
2319
2320 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2321
2322 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2323 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2324 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2325
2326 DECLARE_INSTRUCTION(IntConstant);
2327
2328 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002329 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2330 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2331 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2332 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002333
2334 const int32_t value_;
2335
2336 friend class HGraph;
2337 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2338 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2339 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2340};
2341
2342class HLongConstant : public HConstant {
2343 public:
2344 int64_t GetValue() const { return value_; }
2345
David Brazdil77a48ae2015-09-15 12:34:04 +00002346 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2347
Roland Levillain9867bc72015-08-05 10:21:34 +01002348 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2349 DCHECK(other->IsLongConstant());
2350 return other->AsLongConstant()->value_ == value_;
2351 }
2352
2353 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2354
2355 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2356 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2357 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2358
2359 DECLARE_INSTRUCTION(LongConstant);
2360
2361 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002362 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2363 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002364
2365 const int64_t value_;
2366
2367 friend class HGraph;
2368 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2369};
Dave Allison20dfc792014-06-16 20:44:29 -07002370
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002371// Conditional branch. A block ending with an HIf instruction must have
2372// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002373class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002374 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002375 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2376 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002377 SetRawInputAt(0, input);
2378 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002379
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002380 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002381
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002382 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002383 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002384 }
2385
2386 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002387 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002388 }
2389
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002390 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002391
2392 private:
2393 DISALLOW_COPY_AND_ASSIGN(HIf);
2394};
2395
David Brazdilfc6a86a2015-06-26 10:33:45 +00002396
2397// Abstract instruction which marks the beginning and/or end of a try block and
2398// links it to the respective exception handlers. Behaves the same as a Goto in
2399// non-exceptional control flow.
2400// Normal-flow successor is stored at index zero, exception handlers under
2401// higher indices in no particular order.
2402class HTryBoundary : public HTemplateInstruction<0> {
2403 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002404 enum BoundaryKind {
2405 kEntry,
2406 kExit,
2407 };
2408
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002409 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2410 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002411
2412 bool IsControlFlow() const OVERRIDE { return true; }
2413
2414 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002415 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002416
David Brazdild26a4112015-11-10 11:07:31 +00002417 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2418 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2419 }
2420
David Brazdilfc6a86a2015-06-26 10:33:45 +00002421 // Returns whether `handler` is among its exception handlers (non-zero index
2422 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002423 bool HasExceptionHandler(const HBasicBlock& handler) const {
2424 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002425 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002426 }
2427
2428 // If not present already, adds `handler` to its block's list of exception
2429 // handlers.
2430 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002431 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002432 GetBlock()->AddSuccessor(handler);
2433 }
2434 }
2435
David Brazdil56e1acc2015-06-30 15:41:36 +01002436 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002437
David Brazdilffee3d32015-07-06 11:48:53 +01002438 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2439
David Brazdilfc6a86a2015-06-26 10:33:45 +00002440 DECLARE_INSTRUCTION(TryBoundary);
2441
2442 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002443 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002444
2445 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2446};
2447
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002448// Deoptimize to interpreter, upon checking a condition.
2449class HDeoptimize : public HTemplateInstruction<1> {
2450 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002451 explicit HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2452 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002453 SetRawInputAt(0, cond);
2454 }
2455
2456 bool NeedsEnvironment() const OVERRIDE { return true; }
2457 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002458
2459 DECLARE_INSTRUCTION(Deoptimize);
2460
2461 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002462 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2463};
2464
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002465// Represents the ArtMethod that was passed as a first argument to
2466// the method. It is used by instructions that depend on it, like
2467// instructions that work with the dex cache.
2468class HCurrentMethod : public HExpression<0> {
2469 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002470 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2471 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002472
2473 DECLARE_INSTRUCTION(CurrentMethod);
2474
2475 private:
2476 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2477};
2478
Mark Mendellfe57faa2015-09-18 09:26:15 -04002479// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2480// have one successor for each entry in the switch table, and the final successor
2481// will be the block containing the next Dex opcode.
2482class HPackedSwitch : public HTemplateInstruction<1> {
2483 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002484 HPackedSwitch(int32_t start_value,
2485 uint32_t num_entries,
2486 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002487 uint32_t dex_pc = kNoDexPc)
2488 : HTemplateInstruction(SideEffects::None(), dex_pc),
2489 start_value_(start_value),
2490 num_entries_(num_entries) {
2491 SetRawInputAt(0, input);
2492 }
2493
2494 bool IsControlFlow() const OVERRIDE { return true; }
2495
2496 int32_t GetStartValue() const { return start_value_; }
2497
Vladimir Marko211c2112015-09-24 16:52:33 +01002498 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002499
2500 HBasicBlock* GetDefaultBlock() const {
2501 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002502 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002503 }
2504 DECLARE_INSTRUCTION(PackedSwitch);
2505
2506 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002507 const int32_t start_value_;
2508 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002509
2510 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2511};
2512
Roland Levillain88cb1752014-10-20 16:36:47 +01002513class HUnaryOperation : public HExpression<1> {
2514 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002515 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2516 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002517 SetRawInputAt(0, input);
2518 }
2519
2520 HInstruction* GetInput() const { return InputAt(0); }
2521 Primitive::Type GetResultType() const { return GetType(); }
2522
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002523 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002524 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002525 return true;
2526 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002527
Roland Levillain9240d6a2014-10-20 16:47:04 +01002528 // Try to statically evaluate `operation` and return a HConstant
2529 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002530 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002531 HConstant* TryStaticEvaluation() const;
2532
2533 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002534 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2535 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002536
Roland Levillain88cb1752014-10-20 16:36:47 +01002537 DECLARE_INSTRUCTION(UnaryOperation);
2538
2539 private:
2540 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2541};
2542
Dave Allison20dfc792014-06-16 20:44:29 -07002543class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002544 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002545 HBinaryOperation(Primitive::Type result_type,
2546 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002547 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002548 SideEffects side_effects = SideEffects::None(),
2549 uint32_t dex_pc = kNoDexPc)
2550 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002551 SetRawInputAt(0, left);
2552 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002553 }
2554
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002555 HInstruction* GetLeft() const { return InputAt(0); }
2556 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002557 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002558
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002559 virtual bool IsCommutative() const { return false; }
2560
2561 // Put constant on the right.
2562 // Returns whether order is changed.
2563 bool OrderInputsWithConstantOnTheRight() {
2564 HInstruction* left = InputAt(0);
2565 HInstruction* right = InputAt(1);
2566 if (left->IsConstant() && !right->IsConstant()) {
2567 ReplaceInput(right, 0);
2568 ReplaceInput(left, 1);
2569 return true;
2570 }
2571 return false;
2572 }
2573
2574 // Order inputs by instruction id, but favor constant on the right side.
2575 // This helps GVN for commutative ops.
2576 void OrderInputs() {
2577 DCHECK(IsCommutative());
2578 HInstruction* left = InputAt(0);
2579 HInstruction* right = InputAt(1);
2580 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2581 return;
2582 }
2583 if (OrderInputsWithConstantOnTheRight()) {
2584 return;
2585 }
2586 // Order according to instruction id.
2587 if (left->GetId() > right->GetId()) {
2588 ReplaceInput(right, 0);
2589 ReplaceInput(left, 1);
2590 }
2591 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002592
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002593 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002594 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002595 return true;
2596 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002597
Roland Levillain9240d6a2014-10-20 16:47:04 +01002598 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002599 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002600 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002601 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002602
2603 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002604 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2605 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2606 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2607 HLongConstant* y ATTRIBUTE_UNUSED) const {
2608 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2609 return nullptr;
2610 }
2611 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2612 HIntConstant* y ATTRIBUTE_UNUSED) const {
2613 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2614 return nullptr;
2615 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002616 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2617 HNullConstant* y ATTRIBUTE_UNUSED) const {
2618 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2619 return nullptr;
2620 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002621
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002622 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002623 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002624 HConstant* GetConstantRight() const;
2625
2626 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002627 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002628 HInstruction* GetLeastConstantLeft() const;
2629
Roland Levillainccc07a92014-09-16 14:48:16 +01002630 DECLARE_INSTRUCTION(BinaryOperation);
2631
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002632 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002633 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2634};
2635
Mark Mendellc4701932015-04-10 13:18:51 -04002636// The comparison bias applies for floating point operations and indicates how NaN
2637// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002638enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002639 kNoBias, // bias is not applicable (i.e. for long operation)
2640 kGtBias, // return 1 for NaN comparisons
2641 kLtBias, // return -1 for NaN comparisons
2642};
2643
Dave Allison20dfc792014-06-16 20:44:29 -07002644class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002645 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002646 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2647 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002648 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002649 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002650
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002651 bool NeedsMaterialization() const { return needs_materialization_; }
2652 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002653
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002654 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002655 // `instruction`, and disregard moves in between.
2656 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002657
Dave Allison20dfc792014-06-16 20:44:29 -07002658 DECLARE_INSTRUCTION(Condition);
2659
2660 virtual IfCondition GetCondition() const = 0;
2661
Mark Mendellc4701932015-04-10 13:18:51 -04002662 virtual IfCondition GetOppositeCondition() const = 0;
2663
Roland Levillain4fa13f62015-07-06 18:11:54 +01002664 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002665
2666 void SetBias(ComparisonBias bias) { bias_ = bias; }
2667
2668 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2669 return bias_ == other->AsCondition()->bias_;
2670 }
2671
Roland Levillain4fa13f62015-07-06 18:11:54 +01002672 bool IsFPConditionTrueIfNaN() const {
2673 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2674 IfCondition if_cond = GetCondition();
2675 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2676 }
2677
2678 bool IsFPConditionFalseIfNaN() const {
2679 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2680 IfCondition if_cond = GetCondition();
2681 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2682 }
2683
Dave Allison20dfc792014-06-16 20:44:29 -07002684 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002685 // For register allocation purposes, returns whether this instruction needs to be
2686 // materialized (that is, not just be in the processor flags).
2687 bool needs_materialization_;
2688
Mark Mendellc4701932015-04-10 13:18:51 -04002689 // Needed if we merge a HCompare into a HCondition.
2690 ComparisonBias bias_;
2691
Dave Allison20dfc792014-06-16 20:44:29 -07002692 DISALLOW_COPY_AND_ASSIGN(HCondition);
2693};
2694
2695// Instruction to check if two inputs are equal to each other.
2696class HEqual : public HCondition {
2697 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002698 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2699 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002700
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002701 bool IsCommutative() const OVERRIDE { return true; }
2702
Roland Levillain9867bc72015-08-05 10:21:34 +01002703 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002704 return GetBlock()->GetGraph()->GetIntConstant(
2705 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002706 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002707 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002708 return GetBlock()->GetGraph()->GetIntConstant(
2709 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002710 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002711 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2712 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002713 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002714 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002715
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002716 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002717
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002718 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002719 return kCondEQ;
2720 }
2721
Mark Mendellc4701932015-04-10 13:18:51 -04002722 IfCondition GetOppositeCondition() const OVERRIDE {
2723 return kCondNE;
2724 }
2725
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002726 private:
Aart Bike9f37602015-10-09 11:15:55 -07002727 template <typename T> bool Compute(T x, T y) const { return x == y; }
2728
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002729 DISALLOW_COPY_AND_ASSIGN(HEqual);
2730};
2731
Dave Allison20dfc792014-06-16 20:44:29 -07002732class HNotEqual : public HCondition {
2733 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002734 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2735 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002736
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002737 bool IsCommutative() const OVERRIDE { return true; }
2738
Roland Levillain9867bc72015-08-05 10:21:34 +01002739 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002740 return GetBlock()->GetGraph()->GetIntConstant(
2741 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002742 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002743 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002744 return GetBlock()->GetGraph()->GetIntConstant(
2745 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002746 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002747 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2748 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002749 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002750 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002751
Dave Allison20dfc792014-06-16 20:44:29 -07002752 DECLARE_INSTRUCTION(NotEqual);
2753
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002754 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002755 return kCondNE;
2756 }
2757
Mark Mendellc4701932015-04-10 13:18:51 -04002758 IfCondition GetOppositeCondition() const OVERRIDE {
2759 return kCondEQ;
2760 }
2761
Dave Allison20dfc792014-06-16 20:44:29 -07002762 private:
Aart Bike9f37602015-10-09 11:15:55 -07002763 template <typename T> bool Compute(T x, T y) const { return x != y; }
2764
Dave Allison20dfc792014-06-16 20:44:29 -07002765 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2766};
2767
2768class HLessThan : public HCondition {
2769 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002770 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2771 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002772
Roland Levillain9867bc72015-08-05 10:21:34 +01002773 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002774 return GetBlock()->GetGraph()->GetIntConstant(
2775 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002776 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002777 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002778 return GetBlock()->GetGraph()->GetIntConstant(
2779 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002780 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002781
Dave Allison20dfc792014-06-16 20:44:29 -07002782 DECLARE_INSTRUCTION(LessThan);
2783
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002784 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002785 return kCondLT;
2786 }
2787
Mark Mendellc4701932015-04-10 13:18:51 -04002788 IfCondition GetOppositeCondition() const OVERRIDE {
2789 return kCondGE;
2790 }
2791
Dave Allison20dfc792014-06-16 20:44:29 -07002792 private:
Aart Bike9f37602015-10-09 11:15:55 -07002793 template <typename T> bool Compute(T x, T y) const { return x < y; }
2794
Dave Allison20dfc792014-06-16 20:44:29 -07002795 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2796};
2797
2798class HLessThanOrEqual : public HCondition {
2799 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002800 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2801 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002802
Roland Levillain9867bc72015-08-05 10:21:34 +01002803 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002804 return GetBlock()->GetGraph()->GetIntConstant(
2805 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002806 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002807 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002808 return GetBlock()->GetGraph()->GetIntConstant(
2809 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002810 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002811
Dave Allison20dfc792014-06-16 20:44:29 -07002812 DECLARE_INSTRUCTION(LessThanOrEqual);
2813
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002814 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002815 return kCondLE;
2816 }
2817
Mark Mendellc4701932015-04-10 13:18:51 -04002818 IfCondition GetOppositeCondition() const OVERRIDE {
2819 return kCondGT;
2820 }
2821
Dave Allison20dfc792014-06-16 20:44:29 -07002822 private:
Aart Bike9f37602015-10-09 11:15:55 -07002823 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2824
Dave Allison20dfc792014-06-16 20:44:29 -07002825 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2826};
2827
2828class HGreaterThan : public HCondition {
2829 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002830 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2831 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002832
Roland Levillain9867bc72015-08-05 10:21:34 +01002833 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002834 return GetBlock()->GetGraph()->GetIntConstant(
2835 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002836 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002837 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002838 return GetBlock()->GetGraph()->GetIntConstant(
2839 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002840 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002841
Dave Allison20dfc792014-06-16 20:44:29 -07002842 DECLARE_INSTRUCTION(GreaterThan);
2843
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002844 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002845 return kCondGT;
2846 }
2847
Mark Mendellc4701932015-04-10 13:18:51 -04002848 IfCondition GetOppositeCondition() const OVERRIDE {
2849 return kCondLE;
2850 }
2851
Dave Allison20dfc792014-06-16 20:44:29 -07002852 private:
Aart Bike9f37602015-10-09 11:15:55 -07002853 template <typename T> bool Compute(T x, T y) const { return x > y; }
2854
Dave Allison20dfc792014-06-16 20:44:29 -07002855 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2856};
2857
2858class HGreaterThanOrEqual : public HCondition {
2859 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002860 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2861 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002862
Roland Levillain9867bc72015-08-05 10:21:34 +01002863 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002864 return GetBlock()->GetGraph()->GetIntConstant(
2865 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002866 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002867 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002868 return GetBlock()->GetGraph()->GetIntConstant(
2869 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002870 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002871
Dave Allison20dfc792014-06-16 20:44:29 -07002872 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2873
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002874 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002875 return kCondGE;
2876 }
2877
Mark Mendellc4701932015-04-10 13:18:51 -04002878 IfCondition GetOppositeCondition() const OVERRIDE {
2879 return kCondLT;
2880 }
2881
Dave Allison20dfc792014-06-16 20:44:29 -07002882 private:
Aart Bike9f37602015-10-09 11:15:55 -07002883 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2884
Dave Allison20dfc792014-06-16 20:44:29 -07002885 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2886};
2887
Aart Bike9f37602015-10-09 11:15:55 -07002888class HBelow : public HCondition {
2889 public:
2890 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2891 : HCondition(first, second, dex_pc) {}
2892
2893 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2894 return GetBlock()->GetGraph()->GetIntConstant(
2895 Compute(static_cast<uint32_t>(x->GetValue()),
2896 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2897 }
2898 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2899 return GetBlock()->GetGraph()->GetIntConstant(
2900 Compute(static_cast<uint64_t>(x->GetValue()),
2901 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2902 }
2903
2904 DECLARE_INSTRUCTION(Below);
2905
2906 IfCondition GetCondition() const OVERRIDE {
2907 return kCondB;
2908 }
2909
2910 IfCondition GetOppositeCondition() const OVERRIDE {
2911 return kCondAE;
2912 }
2913
2914 private:
2915 template <typename T> bool Compute(T x, T y) const { return x < y; }
2916
2917 DISALLOW_COPY_AND_ASSIGN(HBelow);
2918};
2919
2920class HBelowOrEqual : public HCondition {
2921 public:
2922 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2923 : HCondition(first, second, dex_pc) {}
2924
2925 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2926 return GetBlock()->GetGraph()->GetIntConstant(
2927 Compute(static_cast<uint32_t>(x->GetValue()),
2928 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2929 }
2930 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2931 return GetBlock()->GetGraph()->GetIntConstant(
2932 Compute(static_cast<uint64_t>(x->GetValue()),
2933 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2934 }
2935
2936 DECLARE_INSTRUCTION(BelowOrEqual);
2937
2938 IfCondition GetCondition() const OVERRIDE {
2939 return kCondBE;
2940 }
2941
2942 IfCondition GetOppositeCondition() const OVERRIDE {
2943 return kCondA;
2944 }
2945
2946 private:
2947 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2948
2949 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
2950};
2951
2952class HAbove : public HCondition {
2953 public:
2954 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2955 : HCondition(first, second, dex_pc) {}
2956
2957 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2958 return GetBlock()->GetGraph()->GetIntConstant(
2959 Compute(static_cast<uint32_t>(x->GetValue()),
2960 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2961 }
2962 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2963 return GetBlock()->GetGraph()->GetIntConstant(
2964 Compute(static_cast<uint64_t>(x->GetValue()),
2965 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2966 }
2967
2968 DECLARE_INSTRUCTION(Above);
2969
2970 IfCondition GetCondition() const OVERRIDE {
2971 return kCondA;
2972 }
2973
2974 IfCondition GetOppositeCondition() const OVERRIDE {
2975 return kCondBE;
2976 }
2977
2978 private:
2979 template <typename T> bool Compute(T x, T y) const { return x > y; }
2980
2981 DISALLOW_COPY_AND_ASSIGN(HAbove);
2982};
2983
2984class HAboveOrEqual : public HCondition {
2985 public:
2986 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2987 : HCondition(first, second, dex_pc) {}
2988
2989 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2990 return GetBlock()->GetGraph()->GetIntConstant(
2991 Compute(static_cast<uint32_t>(x->GetValue()),
2992 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2993 }
2994 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2995 return GetBlock()->GetGraph()->GetIntConstant(
2996 Compute(static_cast<uint64_t>(x->GetValue()),
2997 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2998 }
2999
3000 DECLARE_INSTRUCTION(AboveOrEqual);
3001
3002 IfCondition GetCondition() const OVERRIDE {
3003 return kCondAE;
3004 }
3005
3006 IfCondition GetOppositeCondition() const OVERRIDE {
3007 return kCondB;
3008 }
3009
3010 private:
3011 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3012
3013 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3014};
Dave Allison20dfc792014-06-16 20:44:29 -07003015
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003016// Instruction to check how two inputs compare to each other.
3017// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3018class HCompare : public HBinaryOperation {
3019 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003020 HCompare(Primitive::Type type,
3021 HInstruction* first,
3022 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003023 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003024 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003025 : HBinaryOperation(Primitive::kPrimInt,
3026 first,
3027 second,
3028 SideEffectsForArchRuntimeCalls(type),
3029 dex_pc),
3030 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003031 DCHECK_EQ(type, first->GetType());
3032 DCHECK_EQ(type, second->GetType());
3033 }
3034
Roland Levillain9867bc72015-08-05 10:21:34 +01003035 template <typename T>
3036 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003037
Roland Levillain9867bc72015-08-05 10:21:34 +01003038 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003039 return GetBlock()->GetGraph()->GetIntConstant(
3040 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003041 }
3042 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003043 return GetBlock()->GetGraph()->GetIntConstant(
3044 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003045 }
3046
Calin Juravleddb7df22014-11-25 20:56:51 +00003047 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3048 return bias_ == other->AsCompare()->bias_;
3049 }
3050
Mark Mendellc4701932015-04-10 13:18:51 -04003051 ComparisonBias GetBias() const { return bias_; }
3052
Roland Levillain4fa13f62015-07-06 18:11:54 +01003053 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003054
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003055
3056 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3057 // MIPS64 uses a runtime call for FP comparisons.
3058 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3059 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003060
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003061 DECLARE_INSTRUCTION(Compare);
3062
3063 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003064 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003065
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003066 DISALLOW_COPY_AND_ASSIGN(HCompare);
3067};
3068
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003069// A local in the graph. Corresponds to a Dex register.
3070class HLocal : public HTemplateInstruction<0> {
3071 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003072 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003073 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003074
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003075 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003076
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003077 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003078
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003079 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003080 // The Dex register number.
3081 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003082
3083 DISALLOW_COPY_AND_ASSIGN(HLocal);
3084};
3085
3086// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003087class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003088 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003089 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3090 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003091 SetRawInputAt(0, local);
3092 }
3093
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003094 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3095
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003096 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003097
3098 private:
3099 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3100};
3101
3102// Store a value in a given local. This instruction has two inputs: the value
3103// and the local.
3104class HStoreLocal : public HTemplateInstruction<2> {
3105 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003106 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3107 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003108 SetRawInputAt(0, local);
3109 SetRawInputAt(1, value);
3110 }
3111
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003112 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3113
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003114 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003115
3116 private:
3117 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3118};
3119
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003120class HFloatConstant : public HConstant {
3121 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003122 float GetValue() const { return value_; }
3123
David Brazdil77a48ae2015-09-15 12:34:04 +00003124 uint64_t GetValueAsUint64() const OVERRIDE {
3125 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3126 }
3127
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003128 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003129 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003130 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003131 }
3132
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003133 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003134
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003135 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003136 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003137 }
3138 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003139 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003140 }
3141 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003142 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3143 }
3144 bool IsNaN() const {
3145 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003146 }
3147
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003148 DECLARE_INSTRUCTION(FloatConstant);
3149
3150 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003151 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3152 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3153 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3154 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003155
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003156 const float value_;
3157
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003158 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003159 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003160 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003161 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3162};
3163
3164class HDoubleConstant : public HConstant {
3165 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003166 double GetValue() const { return value_; }
3167
David Brazdil77a48ae2015-09-15 12:34:04 +00003168 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3169
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003170 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003171 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003172 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003173 }
3174
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003175 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003176
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003177 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003178 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003179 }
3180 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003181 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003182 }
3183 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003184 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3185 }
3186 bool IsNaN() const {
3187 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003188 }
3189
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003190 DECLARE_INSTRUCTION(DoubleConstant);
3191
3192 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003193 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3194 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3195 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3196 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003197
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003198 const double value_;
3199
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003200 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003201 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003202 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003203 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3204};
3205
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003206enum class Intrinsics {
Agi Csaki05f20562015-08-19 14:58:14 -07003207#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003208#include "intrinsics_list.h"
3209 kNone,
3210 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3211#undef INTRINSICS_LIST
3212#undef OPTIMIZING_INTRINSICS
3213};
3214std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3215
Agi Csaki05f20562015-08-19 14:58:14 -07003216enum IntrinsicNeedsEnvironmentOrCache {
3217 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3218 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003219};
3220
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003221class HInvoke : public HInstruction {
3222 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003223 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003224
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003225 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003226
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003227 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003228 SetRawInputAt(index, argument);
3229 }
3230
Roland Levillain3e3d7332015-04-28 11:00:54 +01003231 // Return the number of arguments. This number can be lower than
3232 // the number of inputs returned by InputCount(), as some invoke
3233 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3234 // inputs at the end of their list of inputs.
3235 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3236
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003237 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003238
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003239
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003240 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003241 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003242
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003243 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3244
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003245 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003246 return intrinsic_;
3247 }
3248
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003249 void SetIntrinsic(Intrinsics intrinsic, IntrinsicNeedsEnvironmentOrCache needs_env_or_cache);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003250
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003251 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003252 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003253 }
3254
3255 bool CanThrow() const OVERRIDE { return true; }
3256
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003257 uint32_t* GetIntrinsicOptimizations() {
3258 return &intrinsic_optimizations_;
3259 }
3260
3261 const uint32_t* GetIntrinsicOptimizations() const {
3262 return &intrinsic_optimizations_;
3263 }
3264
3265 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3266
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003267 DECLARE_INSTRUCTION(Invoke);
3268
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003269 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003270 HInvoke(ArenaAllocator* arena,
3271 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003272 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003273 Primitive::Type return_type,
3274 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003275 uint32_t dex_method_index,
3276 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003277 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003278 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003279 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003280 inputs_(number_of_arguments + number_of_other_inputs,
3281 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003282 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003283 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003284 original_invoke_type_(original_invoke_type),
agicsaki57b81ec2015-08-11 17:39:37 -07003285 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003286 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003287 }
3288
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003289 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003290 return inputs_[index];
3291 }
3292
David Brazdil1abb4192015-02-17 18:33:36 +00003293 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003294 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003295 }
3296
Roland Levillain3e3d7332015-04-28 11:00:54 +01003297 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003298 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003299 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003300 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003301 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003302 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003303
3304 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3305 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003306
3307 private:
3308 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3309};
3310
Calin Juravle175dc732015-08-25 15:42:32 +01003311class HInvokeUnresolved : public HInvoke {
3312 public:
3313 HInvokeUnresolved(ArenaAllocator* arena,
3314 uint32_t number_of_arguments,
3315 Primitive::Type return_type,
3316 uint32_t dex_pc,
3317 uint32_t dex_method_index,
3318 InvokeType invoke_type)
3319 : HInvoke(arena,
3320 number_of_arguments,
3321 0u /* number_of_other_inputs */,
3322 return_type,
3323 dex_pc,
3324 dex_method_index,
3325 invoke_type) {
3326 }
3327
3328 DECLARE_INSTRUCTION(InvokeUnresolved);
3329
3330 private:
3331 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3332};
3333
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003334class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003335 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003336 // Requirements of this method call regarding the class
3337 // initialization (clinit) check of its declaring class.
3338 enum class ClinitCheckRequirement {
3339 kNone, // Class already initialized.
3340 kExplicit, // Static call having explicit clinit check as last input.
3341 kImplicit, // Static call implicitly requiring a clinit check.
3342 };
3343
Vladimir Marko58155012015-08-19 12:49:41 +00003344 // Determines how to load the target ArtMethod*.
3345 enum class MethodLoadKind {
3346 // Use a String init ArtMethod* loaded from Thread entrypoints.
3347 kStringInit,
3348
3349 // Use the method's own ArtMethod* loaded by the register allocator.
3350 kRecursive,
3351
3352 // Use ArtMethod* at a known address, embed the direct address in the code.
3353 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3354 kDirectAddress,
3355
3356 // Use ArtMethod* at an address that will be known at link time, embed the direct
3357 // address in the code. If the image is relocatable, emit .patch_oat entry.
3358 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3359 // the image relocatable or not.
3360 kDirectAddressWithFixup,
3361
3362 // Load from resoved methods array in the dex cache using a PC-relative load.
3363 // Used when we need to use the dex cache, for example for invoke-static that
3364 // may cause class initialization (the entry may point to a resolution method),
3365 // and we know that we can access the dex cache arrays using a PC-relative load.
3366 kDexCachePcRelative,
3367
3368 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3369 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3370 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3371 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3372 kDexCacheViaMethod,
3373 };
3374
3375 // Determines the location of the code pointer.
3376 enum class CodePtrLocation {
3377 // Recursive call, use local PC-relative call instruction.
3378 kCallSelf,
3379
3380 // Use PC-relative call instruction patched at link time.
3381 // Used for calls within an oat file, boot->boot or app->app.
3382 kCallPCRelative,
3383
3384 // Call to a known target address, embed the direct address in code.
3385 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3386 kCallDirect,
3387
3388 // Call to a target address that will be known at link time, embed the direct
3389 // address in code. If the image is relocatable, emit .patch_oat entry.
3390 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3391 // the image relocatable or not.
3392 kCallDirectWithFixup,
3393
3394 // Use code pointer from the ArtMethod*.
3395 // Used when we don't know the target code. This is also the last-resort-kind used when
3396 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3397 kCallArtMethod,
3398 };
3399
3400 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003401 MethodLoadKind method_load_kind;
3402 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003403 // The method load data holds
3404 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3405 // Note that there are multiple string init methods, each having its own offset.
3406 // - the method address for kDirectAddress
3407 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003408 uint64_t method_load_data;
3409 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003410 };
3411
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003412 HInvokeStaticOrDirect(ArenaAllocator* arena,
3413 uint32_t number_of_arguments,
3414 Primitive::Type return_type,
3415 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003416 uint32_t method_index,
3417 MethodReference target_method,
3418 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003419 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003420 InvokeType invoke_type,
3421 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003422 : HInvoke(arena,
3423 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003424 // There is potentially one extra argument for the HCurrentMethod node, and
3425 // potentially one other if the clinit check is explicit, and potentially
3426 // one other if the method is a string factory.
3427 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3428 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3429 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003430 return_type,
3431 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003432 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003433 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003434 invoke_type_(invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003435 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003436 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003437 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003438
Vladimir Markodc151b22015-10-15 18:02:30 +01003439 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003440 bool had_current_method_input = HasCurrentMethodInput();
3441 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3442
3443 // Using the current method is the default and once we find a better
3444 // method load kind, we should not go back to using the current method.
3445 DCHECK(had_current_method_input || !needs_current_method_input);
3446
3447 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003448 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3449 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003450 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003451 dispatch_info_ = dispatch_info;
3452 }
3453
Vladimir Markoc53c0792015-11-19 15:48:33 +00003454 void AddSpecialInput(HInstruction* input) {
3455 // We allow only one special input.
3456 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3457 DCHECK(InputCount() == GetSpecialInputIndex() ||
3458 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3459 InsertInputAt(GetSpecialInputIndex(), input);
3460 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003461
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003462 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003463 // We access the method via the dex cache so we can't do an implicit null check.
3464 // TODO: for intrinsics we can generate implicit null checks.
3465 return false;
3466 }
3467
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003468 bool CanBeNull() const OVERRIDE {
3469 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3470 }
3471
Vladimir Markoc53c0792015-11-19 15:48:33 +00003472 // Get the index of the special input, if any.
3473 //
3474 // If the invoke IsStringInit(), it initially has a HFakeString special argument
3475 // which is removed by the instruction simplifier; if the invoke HasCurrentMethodInput(),
3476 // the "special input" is the current method pointer; otherwise there may be one
3477 // platform-specific special input, such as PC-relative addressing base.
3478 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3479
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003480 InvokeType GetInvokeType() const { return invoke_type_; }
Vladimir Marko58155012015-08-19 12:49:41 +00003481 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3482 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3483 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003484 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003485 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003486 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003487 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003488 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3489 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003490 bool HasCurrentMethodInput() const {
3491 // This function can be called only after the invoke has been fully initialized by the builder.
3492 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003493 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003494 return true;
3495 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003496 DCHECK(InputCount() == GetSpecialInputIndex() ||
3497 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003498 return false;
3499 }
3500 }
Vladimir Marko58155012015-08-19 12:49:41 +00003501 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3502 MethodReference GetTargetMethod() const { return target_method_; }
3503
3504 int32_t GetStringInitOffset() const {
3505 DCHECK(IsStringInit());
3506 return dispatch_info_.method_load_data;
3507 }
3508
3509 uint64_t GetMethodAddress() const {
3510 DCHECK(HasMethodAddress());
3511 return dispatch_info_.method_load_data;
3512 }
3513
3514 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003515 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003516 return dispatch_info_.method_load_data;
3517 }
3518
3519 uint64_t GetDirectCodePtr() const {
3520 DCHECK(HasDirectCodePtr());
3521 return dispatch_info_.direct_code_ptr;
3522 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003523
Calin Juravle68ad6492015-08-18 17:08:12 +01003524 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3525
Roland Levillain4c0eb422015-04-24 16:43:49 +01003526 // Is this instruction a call to a static method?
3527 bool IsStatic() const {
3528 return GetInvokeType() == kStatic;
3529 }
3530
Vladimir Markofbb184a2015-11-13 14:47:00 +00003531 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3532 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3533 // instruction; only relevant for static calls with explicit clinit check.
3534 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003535 DCHECK(IsStaticWithExplicitClinitCheck());
3536 size_t last_input_index = InputCount() - 1;
3537 HInstruction* last_input = InputAt(last_input_index);
3538 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003539 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003540 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003541 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003542 clinit_check_requirement_ = new_requirement;
3543 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003544 }
3545
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003546 bool IsStringFactoryFor(HFakeString* str) const {
3547 if (!IsStringInit()) return false;
Vladimir Markob554b5a2015-11-06 12:57:55 +00003548 DCHECK(!HasCurrentMethodInput());
3549 if (InputCount() == (number_of_arguments_)) return false;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003550 return InputAt(InputCount() - 1)->AsFakeString() == str;
3551 }
3552
3553 void RemoveFakeStringArgumentAsLastInput() {
3554 DCHECK(IsStringInit());
3555 size_t last_input_index = InputCount() - 1;
3556 HInstruction* last_input = InputAt(last_input_index);
3557 DCHECK(last_input != nullptr);
3558 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
3559 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003560 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003561 }
3562
Roland Levillain4c0eb422015-04-24 16:43:49 +01003563 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003564 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003565 bool IsStaticWithExplicitClinitCheck() const {
3566 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3567 }
3568
3569 // Is this a call to a static method whose declaring class has an
3570 // implicit intialization check requirement?
3571 bool IsStaticWithImplicitClinitCheck() const {
3572 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3573 }
3574
Vladimir Markob554b5a2015-11-06 12:57:55 +00003575 // Does this method load kind need the current method as an input?
3576 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3577 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3578 }
3579
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003580 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003581
Roland Levillain4c0eb422015-04-24 16:43:49 +01003582 protected:
3583 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3584 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3585 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3586 HInstruction* input = input_record.GetInstruction();
3587 // `input` is the last input of a static invoke marked as having
3588 // an explicit clinit check. It must either be:
3589 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3590 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3591 DCHECK(input != nullptr);
3592 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3593 }
3594 return input_record;
3595 }
3596
Vladimir Markoc53c0792015-11-19 15:48:33 +00003597 void InsertInputAt(size_t index, HInstruction* input);
3598 void RemoveInputAt(size_t index);
3599
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003600 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003601 const InvokeType invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003602 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003603 // The target method may refer to different dex file or method index than the original
3604 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3605 // in derived class to increase visibility.
3606 MethodReference target_method_;
3607 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003608
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003609 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003610};
Vladimir Markofbb184a2015-11-13 14:47:00 +00003611std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003612
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003613class HInvokeVirtual : public HInvoke {
3614 public:
3615 HInvokeVirtual(ArenaAllocator* arena,
3616 uint32_t number_of_arguments,
3617 Primitive::Type return_type,
3618 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003619 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003620 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003621 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003622 vtable_index_(vtable_index) {}
3623
Calin Juravle641547a2015-04-21 22:08:51 +01003624 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003625 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003626 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003627 }
3628
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003629 uint32_t GetVTableIndex() const { return vtable_index_; }
3630
3631 DECLARE_INSTRUCTION(InvokeVirtual);
3632
3633 private:
3634 const uint32_t vtable_index_;
3635
3636 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3637};
3638
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003639class HInvokeInterface : public HInvoke {
3640 public:
3641 HInvokeInterface(ArenaAllocator* arena,
3642 uint32_t number_of_arguments,
3643 Primitive::Type return_type,
3644 uint32_t dex_pc,
3645 uint32_t dex_method_index,
3646 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003647 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003648 imt_index_(imt_index) {}
3649
Calin Juravle641547a2015-04-21 22:08:51 +01003650 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003651 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003652 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003653 }
3654
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003655 uint32_t GetImtIndex() const { return imt_index_; }
3656 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3657
3658 DECLARE_INSTRUCTION(InvokeInterface);
3659
3660 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003661 const uint32_t imt_index_;
3662
3663 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3664};
3665
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003666class HNewInstance : public HExpression<2> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003667 public:
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003668 HNewInstance(HInstruction* cls,
3669 HCurrentMethod* current_method,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003670 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003671 uint16_t type_index,
3672 const DexFile& dex_file,
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003673 bool can_throw,
3674 bool finalizable,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003675 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003676 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003677 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003678 dex_file_(dex_file),
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003679 can_throw_(can_throw),
3680 finalizable_(finalizable),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003681 entrypoint_(entrypoint) {
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003682 SetRawInputAt(0, cls);
3683 SetRawInputAt(1, current_method);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003684 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003685
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003686 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003687 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003688
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003689 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003690 bool NeedsEnvironment() const OVERRIDE { return true; }
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003691
3692 // It may throw when called on type that's not instantiable/accessible.
3693 // It can throw OOME.
3694 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3695 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3696
3697 bool IsFinalizable() const { return finalizable_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003698
Calin Juravle10e244f2015-01-26 18:54:32 +00003699 bool CanBeNull() const OVERRIDE { return false; }
3700
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003701 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3702
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003703 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3704 entrypoint_ = entrypoint;
3705 }
3706
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003707 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003708
3709 private:
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003710 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003711 const DexFile& dex_file_;
Mingyao Yangfb8464a2015-11-02 10:56:59 -08003712 const bool can_throw_;
3713 const bool finalizable_;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003714 QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003715
3716 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3717};
3718
Roland Levillain88cb1752014-10-20 16:36:47 +01003719class HNeg : public HUnaryOperation {
3720 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003721 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3722 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003723
Roland Levillain9867bc72015-08-05 10:21:34 +01003724 template <typename T> T Compute(T x) const { return -x; }
3725
3726 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003727 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003728 }
3729 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003730 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003731 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003732
Roland Levillain88cb1752014-10-20 16:36:47 +01003733 DECLARE_INSTRUCTION(Neg);
3734
3735 private:
3736 DISALLOW_COPY_AND_ASSIGN(HNeg);
3737};
3738
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003739class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003740 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003741 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003742 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003743 uint32_t dex_pc,
3744 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003745 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003746 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003747 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003748 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003749 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003750 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003751 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003752 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003753 }
3754
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003755 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003756 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003757
3758 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003759 bool NeedsEnvironment() const OVERRIDE { return true; }
3760
Mingyao Yang0c365e62015-03-31 15:09:29 -07003761 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3762 bool CanThrow() const OVERRIDE { return true; }
3763
Calin Juravle10e244f2015-01-26 18:54:32 +00003764 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003765
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003766 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3767
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003768 DECLARE_INSTRUCTION(NewArray);
3769
3770 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003771 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003772 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003773 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003774
3775 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3776};
3777
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003778class HAdd : public HBinaryOperation {
3779 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003780 HAdd(Primitive::Type result_type,
3781 HInstruction* left,
3782 HInstruction* right,
3783 uint32_t dex_pc = kNoDexPc)
3784 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003785
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003786 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003787
Roland Levillain9867bc72015-08-05 10:21:34 +01003788 template <typename T> T Compute(T x, T y) const { return x + y; }
3789
3790 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003791 return GetBlock()->GetGraph()->GetIntConstant(
3792 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003793 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003794 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003795 return GetBlock()->GetGraph()->GetLongConstant(
3796 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003797 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003798
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003799 DECLARE_INSTRUCTION(Add);
3800
3801 private:
3802 DISALLOW_COPY_AND_ASSIGN(HAdd);
3803};
3804
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003805class HSub : public HBinaryOperation {
3806 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003807 HSub(Primitive::Type result_type,
3808 HInstruction* left,
3809 HInstruction* right,
3810 uint32_t dex_pc = kNoDexPc)
3811 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003812
Roland Levillain9867bc72015-08-05 10:21:34 +01003813 template <typename T> T Compute(T x, T y) const { return x - y; }
3814
3815 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003816 return GetBlock()->GetGraph()->GetIntConstant(
3817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003818 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003819 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003820 return GetBlock()->GetGraph()->GetLongConstant(
3821 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003822 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003823
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003824 DECLARE_INSTRUCTION(Sub);
3825
3826 private:
3827 DISALLOW_COPY_AND_ASSIGN(HSub);
3828};
3829
Calin Juravle34bacdf2014-10-07 20:23:36 +01003830class HMul : public HBinaryOperation {
3831 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003832 HMul(Primitive::Type result_type,
3833 HInstruction* left,
3834 HInstruction* right,
3835 uint32_t dex_pc = kNoDexPc)
3836 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003837
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003838 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003839
Roland Levillain9867bc72015-08-05 10:21:34 +01003840 template <typename T> T Compute(T x, T y) const { return x * y; }
3841
3842 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003843 return GetBlock()->GetGraph()->GetIntConstant(
3844 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003845 }
3846 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003847 return GetBlock()->GetGraph()->GetLongConstant(
3848 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003849 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003850
3851 DECLARE_INSTRUCTION(Mul);
3852
3853 private:
3854 DISALLOW_COPY_AND_ASSIGN(HMul);
3855};
3856
Calin Juravle7c4954d2014-10-28 16:57:40 +00003857class HDiv : public HBinaryOperation {
3858 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003859 HDiv(Primitive::Type result_type,
3860 HInstruction* left,
3861 HInstruction* right,
3862 uint32_t dex_pc)
3863 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003864
Roland Levillain9867bc72015-08-05 10:21:34 +01003865 template <typename T>
3866 T Compute(T x, T y) const {
3867 // Our graph structure ensures we never have 0 for `y` during
3868 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003869 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003870 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003871 return (y == -1) ? -x : x / y;
3872 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003873
Roland Levillain9867bc72015-08-05 10:21:34 +01003874 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003875 return GetBlock()->GetGraph()->GetIntConstant(
3876 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003877 }
3878 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003879 return GetBlock()->GetGraph()->GetLongConstant(
3880 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003881 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003882
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003883 static SideEffects SideEffectsForArchRuntimeCalls() {
3884 // The generated code can use a runtime call.
3885 return SideEffects::CanTriggerGC();
3886 }
3887
Calin Juravle7c4954d2014-10-28 16:57:40 +00003888 DECLARE_INSTRUCTION(Div);
3889
3890 private:
3891 DISALLOW_COPY_AND_ASSIGN(HDiv);
3892};
3893
Calin Juravlebacfec32014-11-14 15:54:36 +00003894class HRem : public HBinaryOperation {
3895 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003896 HRem(Primitive::Type result_type,
3897 HInstruction* left,
3898 HInstruction* right,
3899 uint32_t dex_pc)
3900 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003901
Roland Levillain9867bc72015-08-05 10:21:34 +01003902 template <typename T>
3903 T Compute(T x, T y) const {
3904 // Our graph structure ensures we never have 0 for `y` during
3905 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003906 DCHECK_NE(y, 0);
3907 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3908 return (y == -1) ? 0 : x % y;
3909 }
3910
Roland Levillain9867bc72015-08-05 10:21:34 +01003911 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003912 return GetBlock()->GetGraph()->GetIntConstant(
3913 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003914 }
3915 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003916 return GetBlock()->GetGraph()->GetLongConstant(
3917 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003918 }
3919
Calin Juravlebacfec32014-11-14 15:54:36 +00003920
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003921 static SideEffects SideEffectsForArchRuntimeCalls() {
3922 return SideEffects::CanTriggerGC();
3923 }
3924
Calin Juravlebacfec32014-11-14 15:54:36 +00003925 DECLARE_INSTRUCTION(Rem);
3926
3927 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00003928 DISALLOW_COPY_AND_ASSIGN(HRem);
3929};
3930
Calin Juravled0d48522014-11-04 16:40:20 +00003931class HDivZeroCheck : public HExpression<1> {
3932 public:
3933 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003934 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00003935 SetRawInputAt(0, value);
3936 }
3937
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003938 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
3939
Calin Juravled0d48522014-11-04 16:40:20 +00003940 bool CanBeMoved() const OVERRIDE { return true; }
3941
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003942 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00003943 return true;
3944 }
3945
3946 bool NeedsEnvironment() const OVERRIDE { return true; }
3947 bool CanThrow() const OVERRIDE { return true; }
3948
Calin Juravled0d48522014-11-04 16:40:20 +00003949 DECLARE_INSTRUCTION(DivZeroCheck);
3950
3951 private:
Calin Juravled0d48522014-11-04 16:40:20 +00003952 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3953};
3954
Calin Juravle9aec02f2014-11-18 23:06:35 +00003955class HShl : public HBinaryOperation {
3956 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003957 HShl(Primitive::Type result_type,
3958 HInstruction* left,
3959 HInstruction* right,
3960 uint32_t dex_pc = kNoDexPc)
3961 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003962
Roland Levillain9867bc72015-08-05 10:21:34 +01003963 template <typename T, typename U, typename V>
3964 T Compute(T x, U y, V max_shift_value) const {
3965 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
3966 "V is not the unsigned integer type corresponding to T");
3967 return x << (y & max_shift_value);
3968 }
3969
3970 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3971 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003972 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003973 }
3974 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
3975 // case is handled as `x << static_cast<int>(y)`.
3976 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
3977 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003978 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003979 }
3980 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3981 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003982 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003983 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003984
3985 DECLARE_INSTRUCTION(Shl);
3986
3987 private:
3988 DISALLOW_COPY_AND_ASSIGN(HShl);
3989};
3990
3991class HShr : public HBinaryOperation {
3992 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003993 HShr(Primitive::Type result_type,
3994 HInstruction* left,
3995 HInstruction* right,
3996 uint32_t dex_pc = kNoDexPc)
3997 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00003998
Roland Levillain9867bc72015-08-05 10:21:34 +01003999 template <typename T, typename U, typename V>
4000 T Compute(T x, U y, V max_shift_value) const {
4001 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4002 "V is not the unsigned integer type corresponding to T");
4003 return x >> (y & max_shift_value);
4004 }
4005
4006 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4007 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004008 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004009 }
4010 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4011 // case is handled as `x >> static_cast<int>(y)`.
4012 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4013 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004014 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004015 }
4016 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4017 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004018 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004019 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004020
4021 DECLARE_INSTRUCTION(Shr);
4022
4023 private:
4024 DISALLOW_COPY_AND_ASSIGN(HShr);
4025};
4026
4027class HUShr : public HBinaryOperation {
4028 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004029 HUShr(Primitive::Type result_type,
4030 HInstruction* left,
4031 HInstruction* right,
4032 uint32_t dex_pc = kNoDexPc)
4033 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004034
Roland Levillain9867bc72015-08-05 10:21:34 +01004035 template <typename T, typename U, typename V>
4036 T Compute(T x, U y, V max_shift_value) const {
4037 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4038 "V is not the unsigned integer type corresponding to T");
4039 V ux = static_cast<V>(x);
4040 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004041 }
4042
Roland Levillain9867bc72015-08-05 10:21:34 +01004043 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4044 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004045 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004046 }
4047 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4048 // case is handled as `x >>> static_cast<int>(y)`.
4049 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4050 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004051 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004052 }
4053 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4054 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004055 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004056 }
4057
4058 DECLARE_INSTRUCTION(UShr);
4059
4060 private:
4061 DISALLOW_COPY_AND_ASSIGN(HUShr);
4062};
4063
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004064class HAnd : public HBinaryOperation {
4065 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004066 HAnd(Primitive::Type result_type,
4067 HInstruction* left,
4068 HInstruction* right,
4069 uint32_t dex_pc = kNoDexPc)
4070 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004071
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004072 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004073
Roland Levillain9867bc72015-08-05 10:21:34 +01004074 template <typename T, typename U>
4075 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4076
4077 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004078 return GetBlock()->GetGraph()->GetIntConstant(
4079 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004080 }
4081 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004082 return GetBlock()->GetGraph()->GetLongConstant(
4083 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004084 }
4085 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004086 return GetBlock()->GetGraph()->GetLongConstant(
4087 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004088 }
4089 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004090 return GetBlock()->GetGraph()->GetLongConstant(
4091 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004092 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004093
4094 DECLARE_INSTRUCTION(And);
4095
4096 private:
4097 DISALLOW_COPY_AND_ASSIGN(HAnd);
4098};
4099
4100class HOr : public HBinaryOperation {
4101 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004102 HOr(Primitive::Type result_type,
4103 HInstruction* left,
4104 HInstruction* right,
4105 uint32_t dex_pc = kNoDexPc)
4106 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004107
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004108 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004109
Roland Levillain9867bc72015-08-05 10:21:34 +01004110 template <typename T, typename U>
4111 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4112
4113 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004114 return GetBlock()->GetGraph()->GetIntConstant(
4115 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004116 }
4117 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004118 return GetBlock()->GetGraph()->GetLongConstant(
4119 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004120 }
4121 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004122 return GetBlock()->GetGraph()->GetLongConstant(
4123 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004124 }
4125 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004126 return GetBlock()->GetGraph()->GetLongConstant(
4127 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004128 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004129
4130 DECLARE_INSTRUCTION(Or);
4131
4132 private:
4133 DISALLOW_COPY_AND_ASSIGN(HOr);
4134};
4135
4136class HXor : public HBinaryOperation {
4137 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004138 HXor(Primitive::Type result_type,
4139 HInstruction* left,
4140 HInstruction* right,
4141 uint32_t dex_pc = kNoDexPc)
4142 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004143
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004144 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004145
Roland Levillain9867bc72015-08-05 10:21:34 +01004146 template <typename T, typename U>
4147 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4148
4149 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004150 return GetBlock()->GetGraph()->GetIntConstant(
4151 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004152 }
4153 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004154 return GetBlock()->GetGraph()->GetLongConstant(
4155 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004156 }
4157 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004158 return GetBlock()->GetGraph()->GetLongConstant(
4159 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004160 }
4161 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004162 return GetBlock()->GetGraph()->GetLongConstant(
4163 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004164 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004165
4166 DECLARE_INSTRUCTION(Xor);
4167
4168 private:
4169 DISALLOW_COPY_AND_ASSIGN(HXor);
4170};
4171
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004172// The value of a parameter in this method. Its location depends on
4173// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004174class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004175 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004176 HParameterValue(const DexFile& dex_file,
4177 uint16_t type_index,
4178 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004179 Primitive::Type parameter_type,
4180 bool is_this = false)
4181 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004182 dex_file_(dex_file),
4183 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004184 index_(index),
4185 is_this_(is_this),
4186 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004187
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004188 const DexFile& GetDexFile() const { return dex_file_; }
4189 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004190 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004191 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004192
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004193 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4194 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004195
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004196 DECLARE_INSTRUCTION(ParameterValue);
4197
4198 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004199 const DexFile& dex_file_;
4200 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004201 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004202 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004203 const uint8_t index_;
4204
Calin Juravle10e244f2015-01-26 18:54:32 +00004205 // Whether or not the parameter value corresponds to 'this' argument.
4206 const bool is_this_;
4207
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004208 bool can_be_null_;
4209
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004210 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4211};
4212
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004213class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004214 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004215 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4216 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004217
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004218 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004219 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004220 return true;
4221 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004222
Roland Levillain9867bc72015-08-05 10:21:34 +01004223 template <typename T> T Compute(T x) const { return ~x; }
4224
4225 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004226 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004227 }
4228 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004229 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004230 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004231
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004232 DECLARE_INSTRUCTION(Not);
4233
4234 private:
4235 DISALLOW_COPY_AND_ASSIGN(HNot);
4236};
4237
David Brazdil66d126e2015-04-03 16:02:44 +01004238class HBooleanNot : public HUnaryOperation {
4239 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004240 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4241 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004242
4243 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004244 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004245 return true;
4246 }
4247
Roland Levillain9867bc72015-08-05 10:21:34 +01004248 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004249 DCHECK(IsUint<1>(x));
4250 return !x;
4251 }
4252
Roland Levillain9867bc72015-08-05 10:21:34 +01004253 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004254 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004255 }
4256 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4257 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004258 UNREACHABLE();
4259 }
4260
4261 DECLARE_INSTRUCTION(BooleanNot);
4262
4263 private:
4264 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4265};
4266
Roland Levillaindff1f282014-11-05 14:15:05 +00004267class HTypeConversion : public HExpression<1> {
4268 public:
4269 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004270 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004271 : HExpression(result_type,
4272 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4273 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004274 SetRawInputAt(0, input);
4275 DCHECK_NE(input->GetType(), result_type);
4276 }
4277
4278 HInstruction* GetInput() const { return InputAt(0); }
4279 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4280 Primitive::Type GetResultType() const { return GetType(); }
4281
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004282 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004283 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004284
Roland Levillaindff1f282014-11-05 14:15:05 +00004285 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004286 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004287
Mark Mendelle82549b2015-05-06 10:55:34 -04004288 // Try to statically evaluate the conversion and return a HConstant
4289 // containing the result. If the input cannot be converted, return nullptr.
4290 HConstant* TryStaticEvaluation() const;
4291
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004292 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4293 Primitive::Type result_type) {
4294 // Some architectures may not require the 'GC' side effects, but at this point
4295 // in the compilation process we do not know what architecture we will
4296 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004297 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4298 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004299 return SideEffects::CanTriggerGC();
4300 }
4301 return SideEffects::None();
4302 }
4303
Roland Levillaindff1f282014-11-05 14:15:05 +00004304 DECLARE_INSTRUCTION(TypeConversion);
4305
4306 private:
4307 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4308};
4309
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004310static constexpr uint32_t kNoRegNumber = -1;
4311
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004312class HPhi : public HInstruction {
4313 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004314 HPhi(ArenaAllocator* arena,
4315 uint32_t reg_number,
4316 size_t number_of_inputs,
4317 Primitive::Type type,
4318 uint32_t dex_pc = kNoDexPc)
4319 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004320 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004321 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004322 type_(ToPhiType(type)),
4323 // Phis are constructed live and marked dead if conflicting or unused.
4324 // Individual steps of SsaBuilder should assume that if a phi has been
4325 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4326 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004327 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004328 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004329 }
4330
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004331 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4332 static Primitive::Type ToPhiType(Primitive::Type type) {
4333 switch (type) {
4334 case Primitive::kPrimBoolean:
4335 case Primitive::kPrimByte:
4336 case Primitive::kPrimShort:
4337 case Primitive::kPrimChar:
4338 return Primitive::kPrimInt;
4339 default:
4340 return type;
4341 }
4342 }
4343
David Brazdilffee3d32015-07-06 11:48:53 +01004344 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4345
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004346 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004347
4348 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004349 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004350
Calin Juravle10e244f2015-01-26 18:54:32 +00004351 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004352 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004353
Calin Juravle10e244f2015-01-26 18:54:32 +00004354 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4355 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4356
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004357 uint32_t GetRegNumber() const { return reg_number_; }
4358
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004359 void SetDead() { is_live_ = false; }
4360 void SetLive() { is_live_ = true; }
4361 bool IsDead() const { return !is_live_; }
4362 bool IsLive() const { return is_live_; }
4363
David Brazdil77a48ae2015-09-15 12:34:04 +00004364 bool IsVRegEquivalentOf(HInstruction* other) const {
4365 return other != nullptr
4366 && other->IsPhi()
4367 && other->AsPhi()->GetBlock() == GetBlock()
4368 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4369 }
4370
Calin Juravlea4f88312015-04-16 12:57:19 +01004371 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4372 // An equivalent phi is a phi having the same dex register and type.
4373 // It assumes that phis with the same dex register are adjacent.
4374 HPhi* GetNextEquivalentPhiWithSameType() {
4375 HInstruction* next = GetNext();
4376 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4377 if (next->GetType() == GetType()) {
4378 return next->AsPhi();
4379 }
4380 next = next->GetNext();
4381 }
4382 return nullptr;
4383 }
4384
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004385 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004386
David Brazdil1abb4192015-02-17 18:33:36 +00004387 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004388 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004389 return inputs_[index];
4390 }
David Brazdil1abb4192015-02-17 18:33:36 +00004391
4392 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004393 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004394 }
4395
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004396 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004397 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004398 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004399 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004400 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004401 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004402
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004403 DISALLOW_COPY_AND_ASSIGN(HPhi);
4404};
4405
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004406class HNullCheck : public HExpression<1> {
4407 public:
4408 HNullCheck(HInstruction* value, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004409 : HExpression(value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004410 SetRawInputAt(0, value);
4411 }
4412
Calin Juravle10e244f2015-01-26 18:54:32 +00004413 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004414 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004415 return true;
4416 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004417
Calin Juravle10e244f2015-01-26 18:54:32 +00004418 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004419
Calin Juravle10e244f2015-01-26 18:54:32 +00004420 bool CanThrow() const OVERRIDE { return true; }
4421
4422 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004423
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004424
4425 DECLARE_INSTRUCTION(NullCheck);
4426
4427 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004428 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4429};
4430
4431class FieldInfo : public ValueObject {
4432 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004433 FieldInfo(MemberOffset field_offset,
4434 Primitive::Type field_type,
4435 bool is_volatile,
4436 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004437 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004438 const DexFile& dex_file,
4439 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004440 : field_offset_(field_offset),
4441 field_type_(field_type),
4442 is_volatile_(is_volatile),
4443 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004444 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004445 dex_file_(dex_file),
4446 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004447
4448 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004449 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004450 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004451 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004452 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004453 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004454 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004455
4456 private:
4457 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004458 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004459 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004460 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004461 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004462 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004463 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004464};
4465
4466class HInstanceFieldGet : public HExpression<1> {
4467 public:
4468 HInstanceFieldGet(HInstruction* value,
4469 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004470 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004471 bool is_volatile,
4472 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004473 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004474 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004475 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004476 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004477 : HExpression(field_type,
4478 SideEffects::FieldReadOfType(field_type, is_volatile),
4479 dex_pc),
4480 field_info_(field_offset,
4481 field_type,
4482 is_volatile,
4483 field_idx,
4484 declaring_class_def_index,
4485 dex_file,
4486 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004487 SetRawInputAt(0, value);
4488 }
4489
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004490 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004491
4492 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4493 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4494 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004495 }
4496
Calin Juravle641547a2015-04-21 22:08:51 +01004497 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4498 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004499 }
4500
4501 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004502 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4503 }
4504
Calin Juravle52c48962014-12-16 17:02:57 +00004505 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004506 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004507 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004508 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004509
4510 DECLARE_INSTRUCTION(InstanceFieldGet);
4511
4512 private:
4513 const FieldInfo field_info_;
4514
4515 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4516};
4517
4518class HInstanceFieldSet : public HTemplateInstruction<2> {
4519 public:
4520 HInstanceFieldSet(HInstruction* object,
4521 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004522 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004523 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004524 bool is_volatile,
4525 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004526 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004527 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004528 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004529 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004530 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4531 dex_pc),
4532 field_info_(field_offset,
4533 field_type,
4534 is_volatile,
4535 field_idx,
4536 declaring_class_def_index,
4537 dex_file,
4538 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004539 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004540 SetRawInputAt(0, object);
4541 SetRawInputAt(1, value);
4542 }
4543
Calin Juravle641547a2015-04-21 22:08:51 +01004544 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4545 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004546 }
4547
Calin Juravle52c48962014-12-16 17:02:57 +00004548 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004549 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004550 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004551 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004552 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004553 bool GetValueCanBeNull() const { return value_can_be_null_; }
4554 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004555
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004556 DECLARE_INSTRUCTION(InstanceFieldSet);
4557
4558 private:
4559 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004560 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004561
4562 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4563};
4564
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004565class HArrayGet : public HExpression<2> {
4566 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004567 HArrayGet(HInstruction* array,
4568 HInstruction* index,
4569 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004570 uint32_t dex_pc,
4571 SideEffects additional_side_effects = SideEffects::None())
4572 : HExpression(type,
4573 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004574 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004575 SetRawInputAt(0, array);
4576 SetRawInputAt(1, index);
4577 }
4578
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004579 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004580 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004581 return true;
4582 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004583 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004584 // TODO: We can be smarter here.
4585 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4586 // which generates the implicit null check. There are cases when these can be removed
4587 // to produce better code. If we ever add optimizations to do so we should allow an
4588 // implicit check here (as long as the address falls in the first page).
4589 return false;
4590 }
4591
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004592 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004593
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004594 HInstruction* GetArray() const { return InputAt(0); }
4595 HInstruction* GetIndex() const { return InputAt(1); }
4596
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004597 DECLARE_INSTRUCTION(ArrayGet);
4598
4599 private:
4600 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4601};
4602
4603class HArraySet : public HTemplateInstruction<3> {
4604 public:
4605 HArraySet(HInstruction* array,
4606 HInstruction* index,
4607 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004608 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004609 uint32_t dex_pc,
4610 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004611 : HTemplateInstruction(
4612 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004613 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4614 additional_side_effects),
4615 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004616 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004617 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004618 value_can_be_null_(true),
4619 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004620 SetRawInputAt(0, array);
4621 SetRawInputAt(1, index);
4622 SetRawInputAt(2, value);
4623 }
4624
Calin Juravle77520bc2015-01-12 18:45:46 +00004625 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004626 // We currently always call a runtime method to catch array store
4627 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004628 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004629 }
4630
Mingyao Yang81014cb2015-06-02 03:16:27 -07004631 // Can throw ArrayStoreException.
4632 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4633
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004634 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004635 // TODO: Same as for ArrayGet.
4636 return false;
4637 }
4638
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004639 void ClearNeedsTypeCheck() {
4640 needs_type_check_ = false;
4641 }
4642
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004643 void ClearValueCanBeNull() {
4644 value_can_be_null_ = false;
4645 }
4646
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004647 void SetStaticTypeOfArrayIsObjectArray() {
4648 static_type_of_array_is_object_array_ = true;
4649 }
4650
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004651 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004652 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004653 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004654
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004655 HInstruction* GetArray() const { return InputAt(0); }
4656 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004657 HInstruction* GetValue() const { return InputAt(2); }
4658
4659 Primitive::Type GetComponentType() const {
4660 // The Dex format does not type floating point index operations. Since the
4661 // `expected_component_type_` is set during building and can therefore not
4662 // be correct, we also check what is the value type. If it is a floating
4663 // point type, we must use that type.
4664 Primitive::Type value_type = GetValue()->GetType();
4665 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4666 ? value_type
4667 : expected_component_type_;
4668 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004669
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004670 Primitive::Type GetRawExpectedComponentType() const {
4671 return expected_component_type_;
4672 }
4673
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004674 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4675 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4676 }
4677
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004678 DECLARE_INSTRUCTION(ArraySet);
4679
4680 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004681 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004682 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004683 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004684 // Cached information for the reference_type_info_ so that codegen
4685 // does not need to inspect the static type.
4686 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004687
4688 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4689};
4690
4691class HArrayLength : public HExpression<1> {
4692 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004693 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004694 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004695 // Note that arrays do not change length, so the instruction does not
4696 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004697 SetRawInputAt(0, array);
4698 }
4699
Calin Juravle77520bc2015-01-12 18:45:46 +00004700 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004701 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004702 return true;
4703 }
Calin Juravle641547a2015-04-21 22:08:51 +01004704 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4705 return obj == InputAt(0);
4706 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004707
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004708 DECLARE_INSTRUCTION(ArrayLength);
4709
4710 private:
4711 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4712};
4713
4714class HBoundsCheck : public HExpression<2> {
4715 public:
4716 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004717 : HExpression(index->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004718 DCHECK(index->GetType() == Primitive::kPrimInt);
4719 SetRawInputAt(0, index);
4720 SetRawInputAt(1, length);
4721 }
4722
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004723 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004724 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004725 return true;
4726 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004727
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004728 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004729
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004730 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004731
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004732 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004733
4734 DECLARE_INSTRUCTION(BoundsCheck);
4735
4736 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004737 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4738};
4739
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004740/**
4741 * Some DEX instructions are folded into multiple HInstructions that need
4742 * to stay live until the last HInstruction. This class
4743 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004744 * HInstruction stays live. `index` represents the stack location index of the
4745 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004746 */
4747class HTemporary : public HTemplateInstruction<0> {
4748 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004749 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4750 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004751
4752 size_t GetIndex() const { return index_; }
4753
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004754 Primitive::Type GetType() const OVERRIDE {
4755 // The previous instruction is the one that will be stored in the temporary location.
4756 DCHECK(GetPrevious() != nullptr);
4757 return GetPrevious()->GetType();
4758 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004759
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004760 DECLARE_INSTRUCTION(Temporary);
4761
4762 private:
4763 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004764 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4765};
4766
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004767class HSuspendCheck : public HTemplateInstruction<0> {
4768 public:
4769 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004770 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004771
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004772 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004773 return true;
4774 }
4775
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004776 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4777 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004778
4779 DECLARE_INSTRUCTION(SuspendCheck);
4780
4781 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004782 // Only used for code generation, in order to share the same slow path between back edges
4783 // of a same loop.
4784 SlowPathCode* slow_path_;
4785
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004786 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4787};
4788
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004789/**
4790 * Instruction to load a Class object.
4791 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004792class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004793 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004794 HLoadClass(HCurrentMethod* current_method,
4795 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004796 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004797 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004798 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004799 bool needs_access_check,
4800 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004801 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004802 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004803 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004804 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004805 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004806 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004807 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004808 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004809 // Referrers class should not need access check. We never inline unverified
4810 // methods so we can't possibly end up in this situation.
4811 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004812 SetRawInputAt(0, current_method);
4813 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004814
4815 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004816
4817 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004818 // Note that we don't need to test for generate_clinit_check_.
4819 // Whether or not we need to generate the clinit check is processed in
4820 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004821 return other->AsLoadClass()->type_index_ == type_index_ &&
4822 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004823 }
4824
4825 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4826
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004827 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004828 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004829 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004830
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004831 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004832 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004833 }
4834
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004835 bool MustGenerateClinitCheck() const {
4836 return generate_clinit_check_;
4837 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004838
Calin Juravle0ba218d2015-05-19 18:46:01 +01004839 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00004840 // The entrypoint the code generator is going to call does not do
4841 // clinit of the class.
4842 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01004843 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004844 }
4845
4846 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004847 return MustGenerateClinitCheck() ||
4848 (!is_referrers_class_ && !is_in_dex_cache_) ||
4849 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01004850 }
4851
4852 bool NeedsAccessCheck() const {
4853 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004854 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004855
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004856 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004857 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004858 }
4859
Calin Juravleacf735c2015-02-12 15:25:22 +00004860 ReferenceTypeInfo GetLoadedClassRTI() {
4861 return loaded_class_rti_;
4862 }
4863
4864 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
4865 // Make sure we only set exact types (the loaded class should never be merged).
4866 DCHECK(rti.IsExact());
4867 loaded_class_rti_ = rti;
4868 }
4869
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004870 const DexFile& GetDexFile() { return dex_file_; }
4871
Vladimir Markodc151b22015-10-15 18:02:30 +01004872 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004873
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004874 static SideEffects SideEffectsForArchRuntimeCalls() {
4875 return SideEffects::CanTriggerGC();
4876 }
4877
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004878 bool IsInDexCache() const { return is_in_dex_cache_; }
4879
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004880 DECLARE_INSTRUCTION(LoadClass);
4881
4882 private:
4883 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004884 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004885 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004886 // Whether this instruction must generate the initialization check.
4887 // Used for code generation.
4888 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004889 const bool needs_access_check_;
4890 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004891
Calin Juravleacf735c2015-02-12 15:25:22 +00004892 ReferenceTypeInfo loaded_class_rti_;
4893
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004894 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
4895};
4896
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004897class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004898 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004899 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004900 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
4901 string_index_(string_index) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004902 SetRawInputAt(0, current_method);
4903 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004904
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004905 bool CanBeMoved() const OVERRIDE { return true; }
4906
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004907 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4908 return other->AsLoadString()->string_index_ == string_index_;
4909 }
4910
4911 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
4912
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004913 uint32_t GetStringIndex() const { return string_index_; }
4914
4915 // TODO: Can we deopt or debug when we resolve a string?
4916 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004917 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004918 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004919
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004920 static SideEffects SideEffectsForArchRuntimeCalls() {
4921 return SideEffects::CanTriggerGC();
4922 }
4923
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004924 DECLARE_INSTRUCTION(LoadString);
4925
4926 private:
4927 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004928
4929 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4930};
4931
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004932/**
4933 * Performs an initialization check on its Class object input.
4934 */
4935class HClinitCheck : public HExpression<1> {
4936 public:
Roland Levillain3887c462015-08-12 18:15:42 +01004937 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004938 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004939 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004940 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
4941 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004942 SetRawInputAt(0, constant);
4943 }
4944
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004945 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004946 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004947 return true;
4948 }
4949
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004950 bool NeedsEnvironment() const OVERRIDE {
4951 // May call runtime to initialize the class.
4952 return true;
4953 }
4954
Nicolas Geoffray729645a2015-11-19 13:29:02 +00004955 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004956
4957 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4958
4959 DECLARE_INSTRUCTION(ClinitCheck);
4960
4961 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004962 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4963};
4964
4965class HStaticFieldGet : public HExpression<1> {
4966 public:
4967 HStaticFieldGet(HInstruction* cls,
4968 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004969 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004970 bool is_volatile,
4971 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004972 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004973 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004974 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004975 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004976 : HExpression(field_type,
4977 SideEffects::FieldReadOfType(field_type, is_volatile),
4978 dex_pc),
4979 field_info_(field_offset,
4980 field_type,
4981 is_volatile,
4982 field_idx,
4983 declaring_class_def_index,
4984 dex_file,
4985 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004986 SetRawInputAt(0, cls);
4987 }
4988
Calin Juravle52c48962014-12-16 17:02:57 +00004989
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004990 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004991
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004992 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004993 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4994 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004995 }
4996
4997 size_t ComputeHashCode() const OVERRIDE {
4998 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4999 }
5000
Calin Juravle52c48962014-12-16 17:02:57 +00005001 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005002 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5003 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005004 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005005
5006 DECLARE_INSTRUCTION(StaticFieldGet);
5007
5008 private:
5009 const FieldInfo field_info_;
5010
5011 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5012};
5013
5014class HStaticFieldSet : public HTemplateInstruction<2> {
5015 public:
5016 HStaticFieldSet(HInstruction* cls,
5017 HInstruction* value,
5018 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005019 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005020 bool is_volatile,
5021 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005022 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005023 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005024 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005025 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005026 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5027 dex_pc),
5028 field_info_(field_offset,
5029 field_type,
5030 is_volatile,
5031 field_idx,
5032 declaring_class_def_index,
5033 dex_file,
5034 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005035 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005036 SetRawInputAt(0, cls);
5037 SetRawInputAt(1, value);
5038 }
5039
Calin Juravle52c48962014-12-16 17:02:57 +00005040 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005041 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5042 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005043 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005044
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005045 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005046 bool GetValueCanBeNull() const { return value_can_be_null_; }
5047 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005048
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005049 DECLARE_INSTRUCTION(StaticFieldSet);
5050
5051 private:
5052 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005053 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005054
5055 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5056};
5057
Calin Juravlee460d1d2015-09-29 04:52:17 +01005058class HUnresolvedInstanceFieldGet : public HExpression<1> {
5059 public:
5060 HUnresolvedInstanceFieldGet(HInstruction* obj,
5061 Primitive::Type field_type,
5062 uint32_t field_index,
5063 uint32_t dex_pc)
5064 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5065 field_index_(field_index) {
5066 SetRawInputAt(0, obj);
5067 }
5068
5069 bool NeedsEnvironment() const OVERRIDE { return true; }
5070 bool CanThrow() const OVERRIDE { return true; }
5071
5072 Primitive::Type GetFieldType() const { return GetType(); }
5073 uint32_t GetFieldIndex() const { return field_index_; }
5074
5075 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5076
5077 private:
5078 const uint32_t field_index_;
5079
5080 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5081};
5082
5083class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5084 public:
5085 HUnresolvedInstanceFieldSet(HInstruction* obj,
5086 HInstruction* value,
5087 Primitive::Type field_type,
5088 uint32_t field_index,
5089 uint32_t dex_pc)
5090 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5091 field_type_(field_type),
5092 field_index_(field_index) {
5093 DCHECK_EQ(field_type, value->GetType());
5094 SetRawInputAt(0, obj);
5095 SetRawInputAt(1, value);
5096 }
5097
5098 bool NeedsEnvironment() const OVERRIDE { return true; }
5099 bool CanThrow() const OVERRIDE { return true; }
5100
5101 Primitive::Type GetFieldType() const { return field_type_; }
5102 uint32_t GetFieldIndex() const { return field_index_; }
5103
5104 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5105
5106 private:
5107 const Primitive::Type field_type_;
5108 const uint32_t field_index_;
5109
5110 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5111};
5112
5113class HUnresolvedStaticFieldGet : public HExpression<0> {
5114 public:
5115 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5116 uint32_t field_index,
5117 uint32_t dex_pc)
5118 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5119 field_index_(field_index) {
5120 }
5121
5122 bool NeedsEnvironment() const OVERRIDE { return true; }
5123 bool CanThrow() const OVERRIDE { return true; }
5124
5125 Primitive::Type GetFieldType() const { return GetType(); }
5126 uint32_t GetFieldIndex() const { return field_index_; }
5127
5128 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5129
5130 private:
5131 const uint32_t field_index_;
5132
5133 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5134};
5135
5136class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5137 public:
5138 HUnresolvedStaticFieldSet(HInstruction* value,
5139 Primitive::Type field_type,
5140 uint32_t field_index,
5141 uint32_t dex_pc)
5142 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5143 field_type_(field_type),
5144 field_index_(field_index) {
5145 DCHECK_EQ(field_type, value->GetType());
5146 SetRawInputAt(0, value);
5147 }
5148
5149 bool NeedsEnvironment() const OVERRIDE { return true; }
5150 bool CanThrow() const OVERRIDE { return true; }
5151
5152 Primitive::Type GetFieldType() const { return field_type_; }
5153 uint32_t GetFieldIndex() const { return field_index_; }
5154
5155 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5156
5157 private:
5158 const Primitive::Type field_type_;
5159 const uint32_t field_index_;
5160
5161 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5162};
5163
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005164// Implement the move-exception DEX instruction.
5165class HLoadException : public HExpression<0> {
5166 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005167 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5168 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005169
David Brazdilbbd733e2015-08-18 17:48:17 +01005170 bool CanBeNull() const OVERRIDE { return false; }
5171
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005172 DECLARE_INSTRUCTION(LoadException);
5173
5174 private:
5175 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5176};
5177
David Brazdilcb1c0552015-08-04 16:22:25 +01005178// Implicit part of move-exception which clears thread-local exception storage.
5179// Must not be removed because the runtime expects the TLS to get cleared.
5180class HClearException : public HTemplateInstruction<0> {
5181 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005182 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5183 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005184
5185 DECLARE_INSTRUCTION(ClearException);
5186
5187 private:
5188 DISALLOW_COPY_AND_ASSIGN(HClearException);
5189};
5190
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005191class HThrow : public HTemplateInstruction<1> {
5192 public:
5193 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005194 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005195 SetRawInputAt(0, exception);
5196 }
5197
5198 bool IsControlFlow() const OVERRIDE { return true; }
5199
5200 bool NeedsEnvironment() const OVERRIDE { return true; }
5201
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005202 bool CanThrow() const OVERRIDE { return true; }
5203
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005204
5205 DECLARE_INSTRUCTION(Throw);
5206
5207 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005208 DISALLOW_COPY_AND_ASSIGN(HThrow);
5209};
5210
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005211/**
5212 * Implementation strategies for the code generator of a HInstanceOf
5213 * or `HCheckCast`.
5214 */
5215enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005216 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005217 kExactCheck, // Can do a single class compare.
5218 kClassHierarchyCheck, // Can just walk the super class chain.
5219 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5220 kInterfaceCheck, // No optimization yet when checking against an interface.
5221 kArrayObjectCheck, // Can just check if the array is not primitive.
5222 kArrayCheck // No optimization yet when checking against a generic array.
5223};
5224
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005225class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005226 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005227 HInstanceOf(HInstruction* object,
5228 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005229 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005230 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005231 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005232 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005233 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005234 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005235 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005236 SetRawInputAt(0, object);
5237 SetRawInputAt(1, constant);
5238 }
5239
5240 bool CanBeMoved() const OVERRIDE { return true; }
5241
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005242 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005243 return true;
5244 }
5245
5246 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005247 return false;
5248 }
5249
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005250 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5251
5252 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005253
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005254 // Used only in code generation.
5255 bool MustDoNullCheck() const { return must_do_null_check_; }
5256 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5257
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005258 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5259 return (check_kind == TypeCheckKind::kExactCheck)
5260 ? SideEffects::None()
5261 // Mips currently does runtime calls for any other checks.
5262 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005263 }
5264
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005265 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005266
5267 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005268 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005269 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005270
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005271 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5272};
5273
Calin Juravleb1498f62015-02-16 13:13:29 +00005274class HBoundType : public HExpression<1> {
5275 public:
Calin Juravle2e768302015-07-28 14:41:11 +00005276 // Constructs an HBoundType with the given upper_bound.
5277 // Ensures that the upper_bound is valid.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005278 HBoundType(HInstruction* input,
5279 ReferenceTypeInfo upper_bound,
5280 bool upper_can_be_null,
5281 uint32_t dex_pc = kNoDexPc)
5282 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005283 upper_bound_(upper_bound),
5284 upper_can_be_null_(upper_can_be_null),
5285 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005286 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005287 SetRawInputAt(0, input);
Calin Juravle2e768302015-07-28 14:41:11 +00005288 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00005289 }
5290
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005291 // GetUpper* should only be used in reference type propagation.
5292 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5293 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00005294
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005295 void SetCanBeNull(bool can_be_null) {
5296 DCHECK(upper_can_be_null_ || !can_be_null);
5297 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005298 }
5299
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005300 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5301
Calin Juravleb1498f62015-02-16 13:13:29 +00005302 DECLARE_INSTRUCTION(BoundType);
5303
5304 private:
5305 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005306 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5307 // It is used to bound the type in cases like:
5308 // if (x instanceof ClassX) {
5309 // // uper_bound_ will be ClassX
5310 // }
5311 const ReferenceTypeInfo upper_bound_;
5312 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5313 // is false then can_be_null_ cannot be true).
5314 const bool upper_can_be_null_;
5315 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005316
5317 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5318};
5319
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005320class HCheckCast : public HTemplateInstruction<2> {
5321 public:
5322 HCheckCast(HInstruction* object,
5323 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005324 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005325 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005326 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005327 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005328 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005329 SetRawInputAt(0, object);
5330 SetRawInputAt(1, constant);
5331 }
5332
5333 bool CanBeMoved() const OVERRIDE { return true; }
5334
5335 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5336 return true;
5337 }
5338
5339 bool NeedsEnvironment() const OVERRIDE {
5340 // Instruction may throw a CheckCastError.
5341 return true;
5342 }
5343
5344 bool CanThrow() const OVERRIDE { return true; }
5345
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005346 bool MustDoNullCheck() const { return must_do_null_check_; }
5347 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005348 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005349
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005350 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005351
5352 DECLARE_INSTRUCTION(CheckCast);
5353
5354 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005355 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005356 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005357
5358 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005359};
5360
Calin Juravle27df7582015-04-17 19:12:31 +01005361class HMemoryBarrier : public HTemplateInstruction<0> {
5362 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005363 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005364 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005365 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005366 barrier_kind_(barrier_kind) {}
5367
5368 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5369
5370 DECLARE_INSTRUCTION(MemoryBarrier);
5371
5372 private:
5373 const MemBarrierKind barrier_kind_;
5374
5375 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5376};
5377
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005378class HMonitorOperation : public HTemplateInstruction<1> {
5379 public:
5380 enum OperationKind {
5381 kEnter,
5382 kExit,
5383 };
5384
5385 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005386 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005387 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5388 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005389 SetRawInputAt(0, object);
5390 }
5391
5392 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005393 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5394
5395 bool CanThrow() const OVERRIDE {
5396 // Verifier guarantees that monitor-exit cannot throw.
5397 // This is important because it allows the HGraphBuilder to remove
5398 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5399 return IsEnter();
5400 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005401
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005402
5403 bool IsEnter() const { return kind_ == kEnter; }
5404
5405 DECLARE_INSTRUCTION(MonitorOperation);
5406
Calin Juravle52c48962014-12-16 17:02:57 +00005407 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005408 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005409
5410 private:
5411 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5412};
5413
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005414/**
5415 * A HInstruction used as a marker for the replacement of new + <init>
5416 * of a String to a call to a StringFactory. Only baseline will see
5417 * the node at code generation, where it will be be treated as null.
5418 * When compiling non-baseline, `HFakeString` instructions are being removed
5419 * in the instruction simplifier.
5420 */
5421class HFakeString : public HTemplateInstruction<0> {
5422 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005423 explicit HFakeString(uint32_t dex_pc = kNoDexPc)
5424 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005425
5426 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
5427
5428 DECLARE_INSTRUCTION(FakeString);
5429
5430 private:
5431 DISALLOW_COPY_AND_ASSIGN(HFakeString);
5432};
5433
Vladimir Markof9f64412015-09-02 14:05:49 +01005434class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005435 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005436 MoveOperands(Location source,
5437 Location destination,
5438 Primitive::Type type,
5439 HInstruction* instruction)
5440 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005441
5442 Location GetSource() const { return source_; }
5443 Location GetDestination() const { return destination_; }
5444
5445 void SetSource(Location value) { source_ = value; }
5446 void SetDestination(Location value) { destination_ = value; }
5447
5448 // The parallel move resolver marks moves as "in-progress" by clearing the
5449 // destination (but not the source).
5450 Location MarkPending() {
5451 DCHECK(!IsPending());
5452 Location dest = destination_;
5453 destination_ = Location::NoLocation();
5454 return dest;
5455 }
5456
5457 void ClearPending(Location dest) {
5458 DCHECK(IsPending());
5459 destination_ = dest;
5460 }
5461
5462 bool IsPending() const {
5463 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5464 return destination_.IsInvalid() && !source_.IsInvalid();
5465 }
5466
5467 // True if this blocks a move from the given location.
5468 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005469 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005470 }
5471
5472 // A move is redundant if it's been eliminated, if its source and
5473 // destination are the same, or if its destination is unneeded.
5474 bool IsRedundant() const {
5475 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5476 }
5477
5478 // We clear both operands to indicate move that's been eliminated.
5479 void Eliminate() {
5480 source_ = destination_ = Location::NoLocation();
5481 }
5482
5483 bool IsEliminated() const {
5484 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5485 return source_.IsInvalid();
5486 }
5487
Alexey Frunze4dda3372015-06-01 18:31:49 -07005488 Primitive::Type GetType() const { return type_; }
5489
Nicolas Geoffray90218252015-04-15 11:56:51 +01005490 bool Is64BitMove() const {
5491 return Primitive::Is64BitType(type_);
5492 }
5493
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005494 HInstruction* GetInstruction() const { return instruction_; }
5495
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005496 private:
5497 Location source_;
5498 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005499 // The type this move is for.
5500 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005501 // The instruction this move is assocatied with. Null when this move is
5502 // for moving an input in the expected locations of user (including a phi user).
5503 // This is only used in debug mode, to ensure we do not connect interval siblings
5504 // in the same parallel move.
5505 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005506};
5507
5508static constexpr size_t kDefaultNumberOfMoves = 4;
5509
5510class HParallelMove : public HTemplateInstruction<0> {
5511 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005512 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005513 : HTemplateInstruction(SideEffects::None(), dex_pc),
5514 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5515 moves_.reserve(kDefaultNumberOfMoves);
5516 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005517
Nicolas Geoffray90218252015-04-15 11:56:51 +01005518 void AddMove(Location source,
5519 Location destination,
5520 Primitive::Type type,
5521 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005522 DCHECK(source.IsValid());
5523 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005524 if (kIsDebugBuild) {
5525 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005526 for (const MoveOperands& move : moves_) {
5527 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005528 // Special case the situation where the move is for the spill slot
5529 // of the instruction.
5530 if ((GetPrevious() == instruction)
5531 || ((GetPrevious() == nullptr)
5532 && instruction->IsPhi()
5533 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005534 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005535 << "Doing parallel moves for the same instruction.";
5536 } else {
5537 DCHECK(false) << "Doing parallel moves for the same instruction.";
5538 }
5539 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005540 }
5541 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005542 for (const MoveOperands& move : moves_) {
5543 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005544 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005545 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005546 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005547 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005548 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005549 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005550 }
5551
Vladimir Marko225b6462015-09-28 12:17:40 +01005552 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005553 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005554 }
5555
Vladimir Marko225b6462015-09-28 12:17:40 +01005556 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005557
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005558 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005559
5560 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005561 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005562
5563 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5564};
5565
Mark Mendell0616ae02015-04-17 12:49:27 -04005566} // namespace art
5567
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005568#ifdef ART_ENABLE_CODEGEN_arm64
5569#include "nodes_arm64.h"
5570#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005571#ifdef ART_ENABLE_CODEGEN_x86
5572#include "nodes_x86.h"
5573#endif
5574
5575namespace art {
5576
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005577class HGraphVisitor : public ValueObject {
5578 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005579 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5580 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005581
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005582 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005583 virtual void VisitBasicBlock(HBasicBlock* block);
5584
Roland Levillain633021e2014-10-01 14:12:25 +01005585 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005586 void VisitInsertionOrder();
5587
Roland Levillain633021e2014-10-01 14:12:25 +01005588 // Visit the graph following dominator tree reverse post-order.
5589 void VisitReversePostOrder();
5590
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005591 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005592
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005593 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005594#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005595 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5596
5597 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5598
5599#undef DECLARE_VISIT_INSTRUCTION
5600
5601 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005602 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005603
5604 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5605};
5606
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005607class HGraphDelegateVisitor : public HGraphVisitor {
5608 public:
5609 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5610 virtual ~HGraphDelegateVisitor() {}
5611
5612 // Visit functions that delegate to to super class.
5613#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005614 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005615
5616 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5617
5618#undef DECLARE_VISIT_INSTRUCTION
5619
5620 private:
5621 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5622};
5623
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005624class HInsertionOrderIterator : public ValueObject {
5625 public:
5626 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5627
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005628 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005629 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005630 void Advance() { ++index_; }
5631
5632 private:
5633 const HGraph& graph_;
5634 size_t index_;
5635
5636 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5637};
5638
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005639class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005640 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005641 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5642 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005643 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005644 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005645
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005646 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5647 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005648 void Advance() { ++index_; }
5649
5650 private:
5651 const HGraph& graph_;
5652 size_t index_;
5653
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005654 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005655};
5656
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005657class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005658 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005659 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005660 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005661 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005662 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005663 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005664
5665 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005666 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005667 void Advance() { --index_; }
5668
5669 private:
5670 const HGraph& graph_;
5671 size_t index_;
5672
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005673 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005674};
5675
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005676class HLinearPostOrderIterator : public ValueObject {
5677 public:
5678 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005679 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005680
5681 bool Done() const { return index_ == 0; }
5682
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005683 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005684
5685 void Advance() {
5686 --index_;
5687 DCHECK_GE(index_, 0U);
5688 }
5689
5690 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005691 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005692 size_t index_;
5693
5694 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5695};
5696
5697class HLinearOrderIterator : public ValueObject {
5698 public:
5699 explicit HLinearOrderIterator(const HGraph& graph)
5700 : order_(graph.GetLinearOrder()), index_(0) {}
5701
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005702 bool Done() const { return index_ == order_.size(); }
5703 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005704 void Advance() { ++index_; }
5705
5706 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005707 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005708 size_t index_;
5709
5710 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5711};
5712
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005713// Iterator over the blocks that art part of the loop. Includes blocks part
5714// of an inner loop. The order in which the blocks are iterated is on their
5715// block id.
5716class HBlocksInLoopIterator : public ValueObject {
5717 public:
5718 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5719 : blocks_in_loop_(info.GetBlocks()),
5720 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5721 index_(0) {
5722 if (!blocks_in_loop_.IsBitSet(index_)) {
5723 Advance();
5724 }
5725 }
5726
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005727 bool Done() const { return index_ == blocks_.size(); }
5728 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005729 void Advance() {
5730 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005731 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005732 if (blocks_in_loop_.IsBitSet(index_)) {
5733 break;
5734 }
5735 }
5736 }
5737
5738 private:
5739 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005740 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005741 size_t index_;
5742
5743 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5744};
5745
Mingyao Yang3584bce2015-05-19 16:01:59 -07005746// Iterator over the blocks that art part of the loop. Includes blocks part
5747// of an inner loop. The order in which the blocks are iterated is reverse
5748// post order.
5749class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5750 public:
5751 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5752 : blocks_in_loop_(info.GetBlocks()),
5753 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5754 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005755 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005756 Advance();
5757 }
5758 }
5759
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005760 bool Done() const { return index_ == blocks_.size(); }
5761 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005762 void Advance() {
5763 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005764 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5765 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005766 break;
5767 }
5768 }
5769 }
5770
5771 private:
5772 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005773 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005774 size_t index_;
5775
5776 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5777};
5778
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005779inline int64_t Int64FromConstant(HConstant* constant) {
5780 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5781 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5782 : constant->AsLongConstant()->GetValue();
5783}
5784
Vladimir Marko58155012015-08-19 12:49:41 +00005785inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5786 // For the purposes of the compiler, the dex files must actually be the same object
5787 // if we want to safely treat them as the same. This is especially important for JIT
5788 // as custom class loaders can open the same underlying file (or memory) multiple
5789 // times and provide different class resolution but no two class loaders should ever
5790 // use the same DexFile object - doing so is an unsupported hack that can lead to
5791 // all sorts of weird failures.
5792 return &lhs == &rhs;
5793}
5794
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005795} // namespace art
5796
5797#endif // ART_COMPILER_OPTIMIZING_NODES_H_